danielhardej / Auto-Branch-Protect

A GitHub App to automatically protect the main branches in an organisation's repos.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Auto Branch Protect

A GitHub App to automatically protect the main branches in an organisation's repos.

bouncer cat protect branch

How to use it

It's easy! All you need to do is:

  1. Install the GitHub app on your organisation's account.
  2. Create a new repository in your organisation (make sure you initialise the new repository with a README.md!)
  3. The GitHub app will automatically create a branch protection rule for the main branch of each new repository under your organisation.
  4. The app will also automatically create an issue in the new repository, tagging the org admins and notifying them of the new branch protection rules.

The branch protection rules

The default branch protection rules are as follows:

  • Require pull request reviews before merging into main
  • Dismiss stale pull request approvals when new commits are pushed
  • Require review from Code Owners
  • Require status checks to pass before merging
  • Require branches to be up to date before merging
  • Require linear history
  • Do not allow deletions

Top 3 Gotchas

  1. READMEs: Always initialise your new repo with a README.md file. Otherwise, there are no branches to protect!
  2. Private repositories are only for Team plans: Make sure your organisation is on a team plan if you're creating a private repository. Otherwise, you won't be able to create a branch protection rule or assign reviewers for pull requests!
  3. Permissions: Make sure you have the correct permissions to install the GitHub app on your organisation's account. You'll need to be an organisation owner or have admin permissions.

How it works

The GitHub app is written in Node.js and uses GitHub webhooks and the Octokit library to interact with the GitHub API.

The app uses Azure functions, which are triggered by GitHub webhooks, to create a branch protection rule for the main branch of each new repository under your organisation via the GitHub API. The GitHub API is also used to create an issue in the new repository, tagging the org admins and notifying them of the new branch protection settings that were changed (or providing a reason why the branch protections failed, if not.)

99% of the time, the branch protection rules are created with no problems. If an error occurs, it's almost always due to one of the three gotchas mentioned above and an issue will be created telling you what went wrong.

References:

About

A GitHub App to automatically protect the main branches in an organisation's repos.


Languages

Language:JavaScript 100.0%