Skip to content

Instantly share code, notes, and snippets.

@cdrini
Created July 13, 2020 14:33
Show Gist options
  • Save cdrini/c6c0c3d5cecd5609b17f12f837170edb to your computer and use it in GitHub Desktop.
Save cdrini/c6c0c3d5cecd5609b17f12f837170edb to your computer and use it in GitHub Desktop.
const packageJson = require('./package.json');
const shrinkwrapJson = require('./npm-shrinkwrap.json');
const fetch = require('node-fetch');
const fs = require('fs');
const { execSync } = require('child_process');
const GH_API_ROOT = 'https://api.github.com';
const REPO = "internetarchive/bookreader";
async function main() {
// Make sure branching off of latest master
execSync(`git checkout master`, {stdio: 'inherit'});
execSync(`git pull origin master`, {stdio: 'inherit'});
// Fetch the latest release from GitHub
const latestRelease = await fetch(`${GH_API_ROOT}/repos/${REPO}/releases/latest`).then(r => r.json());
const newTag = latestRelease.tag_name;
const [ref] = await fetch(`${GH_API_ROOT}/repos/${REPO}/git/matching-refs/tags/${newTag}`).then(r => r.json());
const newSHA = ref.object.sha;
// Update package.json
packageJson.dependencies.bookreader = `git://github.com/${REPO}.git#${newTag}`;
fs.writeFileSync('package.json', JSON.stringify(packageJson, null, " ") + '\n');
// Update shrinkwrap
shrinkwrapJson.dependencies.bookreader.version = `git://github.com/${REPO}.git#${newSHA}`;
shrinkwrapJson.dependencies.bookreader.from = `git://github.com/${REPO}.git#${newTag}`;
fs.writeFileSync('npm-shrinkwrap.json', JSON.stringify(shrinkwrapJson, null, " ") + '\n');
// Bail if no changes
const diff = execSync('git diff npm-shrinkwrap.json package.json').toString();
if (!diff) {
console.log("Everything already up-to-date");
return;
}
// Create/push branch
execSync(`git checkout -b br-${newTag}`, {stdio: 'inherit'});
execSync(`git add npm-shrinkwrap.json package.json`, {stdio: 'inherit'});
execSync(`git commit -m "Update BookReader to ${newTag}"`, {stdio: 'inherit'});
execSync(`git push origin HEAD`, {stdio: 'inherit'});
// Create MR
const newMR = await fetch(`https://git.archive.org/api/v4/projects/58/merge_request`, {
method: 'POST',
headers: {
'PRIVATE-TOKEN': process.env['GITLAB_TOKEN'],
'Content-type': 'application/json',
},
body: JSON.stringify({
source_branch: `br-${newTag}`,
target_branch: 'master',
title: `Update BookReader to ${newTag}`,
description: `See changelog: ${latestRelease.html_url}`,
labels: 'team-ux',
remove_source_branch: true,
}),
}).then(r => r.json());
// post link to MR on Slack
await fetch(process.env['SLACK_HOOK'], {
method: 'POST',
headers: {
'Content-type': 'application/json',
},
body: JSON.stringify({
text: `Creating a new MR for BookReader ${newTag} at ${newMR.web_url}`
}),
});
}
if (execSync('git status --untracked-files=no --porcelain').toString()) {
throw "Uncommited changes!";
}
main();
@traceypooh
Copy link

ooh, thanks for sharing the stdio inherit - TIL! :)
eg:

execSync(git checkout -b br-${newTag}, {stdio: 'inherit'});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment