GitHub Action
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.
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.
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 }}
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
.
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 }}"
}
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.
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.
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.
- Tests!
- More examples/scenarios!
- Common derived data, maybe? (e.g. PR id -- stuff that might not be immeditely available without parsing first)