Skip to content

Commit d1a9086

Browse files
committed
Refactor label check logic into a func
1 parent b5370f1 commit d1a9086

File tree

3 files changed

+215
-199
lines changed

3 files changed

+215
-199
lines changed

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

Lines changed: 107 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = (
@@ -9855,102 +9852,7 @@ async function run() {
98559852
core.info("Labels already exist on PR, skipping adding new labels");
98569853
} else {
98579854
// Get PR details to check for semantic commit messages
9858-
const prNumber = github.context.issue.number;
9859-
core.debug(`Processing PR #${prNumber}`);
9860-
9861-
core.debug("Fetching PR details...");
9862-
const { data: pullRequest } = await octokit.rest.pulls.get({
9863-
...github.context.repo,
9864-
pull_number: prNumber,
9865-
});
9866-
9867-
// Get the PR title and HEAD commit message
9868-
const prTitle = pullRequest.title;
9869-
core.debug(`PR title: "${prTitle}"`);
9870-
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-
});
9877-
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-
}
9885-
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);
9889-
9890-
core.debug("Extracting semantic type from HEAD commit...");
9891-
const commitType = extractSemanticType(headCommitMessage);
9892-
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-
}
9900-
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}"`);
9905-
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({
9912-
...github.context.repo,
9913-
issue_number: prNumber,
9914-
labels: [labelToAdd],
9915-
});
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-
});
9938-
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(", ")}`);
9942-
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`);
9950-
}
9951-
} else if (semanticType) {
9952-
core.debug(`Semantic type "${semanticType}" does not map to any of our labels`);
9953-
}
9855+
await addSemanticLabels(octokit, labels);
99549856
}
99559857

99569858
// ensure exactly one primary label is set
@@ -10006,6 +9908,112 @@ async function run() {
100069908
}
100079909
}
100089910

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

1001110019
})();

.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.

0 commit comments

Comments
 (0)