Jeff
Jeff

Reputation: 36583

Azure DevOps REST API Perform Git Merge

I am trying to merge a branch using the NodeJS SDK for the Azure DevOps REST API.

I have the following typescript code using the Azure DevOps Node SDK (https://www.npmjs.com/package/azure-devops-node-api)

  // get master commit
  const masterRef = (await gitApi.getRefs(repository.id!, devOps.projectName, 'heads/master'))[0];

  // merge branch into master
  let merge = await gitApi.createMergeRequest(
    {
      comment: 'Merge',
      parents: [branchLatestCommitId, masterRef.objectId!]
    },
    devOps.projectName,
    repository.id!
  );

And I can see in the console when I print the result of the merge object (with a status of 3, indicating Completed):

Object
comment: "Merge"
detailedStatus: {mergeCommitId: "faaeb4adef7640d9dac56592ef96e2535dd46078"}
mergeOperationId: 38
parents: (2) ["67f62907ec3ab10afc8452a649bf010e02d38af9", "8ac25ac12c7e9b28195aa64ed7a23d3939a3491d"]
status: 3
__proto__: Object

Alas...nothing has changed in my git repository. The branch is still there, unmerged into master.

Upvotes: 0

Views: 1378

Answers (2)

@Jeff I found aditional steps (without documentation) to complete the merge process. After createMergeRequest you need to get mergeCommitId with get merge and update you branch with update ref and you mergeCommitId

Sample code:

 const mergeRequestCreate = await git.createMergeRequest(
    {
      comment: "Merge develop into staging",
      parents: [develop.commit!.commitId!, staging.commit!.commitId!]
    }, project, repo.id!)

  await sleep(10000); //need sleep to wait azure process the merge

  const mergeRequest = await git.getMergeRequest(project, repo.id!, mergeRequestCreate.mergeOperationId!)

  const updateRefs = await git.updateRefs([{
    name: 'refs/heads/staging',
    newObjectId: mergeRequest.detailedStatus!.mergeCommitId!,
    oldObjectId: staging.commit!.commitId
  }], repo.id!);

Other way is create an PR and complete:

 const pr = await git.createPullRequest({
    sourceRefName: 'refs/heads/develop',
    targetRefName: "refs/heads/staging",
    title: 'Merge develop into staging'
  }, repo.id!);

  const commitsPR = await git.getPullRequestCommits(repo.id!, pr.pullRequestId!, project);

  if (commitsPR.length <= 0) {
    const abandoned = await git.updatePullRequest(
      { status: PullRequestStatus.Abandoned }, repo.id!, pr.pullRequestId!, project);
  } else {
    const completed = await git.updatePullRequest(
      {
        status: PullRequestStatus.Completed,
        lastMergeSourceCommit: pr.lastMergeSourceCommit!
      }, repo.id!, pr.pullRequestId!, project);
  }

Upvotes: 2

Related Questions