Azure DevOps API issue when completing new pull request

huangapple go评论80阅读模式
英文:

Azure DevOps API issue when completing new pull request

问题

I'm here to help with the Chinese translation. Here's the translated code portion:

// 创建 PR
const prRequestUrl = `https://system:${personalAccessToken}@dev.azure.com/${organization}/${project}/_apis/git/repositories/${repo}/pullRequests?api-version=7.0`;
const prRequestBody = {
  sourceRefName: 'refs/heads/source',
  targetRefName: 'refs/heads/target',
  title: 'PR 标题',
  description: 'PR 描述',
};
const prData = (await axios.post(prRequestUrl, prRequestBody)).data;

// 这段代码解决了问题
// await new Promise((resolve) => setTimeout(resolve, 1000));

// 完成 PR
const approvePrRequestUrl = `https://system:${personalAccessToken}@dev.azure.com/${organization}/${project}/_apis/git/repositories/${repo}/pullRequests/${prData.pullRequestId}/?api-version=7.0`;
const approvePrRequestBody = {
  lastMergeSourceCommit: prData.lastMergeSourceCommit,
  completionOptions: {
    deleteSourceBranch: false,
  },
  status: 'completed',
};
await axios.patch(
  approvePrRequestUrl,
  approvePrRequestBody
);

If you need any further assistance, please let me know.

英文:

I'm trying to create a new ADO pull request and complete it immediately (for unit testing purposes) but there seems to be an issue where if I try to complete it right after it was created, it simply doesn't complete it.

On the other hand, if I add some timeout between the PR creation and completion (the commented line in the code below) it does work.

Any idea why this could be happening and how to resolve it?

This is an example code of what I've been testing.

// CREATE PR
const prRequestUrl = `https://system:${personalAccessToken}@dev.azure.com/${organization}/${project}/_apis/git/repositories/${repo}/pullRequests?api-version=7.0`;
const prRequestBody = {
  sourceRefName: 'refs/heads/source',
  targetRefName: 'refs/heads/target',
  title: 'PR title',
  description: 'PR description',
};
const prData = (await axios.post(prRequestUrl, prRequestBody)).data;

// This fixes the issue
// await new Promise((resolve) => setTimeout(resolve, 1000));

// COMPLETE PR
const approvePrRequestUrl = `https://system:${personalAccessToken}@dev.azure.com/${organization}/${project}/_apis/git/repositories/${repo}/pullRequests/${prData.pullRequestId}/?api-version=7.0`;
const approvePrRequestBody = {
  lastMergeSourceCommit: prData.lastMergeSourceCommit,
  completionOptions: {
    deleteSourceBranch: false,
  },
  status: 'completed',
};
await axios.patch(
  approvePrRequestUrl,
  approvePrRequestBody
);

答案1

得分: 2

Azure DevOps 使用数据库作为后备存储。可能是创建 PR 请求已排队等待处理,但 PR 尚未完全存在。

如果您的测试只需要一个已完成的 PR,请使用已完成状态创建 PR。

例如:

const prRequestBody = {
  sourceRefName: 'refs/heads/source',
  targetRefName: 'refs/heads/target',
  title: 'PR title',
  description: 'PR description',
  completionOptions: {
    deleteSourceBranch: false,
  },
  status: 'completed',
};

还可以考虑查询初始请求提供的 PR Id。

如果您使用两个请求创建已完成的 PR,进行检查以确保 PR 可检索可能比在请求之间设置任意超时更好。

如果您使用一个请求创建已完成的 PR,检查 PR 是否可检索可以增加单元测试的稳健性(以区分 Azure DevOps 的问题和待测试的代码的问题)。

英文:

Azure DevOps uses a database as a backing store. It may be that the create PR request has queued work but that the PR doesn't fully exist yet.

If your testing just needs a completed PR, create the PR with a completed status.

e.g.

const prRequestBody = {
  sourceRefName: 'refs/heads/source',
  targetRefName: 'refs/heads/target',
  title: 'PR title',
  description: 'PR description',
  completionOptions: {
    deleteSourceBranch: false,
  },
  status: 'completed',
};

Also consider querying on the PR Id provided by the initial request.

If you are using two request to create the completed PR, spinning on checking that the PR is retrievable may be better than an arbitrary timeout between the requests.

If you are using one request to create the completed PR, checking that the PR is retrievable adds robustness to your unit test. (You will want a difference between an issue with Azure DevOps and an issue with your code that is under test.)

答案2

得分: 1

When you create a PR, the thing you are waiting for is for the temporary merge commit to be created. This is how AzDO determines whether or not you have conflicts, and if you do, you won't be able to complete the PR. The time it takes for the temporary merge can vary, but as you've witnessed, it's usually fast, but is certainly greater than the time it takes to make multiple API calls.

Some possible solutions to this problem are:

  1. Instead of trying to complete the PR yourself, you could create the PR and then once you have the PR ID you can immediately update the PR to enable Auto-Completion. Assuming you don't have any policies blocking the PR, it will complete itself as soon as it's ready. The nice thing about this is if the merge commit takes an unusually long time to be created for some reason, your PR will still auto-complete once it's ready.
  2. You may not need a PR at all to achieve your goal. You can modify the security of your branch to allow a specific user to bypass branch policies, and then modify the automation you're currently using to run as that specific user. Now instead of using the AzDO API to create a PR, you can simply use git commands to perform the same merge the PR would do and push it. Using bypass is a nice way to automate certain merges on branches that have policies. Note this assumes your trigger will run when new commits appear on a specific branch (this is the norm), rather than on the completion of a PR into that branch. This could easily be setup as an AzDO pipeline that you can trigger with a single button press, or on some sort of schedule.

1: Link to Stack Overflow

英文:

When you create a PR, the thing you are waiting for is for the temporary merge commit to be created. This is how AzDO determines whether or not you have conflicts, and if you do, you won't be able to complete the PR. The time it takes for the temporary merge can vary, but as you've witnessed, it's usually fast, but is certainly greater than the time it takes to make multiple API calls.

Some possible solutions to this problem are:

  1. Instead of trying to complete the PR yourself, you could create the PR and then once you have the PR ID you can immediately update the PR to enable Auto-Completion. Assuming you don't have any policies blocking the PR, it will complete itself as soon as it's ready. The nice thing about this is if the merge commit takes an unusually long time to be created for some reason, your PR will still auto-complete once it's ready.
  2. You may not need a PR at all to achieve your goal. You can modify the security of your branch to allow a specific user to bypass branch policies, and then modify the automation you're currently using to run as that specific user. Now instead of using the AzDO API to create a PR, you can simply use git commands to perform the same merge the PR would do and push it. Using bypass is a nice way to automate certain merges on branches that have policies. Note this assumes your trigger will run when new commits appear on a specific branch (this is the norm), rather than on the completion of a PR into that branch. This could easily be setup as an AzDO pipeline that you can trigger with a single button press, or on some sort of schedule.

huangapple
  • 本文由 发表于 2023年4月6日 21:28:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950105.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定