@@ -52,48 +52,70 @@ jobs:
5252 role-label :
5353 runs-on : ubuntu-latest
5454 needs : pr-label
55- if : github.event.pull_request.labels.length == 0
55+ permissions :
56+ pull-requests : write
5657 steps :
57- - uses : actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd # v5.0.1
58- with :
59- fetch-depth : 0
60-
61- - name : Get changed roles
62- id : changed-roles
63- env :
64- BASE_REF : ${{ github.event.pull_request.base.ref }}
65- HEAD_REF : ${{ github.event.pull_request.head.ref }}
66- run : |
67- files="$(git diff --name-only "origin/${BASE_REF}...origin/${HEAD_REF}" \
68- | grep -E '^roles/' \
69- | cut -f 1-2 -d'/' \
70- | sort -u \
71- | tr '\n' ' ' \
72- | sed 's/[[:space:]]*$//')"
73-
74- echo "all_changed_and_modified_files=${files}" >> "$GITHUB_OUTPUT"
75-
7658 - name : Add changed roles labels
7759 uses : actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
78- if : |
79- steps.changed-roles.outputs.all_changed_and_modified_files
8060 with :
8161 script : |
82- const changedRoles = '${{ steps.changed-roles.outputs.all_changed_and_modified_files }}'.split(' ');
83- let labels = changedRoles.map(i => 'roles/' + i);
62+ const prNumber = context.issue.number;
63+ const owner = context.repo.owner;
64+ const repo = context.repo.repo;
65+
66+ // List all files in the PR
67+ const files = await github.paginate(
68+ github.rest.pulls.listFiles,
69+ {
70+ owner,
71+ repo,
72+ pull_number: prNumber,
73+ }
74+ );
75+
76+ // Collect changed role names
77+ const changedRoles = new Set();
78+ for (const file of files) {
79+ const path = file.filename;
80+ if (!path.startsWith("roles/")) continue;
8481
85- if (changedRoles.includes('_common')) {
86- const allLabels = await github.paginate(github.rest.issues.listLabelsForRepo, {
87- owner: context.repo.owner,
88- repo: context.repo.repo,
89- });
90- const roleLabels = allLabels.map(label => label.name).filter(name => name.startsWith('roles/'));
91- labels = [...new Set([...labels, ...roleLabels])];
82+ const parts = path.split("/");
83+ if (parts.length < 2) continue;
84+
85+ const roleName = parts[1];
86+ changedRoles.add(roleName);
87+ }
88+
89+ if (changedRoles.size === 0) {
90+ console.log("No role changes detected; skipping role labels.");
91+ return;
9292 }
9393
94- github.rest.issues.addLabels({
95- issue_number: context.issue.number,
96- owner: context.repo.owner,
97- repo: context.repo.repo,
98- labels: labels
99- })
94+ // Turn role names into labels
95+ const labels = new Set(
96+ Array.from(changedRoles, (name) => `roles/${name}`)
97+ );
98+
99+ // If _common changed, add ALL existing roles/* labels
100+ if (changedRoles.has("_common")) {
101+ const allLabels = await github.paginate(
102+ github.rest.issues.listLabelsForRepo,
103+ { owner, repo }
104+ );
105+
106+ for (const label of allLabels) {
107+ if (label.name.startsWith("roles/")) {
108+ labels.add(label.name);
109+ }
110+ }
111+ }
112+
113+ // Apply labels to the PR
114+ await github.rest.issues.addLabels({
115+ owner,
116+ repo,
117+ issue_number: prNumber,
118+ labels: Array.from(labels),
119+ });
120+
121+ console.log("Added labels:", Array.from(labels).join(", "));
0 commit comments