BranchProtectionBot is a GitHub App that enables you to protect your default branch.
To prevent commits from being lost due to accidental force pushes, you should protect your branch. However, it's very hard to apply protection rules every single time right after your organization member creates a repository. This bot automates the process to apply the rule and inform you about the protection rules applied to the repository.
BranchProtectionBot is easy to deploy. Please visit HERE to install the bot in your organization.
There are three steps to protect your branch and manage your protection rules
- Installing the bot.
- A new branch created in the organization will be protected.
- You can edit the default rule settings for branch protection.
- When your organization member creates a new repository in the organization, BranchProtectionBot streams the repository's "created" event.
- Azure Functions trigger BranchProtectionBot API. (
POST: /api/ProtectMaster) - BranchProtectionBot API initiates the repository with README.md. The default branch is also created at the same time.
- BranchProtectionBot API protects the default branch. BranchProtectionBot supports both master and main as the default branch.
- BranchProtectionBot API creates an issue in the repository. It also mentions a specific user.
- When the bot is installed, registration callback api on Azure Functions is called. (
GET: /api/ReceiveInstallation) - The api registers organization and installation ID. After registration, the api provide a password to manage the servie setting.
- User can edit BranchProtectionBot setting by accessing the edit page.(
GET: /api/EditRule) - The API updates the CosmosDB.(
POST: /api/UpdateRule)
You can host BranchProtectionBot by yourself. If you want to deploy the app, you will need an Azure environment. The app is hosted on Azure Functions and connect to Azure CosmosDB. Deployment can be easily done by Azure Bicep (Infrastructure as Code) and GitHub Actions.
- Your GitHub Apps
- Azure (Azure Functions and Azure CosmosDB)
-
Fork and clone this repository
-
Deploy Azure Resources with Infrastructure as Code templates. Run the script as follow
# Define your resource group name and the region to deploy ResourceGroupName=ProtectionBotRG ResourceRegion=japaneast # Deploy Azure resources with Azure Bicep, IaC service az login az group create --name $ResourceGroupName --location $ResourceRegion az deployment group create --resource-group $ResourceGroupName --template-file main.bicep --parameters appInsightsLocation=$ResourceRegion
NOTE: You will get the param,
postInstallationUrlandwebhookUrlafter the successfull deployment. Please note the params. These params will be used for setting up GitHub App. -
Setup GitHub Actions for the app deployment Get the secrets that you will use for GitHub Actions pipeline beforehand
# Get the Service Principal Info az ad sp create-for-rbac --name "BranchProtectionBot" --role contributor --scopes /subscriptions/<YOUR SUBSCRIPTION ID> --sdk-auth
Set the params in the GitHub Actions secrets
Input Parameters AZURE_CREDENTIALS json string you get from running az ad sp create-for-rbaccommandAZURE_FUNCTIONAPP_NAME What you get from the IaC deployment output (example: botapp-ph7bvwxlx7l3g) -
Run GitHub Actions
After you have done the configuration, next you need to setup the GitHub App
-
On GitHub, go to
Settings>Developer settings>GitHub Apps -
Then click
New GitHub Appbutton -
Register new GitHub App
-
Set the
Repository PermissionsOnly the important settings are listed here.-
General Settings
Permissions Value Detail Administration Read & Write This permission is necessary to apply branch protection rule Contents Read & Write This permission is necessary to initiate repository with README.md Issues Read & Write This permission is necessary to create an issue -
Events subscription settings
Subscribe to events Check Repository true -
Confirm Creation
-
-
After the creation Please memo your App ID. This will be used for API's App Configuration.

-
Generate and Download GitHub Apps' PEM key You will need PEM as well for API's App Configuration.

Please refer to the documentation to get PEM file and App ID. And to learn more about GitHub Apps, please check this documentation
-
Configure your Azure Function's apps config. You are almost there! Finally, you need to run the GitHub App PEM and App ID for function app
# Set your GitHub App ID to a variable app_id=<YOUR_APP_ID> # Set your GitHub App PEM to a variable pem_path=<YOUR PATH TO THE>/branchprotectionbot.private-key.pem pem_str=$(awk 'NF {sub(/\r/, ""); printf "%s\\n",$0;}' $pem_path) echo $pem_str # Configure API's setting az webapp config appsettings set -g $ResourceGroupName -n <YOUR_APP_NAME> --settings gh_app_pem=$pem_str gh_app_id=$app_idPlease refer to the link for Azure Functions configuration
The GitHub App is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Please see LICENSE.txt in this repository for the full text of the license.






