Skip to content

Commit

Permalink
feat: set outputs.status in case of an error (#92)
Browse files Browse the repository at this point in the history
* docs(README): handle errors

* test: handle errors

* feat: set `outputs.status` in case of an error

* fixup! test: handle errors

* fixup! test: handle errors
  • Loading branch information
gr2m authored Apr 22, 2021
1 parent f045cff commit c101961
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
27 changes: 26 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,33 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

# handle error
- name: "Handle error"
continue-on-error: true
uses: ./
id: get_release
with:
route: GET /repos/{owner}/{repo}/releases/v0.9.9
owner: octokit
repo: request-action
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- run: "echo Release cound not be found. Request failed with status ${{ steps.get_release.outputs.status }}"
if: ${{ failure() }}

issues:
name: "[TEST] Issues"
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- uses: actions/setup-node@v2
with:
node-version: "12.x"
- run: "npm ci"
- run: "npm run build"

# See https://github.com/octokit/request-action/issues/71
- name: "Un-encode {repo} URL parameter when it's set to github.repository"
- name: "Un-encode {repo} URL parameter when it's set to github.repository (#71)"
uses: ./
with:
route: GET /repos/{repository}
Expand Down
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,32 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```

Handle errors

```
name: Log latest release
on:
push:
branches:
- master

jobs:
handleError:
runs-on: ubuntu-latest
steps:
- uses: octokit/request-[email protected]
id: get_release
with:
route: GET /repos/{owner}/{repo}/releases/v0.9.9
owner: octokit
repo: request-action
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- run: "Release found: ${{ steps.get_release.outputs.data }}"
- name: "Release cound not be found. Request failed with status ${{ steps.get_release.outputs.status }}
if: ${{ failure() }}
```
## Inputs
To use request body parameters, simply pass in an `input` matching the parameter name. See previous examples.
Expand All @@ -89,7 +115,7 @@ env:
with:
# As JSON
body: ${{ toJSON(env.REQUEST_BODY) }}
# As block scalar
body: |
|
Expand Down
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const { Octokit } = require("@octokit/action");
main();

async function main() {
const time = Date.now();

try {
const octokit = new Octokit();
const { route, ...parameters } = getAllInputs();
Expand Down Expand Up @@ -35,8 +37,6 @@ async function main() {
core.debug(`parameters: ${inspect(parameters)}`);
core.debug(`parsed request options: ${inspect(requestOptions)}`);

const time = Date.now();

const { status, headers, data } = await octokit.request(requestOptions);

core.info(`< ${status} ${Date.now() - time}ms`);
Expand All @@ -45,6 +45,11 @@ async function main() {
core.setOutput("headers", JSON.stringify(headers, null, 2));
core.setOutput("data", JSON.stringify(data, null, 2));
} catch (error) {
if (error.status) {
core.info(`< ${error.status} ${Date.now() - time}ms`);
}

core.setOutput("status", error.status);
core.debug(inspect(error));
core.setFailed(error.message);
}
Expand Down

0 comments on commit c101961

Please sign in to comment.