Skip to content

Commit

Permalink
feat: add retag-release workflow to add a vX.Y.Z tag for our GitHub A…
Browse files Browse the repository at this point in the history
…ction (#545)

* feat: add retag-release workflow to add a vX.Y.Z tag for our GitHub Action

* Update .github/workflows/retag-release.yaml

Co-authored-by: Kanad Gupta <[email protected]>

* refactor: move code out of YAML and into bin/ script

* fix: lint fixes

Co-authored-by: Kanad Gupta <[email protected]>
  • Loading branch information
RyanGWU82 and kanadgupta authored Jul 28, 2022
1 parent fe24984 commit d600ef4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/retag-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# This script adds a "vX.Y.Z" tag to every new release.
#
# Our releases are tagged like "1.2.3" but we want people to be able to write
# GitHub Action workflows to say "uses: readmeio/[email protected]" because that's
# the usual GitHub convention.

name: retag-release

on:
release:
types: [created]

jobs:
retag-release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/github-script@v6
with:
script: require('./bin/retag-release.js')(github, context);
42 changes: 42 additions & 0 deletions bin/retag-release.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* eslint-disable no-console */
module.exports = async (github, context) => {
const { owner, repo } = context.repo;
const oldTag = context.payload.release.tag_name;
if (!oldTag.match(/^[0-9]+\.[0-9]+\.[0-9]+$/)) {
console.log('Not retagging this release: This script will only retag releases that use');
console.log(`semantic versioning, like "1.2.3", but this release's tag is "${oldTag}".`);
return {};
}
const newTag = `v${oldTag}`;
console.log(`Retagging release "${oldTag}" as "${newTag}".`);

const oldRef = await github.rest.git.getRef({
owner,
repo,
ref: `tags/${oldTag}`,
});
if (oldRef.status < 200 || oldRef.status >= 400) {
console.log(oldRef);
throw new Error(`GitHub API call returned HTTP status code ${oldRef.status}`);
}
const sha = oldRef.data.object.sha;
console.log(`Found tag "${oldTag}"; commit hash is ${sha}`);

console.log(`Creating tag "${newTag}" pointing to commit hash ${sha}...`);
const newRef = await github.rest.git.createRef({
owner,
repo,
ref: `refs/tags/${newTag}`,
sha,
});
if (newRef.status < 200 || newRef.status >= 400) {
console.log(newRef);
throw new Error(`GitHub API call returned HTTP status code ${newRef.status}`);
}
console.log('Successfully retagged this release.');
return {
original_tag: oldTag,
new_tag: newTag,
sha,
};
};

0 comments on commit d600ef4

Please sign in to comment.