Skip to content

Commit

Permalink
Merge pull request #13 from BigSamu/feat/issue-9/delete-changeset-skip
Browse files Browse the repository at this point in the history
Refactor deleteFile function and incorporate into handleSkipOption
  • Loading branch information
BigSamu committed Dec 20, 2023
2 parents 8fade6b + e24cf85 commit 1dc52fd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
6 changes: 5 additions & 1 deletion changeset-creator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
formatErrorMessage
} from "../utils/formattingUtils.js";
import {
deleteFile,
extractPullRequestData,
createOrUpdateFile,
updatePRLabel,
Expand Down Expand Up @@ -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
Expand Down
35 changes: 24 additions & 11 deletions utils/githubUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 (
Expand All @@ -124,16 +126,25 @@ 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
if (Object.keys(entryMap).length > 1) {
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;
}
Expand Down Expand Up @@ -279,8 +290,8 @@ export const createOrUpdateFile = async (
* @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} 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.
Expand All @@ -289,8 +300,8 @@ export const deleteFile = async (
octokit,
owner,
repo,
prNumber,
path,
message,
branchRef
) => {
let sha;
Expand All @@ -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,
Expand All @@ -323,7 +336,7 @@ export const deleteFile = async (
});
console.log(`File: ${path} deleted successfully.`);
} catch (error) {
throw new UpdateFileError();
throw new DeleteFileError();
}
};

Expand Down

0 comments on commit 1dc52fd

Please sign in to comment.