有没有办法验证 GitHub Pull Request 的标题和正文。我知道我们可以使用拉取请求模板来预填充拉取请求消息。此外,GitHub 确实提供了 WebHooks 、 Checks API,它们针对每个提交运行。
但是我正在寻找某种方法,当贡献者点击 “发送 Pull 请求” 按钮并且基于验证接受或拒绝拉取请求。 我正在考虑通过
GitHub App来实现这一点,它将安装在组织级别的存储库上。 我不关心检查提交。
我遇到了
ContentReferenceEvent,它似乎提供了一个类似但不完整的功能,其中 仅读取 URL 并触发 content_reference
事件。我希望这也能提供一种获取 PullRequest 标题和正文的方法。
但是,使用内容附件
注意:内容附件 API 目前处于公开测试阶段,并且仅 可与 GitHub 应用程序一起使用。特点和要求可能 在此期间随时更改。
由于目前处于测试阶段,我无法使用此功能。
请让我知道还有哪些其他替代方案,或者如果不存在类似的方案,我该如何自己实现这一点。
例如,以下任务使用 github-script 操作来验证拉取请求标题。
- name: Test PR title for conventional commit format
uses: actions/github-script@v6
with:
script: |
const pullRequestTitle = context.payload.pull_request.title;
console.log(`Pull request title: ${pullRequestTitle}`);
const pattern = /^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\([a-z0-9-]+\))?: .{1,50}/;
if (pullRequestTitle.match(pattern)) {
console.log("Title matches conventional commit. Good job");
} else {
var message = '@${{ github.event.pull_request.user.login }} Pull request title does not match conventional commit format. Please edit the PR title and the pipeline will run again. More at -> https://www.conventionalcommits.org/en/v1.0.0/#summary';
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: message
})
core.setFailed(message)
}