Automate your PR reviews with GitHub Action scripting in JavaScript
Tags: github, devops, githubactions, javascript
Published: 2021-02-14 20:47:08

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](https://github.com/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 https://github.com/actions/github-script %}

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](https://octokit.github.io/rest.js/v18/) or [Probot](https://probot.github.io/) 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.

```yml
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"
            });
``` 

See [octokit/rest.js](https://octokit.github.io/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._

{% link https://dev.to/github/run-your-github-actions-locally-5gfl %}