Skip to content

Commit

Permalink
feat: GitHubProjectNotFoundError
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Oct 5, 2023
1 parent 027efb4 commit a0749fc
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 21 deletions.
11 changes: 11 additions & 0 deletions api/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ export class GitHubProjectError extends Error {
/* c8 ignore stop */
}

export class GitHubProjectNotFoundError extends GitHubProjectError {
constructor(details) {
super("Project cannot be found");
this.details = details;
}

toHumanMessage() {
return `Project #${this.details.number} could not be found for @${this.details.owner}`;
}
}

export class GitHubProjectUnknownFieldError extends GitHubProjectError {
constructor(details) {
super("Project field cannot be found");
Expand Down
29 changes: 25 additions & 4 deletions api/lib/get-state-with-project-fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { getProjectCoreDataQuery } from "./queries.js";
import { projectFieldsNodesToFieldsMap } from "./project-fields-nodes-to-fields-map.js";
import { GitHubProjectNotFoundError } from "../../index.js";

/**
* This method assures that the project fields are loaded. It returns the new
Expand All @@ -17,10 +18,7 @@ export async function getStateWithProjectFields(project, state) {
return state;
}

const response = await project.octokit.graphql(getProjectCoreDataQuery, {
owner: project.owner,
number: project.number,
});
const response = await getProjectCoreData(project);

const {
userOrOrganization: { projectV2 },
Expand All @@ -45,3 +43,26 @@ export async function getStateWithProjectFields(project, state) {
fields,
});
}

/**
*
* @param {import("../..").default} project
* @returns {Promise<any>}
*/
async function getProjectCoreData(project) {
try {
return await project.octokit.graphql(getProjectCoreDataQuery, {
owner: project.owner,
number: project.number,
});
} catch (error) {
if (error?.response?.errors[0]?.type !== "NOT_FOUND") {
throw error;
}

throw new GitHubProjectNotFoundError({
owner: project.owner,
number: project.number,
});
}
}
47 changes: 30 additions & 17 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default class GitHubProject<
Record<ConditionalKeys<TFields, { optional: true }>, string | null>
>,
>(
options: GitHubProjectOptions<TCustomFields>,
options: GitHubProjectOptions<TCustomFields>
): Promise<GitHubProject<TCustomFields, TFields, TItemFields>>;

constructor(options: GitHubProjectOptions<TCustomFields>);
Expand All @@ -72,67 +72,67 @@ export default class GitHubProject<
list(): Promise<GitHubProjectItem<TItemFields>[]>;
addDraft(
content: DraftItemContent,
fields?: Partial<TItemFields>,
fields?: Partial<TItemFields>
): Promise<ProjectItem_DraftIssue<TItemFields>>;
add(
contentNodeId: string,
fields?: Partial<TItemFields>,
fields?: Partial<TItemFields>
): Promise<
ProjectItem_PullRequest<TItemFields> | ProjectItem_Issue<TItemFields>
>;
get(
itemNodeId: string,
itemNodeId: string
): Promise<GitHubProjectItem<TItemFields> | undefined>;
getByContentId(
contentNodeId: string,
contentNodeId: string
): Promise<GitHubProjectItem<TItemFields> | undefined>;
getByContentRepositoryAndNumber(
repositoryName: string,
issueOrPullRequestNumber: number,
issueOrPullRequestNumber: number
): Promise<GitHubProjectItem<TItemFields> | undefined>;
update(
itemNodeId: string,
fields: Partial<TItemFields>,
fields: Partial<TItemFields>
): Promise<GitHubProjectItem<TItemFields> | undefined>;
updateByContentId(
contentNodeId: string,
fields: Partial<TItemFields>,
fields: Partial<TItemFields>
): Promise<GitHubProjectItem<TItemFields> | undefined>;
updateByContentRepositoryAndNumber(
repositoryName: string,
issueOrPullRequestNumber: number,
fields: Partial<TItemFields>,
fields: Partial<TItemFields>
): Promise<GitHubProjectItem<TItemFields> | undefined>;
archive(
itemNodeId: string,
itemNodeId: string
): Promise<GitHubProjectItem<TItemFields> | undefined>;
archiveByContentId(
contentNodeId: string,
contentNodeId: string
): Promise<GitHubProjectItem<TItemFields> | undefined>;
archiveByContentRepositoryAndNumber(
repositoryName: string,
issueOrPullRequestNumber: number,
issueOrPullRequestNumber: number
): Promise<GitHubProjectItem<TItemFields> | undefined>;
remove(
itemNodeId: string,
itemNodeId: string
): Promise<GitHubProjectItem<TItemFields> | undefined>;
removeByContentId(
contentNodeId: string,
contentNodeId: string
): Promise<GitHubProjectItem<TItemFields> | undefined>;
removeByContentRepositoryAndNumber(
repositoryName: string,
issueOrPullRequestNumber: number,
issueOrPullRequestNumber: number
): Promise<GitHubProjectItem<TItemFields> | undefined>;
};
}

export type MatchFieldNameFn = (
projectFieldName: string,
userFieldName: string,
userFieldName: string
) => boolean;
export type MatchFieldOptionValueFn = (
fieldOptionValue: string,
userValue: string,
userValue: string
) => boolean;

export type GitHubProjectOptions<
Expand Down Expand Up @@ -311,6 +311,19 @@ export declare class GitHubProjectError extends Error {
toHumanMessage(): string;
}

type GitHubProjectNotFoundErrorDetails = {
owner: string;
number: number;
};

export declare class GitHubProjectNotFoundError<
TDetails extends GitHubProjectNotFoundErrorDetails,
> extends GitHubProjectError {
name: "GitHubProjectNotFoundError";
details: TDetails;
constructor(details: TDetails);
}

type GitHubProjectUnknownFieldErrorDetails = {
projectFieldNames: string[];
userFieldName: string;
Expand Down

0 comments on commit a0749fc

Please sign in to comment.