Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made the code modular and added modifications to check the recent com… #1

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added AzureGPT-PRReviewer/comment.ts
Empty file.
71 changes: 71 additions & 0 deletions AzureGPT-PRReviewer/del_com.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import tl = require('azure-pipelines-task-lib/task');
import fetch = require('node-fetch');
const https = require("https");

let httpsAgent: any;


function getCollectionName(collectionUri: string) {
const collectionUriWithoutProtocol = collectionUri!.replace('https://', '').replace('http://', '');

if (collectionUriWithoutProtocol.includes('.visualstudio.')) {
return collectionUriWithoutProtocol.split('.visualstudio.')[0];
}
else {
return collectionUriWithoutProtocol.split('/')[1];
}
}

export async function DeleteExistingComments() {
// async function DeleteExistingComments(filenames: string[]) {
console.log("Start deleting existing comments added by the previous Job ...");

const threadsUrl = `${tl.getVariable('SYSTEM.TEAMFOUNDATIONCOLLECTIONURI')}${tl.getVariable('SYSTEM.TEAMPROJECTID')}/_apis/git/repositories/${tl.getVariable('Build.Repository.Name')}/pullRequests/${tl.getVariable('System.PullRequest.PullRequestId')}/threads?api-version=5.1`;
const threadsResponse = await fetch.default(threadsUrl, {
headers: { Authorization: `Bearer ${tl.getVariable('SYSTEM.ACCESSTOKEN')}` },
agent: httpsAgent
});

const threads = await threadsResponse.json() as { value: [] };
const threadsWithContext = threads.value.filter((thread: any) => thread.threadContext !== null);

const collectionUri = tl.getVariable('SYSTEM.TEAMFOUNDATIONCOLLECTIONURI') as string;
const collectionName = getCollectionName(collectionUri);
const buildServiceName = `${tl.getVariable('SYSTEM.TEAMPROJECT')} Build Service (${collectionName})`;

for (const thread of threadsWithContext as any[]) {
const commentsUrl = `${tl.getVariable('SYSTEM.TEAMFOUNDATIONCOLLECTIONURI')}${tl.getVariable('SYSTEM.TEAMPROJECTID')}/_apis/git/repositories/${tl.getVariable('Build.Repository.Name')}/pullRequests/${tl.getVariable('System.PullRequest.PullRequestId')}/threads/${thread.id}/comments?api-version=5.1`;
const commentsResponse = await fetch.default(commentsUrl, {
headers: { Authorization: `Bearer ${tl.getVariable('SYSTEM.ACCESSTOKEN')}` },
agent: httpsAgent
});

const comments = await commentsResponse.json() as { value: [] };

for (const comment of comments.value.filter((comment: any) => comment.author.displayName === buildServiceName) as any[]) {

/*
const commentFilename = comment.threadContext.filePath;
if (filenames.includes(commentFilename)) {
const removeCommentUrl = `${tl.getVariable('SYSTEM.TEAMFOUNDATIONCOLLECTIONURI')}${tl.getVariable('SYSTEM.TEAMPROJECTID')}/_apis/git/repositories/${tl.getVariable('Build.Repository.Name')}/pullRequests/${tl.getVariable('System.PullRequest.PullRequestId')}/threads/${thread.id}/comments/${comment.id}?api-version=5.1`;
await fetch.default(removeCommentUrl, {
method: 'DELETE',
headers: { Authorization: `Bearer ${tl.getVariable('SYSTEM.ACCESSTOKEN')}` },
agent: httpsAgent
});
}
*/

const removeCommentUrl = `${tl.getVariable('SYSTEM.TEAMFOUNDATIONCOLLECTIONURI')}${tl.getVariable('SYSTEM.TEAMPROJECTID')}/_apis/git/repositories/${tl.getVariable('Build.Repository.Name')}/pullRequests/${tl.getVariable('System.PullRequest.PullRequestId')}/threads/${thread.id}/comments/${comment.id}?api-version=5.1`;

await fetch.default(removeCommentUrl, {
method: 'DELETE',
headers: { Authorization: `Bearer ${tl.getVariable('SYSTEM.ACCESSTOKEN')}` },
agent: httpsAgent
});

}
}

console.log("Existing comments deleted.");
}
55 changes: 55 additions & 0 deletions AzureGPT-PRReviewer/file_ret.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import tl = require('azure-pipelines-task-lib/task');
import simpleGit = require('simple-git');
import binaryExtensions = require('binary-extensions');

const gitOptions: Partial<simpleGit.SimpleGitOptions> = {
baseDir: `${tl.getVariable('System.DefaultWorkingDirectory')}`,
binary: 'git'
};

let git: simpleGit.SimpleGit;

function getFileExtension(fileName: string) {
return fileName.slice((fileName.lastIndexOf(".") - 1 >>> 0) + 2);
}

export async function GetChangedFiles(targetBranch: string) {
await git.addConfig('core.pager', 'cat');
await git.addConfig('core.quotepath', 'false');
await git.fetch();

const diffs = await git.diff([targetBranch, '--name-only']);
const files = diffs.split('\n').filter(line => line.trim().length > 0);
const nonBinaryFiles = files.filter(file => !binaryExtensions.includes(getFileExtension(file)));

console.log(`Changed Files (excluding binary files) : \n ${nonBinaryFiles.join('\n')}`);

return nonBinaryFiles;
}

