Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force a branch naming convention in Azure DevOps Git

We use Git hosted in Azure DevOps for all of our source code. So far we have used Git Hooks to ensure that team members follow a branch naming convention {branchtype}/{username}/{friendlyname}/{workitemtype}{workitemid}.

Examples:

  • dev/dparkar/addauth/ta123456
  • hf/jsmith/memoryleak/bu11111

The branch naming convention allow us to clearly understand whether it's a regular development branch or a hotfix branch and which work item it is associated with, among other things.

To setup Git Hooks locally, team members were required to run a script locally before starting to contribute. This was fine as the script was doing additional setup besides just setting up Git Hooks. But we don't need that additional setup anymore and therefore we are looking to remove the script completely.

Is there a way to force branch naming conventions on the server side?

I noticed there is documentation for Pull Request Status Server, but was wondering if there is something OOTB which just needs to be configured in Azure DevOps.

like image 430
dparkar Avatar asked Dec 05 '17 23:12

dparkar


3 Answers

I enforced it at the build level, by making the build pipeline fail if the branch does not match the required pattern. It is a little inefficient doing it this way but people soon learn. Add this task to the pipeline (early on, but needs to be after the checkout):

      - task: Bash@3
        displayName: Branch name check
        inputs:
          targetType: inline
          script: |
            echo "##[debug] BUILD_SOURCEBRANCH: [${BUILD_SOURCEBRANCH}]"
            BRANCH="${BUILD_SOURCEBRANCH#refs/heads/}"
            case ${BRANCH} in
              refs/*) ;; # PRs, merges, tags etc.
              release/*) ;;
              feature/*) ;;
              bugfix/*) ;;
              develop) ;;
              *)
                echo "##vso[task.logissue type=error]Branch name [${BRANCH}] does not follow convention: [bugfix/* | feature/* | release/* | develop]."
                echo "##vso[task.complete result=Failed;]"
                ;;
            esac
like image 133
Ed Randall Avatar answered Nov 06 '22 20:11

Ed Randall


TFS 2018 and Azure Repos allow you to require branches be created in folders.

See https://learn.microsoft.com/en-us/azure/devops/repos/git/require-branch-folders?view=azure-devops for instructions how to configure permissions to enable this.

like image 8
David Gardiner Avatar answered Nov 06 '22 19:11

David Gardiner


There are two kinds of git hooks:

  • Client-side hooks, works for the local git repo.
  • Server-side hooks, works for the remote repo (VSTS git repo as you used).

More details about git hooks, you can refer Customizing Git - Git Hooks in git book.

For now, only client-side hooks (such as pre-push hook, pre-commit hook etc) are supported for VSTS git repo.

The Server-side hooks is not available for now (but already in our backlog) for VSTS git repo, you can also find it in this user voice. Once server-side hooks are available in future, such as you can use pre-receive hook (or post-receive hook) to check and force convert branch name in remote repo.

Besides, the link in your question is using VSTS web hook which is quite different from git hooks. And of course you can check and force convert branch name by web hook, but you additional website to receive information and convert branch name. Detail steps as below:

Add a web hook in VSTS service Hooks Tab -> Trigger by code push event -> input your own website url -> Test -> make sure it can connect successful -> Finish.

Once new changes are pushed to VSTS git repo, the web hook will be triggered, and send information to your website. Then you can check and convert branch name in your website and push again.

like image 6
Marina Liu Avatar answered Nov 06 '22 19:11

Marina Liu