Skip to content

Commit

Permalink
Move release logic into sidecar script
Browse files Browse the repository at this point in the history
  • Loading branch information
nwiltsie committed Jul 31, 2024
1 parent a1adc7a commit de8e7ff
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 42 deletions.
63 changes: 21 additions & 42 deletions .github/workflows/create-new-tag.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,30 @@ jobs:
runs-on: ubuntu-latest

steps:
- name: Dump GitHub context
# Get the version of _this_ repository that is in use so that we can use
# sidecar scripts
- id: workflow-parsing
name: Get SHA of reusuable workflow
env:
GITHUB_CONTEXT: ${{ toJson(github) }}
run: echo "$GITHUB_CONTEXT"
REPO: ${{ github.repository }}
RUN_ID: ${{ github.run_id }}
GH_TOKEN: ${{ github.token }}
run: |
ACTION_DATA=$(gh api "repos/$REPO/actions/runs/$RUN_ID")
echo "::debug::$ACTION_DATA"
SHA=$(echo "$ACTION_DATA" | jq -r '.referenced_workflows | .[] | select(.path | startswith("uclahs-cds/tool-create-release")).sha')
echo "SHA=$SHA" >> "$GITHUB_OUTPUT"
- name: Checkout reusable repository
uses: actions/checkout@v4
with:
repository: uclahs-cds/tool-create-release
ref: ${{ steps.workflow-parsing.outputs.SHA }}
token: ${{ secrets.UCLAHS_CDS_REPO_READ_TOKEN }}

- id: parse-version
uses: actions/github-script@v7
with:
script: |
// Sanity-check that this was called from an appropriate merge event and
// extract the version number embedded in the branch name.
if (context.eventName !== 'pull_request') {
core.setFailed('Workflow requires pull_request events')
process.exit()
}
if (!context.payload.pull_request.merged || context.payload.pull_request.state !== 'closed') {
core.setFailed('Workflow should only be called on merged and closed PRs')
process.exit()
}
if (!['Bot', 'Organization'].includes(context.payload.pull_request.user.type)) {
core.setFailed('Workflow should only be called for Bot- or Organization-generated release PRs')
process.exit()
}
// This regex needs to kept in-sync with the pattern in create-release-pr.yaml
const regex = /^automation-create-release-(.*)$/i
const parsed_version = context.payload.pull_request.head.ref.match(regex)
if (!parsed_version || !parsed_version[1].length) {
core.setFailed('Workflow not called from an appropriate branch name')
process.exit()
}
const new_version = parsed_version[1]
github.rest.repos.createRelease({
owner: context.repo.owner,
repo: context.repo.repo,
tag_name: `v${new_version}`,
target_commitish: context.payload.pull_request.merge_commit_sha,
name: `Release ${new_version}`,
draft: true,
generate_release_notes: true,
body: `Automatically generated after merging #${context.payload.number}.`
})
const script = require('./scripts/create-tag.js')
await script({github, context, core})
54 changes: 54 additions & 0 deletions scripts/create-tag.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module.exports = async ({ github, context, core }) => {
// Sanity-check that this was called from an appropriate merge event and
// extract the version number embedded in the branch name.
if (context.eventName !== 'pull_request') {
core.setFailed('Workflow requires pull_request events')
process.exit()
}

if (
!context.payload.pull_request.merged ||
context.payload.pull_request.state !== 'closed'
) {
core.setFailed('Workflow should only be called on merged and closed PRs')
process.exit()
}

if (
!['Bot', 'Organization'].includes(context.payload.pull_request.user.type)
) {
core.setFailed(
'Workflow should only be called for Bot- or Organization-generated release PRs'
)
process.exit()
}

// This regex needs to kept in-sync with the pattern in create-release-pr.yaml
const regex = /^automation-create-release-(.*)$/i
const parsedVersion = context.payload.pull_request.head.ref.match(regex)

if (!parsedVersion || !parsedVersion[1].length) {
core.setFailed('Workflow not called from an appropriate branch name')
process.exit()
}

const newVersion = parsedVersion[1]

await github.rest.repos.createRelease({
owner: context.repo.owner,
repo: context.repo.repo,
tag_name: `v${newVersion}`,
target_commitish: context.payload.pull_request.merge_commit_sha,
name: `Release ${newVersion}`,
draft: true,
generate_release_notes: true,
body: `Automatically generated after merging #${context.payload.number}.`
})

// Attempt to delete the release branch
await github.rest.git.deleteRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `heads/${context.payload.pull_request.head.ref}`
})
}

0 comments on commit de8e7ff

Please sign in to comment.