Skip to content

Commit

Permalink
🏗 Create tag with release tagger if it does not exists (for CPs) (amp…
Browse files Browse the repository at this point in the history
  • Loading branch information
estherkim authored Oct 19, 2021
1 parent f57e57f commit b6aab41
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 21 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/release-tagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ on:
time:
description: 'action time'
required: true
sha:
description: 'commit sha'
required: true
jobs:
tagger:
environment: release_tagger
Expand All @@ -26,6 +29,7 @@ jobs:
with:
node-version: lts/*
- uses: actions/checkout@v2
- run: bash ./.github/workflows/install_dependencies.sh
- name: Run tagger
run: |
npm ci
Expand All @@ -34,6 +38,7 @@ jobs:
${{ github.event.inputs.head }} \
${{ github.event.inputs.base }} \
${{ github.event.inputs.channel }} \
${{ github.event.inputs.time }}
${{ github.event.inputs.time }} \
${{ github.event.inputs.sha }}
env:
GITHUB_TOKEN: ${{ secrets.AMPPROJECTBOT }}
25 changes: 15 additions & 10 deletions build-system/release-tagger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
* 5. time (in UTC, Y-%m-%d %H:%M:%S)
*/

const [action, head, base, channel, time] = process.argv.slice(2);

const argv = require('minimist')(process.argv.slice(2));
const dedent = require('dedent');
const {addLabels, removeLabels} = require('./label-pull-requests');
const {createOrUpdateTracker} = require('./update-issue-tracker');
const {cyan, magenta} = require('kleur/colors');
const {log} = require('../common/logging');
const {makeRelease} = require('./make-release');
const {makeRelease, maybeGetRelease} = require('./make-release');
const {publishRelease, rollbackRelease} = require('./update-release');

const {action, base, channel, head, sha, time} = argv;

/**
* Promote actions
* @return {Promise<void>}
Expand All @@ -30,22 +31,26 @@ async function _promote() {
head: ${magenta(head)}
base: ${magenta(base)}
channel: ${magenta(channel)}
time: ${magenta(time)}`)
time: ${magenta(time)}
sha: ${magenta(sha)}`)
);

const supportedChannels = ['beta-opt-in', 'beta-percent', 'stable', 'lts'];
if (!supportedChannels.includes(channel)) {
return;
}

if (channel == 'stable') {
await publishRelease(head);
log('Published release', magenta(head));
const release = await maybeGetRelease(head);
if (!release) {
const {'html_url': url} = await makeRelease(head, base, channel, sha);
log('Created release', magenta(head), 'at', cyan(url));
} else {
log('Found release', magenta(head), 'at', cyan(release.url));
}

if (channel == 'beta-opt-in') {
await makeRelease(head, base, channel);
log('Created release', magenta(head));
if (['stable', 'lts'].includes(channel)) {
const {'html_url': url} = await publishRelease(head);
log('Published release', magenta(head), 'at', cyan(url));
}

if (['beta-percent', 'stable', 'lts'].includes(channel)) {
Expand Down
35 changes: 29 additions & 6 deletions build-system/release-tagger/make-release.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
const dedent = require('dedent');
const {
createRelease,
createTag,
getPullRequestsBetweenCommits,
getRef,
getRelease,
} = require('./utils');
const {getExtensions, getSemver} = require('../npm-publish/utils');
const {GraphQlQueryResponseData} = require('@octokit/graphql'); //eslint-disable-line no-unused-vars

const prereleaseConfig = {
'beta-opt-in': true,
'beta-percent': true,
'stable': false,
'lts': false,
Expand Down Expand Up @@ -160,18 +163,21 @@ function _createBody(head, base, prs) {
([major, {packages, unchanged}]) => dedent`\
<h2>npm packages @ ${getSemver(major, head)}</h2>
${Object.entries(packages)
.sort()
.map(
([extension, prs]) =>
`<b>${extension}</b>\n<ul>${[...prs].join('\n')}</ul>`
)
.join('\n')}
<b>Packages not changed:</b> <i>${[...unchanged].join(', ')}</i>`
<b>Packages not changed:</b> <i>${[...unchanged]
.sort()
.join(', ')}</i>`
)
.join('\n')}
<h2>Changes by component</h2>
${components.join('')}\
${components.sort().join('')}\
`;

const patched = head.slice(0, -3) + '000';
Expand All @@ -187,19 +193,36 @@ function _createBody(head, base, prs) {
}

/**
* Main function
* Get release if exists
* @param {string} head
* @return {!Promise}
*/
async function maybeGetRelease(head) {
try {
return await getRelease(head);
} catch {}
}

/**
* Make release
* @param {string} head
* @param {string} base
* @param {string} channel
* @param {string} sha
* @return {Promise<Object>}
*/
async function makeRelease(head, base, channel) {
const {object: headRef} = await getRef(head);
async function makeRelease(head, base, channel, sha) {
let headRef;
try {
headRef = (await getRef(head)).object;
} catch (_) {
headRef = (await createTag(head, sha)).object;
}
const {object: baseRef} = await getRef(base);
const prs = await getPullRequestsBetweenCommits(headRef.sha, baseRef.sha);
const body = _createBody(head, base, prs);
const prerelease = prereleaseConfig[channel];
return await createRelease(head, headRef.sha, body, prerelease);
}

module.exports = {makeRelease};
module.exports = {maybeGetRelease, makeRelease};
38 changes: 34 additions & 4 deletions build-system/release-tagger/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function createIssue(body, label, title) {
* @return {Promise<Object>}
*/
async function createRelease(tag, commit, body, prerelease) {
return await octokit.rest.repos.createRelease({
const {data} = await octokit.rest.repos.createRelease({
owner,
repo,
name: tag,
Expand All @@ -84,6 +84,7 @@ async function createRelease(tag, commit, body, prerelease) {
body,
prerelease,
});
return data;
}

/**
Expand Down Expand Up @@ -121,15 +122,16 @@ async function getRelease(tag) {
* @param {'open' | 'closed'} state
* @return {Promise<Object>}
*/
function updateIssue(body, number, title, state = 'open') {
return octokit.rest.issues.update({
async function updateIssue(body, number, title, state = 'open') {
const {data} = await octokit.rest.issues.update({
owner,
repo,
'issue_number': number,
title,
body,
state,
});
return data;
}

/**
Expand All @@ -139,12 +141,13 @@ function updateIssue(body, number, title, state = 'open') {
* @return {Promise<Object>}
*/
async function updateRelease(id, changes) {
return await octokit.rest.repos.updateRelease({
const {data} = await octokit.rest.repos.updateRelease({
owner,
repo,
'release_id': id,
...changes,
});
return data;
}

/**
Expand Down Expand Up @@ -268,9 +271,36 @@ async function getRef(tag) {
return data;
}

/**
* Create git tag and ref
* @param {string} tag
* @param {string} sha
* @return {!Promise}
*/
async function createTag(tag, sha) {
await octokit.rest.git.createTag({
owner,
repo,
tag,
message: tag,
object: sha,
type: 'commit',
});

// once a tag object is created, create a reference
const {data} = await octokit.rest.git.createRef({
owner,
repo,
ref: `refs/tags/${tag}`,
sha,
});
return data;
}

module.exports = {
createIssue,
createRelease,
createTag,
getLabel,
getIssue,
getPullRequestsBetweenCommits,
Expand Down

0 comments on commit b6aab41

Please sign in to comment.