Skip to content

Commit

Permalink
Add post-merge hook to update nightly versions
Browse files Browse the repository at this point in the history
  • Loading branch information
lettertwo committed May 5, 2022
1 parent e597c61 commit 9ff055f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 13 deletions.
15 changes: 15 additions & 0 deletions .husky/post-merge
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh
# Generated with husky-init and https://github.com/typicode/husky-4-to-6
. "$(dirname "$0")/_/husky.sh"

# ATLASSIAN: Update self-published (nightly) versions as part of a merge.
# Ideally, this would be done as part of a `pre-merge-commit` hook,
# but git does not support modifying the index during a merge commit.
# So, we hack it in this `post-merge` hook by immediately ammending the commit.
# Note that in cases where the merge commit did not automatically succeed,
# this hook will not be run. See comments in `pre-commit` hook for more.
yarn update-self-published -a
if [[ -f .git/MERGE_HEAD ]]; then
rm .git/MERGE_HEAD
fi
git log -n 1 --pretty=tformat:%s%n%n%b | git commit -F - --amend
11 changes: 11 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,14 @@
. "$(dirname "$0")/_/husky.sh"

yarn lint-staged

# ATLASSIAN: Update self-published (nightly) versions as part of a merge.
# If a merge is committed automatically (e.g., no conflicts), this hook
# will not be run (see comments in `post-merge` for more). However,
# if a merge is not committed automatically (e.g., there are conflicts),
# the `post-merge` hook will not be run. Here we detect that a merge
# is being manually committed and replicate the work our `post-merge` hook
# would have done if the merge commit had proceeded automatically.
if git rev-parse -q --verify MERGE_HEAD >/dev/null 2>&1; then
yarn update-self-published -a
fi
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@
"test:integration-ci": "yarn workspace @parcel/integration-tests test-ci",
"test": "yarn test:unit && yarn test:integration",
"update-readme-toc": "doctoc README.md",
"update-self-published": "node scripts/update-self-published.js",
"version:atlassian": "yarn lerna version patch --exact --force-publish=@atlassian/internal-parcel-utils,@atlassian/parcel-reporter-analytics",
"nightly:release": "lerna publish -y --canary --preid nightly --dist-tag=nightly --exact --force-publish=* --no-git-tag-version --no-push",
"tag:prerelease": "lerna version --exact --force-publish=* --no-git-tag-version --no-push && yarn adjust-versions --exact",
"tag:release": "lerna version --exact --force-publish=* --no-git-tag-version --no-push && yarn adjust-versions",
"adjust-versions": "node scripts/update-config-dependencies.js && node scripts/update-engines-peerdeps.js && node scripts/update-self-published.js",
"adjust-versions": "node scripts/update-config-dependencies.js && node scripts/update-engines-peerdeps.js",
"release": "lerna publish -y from-package --pre-dist-tag=next --no-git-tag-version --no-push",
"prepare": "husky install"
},
Expand Down
46 changes: 34 additions & 12 deletions scripts/update-self-published.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,31 @@ const exec = require('child_process').execSync;
const UPSTREAM = /github.+parcel-bundler\/parcel/;
const NIGHTLY = /.*-nightly\..*/;

if (process.argv.includes('-h') || process.argv.includes('--help')) {
console.log(
[
` Usage: ${path.basename(process.argv[1])} [opts]`,
'',
' Options:',
' -h, --help Show help',
' -a Add updated files to git index',
' (handy in a precommit hook)',
'',
' Looks for self-published packages (e.g., @parcel/transformer-js),',
' and compares their nightly version numbers to the list',
' of published nightly versions (via `yarn info`).',
' If it finds a version that is newer, it updates the version',
' in the package.json.',
'',
' It will use the oldest nightly version that is newer than',
' the latest common commit between HEAD and the upstream default branch.',
].join('\n'),
);
process.exit();
}

const shouldStage = process.argv.includes('-a');

const fromVersionString = str => str.match(/npm:.+@(.+)$/)[1];
const toVersionString = (name, version) => `npm:${name}@${version}`;

Expand Down Expand Up @@ -43,31 +68,29 @@ function getDefaultBranchName(remote) {
* with the timestamp closest to `time` without being younger.
*/
function getNearestNightlyVersion(pkgName, time) {
let candidate;
let candidate = null;
let info = run(`yarn info ${pkgName} --json`);
let versions = [...Object.entries(info.data.time)].reverse();
for (let [version, timestamp] of versions) {
if (NIGHTLY.test(version)) {
let versionTime = new Date(timestamp);
if (versionTime >= time) {
candidate = version;
} else {
break;
}
if (versionTime < time) break;
candidate = version;
}
}
if (candidate) return candidate;
throw new Error('Could not determine an appropriate nightly version!');
return candidate;
}

console.log(`Updating self-published (nightly) versions...`);

let packages = run(
`${path.join(__dirname, '..', 'node_modules', '.bin', 'lerna')} ls --json`,
);

// Fetch the default upstream branch...
let upstream = getUpstreamRemoteName();
let branch = getDefaultBranchName(upstream);
run(`git fetch ${upstream} ${branch}`);
run(`git fetch -q ${upstream} ${branch}`);
// ...so we can determine the latest common ancestor commit.
let baseRef = run(`git merge-base HEAD ${upstream}/${branch}`).split(/\s+/)[0];
// Get the commit time of the latest common ancestor between HEAD and upstream.
Expand All @@ -79,12 +102,11 @@ for (let {location, name} of packages) {
if (pkg.dependencies && 'self-published' in pkg.dependencies) {
let current = fromVersionString(pkg.dependencies['self-published']);
let nightly = getNearestNightlyVersion(name, baseRefTime);
if (current === nightly) {
console.log(`${name} is already on nearest nightly ${nightly}`);
} else {
if (nightly && current !== nightly) {
console.log(`updating ${name} to nearest nightly ${nightly}`);
pkg.dependencies['self-published'] = toVersionString(name, nightly);
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n');
if (shouldStage) run(`git add -u ${pkgPath}`);
}
}
}

0 comments on commit 9ff055f

Please sign in to comment.