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

Setup github action to enforce branch naming conventions #26

Open
dheadrick1618 opened this issue Sep 26, 2023 · 5 comments
Open

Setup github action to enforce branch naming conventions #26

dheadrick1618 opened this issue Sep 26, 2023 · 5 comments
Assignees

Comments

@dheadrick1618
Copy link
Member

dheadrick1618 commented Sep 26, 2023

Desc: Setup a github action (yaml script) to fail a PR with main if the branch is not named in the standard accepted convention.

Acceptance Criteria:

  • Fails any PR request if the branch is not named properly
  • Branches must have acceptable name format AND vocabulary or the PR 'test' will fail
@ijoffe
Copy link

ijoffe commented Sep 26, 2023

Thanks for making an issue of this, I meant to earlier.

As an update, I've watched some guides/tutorials and made some notes but haven't started building this action.

Just to confirm, what is the exact branch naming convention? In terms of format (min/max length, pattern, etc) and vocabulary

@ijoffe ijoffe moved this from Todo to In Progress in (OLD) ExAlta3 Project Management for Software Sep 26, 2023
@dheadrick1618
Copy link
Member Author

Thanks for the update. For now I have put the naming conventions along with other standards we should follow (open to modification) front and center on the albertasat github page. We should probably create a seperate repo for rule for contributing later.

@ijoffe
Copy link

ijoffe commented Oct 10, 2023

Took me a lot longer than I thought, but here is the yaml file with bash script that seems to work for enforcing our branch naming convention on my own private test repo:


# This GitHub Action rejects all those branches whose name does not follow the prescribed format
# Written by Isaac Joffe

name: Branch Naming Convention Enforcer

on:
  pull_request:
    types:
      - opened    # run as soon as a prospective pr is opened

jobs:
  enforce_branch_naming_convention:
    permissions: write-all    # to ensure it has permissions to close a pr
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2    # access state of pr

      - name: Close Pull Request
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          # fetch the branch name and submitter username
          branch_name=${{ github.event.pull_request.head.ref }}
          branch_username=${{ github.event.pull_request.user.login }}
          # construct regex pattern for the naming convention
          pattern="^${branch_username}/(hotfix|bugfix|experimental|feature)/[a-zA-Z0-9_-]+$"

          if ! [[ $branch_name =~ $pattern ]]; then
            echo "Branch name: $branch_name"
            echo "PR submitted by: $branch_username"
            echo "Branch naming convention is <author_name>/<branch_type>/<branch_description>, where <branch_type> is one of {hotfix,bugfix,experimental,feature}"
            echo "Branch name does not follow convention, automatically closing pull request..."
            gh pr close ${{ github.event.pull_request.number }}
          else
            echo "Branch name follows convention, no action required"
          fi

How should I go about integrating this with the AlbertaSat repo (I'm assuming just the ex3_simulated_subsystems first)?

How do you want me to test it thoroughly first? It's performed as expected on the few test cases I used and it has very simple logic but it's good to get into a testing workflow

@ijoffe ijoffe moved this from In Progress to Requires Review in (OLD) ExAlta3 Project Management for Software Oct 10, 2023
@dheadrick1618
Copy link
Member Author

Apologies for the delayed response.

I have used the ACT tool in the past to verify yaml github actions scripts locally in the past, to ensure they are behaving as expected without having a bunch of commits to a repo to test. See here: https://github.com/nektos/act

I think making a PR with a branch that just includes the yaml file in a .github/workflows/ directory would be the cleanest way to integrate it into each of our repos.

@cbebe
Copy link
Contributor

cbebe commented Jan 19, 2024

Hello, just dropping my .02.

I think simply having a Git pre-commit hook should be enough for enforcing this, they're all run by Git bash so it would also work on Windows.

https://itnext.io/using-git-hooks-to-enforce-branch-naming-policy-ffd81fa01e5e

Yes, someone can simply override it but... why

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Requires Review
Development

No branches or pull requests

3 participants