From 6ef6f7a26ccd1aaab57b1ff5ccb8626506ff0449 Mon Sep 17 00:00:00 2001 From: Ryan Park Date: Thu, 28 Jul 2022 11:16:56 -0700 Subject: [PATCH 1/4] feat: add retag-release workflow to add a vX.Y.Z tag for our GitHub Action --- .github/workflows/retag-release.yaml | 53 ++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 .github/workflows/retag-release.yaml diff --git a/.github/workflows/retag-release.yaml b/.github/workflows/retag-release.yaml new file mode 100644 index 000000000..340394c07 --- /dev/null +++ b/.github/workflows/retag-release.yaml @@ -0,0 +1,53 @@ +# 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/readme@v1.2.3" because that's +# the usual GitHub convention. + +name: retag-release + +on: + release: + types: [created] + +jobs: + retag-release: + runs-on: ubuntu-latest + steps: + - uses: actions/github-script@v6.1.0 + with: + script: | + 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.'); From 31655fa6efd5f083b6430069af9e76f8b2909bf1 Mon Sep 17 00:00:00 2001 From: Ryan Park Date: Thu, 28 Jul 2022 12:44:51 -0700 Subject: [PATCH 2/4] Update .github/workflows/retag-release.yaml Co-authored-by: Kanad Gupta --- .github/workflows/retag-release.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/retag-release.yaml b/.github/workflows/retag-release.yaml index 340394c07..9957bef59 100644 --- a/.github/workflows/retag-release.yaml +++ b/.github/workflows/retag-release.yaml @@ -14,7 +14,7 @@ jobs: retag-release: runs-on: ubuntu-latest steps: - - uses: actions/github-script@v6.1.0 + - uses: actions/github-script@v6 with: script: | const { owner, repo } = context.repo; From b95d130e5239c36b870ca9ede8082186a1ec5d08 Mon Sep 17 00:00:00 2001 From: Ryan Park Date: Thu, 28 Jul 2022 13:02:40 -0700 Subject: [PATCH 3/4] refactor: move code out of YAML and into bin/ script --- .github/workflows/retag-release.yaml | 37 ++----------------------- bin/retag-release.js | 41 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 35 deletions(-) create mode 100644 bin/retag-release.js diff --git a/.github/workflows/retag-release.yaml b/.github/workflows/retag-release.yaml index 9957bef59..5f4d26c69 100644 --- a/.github/workflows/retag-release.yaml +++ b/.github/workflows/retag-release.yaml @@ -14,40 +14,7 @@ jobs: retag-release: runs-on: ubuntu-latest steps: + - uses: actions/checkout@v3 - uses: actions/github-script@v6 with: - script: | - 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.'); + script: require('./bin/retag-release.js')(github, context); diff --git a/bin/retag-release.js b/bin/retag-release.js new file mode 100644 index 000000000..311ef10c3 --- /dev/null +++ b/bin/retag-release.js @@ -0,0 +1,41 @@ +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 + }; +} From 2aa4c04ae97b88eeff6ed7bb380f3a550ce52967 Mon Sep 17 00:00:00 2001 From: Ryan Park Date: Thu, 28 Jul 2022 13:05:11 -0700 Subject: [PATCH 4/4] fix: lint fixes --- bin/retag-release.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bin/retag-release.js b/bin/retag-release.js index 311ef10c3..113e6098d 100644 --- a/bin/retag-release.js +++ b/bin/retag-release.js @@ -1,10 +1,11 @@ +/* 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; + return {}; } const newTag = `v${oldTag}`; console.log(`Retagging release "${oldTag}" as "${newTag}".`); @@ -36,6 +37,6 @@ module.exports = async (github, context) => { return { original_tag: oldTag, new_tag: newTag, - sha + sha, }; -} +};