Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/moustacheful/github-api-exec-action

Execute arbitrary commands on Github's API using Github Actions
https://github.com/moustacheful/github-api-exec-action

actions automation github github-api json utility

Last synced: about 2 months ago
JSON representation

Execute arbitrary commands on Github's API using Github Actions

Awesome Lists containing this project

README

        

# 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](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/authenticating-with-the-github_token#about-the-github_token-secret).

#### 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](#Inputs) and [outputs](#Outputs) for more details):

```yml
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](https://octokit.github.io/rest.js/).

For instance, if the example lists this code:
```js
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:

```js
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](https://octokit.github.io/rest.js/).

- You can use [multiline strings](https://yaml-multiline.info/) 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:

```yml
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:
```json
{
"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](thttps://help.github.com/en/actions/automating-your-workflow-with-github-actions/contexts-and-expression-syntax-for-github-actions#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)