-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support Azure DevOps pipeline PR (#324)
Co-authored-by: Tao Lin <[email protected]>
- Loading branch information
Showing
13 changed files
with
1,179 additions
and
662 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
packages/code-review-gpt/src/common/ci/azdev/commentOnPR.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import * as azdev from "azure-devops-node-api"; | ||
import * as gitApiObject from "azure-devops-node-api/GitApi"; | ||
import * as GitInterfaces from "azure-devops-node-api/interfaces/GitInterfaces"; | ||
import { logger } from "../../utils/logger"; | ||
|
||
const gitAzdevEnvVariables = (): Record<string, string> => { | ||
const envVars = [ | ||
"SYSTEM_TEAMFOUNDATIONCOLLECTIONURI", | ||
"API_TOKEN", | ||
"SYSTEM_PULLREQUEST_PULLREQUESTID", | ||
"BUILD_REPOSITORY_ID", | ||
"SYSTEM_TEAMPROJECTID", | ||
]; | ||
const missingVars: string[] = []; | ||
envVars.forEach((envVar) => process.env[envVar] ?? missingVars.push(envVar)); | ||
|
||
if (missingVars.length > 0) { | ||
logger.error(`Missing environment variables: ${missingVars.join(", ")}`); | ||
throw new Error( | ||
"One or more Azure DevOps environment variables are not set" | ||
); | ||
} | ||
|
||
return { | ||
serverUrl: process.env.SYSTEM_TEAMFOUNDATIONCOLLECTIONURI ?? "", | ||
azdevToken: process.env.API_TOKEN ?? "", | ||
pullRequestId: process.env.SYSTEM_PULLREQUEST_PULLREQUESTID ?? "", | ||
project: process.env.SYSTEM_TEAMPROJECTID ?? "", | ||
repositoryId: process.env.BUILD_REPOSITORY_ID ?? "", | ||
}; | ||
}; | ||
|
||
/** | ||
* Publish a comment on the pull request. It always create a new one. | ||
* The comment will be signed off with the provided sign off. | ||
* @param comment The body of the comment to publish. | ||
* @param signOff The sign off to use. | ||
* @returns | ||
*/ | ||
export const commentOnPR = async ( | ||
comment: string, | ||
signOff: string | ||
): Promise<void> => { | ||
try { | ||
const { serverUrl, azdevToken, pullRequestId, repositoryId, project } = | ||
gitAzdevEnvVariables(); | ||
|
||
const pullRequestIdNumber = Number(pullRequestId); | ||
|
||
const authHandler = azdev.getPersonalAccessTokenHandler(azdevToken); | ||
const connection: azdev.WebApi = new azdev.WebApi(serverUrl, authHandler); | ||
|
||
const git: gitApiObject.IGitApi = await connection.getGitApi(); | ||
|
||
const commentThread = <GitInterfaces.GitPullRequestCommentThread>{ | ||
comments: [<GitInterfaces.Comment>{ | ||
content: `${comment}\n\n---\n\n${signOff}` | ||
}] | ||
}; | ||
|
||
await git.createThread(commentThread, repositoryId, pullRequestIdNumber, project); | ||
|
||
} catch (error) { | ||
logger.error(`Failed to comment on PR: ${JSON.stringify(error)}`); | ||
throw error; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
name: Code Review GPT | ||
trigger: | ||
- main | ||
pr: | ||
- main | ||
|
||
# Important: code-reivew-gpt needs additional Git history available for affected to function correctly. | ||
# Make sure Shallow fetching is disabled in your pipeline settings UI. | ||
# For more info, check out this article from Microsoft https://learn.microsoft.com/en-us/azure/devops/pipelines/yaml-schema/steps-checkout?view=azure-pipelines#shallow-fetch. | ||
variables: | ||
TARGET_BRANCH: $[replace(variables['System.PullRequest.TargetBranch'],'refs/heads/','origin/')] | ||
BASE_SHA: $(git merge-base $(TARGET_BRANCH) HEAD) | ||
|
||
pool: | ||
vmImage: ubuntu-latest | ||
|
||
stages: | ||
- stage: GTP_Review | ||
jobs: | ||
- job: gpt_review | ||
displayName: Code Review GPT | ||
workspace: | ||
clean: all | ||
steps: | ||
- script: | | ||
npm install code-review-gpt | ||
displayName: "Install code-review-gpt" | ||
- script: | | ||
npx code-review-gpt review --ci=azdev --model=gpt-3.5-turbo | ||
env: | ||
API_TOKEN: $(API_TOKEN) | ||
OPENAI_API_KEY: $(OPENAI_API_KEY) | ||
BASE_SHA: $(BASE_SHA) | ||
workingDirectory: $(Build.SourcesDirectory) | ||
displayName: "Run code review script" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters