generated from FabricMC/fabric-example-mod
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #284 from boholder/ff-ci-with-changelog
- Loading branch information
Showing
8 changed files
with
406 additions
and
126 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,21 @@ | ||
## Pull Request Checklist | ||
[//]: # (The changelog will be added into CHANGELOG.md file automatically by fast-forward workflow.) | ||
|
||
[//]: # (Use `~~ -[ ] ... ~~` markdown deletion syntax to cross out unrelated entries) | ||
[//]: # (Please delete unrelevant changelog sections.) | ||
|
||
- [ ] A new fragment is added in `./doc/news` that describes what is new (refer to issue #174). | ||
## Changelog | ||
|
||
## Describe what you have changed in this PR | ||
### New Features | ||
|
||
[//]: # (See commit messages.) | ||
### Feature Updates | ||
|
||
### Bug Fixes | ||
|
||
### Translation Changes | ||
|
||
### Others | ||
|
||
[//]: # (The "Development Chores" section won't be included as release notes, it's recorded for developers only.) | ||
|
||
### Development Chores | ||
|
||
[//]: # (## Keep Writting If You Have Anything Else To Say) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,30 @@ | ||
name: Fast-forward | ||
name: Fast-forward and Parse Changelog | ||
# Triggered by "/fast-forward" comment in PR. | ||
# Copied from https://github.com/khanshoaib3/stardew-access/blob/development/.github/workflows/fast-forward.yml | ||
# | ||
# ref: | ||
# https://github.com/orgs/community/discussions/4618#discussioncomment-2795556 | ||
# https://medium.com/@_samkitjain/override-githubs-merge-strategies-cea7eb789e23 | ||
|
||
on: | ||
issue_comment: | ||
types: [ created, edited ] | ||
|
||
jobs: | ||
fast_forward: | ||
name: Fast-forward | ||
name: fast-forward | ||
|
||
# Ref: https://docs.github.com/en/actions/using-jobs/assigning-permissions-to-jobs | ||
# contents: For adding the commit | ||
# issues: For adding the success/failure comments | ||
# pull-requests: For some reason, even though I gave write perms to issues, it wasn't able to send the success/failure comment to PR, so maybe having pull-requests to write will? | ||
permissions: | ||
contents: write | ||
issues: write | ||
pull-requests: write | ||
|
||
runs-on: ubuntu-latest | ||
|
||
# Only run the job if:- | ||
# 1. It is a pull request | ||
# 2. It's not closed | ||
# 3. The comment was by the owner or one of the collaborator | ||
# 4. The comment message is "/fast-forward" | ||
if: | | ||
github.event.issue.pull_request && | ||
!github.event.issue.closed_at && | ||
|
@@ -22,27 +33,104 @@ jobs: | |
contains(github.event.comment.body, '/fast-forward') | ||
steps: | ||
- name: Generate access token | ||
uses: tibdex/github-app-token@v2 | ||
id: generate-token | ||
with: | ||
app_id: ${{ secrets.LOKSMITH_ID }} | ||
private_key: ${{ secrets.LOCKSMITH_PRIVATE_KEY }} | ||
|
||
- name: Fetch repository | ||
uses: actions/checkout@v4 | ||
with: | ||
token: ${{ steps.generate-token.outputs.token }} | ||
fetch-depth: 0 | ||
token: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
# API: https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#get-a-pull-request | ||
- name: Get PR details | ||
uses: octokit/[email protected] | ||
id: get-pr-details | ||
with: | ||
route: GET /repos/{repository}/pulls/{pull_number} | ||
repository: ${{ github.repository }} | ||
pull_number: ${{ github.event.issue.number }} | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- name: Set environment variables | ||
run: | | ||
MERGE_STATUS=${{ fromJson(steps.get-pr-details.outputs.data).mergeable_state }} | ||
if [ "$MERGE_STATUS" != "clean" ]; then echo "COMMENT=[Fast Forward CI] ${{ env.HEAD_REF }} cannot be merged into ${{ env.BASE_REF }} at the moment." >> $GITHUB_ENV; fi | ||
echo "MERGE_STATUS=$MERGE_STATUS" >> $GITHUB_ENV | ||
echo "BASE_REF=${{ fromJson(steps.get-pr-details.outputs.data).base.ref }}" >> $GITHUB_ENV | ||
echo "HEAD_REF=${{ fromJson(steps.get-pr-details.outputs.data).head.ref }}" >> $GITHUB_ENV | ||
- name: Checkout pull request | ||
run: gh pr checkout ${{ github.event.issue.number }} | ||
# Merges the head branch into base branch | ||
# Only runs if the merge status is "clean", clean might refer to when the merge button is green in the PR's page, there's no clear indication of it's values in docs | ||
# For forks the following script adds the fork as a remote, them merges it into base | ||
- name: Merge the head branch into base in a fast forward manner only | ||
if: ${{ env.MERGE_STATUS == 'clean' }} | ||
run: | | ||
git config --global user.email "[email protected]" | ||
git config --global user.name "GitHub Action" | ||
git checkout ${{ env.BASE_REF }} | ||
git pull origin ${{ env.BASE_REF }} | ||
if ${{ fromJson(steps.get-pr-details.outputs.data).head.repo.fork }}; then | ||
USER_NAME=${{ fromJson(steps.get-pr-details.outputs.data).head.user.login }} | ||
git remote add $USER_NAME ${{ fromJson(steps.get-pr-details.outputs.data).head.repo.clone_url }} | ||
git pull $USER_NAME ${{ env.HEAD_REF }} | ||
git merge --ff-only $USER_NAME/${{ env.HEAD_REF }} | ||
else | ||
git pull origin ${{ env.HEAD_REF }} | ||
git merge --ff-only origin/${{ env.HEAD_REF }} | ||
fi | ||
echo "COMMENT=\[Fast Forward CI\] ${{ env.HEAD_REF }} merged into ${{ env.BASE_REF }}..." >> $GITHUB_ENV | ||
# Executes the python script to copy the changelog from the PR's body to docs/changelogs/latest.md file | ||
# It also saves the response into response.txt at the repo's base | ||
# Cuts the lines before "## Changelog" line in the PR's body using the "sed" command | ||
# Ref: https://stackoverflow.com/a/35966027 | ||
- name: Copy the changelogs from pr message to latest.md | ||
if: ${{ env.MERGE_STATUS == 'clean' }} | ||
run: | | ||
cd ./doc/changelogs | ||
echo "$ISSUE_BODY" > pr_msg.txt | ||
python copy_changelogs_to.py pr_msg.txt latest.md > ../../response.txt | ||
cat ../../response.txt | ||
cd ../../ | ||
env: | ||
GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }} | ||
ISSUE_BODY: ${{ github.event.issue.body }} | ||
|
||
- name: Fast-forward & push | ||
# Commit the changes if there was no error i.e., there was no issue in fetching the files and there was actually a changelog found/copied | ||
- name: Commit the changes (if any) | ||
if: ${{ env.MERGE_STATUS == 'clean' }} | ||
run: | | ||
export PR_COMMIT=$(git rev-parse HEAD) | ||
git checkout ${{ github.event.pull_request.base.ref }} | ||
git merge --ff-only "$PR_COMMIT" | ||
git push origin ${{ github.event.pull_request.base.ref }} | ||
if [[ "$( tail ./response.txt -n 1 )" = "No changes to write\!" ]] || [[ "$( tail ./response.txt -n 1 )" =~ "File \`(.*?)\` not found\!" ]]; then | ||
echo "COMMENT=${{ env.COMMENT }}\nNo changelogs found..." >> $GITHUB_ENV | ||
else | ||
git add doc/changelogs/latest.md | ||
git commit -m "chore(ff ci): add changelogs from #${{ github.event.issue.number }} to CHANGELOG.md" | ||
echo "COMMENT=${{ env.COMMENT }}\nCopied the changelogs from pull request body to doc/CHANGELOG.md..." >> $GITHUB_ENV | ||
fi | ||
- name: Push to origin | ||
if: ${{ env.MERGE_STATUS == 'clean' }} | ||
run: | | ||
git push origin | ||
echo "COMMENT=${{ env.COMMENT }}\nPushed the changes to origin." >> $GITHUB_ENV | ||
# Post a success/failure comment to the PR. | ||
- name: Add success/failure comment to PR | ||
uses: octokit/[email protected] | ||
with: | ||
route: POST /repos/{repository}/issues/{issue_number}/comments | ||
repository: ${{ github.repository }} | ||
issue_number: ${{ github.event.issue.number }} | ||
body: ${{ env.COMMENT }} | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
# Post a failure message when any of the previous steps fail. | ||
- name: Add failure comment to PR | ||
if: ${{ failure() }} | ||
uses: octokit/[email protected] | ||
with: | ||
route: POST /repos/{repository}/issues/{issue_number}/comments | ||
repository: ${{ github.repository }} | ||
issue_number: ${{ github.event.issue.number }} | ||
body: \[Fast Forward CI\] PR cannot be merged in. Check the Actions tab for details. | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
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
Oops, something went wrong.