Skip to content

Commit

Permalink
fix: use better error handling for fetching artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
Fredrik Arvidsson committed Mar 23, 2021
1 parent e4db14c commit 165e8a6
Showing 1 changed file with 44 additions and 40 deletions.
84 changes: 44 additions & 40 deletions fetch_artifact.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,61 @@ 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');
const action_ref = getEnvironmentVariable('GITHUB_ACTION_REF');
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);
Expand All @@ -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();

0 comments on commit 165e8a6

Please sign in to comment.