-
Notifications
You must be signed in to change notification settings - Fork 45
96 lines (89 loc) · 3.65 KB
/
sync-labels.yml
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
name: Sync Labels from Issue to PR
on:
pull_request:
types: [edited, opened, reopened]
permissions:
pull-requests: write
jobs:
sync-labels:
runs-on: ubuntu-latest
steps:
- name: Get associated issue number
id: get_issue_number
run: |
ISSUE_NUMBERS=$(echo "${{ github.event.pull_request.body }}" | grep -oE "#[0-9]+" | tr -d "#" | tr '\n' ',')
ISSUE_NUMBERS=$(echo $ISSUE_NUMBERS | sed 's/,$//')
echo "ISSUE_NUMBERS=$ISSUE_NUMBERS" >> $GITHUB_ENV
echo "Issue numbers are $ISSUE_NUMBERS"
- name: Get labels from issue
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea #v7
id: get_issue_labels
with:
script: |
const issueNumbers = process.env.ISSUE_NUMBERS.split(",");
let allLabels = new Set();
for (const issueNumber of issueNumbers) {
try {
const { data: { labels } } = await github.rest.issues.get({
issue_number: issueNumber,
owner: context.repo.owner,
repo: context.repo.repo,
});
labels.forEach(label => allLabels.add(label.name));
} catch (error) {
console.error(`Error fetching labels for issue #${issueNumber}: `, error);
}
}
if (allLabels.size === 0) {
core.setOutput('labels', '');
} else {
const sortedLabels = Array.from(allLabels).sort().join(",");
core.setOutput('labels', sortedLabels);
}
- name: Check if labels are the same
id: check_labels_are_same
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea #v7
with:
script: |
const issueLabels = "${{ steps.get_issue_labels.outputs.labels }}";
const prLabels = await github.rest.issues.listLabelsOnIssue({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const prLabelsNames = prLabels.data.map(label => label.name).sort();
const isSame = issueLabels === prLabelsNames.join(",");
core.setOutput('isSame', isSame.toString());
core.setOutput('labels', issueLabels);
- name: Apply labels to PR
if: steps.check_labels_are_same.outputs.isSame == 'false'
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea #v7
with:
script: |
const isSame = "${{ steps.check_labels_are_same.outputs.isSame }}";
const labels = "${{ steps.check_labels_are_same.outputs.labels }}" === "" ? [] : "${{ steps.check_labels_are_same.outputs.labels }}".split(",");
const owner = context.repo.owner;
const repo = context.repo.repo;
const issue_number = context.issue.number;
if (labels.length === 0) {
const prLabels = await github.rest.issues.listLabelsOnIssue({
owner: owner,
repo: repo,
issue_number: issue_number,
});
for (const label of prLabels.data) {
await github.rest.issues.removeLabel({
owner: owner,
repo: repo,
issue_number: issue_number,
name: label.name,
});
}
} else {
await github.rest.issues.setLabels({
owner: owner,
repo: repo,
issue_number: issue_number,
labels: labels,
});
}