Skip to content

Commit d0e1130

Browse files
committed
Add GPTReview Jenkins example
1 parent 2584308 commit d0e1130

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
pipeline {
2+
agent any
3+
4+
stages {
5+
stage('Clean Workspace') {
6+
steps {
7+
deleteDir()
8+
}
9+
}
10+
11+
stage('GPT Review') {
12+
steps {
13+
script {
14+
checkout([
15+
$class: 'GitSCM',
16+
branches: [[name: '*/main']], // Specify branch
17+
userRemoteConfigs: [[
18+
url: '' // Provide the URL for your repo that has the codereview.gpt file.
19+
]]
20+
])
21+
22+
withCredentials([string(credentialsId: 'OPENAI_API_KEY', variable: 'OPENAI_API_KEY')]){
23+
withCredentials([string(credentialsId: 'GH_TOKEN', variable: 'GH_TOKEN')]) {
24+
// GPTSCript reviews the code
25+
REVIEW = sh(script: "gptscript codereview.gpt --PR_URL=${PR_URL}", returnStdout: true).trim()
26+
27+
// Construct the JSON payload using Groovy's JSON library
28+
def jsonPayload = groovy.json.JsonOutput.toJson([body: REVIEW])
29+
30+
// Post the review comment to the GitHub PR
31+
sh "curl -H \"Authorization: token ${GH_TOKEN}\" -H \"Content-Type: application/json\" -X POST -d '${jsonPayload}' '${PR_COMMENTS_URL}'"
32+
}
33+
}
34+
}
35+
}
36+
}
37+
38+
stage('Check PR Status') {
39+
steps {
40+
script {
41+
// Check if REVIEW contains 'Require Changes'
42+
if (REVIEW.contains('Require Changes')) {
43+
echo 'Code Requires Changes'
44+
currentBuild.result = 'FAILURE' // Mark the build as failed
45+
error 'Code Requires Changes' // Terminate the build with an error
46+
}
47+
48+
// Check if REVIEW contains 'Approved'
49+
if (REVIEW.contains('Approved')) {
50+
echo 'Code Approved'
51+
}
52+
}
53+
}
54+
}
55+
}
56+
}

examples/gptreview-jenkins/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# GPTReview With Jenkins
2+
3+
This folder contains an example of building and implementing your own code reviewer as part of Jenkins Pipeline.
4+
5+
Below are the files present here:
6+
7+
- `codereview.gpt`: Contains the GPTScript code and prompts.
8+
- `Jenkinsfile`: Jenkins pipeline file.
9+
10+
## Pre-requisites
11+
12+
- An OpenAI API Key.
13+
- GitHub repository.
14+
- Jenkins.
15+
- [GPTScript](https://github.com/gptscript-ai/gptscript) and [GH](https://github.com/cli/cli) CLI installed on the system running Jenkins.
16+
17+
## How To Run This Example
18+
19+
- Create a new repository in your GitHub account and create a `codereview.gpt` file in the root of that repo based on the contents provided in this file.
20+
- Configure Jenkins:
21+
- Install required plugins - [GitHub](https://plugins.jenkins.io/github/), [Generic Webhook Trigger Plugin](https://plugins.jenkins.io/generic-webhook-trigger/) & [HTTP Request Plugin](https://plugins.jenkins.io/http_request/).
22+
- Create a Pipeline
23+
- Configure the “Open_AI_API” and “GH_TOKEN” environment variables
24+
25+
- Congfigure GitHub:
26+
- Setup up Webhook by providing your Jenkins pipeline URL: `http://<jenkins-url>/generic-webhook-trigger/invoke?token=<secret-token>`
27+
- Add `Jenkinsfile` in the root of the repo. *Note: Replace the repository URL with your repo URL in the Jenkinsfile provided.*
28+
29+
- Executing the Script:
30+
- Create a new branch, and add some code file to the repository and open a new pull request.
31+
- The Jenkins pipeline will trigger and our GPTReview will review your code and provide review comments.
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Name: Code Reviewer
2+
Description: A tool to help you perform code review of open PRs
3+
Context: learn-gh
4+
Tools: sys.exec, sys.http.html2text?, sys.find, sys.read, sys.write
5+
Args: PR_URL: The GitHub PR_URL
6+
7+
You have the gh cli available to you. Use it to perform code review for a pr from the $(repo) provided.
8+
9+
Perform the following steps in order:
10+
1. Identify the files changed in the pull request ($PR_URL) using the pr number and perform a diff.
11+
1. Analyze the complete code of each identified file and perform a detailed line by line code review.
12+
2. Repeat the process for each changed file in the pr.
13+
2. Share your review comments separately for each file.
14+
3. In a new line write "Code: Approved" or "Code: Require Changes" based on the review comments.
15+
---
16+
Name: learn-gh
17+
Description: A tool to help you learn gh cli
18+
19+
#!/usr/bin/env bash
20+
21+
echo "The following is the help text for the gh cli and some of its sub-commands. Use these when figuring out how to construct new commands. Note that the --search flag is used for filtering and sorting as well; there is no dedicate --sort flag."
22+
gh --help
23+
gh repo --help
24+
gh pr --help
25+
gh pr checkout --help
26+
gh pr diff --help

0 commit comments

Comments
 (0)