Skip to content

Commit

Permalink
Diff features by fetching from GitHub Actions
Browse files Browse the repository at this point in the history
This substantially speeds up feature diffing, where the enumeration has
already been done in GitHub Actions. Where possible, it trys to fetch
from GitHub Actions, or falls back to using a worktree to enumerate
directly.

See mdn#13695 for
background.
  • Loading branch information
ddbeck committed Nov 24, 2021
1 parent b59de14 commit 16ada9c
Showing 1 changed file with 53 additions and 2 deletions.
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

0 comments on commit 16ada9c

Please sign in to comment.