-
-
Notifications
You must be signed in to change notification settings - Fork 124
90 lines (81 loc) · 2.84 KB
/
pr-merge-dependabot.yml
File metadata and controls
90 lines (81 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
name: Auto Merge
on:
pull_request_target:
types:
- opened
- synchronize
jobs:
check:
runs-on: ubuntu-latest
if: github.actor == 'dependabot[bot]'
steps:
- name: Check and merge ⛙
uses: actions/github-script@v9
with:
github-token: ${{ secrets.QWY_SYNC_BOT_TOKEN }}
script: |
const { repo: { owner, repo } } = context;
const pr = context.payload.pull_request;
try {
console.log(`尝试合并 PR: ${pr.html_url}`);
//批准PR
await github.rest.pulls.createReview({
owner,
repo,
pull_number: pr.number,
event: 'APPROVE'
});
// 合并PR
await github.rest.pulls.merge({
owner,
repo,
pull_number: pr.number,
merge_method: "squash", // 挤压合并
commit_title: undefined,
commit_message: '',
});
} catch (error) {
// 3. 合并失败时,可能存在冲突.重新关闭和打开PR,让Dependabot处理冲突.
console.log(`合并失败: ${error.message}`);
// 检查 PR 是否已有 "MERGE-FAILED" 标签
const labels = await github.rest.issues.listLabelsOnIssue({
owner,
repo,
issue_number: pr.number,
});
const hasMergeFailedLabel = labels.data.some(label => label.name === "MERGE-FAILED");
if (hasMergeFailedLabel) {
console.log(`PR 已经有 "MERGE-FAILED" 标签,跳过进一步操作`);
return;
}
// 如果没有标签,则添加 "MERGE-FAILED" 标签
console.log(`添加 "MERGE-FAILED" 标签到 PR: ${pr.html_url}`);
await github.rest.issues.addLabels({
owner,
repo,
issue_number: pr.number,
labels: ["MERGE-FAILED"],
});
// 关闭 PR
console.log(`关闭 PR: ${pr.html_url}`);
await github.rest.pulls.update({
owner,
repo,
pull_number: pr.number,
state: 'closed',
});
// 重新打开 PR
console.log(`重新打开 PR: ${pr.html_url}`);
await github.rest.pulls.update({
owner,
repo,
pull_number: pr.number,
state: 'open',
});
await github.rest.issues.createComment({
issue_number: pr.number,
owner: owner,
repo: repo,
body: '@dependabot recreate'
});
}