Automate your PR reviews with GitHub Action scripting in JavaScript

bdougieyo

Brian Douglas

Posted on February 14, 2021

Automate your PR reviews with GitHub Action scripting in JavaScript

GitHub Actions is a combination of primitives for users to quickly ship integrations for their repos. Some of these primitives include the API, webhooks, and authentication.

In this post, I am going to focus on the API and actions/github-script. This action makes it easy to quickly write a script in your workflow that uses the GitHub API and includes the workflow run context.

GitHub logo actions / github-script

Write workflows scripting the GitHub API in JavaScript

actions/github-script

.github/workflows/integration.yml .github/workflows/ci.yml .github/workflows/licensed.yml

This action makes it easy to quickly write a script in your workflow that uses the GitHub API and the workflow run context.

To use this action, provide an input named script that contains the body of an asynchronous function call The following arguments will be provided:

  • github A pre-authenticated octokit/rest.js client with pagination plugins
  • context An object containing the context of the workflow run
  • core A reference to the @actions/core package
  • glob A reference to the @actions/glob package
  • io A reference to the @actions/io package
  • exec A reference to the @actions/exec package
  • require A proxy wrapper around the normal Node.js require to enable requiring relative paths (relative to the current working directory) and requiring npm packages installed in the current working directory. If for some reason you need the non-wrapped require, there is an escape hatch available: __original_require__ is the original value of require without our wrapping…

In order to use this action, a script input is provided. The value of that input should be the body of an asynchronous function call. The following arguments will be provided:

  • github A pre-authenticated octokit/core.js client with REST endpoints and pagination plugins
  • context An object containing the context of the workflow run
  • core A reference to the @actions/core package
  • io A reference to the @actions/io package

If you are familiar with the octokit.rest.js or Probot library, you will find it pretty similar.

Here is an example script where I am reviewing my PRs based on labels. GitHub Script allows you to write JavaScript to handle different webhook events, and in this case, we are triggered the workflow with labels on the PR.

name: Review with labels

on:
  pull_request:
    types: [labeled]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/github-script@0.8.0
        name: Not an Action
        if: github.event.label.name == 'nocode' || github.event.label.name == 'not-an-action'
        with:
          script: |
            await github.issues.createComment({
              owner: "github-hackathon",
              repo: "hackathon",
              issue_number: context.payload.number,
              body: "Submission is not a usable GitHub Action"
            });

            await github.pulls.update({
              owner: "github-hackathon",
              repo: "hackathon",
              pull_number: context.payload.number,
              state: "closed"
            });
      - uses: actions/github-script@0.8.0
        name: Featured
        if: github.event.label.name == 'featured' || github.event.label.name == 'good'
        with:
          script: |
            await github.pulls.merge({
              owner: "github-hackathon",
              repo: "hackathon",
              pull_number: context.payload.number,
            });
      - uses: actions/github-script@0.8.0
        name: Fork
        if: github.event.label.name == 'fork'
        with:
          script: |
            await github.issues.createComment({
              owner: "github-hackathon",
              repo: "hackathon",
              issue_number: context.payload.number,
              body: "Submission is a fork and does not represent the submitter as the author."
            });

            await github.pulls.update({
              owner: "github-hackathon",
              repo: "hackathon",
              pull_number: context.payload.number,
              state: "closed"
            });
      - uses: actions/github-script@0.8.0
        name: Ended
        if: github.event.label.name == 'late'
        with:
          script: |
            await github.issues.createComment({
              owner: "github-hackathon",
              repo: "hackathon",
              issue_number: context.payload.number,
              body: "Submission received after the Hackathon has ended."
            });

            await github.pulls.update({
              owner: "github-hackathon",
              repo: "hackathon",
              pull_number: context.payload.number,
              state: "closed"
            });
Enter fullscreen mode Exit fullscreen mode

See octokit/rest.js for the API client documentation for more info on what you can do.

This is part of my 28 days of Actions series. To get notified of more GitHub Action tips, follow the GitHub organization right here on Dev.

💖 💪 🙅 🚩
bdougieyo
Brian Douglas

Posted on February 14, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related