Closed BigSamu closed 7 months ago
Logic of the function was changed for the following. This logic is entirely tested and better reflects the functionality required for the function. Further modularization can be done in later stages of the project
**
* Adds or removes a label from a GitHub pull request using Octokit instance.
*
* @param {InstanceType<typeof GitHub>} octokit - An Octokit instance initialized with a GitHub token.
* @param {string} owner - Owner of the repository.
* @param {string} repo - Repository name.
* @param {number} prNumber - Pull request number.
* @param {string} label - Label to be added or removed.
* @param {boolean} addLabel - Flag to add or remove the label.
* @throws {UpdatePRLabelError} If unable to add or remove label.
*/
export const updatePRLabel = async (
octokit,
owner,
repo,
prNumber,
label,
addLabel
) => {
try {
// Get the current labels on the pull request
const { data: currentLabels } = await octokit.rest.issues.listLabelsOnIssue(
{
owner,
repo,
issue_number: prNumber,
}
);
// Check to see if the label is already on the pull request
const labelExists = currentLabels.some((element) => element.name === label);
if (addLabel && !labelExists) {
// Add the label to the pull request
await octokit.rest.issues.addLabels({
owner,
repo,
issue_number: prNumber,
labels: [label],
});
console.log(`Label "${label}" added to PR #${prNumber}`);
} else if (!addLabel && labelExists) {
// Remove the label from the pull request
await octokit.rest.issues.removeLabel({
owner,
repo,
issue_number: prNumber,
name: label,
});
console.log(`Label "${label}" removed from PR #${prNumber}`);
} else {
console.log(
`Label "${label}" is already ${
addLabel ? "present" : "absent"
} on PR #${prNumber}. No action taken.`
);
}
} catch (error) {
console.error(
`Error updating label "${label}" for PR #${prNumber}: ${error.message}`
);
throw new UpdatePRLabelError();
}
};
@JohnathonBowers,
What do you think about simplifying and modularizing the uopdatePRLabel function from:
to: