Skip to content

Commit 4a8e59c

Browse files
修复 actions 的一些 bug
1 parent 6de688f commit 4a8e59c

File tree

3 files changed

+80
-108
lines changed

3 files changed

+80
-108
lines changed

.github/workflows/issue-close.yml

+25-31
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,34 @@
1-
name: Close labeled issues
1+
name: Auto Close Issues Based on Labels
22

33
on:
44
issues:
5-
types:
6-
- labeled
5+
types: [labeled]
76

87
jobs:
9-
manage_issues:
8+
auto-close-issue:
109
runs-on: ubuntu-latest
11-
1210
steps:
13-
- name: Set up authentication
14-
run: echo "GITHUB_TOKEN=${{ secrets.BOT }}" >> $GITHUB_ENV
15-
16-
- name: Install jq
17-
run: sudo apt-get update && sudo apt-get install -y jq
11+
# 处理 not_planned 关闭类型
12+
- name: Close as not planned
13+
if: |
14+
contains('❌ 拒绝,😂 移交上游,❌ 忽略,❌ 第三方,❌ 暂无计划', github.event.label.name)
15+
env:
16+
GITHUB_TOKEN: ${{ secrets.BOT }}
17+
run: |
18+
gh api \
19+
--method PATCH \
20+
/repos/${{ github.repository }}/issues/${{ github.event.issue.number }} \
21+
-f state=closed \
22+
-f state_reason="not_planned"
1823
19-
- name: Handle labeled issues
20-
if: github.event.action == 'labeled'
24+
# 处理 completed 关闭类型
25+
- name: Close as completed
26+
if: github.event.label.name == '👌 完成'
27+
env:
28+
GITHUB_TOKEN: ${{ secrets.BOT }}
2129
run: |
22-
if [[ "${{ github.event.label.name }}" == "❌ 拒绝" || "${{ github.event.label.name }}" == "😂 移交上游" || "${{ github.event.label.name }}" == "❌ 忽略" || "${{ github.event.label.name }}" == "❌ 第三方" || "${{ github.event.label.name }}" == "❌ 暂无计划"]]; then
23-
curl -X PATCH \
24-
-H "Authorization: token ${{ secrets.BOT }}" \
25-
-H "Accept: application/vnd.github.v3+json" \
26-
https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.issue.number }} \
27-
-d '{"state": "closed", "state_reason": "not_planned"}'
28-
elif [[ "${{ github.event.label.name }}" == "👌 完成" ]]; then
29-
curl -X PATCH \
30-
-H "Authorization: token ${{ secrets.BOT }}" \
31-
-H "Accept: application/vnd.github.v3+json" \
32-
https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.issue.number }} \
33-
-d '{"state": "closed", "state_reason": "completed"}'
34-
elif [[ "${{ github.event.label.name }}" == "❌ 重复" ]]; then
35-
curl -X PATCH \
36-
-H "Authorization: token ${{ secrets.BOT }}" \
37-
-H "Accept: application/vnd.github.v3+json" \
38-
https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.issue.number }} \
39-
-d '{"state": "closed", "state_reason": "duplicate"}'
40-
fi
30+
gh api \
31+
--method PATCH \
32+
/repos/${{ github.repository }}/issues/${{ github.event.issue.number }} \
33+
-f state=closed \
34+
-f state_reason="completed"

.github/workflows/labels.yml

+32-36
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,48 @@
1-
name: Add labels based on issue closure reason
1+
name: Manage Labels on Issue Close
22

33
on:
44
issues:
55
types: [closed]
66

