mirror of
https://github.com/softprops/action-gh-release.git
synced 2025-05-10 10:44:19 +00:00
test(release): add unit tests when searching for a release (#603)
* fix(release): break when draft release is found when a release with the desired tag_name is found, break out of the loop that looks for it. this prevents the case where accidentally overwrite a detected release on successive iterations of the for loop fixes: #602 * include built output * add tests for finding tag from releases add tests for updated functionality to break when we find a release. the logic has been extracted into its own function, to make testing simpler by avoiding over mocking/stubbing of network calls that would create or update a release. the tests that were added use jest's describe/it blocks, but use node's assert function to align with other tests. there isn't any prior art for mocking function calls in the codebase, so for now we use simple promises in "mock" objects that adhere to the Releaser interface * refactor findTagFromReleases purely a sytlistic choice to not have to pre-declare the _release variable, and not have to check using `typeof _release === "string"` when detecting a found release * reset dist/index.js to master * update impl after merge with master * update dist
This commit is contained in:
parent
e2b105c98e
commit
6b18c2f260
3 changed files with 287 additions and 13 deletions
|
@ -225,18 +225,13 @@ export const release = async (
|
|||
const discussion_category_name = config.input_discussion_category_name;
|
||||
const generate_release_notes = config.input_generate_release_notes;
|
||||
try {
|
||||
// you can't get an existing draft by tag
|
||||
// so we must find one in the list of all releases
|
||||
let _release: Release | undefined = undefined;
|
||||
for await (const response of releaser.allReleases({
|
||||
const _release: Release | undefined = await findTagFromReleases(
|
||||
releaser,
|
||||
owner,
|
||||
repo,
|
||||
})) {
|
||||
_release = response.data.find((release) => release.tag_name === tag);
|
||||
if (_release !== undefined) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
tag,
|
||||
);
|
||||
|
||||
if (_release === undefined) {
|
||||
return await createRelease(
|
||||
tag,
|
||||
|
@ -331,6 +326,33 @@ export const release = async (
|
|||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Finds a release by tag name from all a repository's releases.
|
||||
*
|
||||
* @param releaser - The GitHub API wrapper for release operations
|
||||
* @param owner - The owner of the repository
|
||||
* @param repo - The name of the repository
|
||||
* @param tag - The tag name to search for
|
||||
* @returns The release with the given tag name, or undefined if no release with that tag name is found
|
||||
*/
|
||||
export async function findTagFromReleases(
|
||||
releaser: Releaser,
|
||||
owner: string,
|
||||
repo: string,
|
||||
tag: string,
|
||||
): Promise<Release | undefined> {
|
||||
for await (const { data: releases } of releaser.allReleases({
|
||||
owner,
|
||||
repo,
|
||||
})) {
|
||||
const release = releases.find((release) => release.tag_name === tag);
|
||||
if (release) {
|
||||
return release;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
async function createRelease(
|
||||
tag: string,
|
||||
config: Config,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue