in src/services/cherryPickService.ts [111:219]
export async function CreatePullRequestAsync(
cherryPick: GitCherryPick,
pullRequestContext: GitPullRequest,
topicBranchName: string,
targetBranchName: string,
pullRequestName: string
): Promise<IRestClientResult<GitPullRequest>> {
try {
if (cherryPick.status !== GitAsyncOperationStatus.Completed) {
throw new Error("Cherry-pick operation is not completed");
}
let sourceRefName = topicBranchName;
if (!sourceRefName.startsWith("refs/heads")) {
sourceRefName = `refs/heads/${sourceRefName}`;
}
let targetRefName = targetBranchName;
if (!targetRefName.startsWith("refs/heads")) {
targetRefName = `refs/heads/${targetRefName}`;
}
//Check that target topic branch doesnt have any open PR's
const prSearchCriteria: GitPullRequestSearchCriteria = {
targetRefName: targetRefName,
creatorId: "",
includeLinks: false,
repositoryId: pullRequestContext.repository.id,
reviewerId: "",
sourceRefName: sourceRefName,
sourceRepositoryId: "",
status: PullRequestStatus.Active
};
const pullRequests = await client.getPullRequestsByProject(
pullRequestContext.repository.project.id,
prSearchCriteria
);
const pullRquestURL = `${
pullRequestContext.repository.webUrl
}/pullrequest/${pullRequestContext.pullRequestId}`;
//If target topic branch has open PR
if (pullRequests && pullRequests.length > 0) {
const existingPullRequest = pullRequests[0];
const updatedDescription: any = {
description: `${existingPullRequest.description}
------------------------------
This PR's commits were cherry-picked from [here](${pullRquestURL}).`
};
//Append new description if one exists
if (existingPullRequest.description !== pullRequestContext.description) {
//Update PR
const updatedPullRequest = await client.updatePullRequest(
updatedDescription,
existingPullRequest.repository.id,
existingPullRequest.pullRequestId
);
//Return updated PR
return {
result: updatedPullRequest
};
} else {
//Return PR with no updates
return {
result: existingPullRequest
};
}
}
const updatedDescription = `${pullRequestContext.description}
------------------------------
This PR's commits were cherry-picked from [here](${pullRquestURL}).`;
//Create a new PR if none previously existed
const completionOptions: any = {
deleteSourceBranch: true,
mergeStrategy: GitPullRequestMergeStrategy.Squash,
squashMerge: true
};
const workItemRefs = await client.getPullRequestWorkItemRefs(
pullRequestContext.repository.id,
pullRequestContext.pullRequestId
);
const pullRequestToCreate: any = {
sourceRefName: `refs/heads/${topicBranchName}`,
targetRefName: `refs/heads/${targetBranchName}`,
completionOptions: completionOptions,
title: pullRequestName,
description: updatedDescription,
workItemRefs: workItemRefs
};
const newPullRequest: GitPullRequest = await client.createPullRequest(
pullRequestToCreate,
pullRequestContext.repository.id,
pullRequestContext.repository.project.id
);
return { result: newPullRequest };
} catch (ex) {
return { error: ex };
}
}