77
jobs:
8-
add-labels:
8+
manage-labels:
99
runs-on: ubuntu-latest
1010
steps:
11-
- name: Check if the issue was closed as completed and add label
11+
- name: Handle labels based on close reason
1212
uses: actions/github-script@v6
1313
with:
1414
github-token: ${{ secrets.BOT }}
1515
script: |
16-
if (context.payload.issue.state_reason === 'completed') {
17-
await github.rest.issues.addLabels({
18-
owner: context.repo.owner,
19-
repo: context.repo.repo,
20-
issue_number: context.payload.issue.number,
21-
labels: ['👌 完成']
22-
});
23-
}
16+
const { issue, repository } = context.payload;
17+
const labelsToRemove = ['⭕ 等待处理', '🚧 正在处理'];
18+
const closedReason = issue.state_reason;
2419
25-
- name: Check if the issue was closed as duplicate and add label
26-
uses: actions/github-script@v6
27-
with:
28-
github-token: ${{ secrets.BOT }}
29-
script: |
30-
if (context.payload.issue.state_reason === 'duplicate') {
31-
await github.rest.issues.addLabels({
32-
owner: context.repo.owner,
33-
repo: context.repo.repo,
34-
issue_number: context.payload.issue.number,
35-
labels: ['❌ 重复']
36-
});
20+
// 移除旧标签
21+
for (const label of labelsToRemove) {
22+
try {
23+
await github.rest.issues.removeLabel({
24+
owner: repository.owner.login,
25+
repo: repository.name,
26+
issue_number: issue.number,
27+
name: label
28+
});
29+
} catch (error) {
30+
if (error.status !== 404) throw error;
31+
}
3732
}
38-
39-
- name: Check if the issue was closed as not planned and add label
40-
uses: actions/github-script@v6
41-
with:
42-
github-token: ${{ secrets.BOT }}
43-
script: |
44-
if (context.payload.issue.state_reason === 'not_planned') {
33+
34+
// 根据关闭原因添加新标签
35+
const reasonMapping = {
36+
'completed': '👌 完成',
37+
'not_planned': '❌ 忽略',
38+
'duplicate': '❌ 重复'
39+
};
40+
41+
if (reasonMapping[closedReason]) {
4542
await github.rest.issues.addLabels({
46-
owner: context.repo.owner,
47-
repo: context.repo.repo,
48-
issue_number: context.payload.issue.number,
49-
labels: ['❌ 忽略']
43+
owner: repository.owner.login,
44+
repo: repository.name,
45+
issue_number: issue.number,
46+
labels: [reasonMapping[closedReason]]
5047
});
51-
}
52-
48+
}

.github/workflows/pr.yml

+23-41
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,33 @@
1-
name: Auto Label and Close PRs
1+
name: PR Label Automation
22

33
on:
44
pull_request:
5-
types: [closed, labeled]
5+
types: [labeled, closed]
66

77
jobs:
8-
9-
label_on_merge:
10-
runs-on: ubuntu-latest
11-
if: github.event.pull_request.merged == true
12-
steps:
13-
- name: Add "👌 完成" label on merge
14-
uses: actions-ecosystem/action-add-labels@v1
15-
with:
16-
labels: "👌 完成"
17-
repo-token: ${{ secrets.BOT }}
18-
19-
close_pr_on_specific_labels:
8+
handle_labels:
209
runs-on: ubuntu-latest
21-
if: github.event.action == 'labeled'
2210
steps:
23-
- name: Check for specific labels and close PR
24-
id: check_label
11+
# 自动关闭带有特定标签的PR
12+
- name: Close PR with specific labels
13+
if: |
14+
github.event.action == 'labeled' &&
15+
contains('❌ 拒绝,😂 移交上游,❌ 忽略,❌ 第三方,❌ 暂无计划,❌ 重复', github.event.label.name)
2516
run: |
26-
case "${{ github.event.label.name }}" in
27-
"❌ 忽略"|"❌ 拒绝"|"❌ 暂无计划"|"❌ 第三方"|"❌ 重复"|"😂 移交上游")
28-
echo "::set-output name=should_close::true"
29-
;;
30-
*)
31-
echo "::set-output name=should_close::false"
32-
;;
33-
esac
34-
- name: Debugging Information
35-
if: steps.check_label.outputs.should_close == 'true'
36-
env:
37-
BOT_TOKEN: ${{ secrets.BOT }}
38-
run: |
39-
echo "Repository: ${{ github.repository }}"
40-
echo "Pull Request Number: ${{ github.event.pull_number }}"
41-
- name: Close PR
42-
if: steps.check_label.outputs.should_close == 'true'
43-
env:
44-
BOT_TOKEN: ${{ secrets.BOT }}
17+
curl -sS -X PATCH \
18+
-H "Accept: application/vnd.github.v3+json" \
19+
-H "Authorization: token ${{ secrets.BOT }}" \
20+
"https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}" \
21+
-d '{"state": "closed"}'
22+
23+
# 合并后添加完成标签
24+
- name: Add merged label
25+
if: |
26+
github.event.action == 'closed' &&
27+
github.event.pull_request.merged == true
4528
run: |
46-
curl -X PATCH \
29+
curl -sS -X POST \
4730
-H "Accept: application/vnd.github.v3+json" \
48-
-H "Authorization: token $BOT_TOKEN" \
49-
https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_number }} \
50-
-d '{"state": "closed"}' \
51-
--verbose
31+
-H "Authorization: token ${{ secrets.BOT }}" \
32+
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/labels" \
33+
-d '{"labels":["👌 完成"]}'

0 commit comments

Comments
 (0)