diff --git a/examples/gptreview-jenkins/Jenkinsfile b/examples/gptreview-jenkins/Jenkinsfile new file mode 100644 index 00000000..b65780d7 --- /dev/null +++ b/examples/gptreview-jenkins/Jenkinsfile @@ -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' + } + } + } + } + } +} \ No newline at end of file diff --git a/examples/gptreview-jenkins/README.md b/examples/gptreview-jenkins/README.md new file mode 100644 index 00000000..60fdb663 --- /dev/null +++ b/examples/gptreview-jenkins/README.md @@ -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:///generic-webhook-trigger/invoke?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. diff --git a/examples/gptreview-jenkins/codereview.gpt b/examples/gptreview-jenkins/codereview.gpt new file mode 100644 index 00000000..f2502b50 --- /dev/null +++ b/examples/gptreview-jenkins/codereview.gpt @@ -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