Skip to content

Commit

Permalink
Skip labeling when no tickernumber was found
Browse files Browse the repository at this point in the history
  • Loading branch information
bbredewold committed Mar 1, 2024
1 parent 995207d commit 5c7e0e6
Showing 1 changed file with 38 additions and 24 deletions.
62 changes: 38 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,29 @@ import { getOctokit, context } from "@actions/github";
import computePrTitle from "./lib/compute-proper-title";
import findIssueInBranch from "./lib/find-issue";

// Find token from request
const token = getInput("token", { required: true });
const octokit = getOctokit(token);

// Prepare reusable query
const prQuery = {
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
};

let ticketNumber = null;

async function updatePrTitle() {
startGroup("Start processing PR...");
startGroup("Start updating PR title...");

try {
// Find token from request
const token = getInput("token", { required: true });
const octokit = getOctokit(token);

// Find input ticket, if any
const inputTicket = getInput("ticket-number", { required: false });

// Prepare reusable query
const prQuery = {
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.issue.number,
};

// Fetch PR
const pullRequest = await octokit.rest.pulls.get(prQuery);

// Determine the ticket number from the input, the branch or the title
const ticketNumber = inputTicket ||
findIssueInBranch(pullRequest.data.head.ref) ||
findIssueInBranch(pullRequest.data.title);

info(`Ticket number "${ticketNumber}" found.`);

// Get the current title as shorthand for comparison
const prHeadRef = pullRequest.data.head.ref;
const currentTitle = String(pullRequest.data.title);
Expand All @@ -40,6 +35,13 @@ async function updatePrTitle() {
return;
}

// Determine the ticket number from the input, the branch or the title
ticketNumber = inputTicket ||
findIssueInBranch(pullRequest.data.head.ref) ||
findIssueInBranch(pullRequest.data.title);

info(`Ticket number: "${ticketNumber}"`);

// Let the system compute the right title
const properTitle = computePrTitle(prHeadRef, currentTitle, ticketNumber);

Expand All @@ -63,16 +65,30 @@ async function updatePrTitle() {
...prQuery,
title: properTitle,
});
} catch (error) {
setFailed(error.message);
}

endGroup();
}

async function updatePrLabels() {
startGroup("Start updating Labels on PR...");
const assignDefaultLabel = getInput("label", { required: false });

if (assignDefaultLabel == 'false') {
if (assignDefaultLabel == "false") {
info(`Skip labeling PR`);

return;
}

// Assign label to PR
if (!ticketNumber) {
info(`No ticket number found, don't label PR`);

return;
}

// Fetch labels
const labels = await octokit.rest.issues.listLabelsForRepo({
owner: prQuery.owner,
repo: prQuery.repo,
Expand All @@ -96,11 +112,9 @@ async function updatePrTitle() {
labels: [matchingLabelWithTicket.name],
});
}
} catch (error) {
setFailed(error.message);
}

endGroup();
}

updatePrTitle();
updatePrLabels();

0 comments on commit 5c7e0e6

Please sign in to comment.