I am working on an Azure DevOps git project and I want to let the Pull requests that has satisfy some needs to ignore the [minimum 2 reviewers] policy, but keep every other policy. What would be the right way?
What I have tried so far:
I have implemented an API to check all those conditions, so this part was no problem
I thought of letting the same code approve the branch and giving the service user extra permissions, but my code fails with
pullRequest.Status = PullRequestStatus.Completed;
pullRequest.CompletionOptions = new GitPullRequestCompletionOptions() { SquashMerge = true };
await gitClient.UpdatePullRequestAsync(pullRequest, pullRequest.Repository.Id, pullRequest.PullRequestId);
Parameter name: You can only update reviewers, descriptions, titles, merge status, and status.
Is there a better to solve this problem?
I see that the problem is not very clear, so I would try to explain it a bit more:
When we think of approval as a requirement, we always think of certain developer doing an action and therefore we need someone else to approve the action to make sure it is safe. However, there are cases that we can automate everything and wouldn't need any human interaction with the code. For example:
It turns out they named their APIs strangely. I tried many things and in the end I did approve the PR with my code. There is 2 notes:
From the API you might think that you can vote (approve, reject, etc) on behalf of everyone, but in reality, you can only reset the others vote (=0) but cannot approve on their behalf. You can only approve the PR for the user you have created the PAT for.
They have named their APIs very strangely. You might think UpdatePullRequestReviewerAsync should let you update your vote, but! you have to use CreatePullRequestReviewerAsync instead! I spent hours trying to find out why it did not work, but eventually I dug the code and saw that the UpdatePullRequestReviewerAsync is calling the PATCH not the PUT. Changing that fixed the problem.
public async Task ApprovePullRequest(Guid repositoryId, int pullRequestId)
{
var gitClient = await GetClient<GitHttpClient>();
var reviewerId = GetConnectionAuthorizedIdentity().ToString();
var identityRefWithVote = new IdentityRefWithVote
{
Id = reviewerId,
Vote = 10,
HasDeclined = false
};
await gitClient.CreatePullRequestReviewerAsync(identityRefWithVote, Config.MainProject, repositoryId, pullRequestId, reviewerId);
}
P.S: Microsoft! Why your SDK is so bad?! It doesn't need to be 1:1 with the API. You can have meaningful methods that only take what you actually expect people to give you. If you expect people to only vote for themselves, have a vote method and don't take all these non-sense parameters from the developers.