Skip to content

Commit b5370f1

Browse files
committed
feat(ci): skip adding labels if a label already exists
Signed-off-by: Kyle Squizzato <[email protected]>
1 parent f22703f commit b5370f1

File tree

3 files changed

+179
-169
lines changed

3 files changed

+179
-169
lines changed

.github/actions/pr-labels/dist/index.js

Lines changed: 86 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -9850,102 +9850,107 @@ async function run() {
98509850
).data.map((label) => label.name);
98519851
core.debug(`Found ${labels.length} labels: ${labels.join(", ")}`);
98529852

9853-
// Get PR details to check for semantic commit messages
9854-
const prNumber = github.context.issue.number;
9855-
core.debug(`Processing PR #${prNumber}`);
9856-
9857-
core.debug("Fetching PR details...");
9858-
const { data: pullRequest } = await octokit.rest.pulls.get({
9859-
...github.context.repo,
9860-
pull_number: prNumber,
9861-
});
9853+
// If labels already exist, skip adding new labels
9854+
if (labels.length > 0) {
9855+
core.info("Labels already exist on PR, skipping adding new labels");
9856+
} else {
9857+
// Get PR details to check for semantic commit messages
9858+
const prNumber = github.context.issue.number;
9859+
core.debug(`Processing PR #${prNumber}`);
98629860

9863-
// Get the PR title and HEAD commit message
9864-
const prTitle = pullRequest.title;
9865-
core.debug(`PR title: "${prTitle}"`);
9861+
core.debug("Fetching PR details...");
9862+
const { data: pullRequest } = await octokit.rest.pulls.get({
9863+
...github.context.repo,
9864+
pull_number: prNumber,
9865+
});
98669866

9867-
// Get the HEAD commit message
9868-
core.debug("Fetching PR commits...");
9869-
const { data: commits } = await octokit.rest.pulls.listCommits({
9870-
...github.context.repo,
9871-
pull_number: prNumber,
9872-
});
9867+
// Get the PR title and HEAD commit message
9868+
const prTitle = pullRequest.title;
9869+
core.debug(`PR title: "${prTitle}"`);
98739870

9874-
core.debug(`Found ${commits.length} commits in PR`);
9875-
const headCommitMessage = commits.length > 0 ? commits[commits.length - 1].commit.message : null;
9876-
if (headCommitMessage) {
9877-
core.debug(`HEAD commit message: "${headCommitMessage}"`);
9878-
} else {
9879-
core.debug("No HEAD commit message found");
9880-
}
9871+
// Get the HEAD commit message
9872+
core.debug("Fetching PR commits...");
9873+
const { data: commits } = await octokit.rest.pulls.listCommits({
9874+
...github.context.repo,
9875+
pull_number: prNumber,
9876+
});
98819877

9882-
// Try to extract semantic type from PR title or HEAD commit
9883-
core.debug("Extracting semantic type from PR title...");
9884-
const prTitleType = extractSemanticType(prTitle);
9878+
core.debug(`Found ${commits.length} commits in PR`);
9879+
const headCommitMessage = commits.length > 0 ? commits[commits.length - 1].commit.message : null;
9880+
if (headCommitMessage) {
9881+
core.debug(`HEAD commit message: "${headCommitMessage}"`);
9882+
} else {
9883+
core.debug("No HEAD commit message found");
9884+
}
98859885

9886-
core.debug("Extracting semantic type from HEAD commit...");
9887-
const commitType = extractSemanticType(headCommitMessage);
9886+
// Try to extract semantic type from PR title or HEAD commit
9887+
core.debug("Extracting semantic type from PR title...");
9888+
const prTitleType = extractSemanticType(prTitle);
98889889

9889-
// Use PR title type first, then fall back to commit type
9890-
const semanticType = prTitleType || commitType;
9891-
if (semanticType) {
9892-
core.debug(`Using semantic type: "${semanticType}"`);
9893-
} else {
9894-
core.debug("No semantic type found in PR title or HEAD commit");
9895-
}
9890+
core.debug("Extracting semantic type from HEAD commit...");
9891+
const commitType = extractSemanticType(headCommitMessage);
98969892

9897-
// If we found a semantic type that maps to one of our labels, add it if not present
9898-
if (semanticType && SEMANTIC_TYPE_TO_LABEL[semanticType]) {
9899-
const labelToAdd = SEMANTIC_TYPE_TO_LABEL[semanticType];
9900-
core.debug(`Semantic type "${semanticType}" maps to label "${labelToAdd}"`);
9893+
// Use PR title type first, then fall back to commit type
9894+
const semanticType = prTitleType || commitType;
9895+
if (semanticType) {
9896+
core.debug(`Using semantic type: "${semanticType}"`);
9897+
} else {
9898+
core.debug("No semantic type found in PR title or HEAD commit");
9899+
}
99019900

9902-
// Only add the label if it's not already present
9903-
if (!labels.includes(labelToAdd)) {
9904-
core.info(`Adding label ${labelToAdd} based on semantic commit type: ${semanticType}`);
9901+
// If we found a semantic type that maps to one of our labels, add it if not present
9902+
if (semanticType && SEMANTIC_TYPE_TO_LABEL[semanticType]) {
9903+
const labelToAdd = SEMANTIC_TYPE_TO_LABEL[semanticType];
9904+
core.debug(`Semantic type "${semanticType}" maps to label "${labelToAdd}"`);
99059905

9906-
core.debug("Calling GitHub API to add label...");
9907-
await octokit.rest.issues.addLabels({
9908-
...github.context.repo,
9909-
issue_number: prNumber,
9910-
labels: [labelToAdd],
9911-
});
9912-
core.debug("Label added successfully via API");
9913-
9914-
// Update our local labels array to include the new label
9915-
labels.push(labelToAdd);
9916-
addedSemanticLabel = true;
9917-
core.debug(`Updated local labels array: ${labels.join(", ")}`);
9918-
9919-
// If we just added a label, give it time to apply
9920-
if (addedSemanticLabel) {
9921-
core.info("Added label based on semantic commit message. Waiting for label to apply...");
9922-
// Short delay to allow the label to be properly registered
9923-
core.debug("Waiting 2 seconds for label to propagate...");
9924-
await new Promise(resolve => setTimeout(resolve, 2000));
9925-
core.debug("Wait completed");
9926-
9927-
// Refetch the labels to ensure we have the most up-to-date set
9928-
core.info("Refetching labels after adding semantic label...");
9929-
core.debug("Calling GitHub API to get updated labels...");
9930-
const updatedLabelsResponse = await octokit.rest.issues.listLabelsOnIssue({
9906+
// Only add the label if it's not already present
9907+
if (!labels.includes(labelToAdd)) {
9908+
core.info(`Adding label ${labelToAdd} based on semantic commit type: ${semanticType}`);
9909+
9910+
core.debug("Calling GitHub API to add label...");
9911+
await octokit.rest.issues.addLabels({
99319912
...github.context.repo,
9932-
issue_number: github.context.issue.number,
9913+
issue_number: prNumber,
9914+
labels: [labelToAdd],
99339915
});
9916+
core.debug("Label added successfully via API");
9917+
9918+
// Update our local labels array to include the new label
9919+
labels.push(labelToAdd);
9920+
addedSemanticLabel = true;
9921+
core.debug(`Updated local labels array: ${labels.join(", ")}`);
9922+
9923+
// If we just added a label, give it time to apply
9924+
if (addedSemanticLabel) {
9925+
core.info("Added label based on semantic commit message. Waiting for label to apply...");
9926+
// Short delay to allow the label to be properly registered
9927+
core.debug("Waiting 2 seconds for label to propagate...");
9928+
await new Promise(resolve => setTimeout(resolve, 2000));
9929+
core.debug("Wait completed");
9930+
9931+
// Refetch the labels to ensure we have the most up-to-date set
9932+
core.info("Refetching labels after adding semantic label...");
9933+
core.debug("Calling GitHub API to get updated labels...");
9934+
const updatedLabelsResponse = await octokit.rest.issues.listLabelsOnIssue({
9935+
...github.context.repo,
9936+
issue_number: github.context.issue.number,
9937+
});
99349938

9935-
// Update our labels array with the freshly fetched labels
9936-
const updatedLabels = updatedLabelsResponse.data.map((label) => label.name);
9937-
core.debug(`Refetched ${updatedLabels.length} labels: ${updatedLabels.join(", ")}`);
9939+
// Update our labels array with the freshly fetched labels
9940+
const updatedLabels = updatedLabelsResponse.data.map((label) => label.name);
9941+
core.debug(`Refetched ${updatedLabels.length} labels: ${updatedLabels.join(", ")}`);
99389942

9939-
// Replace our labels array with the updated one
9940-
labels.length = 0;
9941-
updatedLabels.forEach(label => labels.push(label));
9942-
core.debug(`Updated local labels array after refetch: ${labels.join(", ")}`);
9943+
// Replace our labels array with the updated one
9944+
labels.length = 0;
9945+
updatedLabels.forEach(label => labels.push(label));
9946+
core.debug(`Updated local labels array after refetch: ${labels.join(", ")}`);
9947+
}
9948+
} else {
9949+
core.debug(`Label "${labelToAdd}" already exists on PR, no need to add it`);
99439950
}
9944-
} else {
9945-
core.debug(`Label "${labelToAdd}" already exists on PR, no need to add it`);
9951+
} else if (semanticType) {
9952+
core.debug(`Semantic type "${semanticType}" does not map to any of our labels`);
99469953
}
9947-
} else if (semanticType) {
9948-
core.debug(`Semantic type "${semanticType}" does not map to any of our labels`);
99499954
}
99509955

99519956
// ensure exactly one primary label is set

.github/actions/pr-labels/dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.github/actions/pr-labels/src/index.js

Lines changed: 92 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -74,102 +74,107 @@ async function run() {
7474
).data.map((label) => label.name);
7575
core.debug(`Found ${labels.length} labels: ${labels.join(", ")}`);
7676

77-
// Get PR details to check for semantic commit messages
78-
const prNumber = github.context.issue.number;
79-
core.debug(`Processing PR #${prNumber}`);
80-
81-
core.debug("Fetching PR details...");
82-
const { data: pullRequest } = await octokit.rest.pulls.get({
83-
...github.context.repo,
84-
pull_number: prNumber,
85-
});
86-
87-
// Get the PR title and HEAD commit message
88-
const prTitle = pullRequest.title;
89-
core.debug(`PR title: "${prTitle}"`);
90-
91-
// Get the HEAD commit message
92-
core.debug("Fetching PR commits...");
93-
const { data: commits } = await octokit.rest.pulls.listCommits({
94-
...github.context.repo,
95-
pull_number: prNumber,
96-
});
97-
98-
core.debug(`Found ${commits.length} commits in PR`);
99-
const headCommitMessage = commits.length > 0 ? commits[commits.length - 1].commit.message : null;
100-
if (headCommitMessage) {
101-
core.debug(`HEAD commit message: "${headCommitMessage}"`);
77+
// If labels already exist, skip adding new labels
78+
if (labels.length > 0) {
79+
core.info("Labels already exist on PR, skipping adding new labels");
10280
} else {
103-
core.debug("No HEAD commit message found");
104-
}
81+
// Get PR details to check for semantic commit messages
82+
const prNumber = github.context.issue.number;
83+
core.debug(`Processing PR #${prNumber}`);
10584

106-
// Try to extract semantic type from PR title or HEAD commit
107-
core.debug("Extracting semantic type from PR title...");
108-
const prTitleType = extractSemanticType(prTitle);
85+
core.debug("Fetching PR details...");
86+
const { data: pullRequest } = await octokit.rest.pulls.get({
87+
...github.context.repo,
88+
pull_number: prNumber,
89+
});
10990

110-
core.debug("Extracting semantic type from HEAD commit...");
111-
const commitType = extractSemanticType(headCommitMessage);
91+
// Get the PR title and HEAD commit message
92+
const prTitle = pullRequest.title;
93+
core.debug(`PR title: "${prTitle}"`);
11294

113-
// Use PR title type first, then fall back to commit type
114-
const semanticType = prTitleType || commitType;
115-
if (semanticType) {
116-
core.debug(`Using semantic type: "${semanticType}"`);
117-
} else {
118-
core.debug("No semantic type found in PR title or HEAD commit");
119-
}
95+
// Get the HEAD commit message
96+
core.debug("Fetching PR commits...");
97+
const { data: commits } = await octokit.rest.pulls.listCommits({
98+
...github.context.repo,
99+
pull_number: prNumber,
100+
});
120101

121-
// If we found a semantic type that maps to one of our labels, add it if not present
122-
if (semanticType && SEMANTIC_TYPE_TO_LABEL[semanticType]) {
123-
const labelToAdd = SEMANTIC_TYPE_TO_LABEL[semanticType];
124-
core.debug(`Semantic type "${semanticType}" maps to label "${labelToAdd}"`);
125-
126-
// Only add the label if it's not already present
127-
if (!labels.includes(labelToAdd)) {
128-
core.info(`Adding label ${labelToAdd} based on semantic commit type: ${semanticType}`);
129-
130-
core.debug("Calling GitHub API to add label...");
131-
await octokit.rest.issues.addLabels({
132-
...github.context.repo,
133-
issue_number: prNumber,
134-
labels: [labelToAdd],
135-
});
136-
core.debug("Label added successfully via API");
137-
138-
// Update our local labels array to include the new label
139-
labels.push(labelToAdd);
140-
addedSemanticLabel = true;
141-
core.debug(`Updated local labels array: ${labels.join(", ")}`);
142-
143-
// If we just added a label, give it time to apply
144-
if (addedSemanticLabel) {
145-
core.info("Added label based on semantic commit message. Waiting for label to apply...");
146-
// Short delay to allow the label to be properly registered
147-
core.debug("Waiting 2 seconds for label to propagate...");
148-
await new Promise(resolve => setTimeout(resolve, 2000));
149-
core.debug("Wait completed");
150-
151-
// Refetch the labels to ensure we have the most up-to-date set
152-
core.info("Refetching labels after adding semantic label...");
153-
core.debug("Calling GitHub API to get updated labels...");
154-
const updatedLabelsResponse = await octokit.rest.issues.listLabelsOnIssue({
155-
...github.context.repo,
156-
issue_number: github.context.issue.number,
157-
});
102+
core.debug(`Found ${commits.length} commits in PR`);
103+
const headCommitMessage = commits.length > 0 ? commits[commits.length - 1].commit.message : null;
104+
if (headCommitMessage) {
105+
core.debug(`HEAD commit message: "${headCommitMessage}"`);
106+
} else {
107+
core.debug("No HEAD commit message found");
108+
}
158109

159-
// Update our labels array with the freshly fetched labels
160-
const updatedLabels = updatedLabelsResponse.data.map((label) => label.name);
161-
core.debug(`Refetched ${updatedLabels.length} labels: ${updatedLabels.join(", ")}`);
110+
// Try to extract semantic type from PR title or HEAD commit
111+
core.debug("Extracting semantic type from PR title...");
112+
const prTitleType = extractSemanticType(prTitle);
162113

163-
// Replace our labels array with the updated one
164-
labels.length = 0;
165-
updatedLabels.forEach(label => labels.push(label));
166-
core.debug(`Updated local labels array after refetch: ${labels.join(", ")}`);
167-
}
114+
core.debug("Extracting semantic type from HEAD commit...");
115+
const commitType = extractSemanticType(headCommitMessage);
116+
117+
// Use PR title type first, then fall back to commit type
118+
const semanticType = prTitleType || commitType;
119+
if (semanticType) {
120+
core.debug(`Using semantic type: "${semanticType}"`);
168121
} else {
169-
core.debug(`Label "${labelToAdd}" already exists on PR, no need to add it`);
122+
core.debug("No semantic type found in PR title or HEAD commit");
123+
}
124+
125+
// If we found a semantic type that maps to one of our labels, add it if not present
126+
if (semanticType && SEMANTIC_TYPE_TO_LABEL[semanticType]) {
127+
const labelToAdd = SEMANTIC_TYPE_TO_LABEL[semanticType];
128+
core.debug(`Semantic type "${semanticType}" maps to label "${labelToAdd}"`);
129+
130+
// Only add the label if it's not already present
131+
if (!labels.includes(labelToAdd)) {
132+
core.info(`Adding label ${labelToAdd} based on semantic commit type: ${semanticType}`);
133+
134+
core.debug("Calling GitHub API to add label...");
135+
await octokit.rest.issues.addLabels({
136+
...github.context.repo,
137+
issue_number: prNumber,
138+
labels: [labelToAdd],
139+
});
140+
core.debug("Label added successfully via API");
141+
142+
// Update our local labels array to include the new label
143+
labels.push(labelToAdd);
144+
addedSemanticLabel = true;
145+
core.debug(`Updated local labels array: ${labels.join(", ")}`);
146+
147+
// If we just added a label, give it time to apply
148+
if (addedSemanticLabel) {
149+
core.info("Added label based on semantic commit message. Waiting for label to apply...");
150+
// Short delay to allow the label to be properly registered
151+
core.debug("Waiting 2 seconds for label to propagate...");
152+
await new Promise(resolve => setTimeout(resolve, 2000));
153+
core.debug("Wait completed");
154+
155+
// Refetch the labels to ensure we have the most up-to-date set
156+
core.info("Refetching labels after adding semantic label...");
157+
core.debug("Calling GitHub API to get updated labels...");
158+
const updatedLabelsResponse = await octokit.rest.issues.listLabelsOnIssue({
159+
...github.context.repo,
160+
issue_number: github.context.issue.number,
161+
});
162+
163+
// Update our labels array with the freshly fetched labels
164+
const updatedLabels = updatedLabelsResponse.data.map((label) => label.name);
165+
core.debug(`Refetched ${updatedLabels.length} labels: ${updatedLabels.join(", ")}`);
166+
167+
// Replace our labels array with the updated one
168+
labels.length = 0;
169+
updatedLabels.forEach(label => labels.push(label));
170+
core.debug(`Updated local labels array after refetch: ${labels.join(", ")}`);
171+
}
172+
} else {
173+
core.debug(`Label "${labelToAdd}" already exists on PR, no need to add it`);
174+
}
175+
} else if (semanticType) {
176+
core.debug(`Semantic type "${semanticType}" does not map to any of our labels`);
170177
}
171-
} else if (semanticType) {
172-
core.debug(`Semantic type "${semanticType}" does not map to any of our labels`);
173178
}
174179

175180
// ensure exactly one primary label is set

0 commit comments

Comments
 (0)