export async function GetLatestCommitFiles() {
try{
const latestCommitID = tl.getVariable('Build.SourceVersion') as string;
const commitFiles = await git.diff(['--name-only', latestCommitID]);
const files = commitFiles.split('\n').filter(line => line.trim().length > 0);
const nonBinaryFiles = files.filter(file => !binaryExtensions.includes(getFileExtension(file)));

console.log(`Latest Commit Files (excluding binary files) : \n ${nonBinaryFiles.join('\n')}`);

return nonBinaryFiles;
}
catch(error){
console.error('GetLatestCommitFiles error: ', error)
return [];
}
}

export function getTargetBranchName() {
let targetBranchName = tl.getVariable('System.PullRequest.TargetBranchName');

if (!targetBranchName) {
targetBranchName = tl.getVariable('System.PullRequest.TargetBranch')?.replace('refs/heads/', '');
}

return `origin/${targetBranchName}`;
}
40 changes: 39 additions & 1 deletion AzureGPT-PRReviewer/review.ts → AzureGPT-PRReviewer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,15 @@ async function run() {
git = simpleGit.simpleGit(gitOptions);
targetBranch = getTargetBranchName();

// const latestCommitFiles = await GetLatestCommitFiles(latestCommitID);
// for (const fileName of latestCommitFiles) {
// await reviewFile(fileName)
// }

const filesNames = await GetChangedFiles(targetBranch);

await DeleteExistingComments();
// await DeleteExistingComments(filesNames);

for (const fileName of filesNames) {
await reviewFile(fileName)
Expand All @@ -78,6 +84,23 @@ async function GetChangedFiles(targetBranch: string) {
return nonBinaryFiles;
}

async function GetLatestCommitFiles() {
try{
const latestCommitID = tl.getVariable('Build.SourceVersion') as string;
const commitFiles = await git.diff(['--name-only', latestCommitID]);
const files = commitFiles.split('\n').filter(line => line.trim().length > 0);
const nonBinaryFiles = files.filter(file => !binaryExtensions.includes(getFileExtension(file)));

console.log(`Latest Commit Files (excluding binary files) : \n ${nonBinaryFiles.join('\n')}`);

return nonBinaryFiles;
}
catch(error){
console.error('GetLatestCommitFiles error: ', error)
return [];
}
}

async function reviewFile(fileName: string) {
console.log(`Start reviewing ${fileName} ...`);

Expand Down Expand Up @@ -177,6 +200,7 @@ async function AddCommentToPR(fileName: string, comment: string) {
}

async function DeleteExistingComments() {
// async function DeleteExistingComments(filenames: string[]) {
console.log("Start deleting existing comments added by the previous Job ...");

const threadsUrl = `${tl.getVariable('SYSTEM.TEAMFOUNDATIONCOLLECTIONURI')}${tl.getVariable('SYSTEM.TEAMPROJECTID')}/_apis/git/repositories/${tl.getVariable('Build.Repository.Name')}/pullRequests/${tl.getVariable('System.PullRequest.PullRequestId')}/threads?api-version=5.1`;
Expand All @@ -202,13 +226,27 @@ async function DeleteExistingComments() {
const comments = await commentsResponse.json() as { value: [] };

for (const comment of comments.value.filter((comment: any) => comment.author.displayName === buildServiceName) as any[]) {

/*
const commentFilename = comment.threadContext.filePath;
if (filenames.includes(commentFilename)) {
const removeCommentUrl = `${tl.getVariable('SYSTEM.TEAMFOUNDATIONCOLLECTIONURI')}${tl.getVariable('SYSTEM.TEAMPROJECTID')}/_apis/git/repositories/${tl.getVariable('Build.Repository.Name')}/pullRequests/${tl.getVariable('System.PullRequest.PullRequestId')}/threads/${thread.id}/comments/${comment.id}?api-version=5.1`;
await fetch.default(removeCommentUrl, {
method: 'DELETE',
headers: { Authorization: `Bearer ${tl.getVariable('SYSTEM.ACCESSTOKEN')}` },
agent: httpsAgent
});
}
*/

const removeCommentUrl = `${tl.getVariable('SYSTEM.TEAMFOUNDATIONCOLLECTIONURI')}${tl.getVariable('SYSTEM.TEAMPROJECTID')}/_apis/git/repositories/${tl.getVariable('Build.Repository.Name')}/pullRequests/${tl.getVariable('System.PullRequest.PullRequestId')}/threads/${thread.id}/comments/${comment.id}?api-version=5.1`;

await fetch.default(removeCommentUrl, {
method: 'DELETE',
headers: { Authorization: `Bearer ${tl.getVariable('SYSTEM.ACCESSTOKEN')}` },
agent: httpsAgent
});

}
}

Expand Down Expand Up @@ -240,4 +278,4 @@ function getTargetBranchName() {
return `origin/${targetBranchName}`;
}

run();
run();
Loading