diff --git a/.github/cicd/scripts/version-checker.ts b/.github/cicd/scripts/version-checker.ts index 508d929..9e3f6dc 100644 --- a/.github/cicd/scripts/version-checker.ts +++ b/.github/cicd/scripts/version-checker.ts @@ -36,7 +36,7 @@ if (!await repoClient.exists()) { const tagClient: TagClient = new TagClient(ownerName, repoName, token); -if (await tagClient.tagExists(version)) { +if (await tagClient.exists(version)) { const errorMsg = `The tag '${version}' already exists.`; Utils.printError(errorMsg); Deno.exit(1); diff --git a/GitHubClients/IssueClient.ts b/GitHubClients/IssueClient.ts index f9f9822..8732632 100644 --- a/GitHubClients/IssueClient.ts +++ b/GitHubClients/IssueClient.ts @@ -207,7 +207,7 @@ export class IssueClient extends GitHubClient { Guard.isLessThanOne(issueNumber, "addLabel", "issueNumber"); // First check that the label trying to be added exists in the repo - const labelDoesNotExist = !(await this.labelClient.labelExists(label)); + const labelDoesNotExist = !(await this.labelClient.exists(label)); if (labelDoesNotExist) { const labelsUrl = `https://github.com/KinsonDigital/${super.repoName}/labels`; @@ -297,7 +297,7 @@ export class IssueClient extends GitHubClient { * @returns True if the issue exists, otherwise false. * @throws An {@link AuthError} or {@link IssueError}. */ - public async issueExists(issueNumber: number): Promise { + public async exists(issueNumber: number): Promise { Guard.isLessThanOne(issueNumber, "issueExists", "issueNumber"); return await this.openOrClosedIssueExists(issueNumber, IssueOrPRState.any); @@ -310,7 +310,7 @@ export class IssueClient extends GitHubClient { * @returns True if the issue exists and is open, otherwise false. * @throws An {@link AuthError} or {@link IssueError}. */ - public async openIssueExists(issueNumber: number): Promise { + public async openExists(issueNumber: number): Promise { Guard.isLessThanOne(issueNumber, "openIssueExist", "issueNumber"); return await this.openOrClosedIssueExists(issueNumber, IssueOrPRState.open); @@ -323,7 +323,7 @@ export class IssueClient extends GitHubClient { * @returns True if the issue exists and is open, otherwise false. * @throws An {@link AuthError} or {@link IssueError}. */ - public async closedIssueExists(issueNumber: number): Promise { + public async closedExists(issueNumber: number): Promise { Guard.isLessThanOne(issueNumber, "closedIssueExist", "issueNumber"); return await this.openOrClosedIssueExists(issueNumber, IssueOrPRState.closed); diff --git a/GitHubClients/LabelClient.ts b/GitHubClients/LabelClient.ts index 5e2bca9..f35010b 100644 --- a/GitHubClients/LabelClient.ts +++ b/GitHubClients/LabelClient.ts @@ -85,7 +85,7 @@ export class LabelClient extends GitHubClient { * @remarks Does not require authentication. * @throws An {@link AuthError} or {@link LabelError}. */ - public async labelExists(label: string): Promise { + public async exists(label: string): Promise { Guard.isNothing(label, "labelExists", "label"); const foundLabels = await this.getAllDataUntil( diff --git a/GitHubClients/MilestoneClient.ts b/GitHubClients/MilestoneClient.ts index bdc035d..ac0de93 100644 --- a/GitHubClients/MilestoneClient.ts +++ b/GitHubClients/MilestoneClient.ts @@ -247,7 +247,7 @@ export class MilestoneClient extends GitHubClient { * @remarks Does not require authentication.| * @throws An {@link AuthError} or {@link MilestoneError}. */ - public async milestoneExists(milestoneName: string): Promise { + public async exists(milestoneName: string): Promise { Guard.isNothing(milestoneName, "milestoneExists", "milestoneName"); milestoneName = milestoneName.trim(); diff --git a/GitHubClients/PullRequestClient.ts b/GitHubClients/PullRequestClient.ts index d9674c7..c9dcda4 100644 --- a/GitHubClients/PullRequestClient.ts +++ b/GitHubClients/PullRequestClient.ts @@ -184,7 +184,7 @@ export class PullRequestClient extends GitHubClient { } // First check that the label trying to be added exists in the repo - const labelDoesNotExist = !(await this.labelClient.labelExists(label)); + const labelDoesNotExist = !(await this.labelClient.exists(label)); if (labelDoesNotExist) { const labelsUrl = Utils.buildLabelsUrl(this.ownerName, this.repoName); @@ -236,7 +236,7 @@ export class PullRequestClient extends GitHubClient { * 1. The {@link AuthError} when the request is unauthorized. * 2. The {@link PullRequestError} when something goes wrong with getting all of the pull requests. */ - public async pullRequestExists(prNumber: number): Promise { + public async exists(prNumber: number): Promise { Guard.isLessThanOne(prNumber, "pullRequestExists", "prNumber"); // REST API Docs: https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#list-pull-requests @@ -276,10 +276,10 @@ export class PullRequestClient extends GitHubClient { * 1. The {@link AuthError} when the request is unauthorized. * 2. The {@link PullRequestError} when something goes wrong with getting all of the pull requests. */ - public async openPullRequestExists(prNumber: number): Promise { + public async openExists(prNumber: number): Promise { Guard.isLessThanOne(prNumber, "openPullRequestExists", "issueNumber"); - return await this.openOrClosedPullRequestExists(prNumber, IssueOrPRState.open); + return await this.openOrClosedExists(prNumber, IssueOrPRState.open); } /** @@ -294,7 +294,7 @@ export class PullRequestClient extends GitHubClient { public async updatePullRequest(prNumber: number, prRequestData: IssueOrPRRequestData): Promise { Guard.isLessThanOne(prNumber, "updatePullRequest", "prNumber"); - const prDoesNotExist = !(await this.pullRequestExists(prNumber)); + const prDoesNotExist = !(await this.exists(prNumber)); if (prDoesNotExist) { const errorMsg = `A pull request with the number '${prNumber}' does not exist in the repo '${this.repoName}'.`; @@ -384,10 +384,10 @@ export class PullRequestClient extends GitHubClient { * 1. The {@link AuthError} when the request is unauthorized. * 2. The {@link PullRequestError} when something goes wrong with getting all of the pull requests. */ - public async closedPullRequestExists(prNumber: number): Promise { + public async closedExists(prNumber: number): Promise { Guard.isLessThanOne(prNumber, "closedPullRequestExists", "issueNumber"); - return await this.openOrClosedPullRequestExists(prNumber, IssueOrPRState.closed); + return await this.openOrClosedExists(prNumber, IssueOrPRState.closed); } /** @@ -453,7 +453,7 @@ export class PullRequestClient extends GitHubClient { * 1. The {@link AuthError} when the request is unauthorized. * 2. The {@link PullRequestError} when something goes wrong with getting all of the pull requests. */ - private async openOrClosedPullRequestExists( + private async openOrClosedExists( prNumber: number, state: IssueOrPRState, ): Promise { diff --git a/GitHubClients/RepoClient.ts b/GitHubClients/RepoClient.ts index e0e879f..d68ddf5 100644 --- a/GitHubClients/RepoClient.ts +++ b/GitHubClients/RepoClient.ts @@ -253,7 +253,7 @@ export class RepoClient extends GitHubClient { * 2. An {@link AuthError} if there was a problem with the authentication. * 3. The {@link RepoError} if the there was a problem getting all of the repository variables. */ - public async repoVariableExists(variableName: string): Promise { + public async variableExists(variableName: string): Promise { Guard.isNothing(variableName, "repoVariableExists", "variableName"); const variables = await this.getVariables(); @@ -277,7 +277,7 @@ export class RepoClient extends GitHubClient { Guard.isNothing(variableName, funcName, "variableName"); Guard.isNothing(variableValue, funcName, "variableValue"); - if (!(await this.repoVariableExists(variableName))) { + if (!(await this.variableExists(variableName))) { throw new RepoError(`The variable '${variableName}' does not exist for the repository '${this.repoName}'.`); } diff --git a/GitHubClients/TagClient.ts b/GitHubClients/TagClient.ts index 3478eb3..6dac5a4 100644 --- a/GitHubClients/TagClient.ts +++ b/GitHubClients/TagClient.ts @@ -130,7 +130,7 @@ export class TagClient extends GitHubClient { * 2. An {@link AuthError} if the request was unauthorized. * 3. The {@link TagError} if there was an issue getting the tags. */ - public async tagExists(tagName: string): Promise { + public async exists(tagName: string): Promise { Guard.isNothing(tagName, "tagExists", "tagName"); tagName = tagName.trim(); diff --git a/PackageClients/NuGetClient.ts b/PackageClients/NuGetClient.ts index c52a47b..69448e6 100644 --- a/PackageClients/NuGetClient.ts +++ b/PackageClients/NuGetClient.ts @@ -2,6 +2,7 @@ import { WebApiClient } from "../core/WebApiClient.ts"; import { Guard } from "../core/Guard.ts"; import { NuGetHttpStatusCodes } from "../core/Enums.ts"; import { NuGetError } from "./Errors/NuGetError.ts"; +import { Utils } from "../deps.ts"; /** * References: @@ -24,33 +25,6 @@ export class NuGetClient extends WebApiClient { this.updateOrAddHeader("Accept", "json"); } - /** - * Checks if a package that matches the given {@link packageName} exists in the NuGet registry. - * @param packageName The name of the NuGet package. - * @returns True if the package exists, otherwise false. - * @throws The {@link NuGetError} if there was an issue checking for the package. - */ - public async packageExists(packageName: string): Promise { - Guard.isNothing(packageName, "packageExists", "packageName"); - - packageName = packageName.trim().toLowerCase(); - const url = this.buildUrl(packageName); - - const response: Response = await this.requestGET(url); - const statusCode: NuGetHttpStatusCodes = response.status as NuGetHttpStatusCodes; - - if (this.statusCodeValid(statusCode)) { - return statusCode === NuGetHttpStatusCodes.SuccessWithResponseBody; - } else { - const errorMsg = this.buildErrorMsg( - `There was an issue checking for the '${packageName}' NuGet package.`, - response, - ); - - throw new NuGetError(errorMsg); - } - } - /** * Gets all of the versions for a NuGet package that matches the given {@link packageName}. * @param packageName The name of the NuGet package. @@ -87,13 +61,17 @@ export class NuGetClient extends WebApiClient { * @returns True if the package exists with the given version, otherwise false. * @throws The {@link NuGetError} if there was an issue checking for a specific package version. */ - public async packageWithVersionExists(packageName: string, version: string): Promise { + public async exists(packageName: string, version?: string): Promise { const funcName = "packageWithVersionExists"; Guard.isNothing(packageName, funcName, "packageName"); Guard.isNothing(packageName, funcName, "version"); packageName = packageName.trim().toLowerCase(); - version = version.trim().toLowerCase(); + + if (!Utils.isNothing(version)) { + version = version.trim().toLowerCase(); + version = version.startsWith("v") ? version.slice(1) : version; + } const url = this.buildUrl(packageName); @@ -102,6 +80,10 @@ export class NuGetClient extends WebApiClient { if (this.statusCodeValid(statusCode)) { if (statusCode === NuGetHttpStatusCodes.SuccessWithResponseBody) { + if (Utils.isNothing(version)) { + return true; + } + const versions = (await this.getResponseData<{ versions: string[] }>(response)) .versions.map((v: string) => v.toLowerCase());