command-merge-snapshot PR-773 #5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on: | |
repository_dispatch: | |
types: [ merge-snapshot-command ] | |
name: command-merge-snapshot | |
run-name: command-merge-snapshot PR-${{ github.event.client_payload.github.payload.issue.number }} | |
permissions: | |
contents: write | |
pull-requests: write | |
actions: write | |
jobs: | |
merge-snapshot: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/github-script@v6 | |
with: | |
# FYI: `github.rest` exposes `octokit` client. | |
# The method mappings can be found here: https://github.com/octokit/plugin-rest-endpoint-methods.js/blob/main/src/generated/endpoints.ts | |
# Method parameters are available in the api docs: https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#merge-a-pull-request | |
script: | | |
const pr = await github.rest.pulls.get({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
pull_number: '${{ github.event.client_payload.github.payload.issue.number }}' | |
}); | |
const prTitle = pr.data.title | |
const isSnapshotPr = prTitle.includes("chore(snapshot): Prepare for") | |
if(!isSnapshotPr) { | |
console.log("") | |
console.error("/merge-snapshot is supposed to be executed only on pull requests updating the version to snapshot!") | |
console.log("") | |
process.exit(1) | |
} | |
const listCommits = await github.rest.pulls.listCommits({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
pull_number: '${{ github.event.client_payload.github.payload.issue.number }}', | |
headers: { 'X-GitHub-Api-Version': '2022-11-28' } | |
}); | |
//console.log(listCommits.data); | |
const commits = [...listCommits.data].map(c => c.commit); | |
//console.log("Commits: ", commits); | |
const split = commits[0].message.split('\n'); | |
const commit_raw_title = split[0]; | |
const commit_title = commit_raw_title + " (#${{ github.event.client_payload.github.payload.issue.number }})" | |
split.shift() | |
let commit_message = (split.join('\n') + "\nCo-authored-by: " + commits[0].author.name + " <" + commits[0].author.email + ">").trim(); | |
console.log("Squash title: " + commit_title); | |
console.log("Squash message: \n" + commit_message + "\n/Squash message\n"); | |
await github.rest.pulls.merge({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
pull_number: '${{ github.event.client_payload.github.payload.issue.number }}', | |
merge_method: 'squash', | |
commit_title: commit_title, | |
commit_message: commit_message, | |
headers: { 'X-GitHub-Api-Version': '2022-11-28' } | |
}); | |
- uses: actions/checkout@v3 | |
- name: Trigger prepare-release workflow. | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
gh workflow run prepare-release.yaml | |
- name: Trigger build workflow. | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
gh workflow run build.yml | |
# Add reaction to the comment | |
- name: Add success reaction | |
uses: peter-evans/create-or-update-comment@v2 | |
if: ${{ success() }} | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
repository: ${{ github.event.client_payload.github.payload.repository.full_name }} | |
comment-id: ${{ github.event.client_payload.github.payload.comment.id }} | |
reaction-type: hooray | |
body: | | |
> Success: Action was successful! Check the [logs](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) | |
- name: Add failure response | |
uses: peter-evans/create-or-update-comment@v2 | |
if: ${{ failure() }} | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
repository: ${{ github.event.client_payload.github.payload.repository.full_name }} | |
comment-id: ${{ github.event.client_payload.github.payload.comment.id }} | |
reaction-type: -1 | |
body: | | |
> FAILURE: Action failed! Check the [logs](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}) |