forked from langgenius/dify-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
155 lines (144 loc) · 5.4 KB
/
Copy pathpr-risk-label.yaml
File metadata and controls
155 lines (144 loc) · 5.4 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
name: PR Risk Label
on:
pull_request_target:
types: [opened, edited, reopened, synchronize, ready_for_review]
permissions:
pull-requests: write
issues: write
jobs:
apply-risk-label:
runs-on: ubuntu-latest
steps:
- name: Apply risk label
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
with:
script: |
const riskLabels = {
low: {
name: 'risk: low',
color: '0E8A16',
description: 'Low-risk Marketplace submission',
pattern: /^- \[[xX]\] Low risk\s*$/m,
},
medium: {
name: 'risk: medium',
color: 'FBCA04',
description: 'Medium-risk Marketplace submission',
pattern: /^- \[[xX]\] Medium risk\s*$/m,
},
high: {
name: 'risk: high',
color: 'D93F0B',
description: 'High-risk Marketplace submission',
pattern: /^- \[[xX]\] High risk\s*$/m,
},
missing: {
name: 'risk: missing',
color: 'BFDADC',
description: 'Missing or invalid Marketplace risk selection',
},
};
const marker = '<!-- dify-plugins-risk-label-bot -->';
const warningBody = `${marker}\nPlease select exactly one risk level in the PR template: Low risk, Medium risk, or High risk. This helps Marketplace reviewers route the submission correctly.`;
const { owner, repo } = context.repo;
const pr = context.payload.pull_request;
const issue_number = pr.number;
const body = pr.body || '';
const selected = Object.entries(riskLabels)
.filter(([key, config]) => key !== 'missing' && config.pattern.test(body))
.map(([, config]) => config.name);
const targetLabel = selected.length === 1 ? selected[0] : riskLabels.missing.name;
const allRiskLabelNames = Object.values(riskLabels).map((label) => label.name);
async function updateLabel(label) {
await github.rest.issues.updateLabel({
owner,
repo,
name: label.name,
color: label.color,
description: label.description,
});
}
for (const label of Object.values(riskLabels)) {
try {
const { data: existing } = await github.rest.issues.getLabel({ owner, repo, name: label.name });
if (
existing.color?.toUpperCase() !== label.color ||
(existing.description ?? '') !== (label.description ?? '')
) {
await updateLabel(label);
}
} catch (error) {
if (error.status !== 404) {
throw error;
}
try {
await github.rest.issues.createLabel({
owner,
repo,
name: label.name,
color: label.color,
description: label.description,
});
} catch (createError) {
if (createError.status !== 422) {
throw createError;
}
await updateLabel(label);
}
}
}
for (const label of allRiskLabelNames.filter((name) => name !== targetLabel)) {
try {
await github.rest.issues.removeLabel({ owner, repo, issue_number, name: label });
} catch (error) {
if (error.status !== 404) {
throw error;
}
}
}
await github.rest.issues.addLabels({
owner,
repo,
issue_number,
labels: [targetLabel],
});
const comments = await github.paginate(github.rest.issues.listComments, {
owner,
repo,
issue_number,
per_page: 100,
});
const managedComments = comments.filter(
(comment) => comment.user?.login === 'github-actions[bot]' && comment.body?.includes(marker)
);
if (selected.length === 1) {
for (const comment of managedComments) {
await github.rest.issues.deleteComment({
owner,
repo,
comment_id: comment.id,
});
}
} else if (managedComments.length > 0) {
const [firstComment, ...duplicateComments] = managedComments;
await github.rest.issues.updateComment({
owner,
repo,
comment_id: firstComment.id,
body: warningBody,
});
for (const comment of duplicateComments) {
await github.rest.issues.deleteComment({
owner,
repo,
comment_id: comment.id,
});
}
} else {
await github.rest.issues.createComment({
owner,
repo,
issue_number,
body: warningBody,
});
}