Skip to content

Commit 9fb34ad

Browse files
authored
Merge pull request #11 from replicatedhq/squizzi/label-already-exists
feat(ci): skip adding labels if a label already exists
2 parents f22703f + 7d1d411 commit 9fb34ad

File tree

4 files changed

+190
-200
lines changed

4 files changed

+190
-200
lines changed

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

+94-99
Original file line numberDiff line numberDiff line change
@@ -9800,12 +9800,12 @@ const SEMANTIC_TYPE_TO_LABEL = {
98009800
};
98019801

98029802
/**
9803-
* Extract semantic commit type from a message
9803+
* Extract semantic type from a PR title
98049804
* Supports formats like:
98059805
* - "feat: add new feature"
98069806
* - "fix(component): fix bug"
98079807
* - "chore: update dependencies"
9808-
* @param {string} message - The commit message or PR title
9808+
* @param {string} message - The PR title
98099809
* @returns {string|null} - The semantic type or null if not found
98109810
*/
98119811
function extractSemanticType(message) {
@@ -9837,9 +9837,6 @@ async function run() {
98379837
const octokit = github.getOctokit(token);
98389838
core.debug("GitHub client initialized");
98399839

9840-
// Track if we added a label based on semantic commit
9841-
let addedSemanticLabel = false;
9842-
98439840
// fetch the list of labels
98449841
core.debug("Fetching current PR labels...");
98459842
const labels = (
@@ -9850,102 +9847,16 @@ async function run() {
98509847
).data.map((label) => label.name);
98519848
core.debug(`Found ${labels.length} labels: ${labels.join(", ")}`);
98529849

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-
});
9862-
9863-
// Get the PR title and HEAD commit message
9864-
const prTitle = pullRequest.title;
9865-
core.debug(`PR title: "${prTitle}"`);
9866-
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-
});
9873-
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-
}
9881-
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);
9885-
9886-
core.debug("Extracting semantic type from HEAD commit...");
9887-
const commitType = extractSemanticType(headCommitMessage);
9850+
// Check if any type:: labels already exist
9851+
const existingTypeLabels = labels.filter(label => label.startsWith("type::"));
9852+
core.debug(`Found ${existingTypeLabels.length} type labels: ${existingTypeLabels.join(", ")}`);
98889853

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}"`);
9854+
// If type labels already exist, skip adding new labels
9855+
if (existingTypeLabels.length > 0) {
9856+
core.info("Type labels already exist on PR, skipping adding new labels");
98939857
} else {
9894-
core.debug("No semantic type found in PR title or HEAD commit");
9895-
}
9896-
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}"`);
9901-
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}`);
9905-
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({
9931-
...github.context.repo,
9932-
issue_number: github.context.issue.number,
9933-
});
9934-
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(", ")}`);
9938-
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-
}
9944-
} else {
9945-
core.debug(`Label "${labelToAdd}" already exists on PR, no need to add it`);
9946-
}
9947-
} else if (semanticType) {
9948-
core.debug(`Semantic type "${semanticType}" does not map to any of our labels`);
9858+
// Get PR details to check for semantic PR title
9859+
await addSemanticLabels(octokit, labels);
99499860
}
99509861

99519862
// ensure exactly one primary label is set
@@ -10001,6 +9912,90 @@ async function run() {
100019912
}
100029913
}
100039914

9915+
/**
9916+
* Add labels based on semantic PR title
9917+
* @param {object} octokit - GitHub API client
9918+
* @param {string[]} labels - Current labels array to update
9919+
* @returns {Promise<void>}
9920+
*/
9921+
async function addSemanticLabels(octokit, labels) {
9922+
const prNumber = github.context.issue.number;
9923+
core.debug(`Processing PR #${prNumber}`);
9924+
9925+
core.debug("Fetching PR details...");
9926+
const { data: pullRequest } = await octokit.rest.pulls.get({
9927+
...github.context.repo,
9928+
pull_number: prNumber,
9929+
});
9930+
9931+
// Get the PR title
9932+
const prTitle = pullRequest.title;
9933+
core.debug(`PR title: "${prTitle}"`);
9934+
9935+
// Try to extract semantic type from PR title only
9936+
core.debug("Extracting semantic type from PR title...");
9937+
const semanticType = extractSemanticType(prTitle);
9938+
9939+
if (semanticType) {
9940+
core.debug(`Using semantic type from PR title: "${semanticType}"`);
9941+
} else {
9942+
core.debug("No semantic type found in PR title");
9943+
return;
9944+
}
9945+
9946+
// If we found a semantic type that maps to one of our labels, add it if not present
9947+
if (!SEMANTIC_TYPE_TO_LABEL[semanticType]) {
9948+
core.debug(`Semantic type "${semanticType}" does not map to any of our labels`);
9949+
return;
9950+
}
9951+
9952+
const labelToAdd = SEMANTIC_TYPE_TO_LABEL[semanticType];
9953+
core.debug(`Semantic type "${semanticType}" maps to label "${labelToAdd}"`);
9954+
9955+
// Only add the label if it's not already present
9956+
if (labels.includes(labelToAdd)) {
9957+
core.debug(`Label "${labelToAdd}" already exists on PR, no need to add it`);
9958+
return;
9959+
}
9960+
9961+
core.info(`Adding label ${labelToAdd} based on semantic PR title type: ${semanticType}`);
9962+
9963+
core.debug("Calling GitHub API to add label...");
9964+
await octokit.rest.issues.addLabels({
9965+
...github.context.repo,
9966+
issue_number: prNumber,
9967+
labels: [labelToAdd],
9968+
});
9969+
core.debug("Label added successfully via API");
9970+
9971+
// Update our local labels array to include the new label
9972+
labels.push(labelToAdd);
9973+
core.debug(`Updated local labels array: ${labels.join(", ")}`);
9974+
9975+
core.info("Added label based on semantic PR title. Waiting for label to apply...");
9976+
// Short delay to allow the label to be properly registered
9977+
core.debug("Waiting 2 seconds for label to propagate...");
9978+
await new Promise(resolve => setTimeout(resolve, 2000));
9979+
core.debug("Wait completed");
9980+
9981+
// Refetch the labels to ensure we have the most up-to-date set
9982+
core.info("Refetching labels after adding semantic label...");
9983+
core.debug("Calling GitHub API to get updated labels...");
9984+
const updatedLabelsResponse = await octokit.rest.issues.listLabelsOnIssue({
9985+
...github.context.repo,
9986+
issue_number: github.context.issue.number,
9987+
});
9988+
9989+
// Update our labels array with the freshly fetched labels
9990+
const updatedLabels = updatedLabelsResponse.data.map((label) => label.name);
9991+
core.debug(`Refetched ${updatedLabels.length} labels: ${updatedLabels.join(", ")}`);
9992+
9993+
// Replace our labels array with the updated one
9994+
labels.length = 0;
9995+
updatedLabels.forEach(label => labels.push(label));
9996+
core.debug(`Updated local labels array after refetch: ${labels.join(", ")}`);
9997+
}
9998+
100049999
run();
1000510000

1000610001
})();

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

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)