Skip to content

Commit

Permalink
fix: include main sha as a artifact name fallback (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
prescottprue committed Apr 22, 2022
1 parent cd6a0cc commit 10c93b9
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 19 deletions.
36 changes: 27 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13899,32 +13899,50 @@ function getOctokitInstance() {
* @returns Coverage artifact
*/
async function getCoverageArtifactByName() {
const { ref: branch, sha: lastCommitSha } = github_1.context?.payload?.pull_request?.head || {};
const { sha: mainSha } = github_1.context;
const { sha: prBaseSha } = github_1.context?.payload?.pull_request?.base || {};
const { ref: branch, sha: prHeadSha } = github_1.context?.payload?.pull_request?.head || {};
core.info(`Branch and last commit sha loaded: ${JSON.stringify({
branch,
lastCommitSha,
mainSha,
prBaseSha,
prHeadSha,
})}`);
const coverageKey = core.getInput('coverage-artifact-name') || `coverage-${lastCommitSha}`;
core.info(`Workflow artifacts loaded, looking for artifact with name "${coverageKey}"`);
const coverageKey = core.getInput('coverage-artifact-name') || `coverage-${prHeadSha}`;
const fallbackCoverageKey = `coverage-${prBaseSha}`;
const mainShaCoverageKey = `coverage-${mainSha}`;
const coverageArtifactNames = [
coverageKey,
fallbackCoverageKey,
mainShaCoverageKey,
];
core.info(`Workflow artifacts loaded, looking for artifact with one of the following names: ${coverageArtifactNames.join('\n')}`);
const { rest, paginate } = getOctokitInstance();
// Load all artifacts, paginating until matching name is found
const artifacts = await paginate(rest.actions.listArtifactsForRepo, {
owner: github_1.context.repo.owner,
repo: github_1.context.repo.repo,
}, (response, done) => {
if (response.data.find((artifact) => artifact?.name === coverageKey)) {
core.debug('Found matching artifact - stopping pagination');
const matchingArtifact = response.data.find((artifact) => coverageArtifactNames.includes(artifact?.name));
if (matchingArtifact) {
core.debug(`Found matching artifact in paginate - stopping pagination: ${JSON.stringify(matchingArtifact)}`);
done();
}
return response.data;
});
core.info(`Artifacts loaded: ${artifacts.length}`);
core.debug(JSON.stringify(artifacts.slice(0, 15), null, 2));
// Filter artifacts to coverage-$sha
const matchArtifact = artifacts.find((artifact) => artifact?.name === coverageKey);
const matchArtifact = artifacts.find((artifact) => coverageArtifactNames.includes(artifact?.name));
if (!matchArtifact) {
throw new Error(`Artifact with name "${coverageKey}" not found`);
throw new Error(`Artifact with one of names: ${coverageArtifactNames.join(', ')} not found`);
}
core.info(`Matching coverage artifact found ${matchArtifact?.name}`);
core.info(`Matching coverage artifact found ${matchArtifact.name}`);
core.info(`Coverage artifact name references: ${JSON.stringify({
isFallbackCoverageKey: matchArtifact.name === fallbackCoverageKey,
isMainShaCoverageKey: matchArtifact.name === mainShaCoverageKey,
isCoverageKey: matchArtifact.name === coverageKey,
})}`);
return matchArtifact;
}
/**
Expand Down
53 changes: 43 additions & 10 deletions src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,32 @@ function getOctokitInstance() {
* @returns Coverage artifact
*/
async function getCoverageArtifactByName() {
const { ref: branch, sha: lastCommitSha } =
const { sha: mainSha } = context;
const { sha: prBaseSha } = context?.payload?.pull_request?.base || {};
const { ref: branch, sha: prHeadSha } =
context?.payload?.pull_request?.head || {};

core.info(
`Branch and last commit sha loaded: ${JSON.stringify({
branch,
lastCommitSha,
mainSha,
prBaseSha,
prHeadSha,
})}`,
);
const coverageKey =
core.getInput('coverage-artifact-name') || `coverage-${lastCommitSha}`;
core.getInput('coverage-artifact-name') || `coverage-${prHeadSha}`;
const fallbackCoverageKey = `coverage-${prBaseSha}`;
const mainShaCoverageKey = `coverage-${mainSha}`;
const coverageArtifactNames = [
coverageKey,
fallbackCoverageKey,
mainShaCoverageKey,
];
core.info(
`Workflow artifacts loaded, looking for artifact with name "${coverageKey}"`,
`Workflow artifacts loaded, looking for artifact with one of the following names: ${coverageArtifactNames.join(
'\n',
)}`,
);
const { rest, paginate } = getOctokitInstance();

Expand All @@ -38,23 +52,42 @@ async function getCoverageArtifactByName() {
repo: context.repo.repo,
},
(response, done) => {
if (response.data.find((artifact) => artifact?.name === coverageKey)) {
core.debug('Found matching artifact - stopping pagination');
const matchingArtifact = response.data.find((artifact) =>
coverageArtifactNames.includes(artifact?.name),
);
if (matchingArtifact) {
core.debug(
`Found matching artifact in paginate - stopping pagination: ${JSON.stringify(
matchingArtifact,
)}`,
);
done();
}
return response.data;
},
);
core.info(`Artifacts loaded: ${artifacts.length}`);
core.debug(JSON.stringify(artifacts.slice(0, 15), null, 2));

// Filter artifacts to coverage-$sha
const matchArtifact = artifacts.find(
(artifact) => artifact?.name === coverageKey,
const matchArtifact = artifacts.find((artifact) =>
coverageArtifactNames.includes(artifact?.name),
);
if (!matchArtifact) {
throw new Error(`Artifact with name "${coverageKey}" not found`);
throw new Error(
`Artifact with one of names: ${coverageArtifactNames.join(
', ',
)} not found`,
);
}
core.info(`Matching coverage artifact found ${matchArtifact?.name}`);
core.info(`Matching coverage artifact found ${matchArtifact.name}`);
core.info(
`Coverage artifact name references: ${JSON.stringify({
isFallbackCoverageKey: matchArtifact.name === fallbackCoverageKey,
isMainShaCoverageKey: matchArtifact.name === mainShaCoverageKey,
isCoverageKey: matchArtifact.name === coverageKey,
})}`,
);
return matchArtifact;
}

Expand Down

0 comments on commit 10c93b9

Please sign in to comment.