Integrating ChatGPT with GitHub Actions in your CI/CD Flow

Adolfo
Globant
Published in
6 min readMay 23, 2023
Foto de Desola Lanre-Ologun en Unsplash

In a previous article, we discussed the use of ChatGPT in Xcode, Apple's development environment. This time, we will discuss its use within our CI/CD workflows with GitHub Actions.

GitHub Actions makes it easy to automate all your software workflows, now with a world-renowned CI/CD. Build, test, and deploy your code directly from GitHub. Make code reviews, branch management, and troubleshooting work how you want.

Let us look at how ChatGPT can help us perform code reviews during a pull request in our project repository. We will assign the following tasks to the OpenAI AI when we perform a Pull Request operation in our repository thanks to the GitHub Action we are going to show you in this article:

  • Searching for potential bugs
  • Detecting code smells in the code
  • Suggesting improvements
  • Producing output in Markdown format
  • Integrating the ChatGPT response as a comment in the pull request.

Per each file available in the Pull Request, the GitHub Action will generate a comment, in Markdown format, with the results of the request to ChatGPT for the code file. This comment provides feedback for the questions mentioned in the previous list.

Upon completion, we will be able to get automatic code reviews as seen in this screenshot below. The image shows a Pull Request after our GitHub Action integrated with ChatGPT has finished.

Project structure

We will use Python to develop a workflow that generates the code review from ChatGPT based on the files available in the Pull Request. So you should be familiar with this language to better understand the code.

The files that belong to this workflow are:

  • action.yml Defines the workflow. Among other things, the input parameters, name, and description are defined here.
  • main.py This Python script is the heart of the project. Calls to the OpenAI API are made here, as well as retrieving information from the pull request and all associated files.
  • requirements.txt Contains a list of libraries used by the script and their version.
  • main.sh A small bash script used to instantiate the Python script in the Docker container and pass it the expected parameters.
  • Dockerfile Definition file of the Docker container where our script will be executed.

We will first develop a Python script with all the logic of the Action, which we will name main.py.

​​The script expects some parameters, such as API keys for OpenAI and GitHub, the programming language used in the pull request to be reviewed, and its identifier, to name a few.

Once we have collected these parameters, we create the prompt we will use with ChatGPT by calling the function…

And now, we need to invoke the function that we use to retrieve the information from the pull request, and we will iterate over every one of the files that make up the pull request.

It costs nothing to do prior filtering to avoid reviewing unnecessary files like the Xcode project file.

In this exercise, we will review it to see where ChatGPT throws an error due to the maximum number of tokens used.

When we get a response, we add it to the pull request as a comment and generate one for each file reviewed.

As you can see from the screenshot, the output is quite extensive and sometimes includes code samples.

To prevent ChatGPT from going off-topic in its review, we have set the value of the temperature parameter to 0.0.

Wait, did you say going off-topic?

Yes, there is the temperature concept for that, which OpenAI uses with its ChatGPT model.

This parameter ranges between 0.0 and 2.0 and indicates the degree of randomness in its response.

What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.

In this case, the default value we use for the parameter is 0.0.

Defining the GitHub Action

We do this in the action.yml file, where we find three sections.

Workflow definition

Here we specify the name and description of the workflow, as well as the icon and its color, in case we want to share it on the GitHub marketplace.

Input parameters

We list the parameters that the Python script will work with. As you can see, only four are mandatory, while the rest have default values.

The mandatory parameters are:

  • open-api-key: it is necessary to register on the OpenAI website and create access keys for the API.
  • github-token: In this case, we register as a developer on GitHub and use their API access key.
  • github-pr-id: Unique identifier of the pull request that we will review.
  • dev-lang: Programming language used in the source code files in this pull request.

Other parameters allowed by the script are the following:

  • openai-engine: You could select between the different models provided by OpenAI
  • openai-temperature: This value sets the degree of randomness in the model response
  • openai-max-tokens: Set the number of tokens per request.

Later, we'll explain how the OpenAI and GitHub API keys are stored securely.

Execution

In the Runs section, we specify the execution infrastructure of our workflow.

We specify that it runs in a Docker container and receives the parameters we listed in the previous section.

Docker Image

In the file named Dockerfile, we specify the Python image that we will use to run the main script, along with the necessary instructions to prepare the execution of the workflow.

First, we copy the requirements.txt file and install the libraries specified in it.

Then we copy the main.sh and main.py files. Finally, we specify that the entry point in the Docker image is the main.sh file.

Adding the workflow to a repository

To create a new Action in our repository, click on the "Actions" tab.

We can click the "Set up workflow yourself" link on the new page to create an Action from scratch.

To run the workflow, we need to grant read permissions to the pull request content and write permissions to add comments with the code review result.

We specify the workflow will be executed whether the pull request is just open or reopened in the future.

Finally, we specify the job to run: the ChatGPT review workflow located in the fitomad/github-chatgpt-integration repository.

If you remember, our workflow expects some mandatory parameters, so we need to supply them.

  • In the case of OpenAI and GitHub API keys, we have them securely stored as repository secrets.
  • GitHub itself provides the pull request ID, and we retrieve it using the variables provided by GitHub Actions.
  • We specify the development language with a constant since we know the repository we want to examine contains an Xcode project written with Swift.

Conclusion

From this exercise, the code review task still cannot be left in the hands of ChatGPT, nor can all the suggestions be taken literally, but it is true that some of the suggested improvements can and should be considered.

It is a way to focus on specific aspects of the pull request that reviewers might otherwise overlook.

Source code

Below these lines, you can find the GitHub repository with the source code of the ChatGPT code review workflow.

Acknowledgments

Many thanks to my colleagues Pablo Calcagnino and Hugo Domínguez, both engineers at Globant, for reviewing this text and helping to improve it.

Notice about the use of ChatGPT

Please note that due to factors beyond our control, such as sudden increases in demand or unforeseen technical issues, the availability of the free ChatGPT API service may be subject to change without notice.

--

--

Adolfo
Globant

Me gusta leer, las camisetas y las zapatillas de deporte // iOS Software Designer @ Globant // Creador de MoveMAD, Ambiently, Shelves y alguna cosa más