diff --git a/changeset-creator/index.js b/changeset-creator/index.js index ca2a0d7..01c6d96 100644 --- a/changeset-creator/index.js +++ b/changeset-creator/index.js @@ -11,6 +11,7 @@ import { formatErrorMessage } from "../utils/formattingUtils.js"; import { + deleteFile, extractPullRequestData, createOrUpdateFile, updatePRLabel, @@ -67,7 +68,10 @@ export const create_or_update_changeset = async (ghApp, octokit, payload) => { owner, repo, prNumber, - updatePRLabel + prBranchRef, + updatePRLabel, + deleteFile, + CHANGESET_PATH ); // Skip changeset file creation if the "skip" label was added to the PR diff --git a/utils/githubUtils.js b/utils/githubUtils.js index 35eceea..d457372 100644 --- a/utils/githubUtils.js +++ b/utils/githubUtils.js @@ -9,7 +9,7 @@ import { CategoryWithSkipOptionError, UpdatePRLabelError, } from "./customErrors.js"; -import { SKIP_LABEL } from "../config/constants.js"; +import { SKIP_LABEL, FAILED_CHANGESET_LABEL } from "../config/constants.js"; /** * Extracts relevant data from a GitHub Pull Request from the GitHub action context. @@ -116,6 +116,8 @@ export const updatePRLabel = async ( * @param {string} repo - Repository name. * @param {number} prNumber - Pull request number. * @param {Function} updateLabel - Function to add or remove a label from a PR. + * @param {Function} deleteFile - Function to delete a file from a repository if it exists. + * @param {string} path - Path to the changeset file to be deleted, if it exists. * @throws {CategoryWithSkipOptionError} If 'skip' and other entries are present. */ export const handleSkipOption = async ( @@ -124,7 +126,10 @@ export const handleSkipOption = async ( owner, repo, prNumber, - updateLabel + prBranchRef, + updateLabel, + deleteFile, + path ) => { if (entryMap && Object.keys(entryMap).includes("skip")) { // Check if "skip" is the only prefix in the changeset entries @@ -132,8 +137,14 @@ export const handleSkipOption = async ( throw new CategoryWithSkipOptionError(); } else { console.log("No changeset file created or updated."); - // Adds "skip-changelog" label in PR if not present + // Adds "Skip-Changelog" label in PR if not present await updateLabel(octokit, owner, repo, prNumber, SKIP_LABEL, true); + // Removes "failed changeset" label in PR if present + await updateLabel(octokit, owner, repo, prNumber, FAILED_CHANGESET_LABEL, false); + // Define path to send to deleteFile function + const changesetFilePath = `${path}/${prNumber}.yml`; + // Delete a previous changeset file if it exists + await deleteFile(octokit, owner, repo, prNumber, changesetFilePath, prBranchRef) // Indicates to index.js that the program should exit without creating or updating the changeset file return true; } @@ -279,8 +290,8 @@ export const createOrUpdateFile = async ( * @param {InstanceType} 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} path - File path within the repository. - * @param {string} message - Commit message. * @param {string} branchRef - Branch reference for the commit. * @throws {GetGithubContentError} If retrieving the file content fails. * @throws {DeleteFileError} If deleting the file fails. @@ -289,8 +300,8 @@ export const deleteFile = async ( octokit, owner, repo, + prNumber, path, - message, branchRef ) => { let sha; @@ -304,15 +315,17 @@ export const deleteFile = async ( ref: branchRef, }); sha = data?.sha; - message = `${ - sha ? "update" : "create" - } changeset file ${prNumber}.yml for PR #${prNumber}`; } catch (error) { - throw new GetGithubContentError(); + if(error.status === 404) { + console.log("No changeset file to delete.") + } else { + throw new GetGithubContentError(); + } } - + // Delete the file using its SHA try { + const message = `Delete changeset file ${prNumber}.yml for PR #${prNumber}`; await octokit.rest.repos.deleteFile({ owner, repo, @@ -323,7 +336,7 @@ export const deleteFile = async ( }); console.log(`File: ${path} deleted successfully.`); } catch (error) { - throw new UpdateFileError(); + throw new DeleteFileError(); } };