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.
actions
/
github-script
Write workflows scripting the GitHub API in JavaScript
actions/github-script
This action makes it easy to quickly write a script in your workflow that uses the GitHub API and the workflow run context.
Note
Thank you for your interest in this GitHub action, however, right now we are not taking contributions.
We continue to focus our resources on strategic areas that help our customers be successful while making developers' lives easier. While GitHub Actions remains a key part of this vision, we are allocating resources towards other areas of Actions and are not taking contributions to this repository at this time. The GitHub public roadmap is the best place to follow along for any updates on features we’re working on and what stage they’re in.
We are taking the following steps to better direct requests related to GitHub Actions, including:
-
We will be directing questions and support requests to our Community Discussions area
-
High Priority bugs can be reported…
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"
});
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.
Top comments (0)