Ash
Ash

Reputation: 2304

Merging one branch to another through VSTS API

Is there a way to merge one branch to another using the API?

I had a look here, but it doesn't seem to offer me a solution to what I want (pushing individual files is tedious): https://www.visualstudio.com/en-us/docs/integrate/api/git/pushes

I suppose if there isn't a way to do this using the rest API, can it be done via git commands run through a powershell script on the private build agent?

Upvotes: 3

Views: 1850

Answers (3)

Kriss Milne
Kriss Milne

Reputation: 529

As a follow up to VonC's solution, you'll need to grant the VSTS Build service account permissions to contribute to the repository.

  1. In the menu of the VSTS Project, select the Version Control tab
  2. Either on the security for All Git Repositories, or a specific Git Repository select the "Project Collection Build Service"
  3. Click 'allow' for 'Contribute' and 'Create Branch'

Note: Read is also required but should already be allowed.

You'll also need to create the build variable: system.prefergit and set that value to true.

These steps are detailed in the link provided by VonC

Upvotes: 1

starian chen-MSFT
starian chen-MSFT

Reputation: 33738

First, refer to VonC’s solution.

Secondly, you can do it through Pull Request REST API. (Create a pull request during build/release, update pull request status to completed)

Upvotes: 2

VonC
VonC

Reputation: 1329282

No, there does not seem to be any merge operation exposed in the VSTS Git or Version Control API.

That leaves you with the official "Build and Release step": "Run Git commands in a script", which does enable you to run Git commands, then push back to the VSTS-Git repo.
That page has a script with a merge example (simple bat script though, not a Powershell one):

@echo off
ECHO SOURCE BRANCH IS %BUILD_SOURCEBRANCH%
IF %BUILD_SOURCEBRANCH% == refs/heads/master (
   ECHO Building master branch so no merge is needed.
   EXIT
)
SET sourceBranch=origin/%BUILD_SOURCEBRANCH:refs/heads/=%
ECHO GIT CHECKOUT MASTER
git checkout master
ECHO GIT STATUS
git status
ECHO GIT MERGE
git merge %sourceBranch% -m "Merge to master"
ECHO GIT STATUS
git status
ECHO GIT PUSH
git push origin
ECHO GIT STATUS
git status

Upvotes: 4

Related Questions