From c1019613b6431924ce26cdfed6cf54b21d4861f5 Mon Sep 17 00:00:00 2001 From: Gregor Martynus <39992+gr2m@users.noreply.github.com> Date: Thu, 22 Apr 2021 10:41:18 -0700 Subject: [PATCH] feat: set `outputs.status` in case of an error (#92) * docs(README): handle errors * test: handle errors * feat: set `outputs.status` in case of an error * fixup! test: handle errors * fixup! test: handle errors --- .github/workflows/test.yml | 27 ++++++++++++++++++++++++++- README.md | 28 +++++++++++++++++++++++++++- index.js | 9 +++++++-- 3 files changed, 60 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2e14b16..0af1ef6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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} diff --git a/README.md b/README.md index 698d95c..5a6c958 100644 --- a/README.md +++ b/README.md @@ -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-action@v2.x + 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. @@ -89,7 +115,7 @@ env: with: # As JSON body: ${{ toJSON(env.REQUEST_BODY) }} - + # As block scalar body: | | diff --git a/index.js b/index.js index afa95a1..d354f06 100644 --- a/index.js +++ b/index.js @@ -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(); @@ -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`); @@ -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); }