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

[FEATURE] Add GitHub Action to Check PR Sync with Base Branch #123

Open
1 task done
jvedsaqib opened this issue Oct 15, 2024 · 1 comment
Open
1 task done

[FEATURE] Add GitHub Action to Check PR Sync with Base Branch #123

jvedsaqib opened this issue Oct 15, 2024 · 1 comment
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@jvedsaqib
Copy link
Collaborator

Is this a unique feature?

  • I have checked "open" AND "closed" issues and this is not a duplicate

Is your feature request related to a problem/unavailable functionality? Please describe.

To avoid merge conflicts and ensure that contributors are working with the latest code before opening or updating a Pull Request (PR), we need a GitHub Action that checks whether the contributor has synced the latest changes from the base branch i.e. the main branch. If their branch is not up-to-date, the action should block the PR and notify the contributor to sync with the latest changes.

Benefits:

  • Reduces the likelihood of merge conflicts.
  • Ensures contributors are working with the most recent version of the codebase.
  • Automates the process of checking if PRs are up-to-date with the base branch.

Proposed Solution

Contributor can check the following :

  • Action triggers on PR events (opened and updated).
  • It checks if the PR branch has the latest commits from the base branch.
  • If not synced, it throws an error and tells the contributor to rebase or pull.
  • If synced, the action passes with a success message.

Screenshots

No response

Do you want to work on this issue?

No

If "yes" to above, please explain how you would technically implement this (issue will not be assigned if this is skipped)

No response

@jvedsaqib jvedsaqib added the enhancement New feature or request label Oct 15, 2024
@rohitinu6 rohitinu6 added the help wanted Extra attention is needed label Oct 17, 2024
@NailaRais
Copy link

To implement a GitHub Action that checks if a pull request (PR) branch is synchronized with the main branch, I propose the following workflow setup. This action aims to reduce merge conflicts and ensure contributors work with the latest code, improving overall collaboration efficiency. Here are the steps and additional features for this solution:

Workflow Setup

  1. Define Workflow Triggers:
    The action should trigger on pull_request events, specifically for opened, synchronize, and reopened event types. This ensures the check runs whenever a PR is first created, updated, or reopened.

  2. Create Workflow File:
    Add a new YAML file in the repository named .github/workflows/check-pr-sync.yml.

  3. Core Workflow Code:
    Below is the code configuration for the action:

name: PR Sync Check with Base Branch

on:
  pull_request:
    types: [opened, synchronize, reopened]

jobs:
  verify-sync:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout PR branch code
        uses: actions/checkout@v3
        with:
          fetch-depth: 0  # Ensures full history for accurate comparison

      - name: Fetch base branch for comparison
        run: |
          BASE_BRANCH="main"
          git fetch origin $BASE_BRANCH

      - name: Verify PR branch is synced with base branch
        id: sync-check
        run: |
          git diff --exit-code origin/$BASE_BRANCH || exit 1

      - name: Notify if PR branch is out-of-sync
        if: failure()
        run: |
          echo "::error ::Your branch is not in sync with the latest changes on the $BASE_BRANCH branch. Please update your branch to avoid merge conflicts."
          exit 1

Additional Features

  • Flexible Base Branch: This workflow can easily support other branches (like develop) by modifying the BASE_BRANCH variable. This is particularly useful in projects where the main branch isn’t the default base for PRs.
  • Detailed Notifications: If the branch is out-of-sync, the workflow logs a detailed error, guiding the contributor to update their branch. This message can also be customized with links to documentation on rebasing and merging for new contributors.
  • Multi-Event Trigger: By including the reopened event trigger, the workflow automatically checks branches that were closed and then reopened, reducing the risk of outdated code in such cases.
  • Optional Slack or Email Notification: This action could be extended to send notifications to a team channel (e.g., Slack) or specific email addresses for additional awareness if a PR fails the sync check.

Explanation of Key Steps

  • Triggering Events: Ensures that every time a PR is opened, updated, or reopened, the action checks if it’s in sync.
  • Checking Out the PR Branch: The action checks out the PR branch code with full history, allowing a complete diff comparison.
  • Sync Verification: Compares the current branch with the latest base branch (usually main). If there’s a mismatch, it exits with an error.
  • Error Notification: The contributor receives a clear error message if their branch is outdated, with instructions to update.

Key Benefits

  • Enhanced Code Consistency: Ensures that every PR branch is working with the most recent code version, reducing potential conflicts.
  • Improved Contributor Experience: Contributors receive prompt notifications and instructions, helping them resolve issues quickly.
  • Automated Code Sync Checks: Eliminates the need for manual checks, making the review process more efficient and consistent.
  • Customizable: Easy to adapt for different branches, specific teams, or customized notifications.

This GitHub Action will help enforce sync checks, maintain code quality, and provide contributors with clear guidelines, ultimately reducing the time spent on merge conflict resolution.

@jvedsaqib Yes, I want to work on this issue. Kindly assign it to me with further info if you have some more guidelines/preferences in mind.

Thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants