Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diff features by fetching from GitHub Actions #13720

Merged
merged 1 commit into from
Nov 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions scripts/diff-features.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ function main({ ref1, ref2, format }) {
refB = `${ref1}`;
}

const aSide = new Set(enumerateFeatures(refA));
const bSide = new Set(enumerateFeatures(refB));
let aSide = enumerate(refA);
let bSide = enumerate(refB);

const results = {
added: [...bSide].filter(feature => !aSide.has(feature)),
Expand All @@ -35,6 +35,57 @@ function main({ ref1, ref2, format }) {
}
}

function enumerate(ref) {
try {
return new Set(getEnumerationFromGithub(ref));
} catch {
console.error('Fetching artifact from GitHub failed. Using fallback.');
return new Set(enumerateFeatures(ref));
}
}

function getEnumerationFromGithub(ref) {
const ENUMERATE_WORKFLOW = '15595228';
const ENUMERATE_WORKFLOW_ARTIFACT = 'enumerate-features';
const ENUMERATE_WORKFLOW_FILE = 'features.json';

const unlinkFile = () => {
try {
fs.unlinkSync(ENUMERATE_WORKFLOW_FILE);
} catch (err) {
if (err.code == 'ENOENT') {
return;
} else {
throw err;
}
}
};

const hash = execSync(`git rev-parse ${ref}`, {
encoding: 'utf-8',
}).trim();
const workflowRun = execSync(
`gh api /repos/:owner/:repo/actions/workflows/${ENUMERATE_WORKFLOW}/runs --jq '.workflow_runs[] | select(.head_sha=="${hash}") | .id'`,
{
encoding: 'utf-8',
},
).trim();

if (!workflowRun) throw Error('No workflow run found for commit.');

try {
unlinkFile();
execSync(
`gh run download ${workflowRun} -n ${ENUMERATE_WORKFLOW_ARTIFACT}`,
);
return JSON.parse(
fs.readFileSync(ENUMERATE_WORKFLOW_FILE, { encoding: 'utf-8' }),
);
} finally {
unlinkFile();
}
}

function enumerateFeatures(ref = 'HEAD') {
// Get the short hash for this ref.
// Most of the time, you check out named references (a branch or a tag).
Expand Down