From 165e8a6746a728f3edddffbcead798734d80ff2b Mon Sep 17 00:00:00 2001 From: Fredrik Arvidsson Date: Wed, 24 Mar 2021 00:00:08 +0100 Subject: [PATCH] fix: use better error handling for fetching artifacts --- fetch_artifact.js | 84 +++++++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 40 deletions(-) diff --git a/fetch_artifact.js b/fetch_artifact.js index cbc625e4..a04de4af 100644 --- a/fetch_artifact.js +++ b/fetch_artifact.js @@ -2,14 +2,14 @@ const axios = require('axios'); const fs = require('fs'); const core = require('@actions/core'); -try { - const getEnvironmentVariable = (name) => { - const value = process.env[name]; - if (value == null) throw new Error(`${name} is not set`); - core.debug(`${name}: ${value}`); - return value; - }; +const getEnvironmentVariable = (name) => { + const value = process.env[name]; + if (value == null) throw new Error(`${name} is not set`); + core.debug(`${name}: ${value}`); + return value; +}; +try { const artifact_name = 'main.js'; core.debug(`Artifact name: ${artifact_name}`); const action_repository = getEnvironmentVariable('GITHUB_ACTION_REPOSITORY'); @@ -17,42 +17,46 @@ try { const action_path = getEnvironmentVariable('GITHUB_ACTION_PATH'); const server_url = getEnvironmentVariable('GITHUB_SERVER_URL'); const api_url = getEnvironmentVariable('GITHUB_API_URL'); +} catch (error) { + core.setFailed(`Action failed with error ${error}`); +} - const ensureSuccessStatusCode = (response) => { - if (response.status !== 200) { - throw Error( - `${response.status} ${response.statusText}: (${response.data})` - ); - } - }; +const ensureSuccessStatusCode = (response) => { + if (response.status !== 200) { + throw Error( + `${response.status} ${response.statusText}: (${response.data})` + ); + } +}; - const getReleaseArtifact = async (releaseTag) => { - const url = `${server_url}/${action_repository}/releases/download/${releaseTag}/${artifact_name}`; - core.info(`Getting release artifact from ${url}`); - return axios.get(url, { responseType: 'stream' }); - }; +const getReleaseArtifact = async (releaseTag) => { + const url = `${server_url}/${action_repository}/releases/download/${releaseTag}/${artifact_name}`; + core.info(`Getting release artifact from ${url}`); + return axios.get(url, { responseType: 'stream' }); +}; - const getReleaseTagFromAnnotatedTag = async (tag) => { - const url = `${api_url}/repos/${action_repository}/git/refs/tags/${tag}`; - console.info(`Getting release tag from ${url}`); - const tagResponse = await axios.get(url); +const getReleaseTagFromAnnotatedTag = async (tag) => { + const url = `${api_url}/repos/${action_repository}/git/refs/tags/${tag}`; + console.info(`Getting release tag from ${url}`); + const tagResponse = await axios.get(url); - ensureSuccessStatusCode(tagResponse); + ensureSuccessStatusCode(tagResponse); - if (tagResponse.data.object.type === 'tag') { - const commitUrl = tagResponse.data.object.url; - core.info(`Tag found, getting commit from ${commitUrl}`); - const refTagResponse = await axios.get(commitUrl); + if (tagResponse.data.object.type === 'tag') { + const commitUrl = tagResponse.data.object.url; + core.info(`Tag found, getting commit from ${commitUrl}`); + const refTagResponse = await axios.get(commitUrl); - var releaseTag = refTagResponse.data.message.trim(); - core.info(`Tag ${tag} is pointing at release ${releaseTag}`); - return releaseTag; - } + var releaseTag = refTagResponse.data.message.trim(); + core.info(`Tag ${tag} is pointing at release ${releaseTag}`); + return releaseTag; + } - throw Error(`${tag} is not an annotated tag`); - }; + throw Error(`${tag} is not an annotated tag`); +}; - const run = async () => { +const run = async () => { + try { let getReleaseArtifactResponse; try { getReleaseArtifactResponse = await getReleaseArtifact(action_ref); @@ -76,9 +80,9 @@ try { core.debug(`Writing script to ${artifactPath}`); const artifactWriter = fs.createWriteStream(artifactPath); getReleaseArtifactResponse.data.pipe(artifactWriter); - }; + } catch (error) { + core.setFailed(`Action failed with error ${error}`); + } +}; - run(); -} catch (error) { - core.setFailed(`Action failed with error ${error}`); -} +run();