Force a branch naming convention in Azure DevOps Git
Asked Answered
E

3

16

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.

Escapism answered 5/12, 2017 at 23:30 Comment(3)
Why would you put a username in a branch name? Surely the commit history will show the authorship of files?Malaria
it makes it easy to sort/filter in different tools and shows who is working on whatEscapism
User names in branches help define the owner of the branch -- the one who is responsible for finishing it and deleting it after it is finished.Cung
S
7

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.

Suggestible answered 6/12, 2017 at 2:50 Comment(1)
Looks like pre-receive hook is being worked on now. developercommunity.visualstudio.com/idea/365841/…Cung
G
10

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.

Gust answered 7/5, 2019 at 6:30 Comment(4)
Thanks. That's definitely a step in that direction.Escapism
This seems to require an external windows tool, tf.exe. I couldn't figure out how to get hold of it.Salable
@EdRandall see #5504358Daylong
@Daylong yeah it seems I found that one later & even left an answer there, lolSalable
S
7

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.

Suggestible answered 6/12, 2017 at 2:50 Comment(1)
Looks like pre-receive hook is being worked on now. developercommunity.visualstudio.com/idea/365841/…Cung
S
5

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 so long as the PR merge rules stipulate that the pipeline must succeed, 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
Salable answered 25/6, 2021 at 13:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.