我需要创建一个YAML作业,检查是否在PR上设置了里程碑,如果没有设置里程碑,则自动检查失败。
这是我在。yml文件中的工作:
jobs:
milestone:
name: Check if milestone is set
runs-on: ubuntu-latest
steps:
- name: Check milestone
uses: actions/github-script@v5
with:
script: |
const pr = context.payload.pull_request;
if (pr.milestone) {
core.info(`This pull request has a milestone set: ${pr.milestone.title}`);
} else {
core.setFailed(`A maintainer needs to set the milestone for this pull request. Milestone is ${pr.milestone}`);
}
在我向现有PR推送提交后,如果该PR设置了里程碑,则此检查成功通过。如果没有设置里程碑,它也会成功失败。
在我从PR中添加或删除里程碑并尝试重新运行作业后出现问题。看起来作业只使用推送最后一次提交时可用的数据。例子:
- 我有一个没有里程碑的PR
- 我推送一个新的提交到PR
- 作业按预期失败
- 我在PR上设定了一个里程碑
- 我重新运行作业
- 作业仍然失败,但我希望它能通过
我用它作为我脚本https://github.com/pllim/action-check_milestone_exists的灵感来源。我唯一改变的是GitHub脚本的版本从3到5。当使用版本3时,结果是相同的。
我也看了看这个https://github.community/t/feature-request-add-milestone-changes-as-activity-type-to-pull-request/16778/12。这是否意味着这是不可能的,还是另有原因?
你可以使用Github API调用Github -script动作使用github.request
如下:
github.request("GET /repos/{owner}/{repo}/pulls/{pr}", {
owner: context.repo.owner,
repo: context.repo.repo,
pr: context.payload.pull_request.number
});
使用ockit请求格式
下面将检查当前PR是否存在里程碑:
on: [pull_request]
name: build
jobs:
milestone:
name: Check if milestone is set
runs-on: ubuntu-latest
steps:
- name: Check milestone
uses: actions/github-script@v5
with:
script: |
const { data } = await github.request("GET /repos/{owner}/{repo}/pulls/{pr}", {
owner: context.repo.owner,
repo: context.repo.repo,
pr: context.payload.pull_request.number
});
if (data.milestone) {
core.info(`This pull request has a milestone set: ${data.milestone.title}`);
} else {
core.setFailed(`A maintainer needs to set the milestone for this pull request.`);
}