Skip to content

Add GPTReview Jenkins example #676

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

Merged
merged 1 commit into from
Jul 30, 2024
Merged
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
56 changes: 56 additions & 0 deletions examples/gptreview-jenkins/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
pipeline {
agent any

stages {
stage('Clean Workspace') {
steps {
deleteDir()
}
}

stage('GPT Review') {
steps {
script {
checkout([
$class: 'GitSCM',
branches: [[name: '*/main']], // Specify branch
userRemoteConfigs: [[
url: '' // Provide the URL for your repo that has the codereview.gpt file.
]]
])

withCredentials([string(credentialsId: 'OPENAI_API_KEY', variable: 'OPENAI_API_KEY')]){
withCredentials([string(credentialsId: 'GH_TOKEN', variable: 'GH_TOKEN')]) {
// GPTSCript reviews the code
REVIEW = sh(script: "gptscript codereview.gpt --PR_URL=${PR_URL}", returnStdout: true).trim()

// Construct the JSON payload using Groovy's JSON library
def jsonPayload = groovy.json.JsonOutput.toJson([body: REVIEW])

// Post the review comment to the GitHub PR
sh "curl -H \"Authorization: token ${GH_TOKEN}\" -H \"Content-Type: application/json\" -X POST -d '${jsonPayload}' '${PR_COMMENTS_URL}'"
}
}
}
}
}

stage('Check PR Status') {
steps {
script {
// Check if REVIEW contains 'Require Changes'
if (REVIEW.contains('Require Changes')) {
echo 'Code Requires Changes'
currentBuild.result = 'FAILURE' // Mark the build as failed
error 'Code Requires Changes' // Terminate the build with an error
}

// Check if REVIEW contains 'Approved'
if (REVIEW.contains('Approved')) {
echo 'Code Approved'
}
}
}
}
}
}
31 changes: 31 additions & 0 deletions examples/gptreview-jenkins/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# GPTReview With Jenkins

This folder contains an example of building and implementing your own code reviewer as part of Jenkins Pipeline.

Below are the files present here:

- `codereview.gpt`: Contains the GPTScript code and prompts.
- `Jenkinsfile`: Jenkins pipeline file.

## Pre-requisites

- An OpenAI API Key.
- GitHub repository.
- Jenkins.
- [GPTScript](https://github.com/gptscript-ai/gptscript) and [GH](https://github.com/cli/cli) CLI installed on the system running Jenkins.

## How To Run This Example

- 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.
- Configure Jenkins:
- 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/).
- Create a Pipeline
- Configure the “Open_AI_API” and “GH_TOKEN” environment variables

- Congfigure GitHub:
- Setup up Webhook by providing your Jenkins pipeline URL: `http://<jenkins-url>/generic-webhook-trigger/invoke?token=<secret-token>`
- Add `Jenkinsfile` in the root of the repo. *Note: Replace the repository URL with your repo URL in the Jenkinsfile provided.*

- Executing the Script:
- Create a new branch, and add some code file to the repository and open a new pull request.
- The Jenkins pipeline will trigger and our GPTReview will review your code and provide review comments.
26 changes: 26 additions & 0 deletions examples/gptreview-jenkins/codereview.gpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Name: Code Reviewer
Description: A tool to help you perform code review of open PRs
Context: learn-gh
Tools: sys.exec, sys.http.html2text?, sys.find, sys.read, sys.write
Args: PR_URL: The GitHub PR_URL

You have the gh cli available to you. Use it to perform code review for a pr from the $(repo) provided.

Perform the following steps in order:
1. Identify the files changed in the pull request ($PR_URL) using the pr number and perform a diff.
1. Analyze the complete code of each identified file and perform a detailed line by line code review.
2. Repeat the process for each changed file in the pr.
2. Share your review comments separately for each file.
3. In a new line write "Code: Approved" or "Code: Require Changes" based on the review comments.
---
Name: learn-gh
Description: A tool to help you learn gh cli

#!/usr/bin/env bash

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."
gh --help
gh repo --help
gh pr --help
gh pr checkout --help
gh pr diff --help