Skip to content
cloud-lightning

GitHub Action

GitHub API exec action

v0 Pre-release

GitHub API exec action

cloud-lightning

GitHub API exec action

Executes commands on with GitHub's API

Installation

Copy and paste the following snippet into your .yml file.

              

- name: GitHub API exec action

uses: moustacheful/github-api-exec-action@v0

Learn more about this action in moustacheful/github-api-exec-action

Choose a version

Github API exec action

This action executes arbitrary commands on Github's API, using octokit, such as creating pull requests.

It passes a payload to the JS Octokit library, if the command doesn't exist, it will fail. Other than that, it should let you to do whatever you need with the GitHub API without having to create a new action.

Disclaimer: I'm not responsible if you nuke your whole repository/account/existence. Be responsible and create a scoped PAT that can only do what you need it to do!

If you do use secrets.GITHUB_TOKEN keep in mind the breadth of the permissions given to this token.

Why?

Needed a quick way to execute GitHub's API commands, and GitHub's CLI doesn't seem to provide all possible commands (i.e. creating a PR, adding labels,etc.). This might work best for "prototyping" purposes, for which you can later create a proper action.

Usage

Creating a pull request and get the PR# (see inputs and outputs for more details):

jobs:
  create-pr:
    runs-on: ubuntu-latest
    steps:
      - uses: moustacheful/github-api-exec-action@v0
        with:
          # This will add the repo name and owner to the payload
          withRepo: true
          # See https://octokit.github.io/rest.js/ for available commands
          command: pulls.create 
          # You can use interpolation here!
          payload: > 
            {
              "title": "Test PR by ${{ github.actor }}",
              "head": "feature/some-feature",
              "base": "master"
            }
          # The path of the result, this uses dot notation to access the data, 
          # If you want the whole response, don't include this.
          resultPath: 'number' 
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Inputs

command

A string representing the command to execute based on Octokits' methods. Available commands should be the same as found in Octokit's documentation.

For instance, if the example lists this code:

octokit.gists.createComment({
  //...
})

The command would be gists.createComment.

payload

A string with a JSON representing the payload to be sent to that command. This is optional, and defaults to an empty object. Using the example below:

octokit.gists.createComment({
  gist_id,
  body
})

The object inside is what we should add here, again, you can find more information on what every command accepts in Octokit's documentation.

  • You can use multiline strings to make this more readable. Or if you prefer a compact way, wrap in single quotes, e.g.: '{"a": 10}'.
  • You can use string interpolation and all information available in contexts. For instance:
steps:
  - uses: moustacheful/github-api-exec-action@releases/v0
    with:
      command: gists.createComment
      payload: >
        { 
          "gist_id": 10,
          "body": "Lorem ipsum\n Generated by ${{ github.actor }}" 
        }
withRepo

Whether or not the current repository information should be included with the payload. This adds: owner and repo to your payload. This is useful to scope calls to this specific repository.

resultPath

An optional string to represent a value to extract from the API's response.

For instance, for the following structure:

{
  "labels": [
    {
      "id": 208045946,
      "description": "Something isn't working",
    }
  ],
}

We can extract description by using dot notation to describe its path, in this case labels.0.description.

If this is not specified, the result will be the whole response.

Outputs

result

This action should always output the result. The value of this output depends on the given resultPath.

You can later acess this data using the steps context to chain the output to a different action.

If you choose to work with the whole JSON for the response, you can use jq (which is already included in the runner context) to extract values later.

TODO:

  • Tests!
  • More examples/scenarios!
  • Common derived data, maybe? (e.g. PR id -- stuff that might not be immeditely available without parsing first)