Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update GitHub Actions Workflow for Merge Conflict #269

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions .github/workflows/pr-merge-conflict.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# This action is centrally managed in https://github.com/asyncapi/.github/
# Don't make changes to this file in this repo as they will be overwritten with changes made to the same file in above mentioned repo

# This action handles merge conflicts and provides auto-rebase functionality for pull requests
name: Merge Conflict and Auto-Rebase

on:
pull_request_target:
types: [opened, reopened, synchronize, edited, ready_for_review]
issue_comment:
types: [created]

jobs:
check-merge-conflicts:
if: github.event_name == 'pull_request_target'
runs-on: ubuntu-latest
outputs:
conflicts: ${{ steps.conflicts.outputs.conflicts }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Check for merge conflicts
id: conflicts
run: |
git fetch origin ${{ github.base_ref }}
git merge origin/${{ github.base_ref }} --no-commit --no-ff
if [[ $(git ls-files -u | wc -l) -gt 0 ]]; then
echo "Merge conflicts detected."
echo "conflicts=true" >> $GITHUB_ENV
git merge --abort
else
echo "No merge conflicts."
echo "conflicts=false" >> $GITHUB_ENV
fi
- name: Set Output
id: set_output
run: echo "::set-output name=conflicts::${{ env.conflicts }}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this way is a long time deprecated method for exposing data from step
also, what is the point of this step if in the previous one you already properly expose conflicts variable?

example from other workflows -> https://github.com/asyncapi/.github/blob/master/.github/workflows/if-nodejs-pr-testing.yml#L35

- name: Post Merge Conflict Notification
if: steps.conflicts.outputs.conflicts == 'true'
uses: marocchino/sticky-pull-request-comment@3d60a5b2dae89d44e0c6ddc69dd7536aec2071cd #use 2.5.0 https://github.com/marocchino/sticky-pull-request-comment/releases/tag/v2.5.0
with:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
header: merge-conflict-warning
message: |
⚠️ Merge conflicts detected. Please resolve them.
**Options to Resolve Conflicts:**
- **Manually:** Resolve the conflicts in your local environment and push the changes.
- **Automated Commands:** Use `/rebase`, `/solve_conflict`, or `/sf` to automatically rebase this PR.
**Good Practices:**
- Fetch and merge the latest changes from the base branch into your branch.
- Consider using a visual diff tool to clearly see and resolve conflicts.
- Test your changes after resolving to ensure nothing is broken.
For more detailed guidance, refer to your project's contributing guidelines or documentation.
auto-rebase:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the indentation proper? should't it be a new job instead of part of message?

did you test the workflow somewhere?

if: github.event_name == 'issue_comment' && github.event.issue.pull_request && startsWith(github.event.comment.body, '/rebase')
runs-on: ubuntu-latest
steps:
- name: Checkout the latest code
uses: actions/checkout@v4
with:
token: ${{ secrets.GH_TOKEN }}
fetch-depth: 0
- name: Automatic Rebase
run: |
git fetch origin ${{ github.base_ref }}
git rebase origin/${{ github.base_ref }}
if [[ $? -ne 0 ]]; then
echo "::error::Automatic rebase failed. Manual resolution required."
exit 1
fi
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
- name: Post Success Message
if: success()
uses: actions/github-script@v6
with:
script: |
await github.rest.issues.createComment({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why one time you use marocchino/sticky-pull-request-comment and another manually add comment using GH API?

issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'Congratulations! 🎉 The automatic rebase was successful.'
});
- name: Post Failure Message
if: failure()
uses: actions/github-script@v6
with:
script: |
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'The automatic rebase encountered issues and was not successful. Please manually resolve these conflicts and push your changes.'
});




Loading