GitHub Actions offer a powerful platform for automating various development workflows, including code review. By creating custom GitHub Actions, teams can automate parts of their review processes, such as running linters, checking coding standards, or even enforcing certain file changes before a pull request can be merged. This guide walks through the steps to develop custom GitHub Actions specifically tailored for code review automation.
Introduction to GitHub Actions
GitHub Actions enable automation of workflows directly within your GitHub repository. Actions are triggered by GitHub events like pushing to branches, opening pull requests, or leaving comments, and can run a series of commands in response.
Why use custom GitHub Actions for code reviews?
Custom GitHub Actions can streamline the code review process by:
- Automating repetitive tasks like code linting and syntax checking.
- Ensuring adherence to coding standards.
- Providing immediate feedback on pull requests.
Setting up your environment
Before you begin, ensure you have:
- A GitHub account and a repository where you can implement Actions.
- Basic knowledge of YAML syntax and workflow configuration in GitHub.
- Familiarity with the programming language you will use to write scripts (e.g., JavaScript, Python).
Writing custom GitHub Actions
1. Set up the action metadata file
Create a file named action.yml
in the root of your repository. This file contains metadata about the Action. For example, an action to execute a JavaScript program to leave comments on PRs would look something like this.
name: 'Leave comment'description: 'Leaves a comment on specified PRs'inputs:github_token:description: 'GitHub token for authentication'required: truepr_number:description: 'The number of the Pull Request to comment on'required: truecomment_body:description: 'The content of the comment to post'required: trueruns:using: 'node12'main: 'index.js'
inputs:
This section now includes three parameters that the Action requires to function:github_token:
This input is necessary for the Action to authenticate with GitHub and perform operations like commenting on a PR.pr_number:
This is the identifier for the pull request on which the comment should be posted. This must be supplied by the user when they configure the workflow that uses this action.comment_body:
This is the actual text of the comment that will be posted to the pull request. It allows the user of the action to specify what the comment should say.
runs:
This section defines how the Action is executed:using:
Specifies that this action runs in a Node.js environment. Here, it's specifically set to use Node.js version 12.main:
Points toindex.js
as the entry point script for the action. This JavaScript file should contain the code that uses the provided inputs to post a comment on the pull request.
2. Implement your action logic
The main logic of the Action is typically written in a file like index.js
. Here's an example of a simple Node.js script that comments on a pull request:
const github = require('@actions/github')const core = require('@actions/core')async function run() {try {const token = core.getInput('github_token')const octokit = github.getOctokit(token)const { context = {} } = githubconst prNumber = context.payload.pull_request.numberconst owner = context.repo.ownerconst repo = context.repo.repoconst comment = {owner,repo,issue_number: prNumber,body: 'Automated code review: All checks passed.'}await octokit.issues.createComment(comment)} catch (error) {core.setFailed(error.message)}}run()
3. Publish your action
After you've tested your Action locally and ensured it works as expected:
- Push the changes to your GitHub repository.
- Tag your repository with a version number (e.g.,
v1.0.0
). - Reference this tag when using the Action in workflows.
Using your custom action in workflows
To use your custom Action within a workflow in another repository, you can reference it in a workflow file:
name: Automated Code Reviewon: [pull_request]jobs:lint-and-review:runs-on: ubuntu-lateststeps:- name: Checkout codeuses: actions/checkout@v2- name: Run custom code reviewuses: your-username/code-review-linter@v1.0.0with:github_token: ${{ secrets.GITHUB_TOKEN }}
This workflow triggers every time a pull request is opened and runs your custom code review Action.
Use Graphite Automations to simplify GitHub Actions
Instead of writing and maintaining custom GitHub Actions to automate commenting on PRs, you can leverage Graphite Automations, which offers a streamlined and more robust way to automate PR interactions. Here's an alternative approach using Graphite Automations:
Setting Up PR comment automation with Graphite
1. Access Graphite Automations
- Log into the Graphite web app.
- Navigate to the Automations section from the sidebar, where you can manage and create automation rules.
2. Create a new automation rule
- Click on 'Create rule' to start defining your automation.
- Select the repository you want to apply the rule to. Ensure it is one of your synced repositories.
3. Define the trigger conditions
- Set the filter trigger for the PRs you want to target. For example, you could set it to trigger when a PR is updated or when new commits are pushed.
- Specify additional conditions such as the type of files changed (e.g., only
.js
files), the branch name, or PR labels.
4. Set Up the action to comment on PRs
- Choose 'Leave a comment on the PR' as the action.
- Configure the comment content you want to automate. For instance, the comment could be a reminder to run certain tests or a notification that a review is needed.
5. Preview and activate the rule
- Preview the rule to check how it would behave with past PRs to ensure your conditions are correctly configured.
- Activate the rule. It will now automatically apply to all open PRs that meet the specified conditions, as well as any future PRs.
For further reading see the official Graphite documentation.