diff --git a/src/get-activity.test.ts b/src/get-activity.test.ts index 340d952c..b294644e 100644 --- a/src/get-activity.test.ts +++ b/src/get-activity.test.ts @@ -16,8 +16,7 @@ describe("GetActivity class", () => { beforeAll(async () => { const issue22 = parseGitHubUrl("https://github.com/ubiquibot/comment-incentives/issues/22"); activity = new GetActivity(issue22); - // Wait for all promises to resolve - await Promise.all([activity.self, activity.events, activity.comments, activity.linkedReviews]); + await activity.init(); console.dir(activity, { depth: null, colors: true }); }); diff --git a/src/get-activity.ts b/src/get-activity.ts index 90a1a850..101e278c 100644 --- a/src/get-activity.ts +++ b/src/get-activity.ts @@ -1,5 +1,5 @@ import collectLinkedPulls from "./data-collection/collect-linked-pulls"; -import { GitHubPullRequest, GitHubPullRequestComment, GitHubPullRequestReview } from "./github-types"; +import { GitHubIssue, GitHubIssueComment, GitHubIssueEvent, GitHubPullRequest, GitHubPullRequestReview, GitHubPullRequestReviewComment } from "./github-types"; import { IssueParams, PullParams, @@ -12,37 +12,52 @@ import { } from "./start"; export class GetActivity { - self: null; - events: null; - comments: null; - linkedReviews: []; - constructor(issueParams: IssueParams) { - this.self = getIssue(issueParams).catch(console.error); - this.events = getIssueEvents(issueParams).catch(console.error); - this.comments = getIssueComments(issueParams).catch(console.error); - this.linkedReviews = collectLinkedPulls(issueParams) - .then(async (pulls) => { - const promises = pulls.map((pull) => { - const pullParams = { - owner: pull.source.issue.repository.owner.login, - repo: pull.source.issue.repository.name, - pull_number: pull.source.issue.number, - }; - return new Review(pullParams); - }); - this.linkedReviews = await Promise.all(promises).catch(console.error); - }) - .catch(console.error); + self: Promise | GitHubIssue | null = null; + events: Promise | GitHubIssueEvent[] | null = null; + comments: Promise | GitHubIssueComment[] | null = null; + linkedReviews: Promise | Review[] | null = null; + + constructor(private _issueParams: IssueParams) {} + + async init() { + this.self = getIssue(this._issueParams); + this.events = getIssueEvents(this._issueParams); + this.comments = getIssueComments(this._issueParams); + this.linkedReviews = this._getLinkedReviews(); + [this.self, this.events, this.comments, this.linkedReviews] = await Promise.all([this.self, this.events, this.comments, this.linkedReviews]); + } + + private async _getLinkedReviews(): Promise { + const pulls = await collectLinkedPulls(this._issueParams); + const promises = pulls.map((pull) => { + const repository = pull.source.issue.repository; + + if (!repository) { + throw new Error("No repository found"); + } + + const pullParams = { + owner: repository.owner.login, + repo: repository.name, + pull_number: pull.source.issue.number, + }; + const review = new Review(pullParams); + return review.init().then(() => review); + }); + return Promise.all(promises); } } class Review { - self: GitHubPullRequest; - reviews: GitHubPullRequestReview[]; - reviewComments: GitHubPullRequestComment[]; - constructor(pullParams: PullParams) { - this.self = getPullRequest(pullParams).catch(console.error); - this.reviews = getPullRequestReviews(pullParams).catch(console.error); - this.reviewComments = getPullRequestReviewComments(pullParams).catch(console.error); + self: Promise | null = null; + reviews: Promise | null = null; + reviewComments: Promise | null = null; + + constructor(private _pullParams: PullParams) {} + + async init() { + this.self = getPullRequest(this._pullParams); + this.reviews = getPullRequestReviews(this._pullParams); + this.reviewComments = getPullRequestReviewComments(this._pullParams); } } diff --git a/src/github-types.ts b/src/github-types.ts index d516c5d7..f5b3c41d 100644 --- a/src/github-types.ts +++ b/src/github-types.ts @@ -2,14 +2,14 @@ import { RestEndpointMethodTypes } from "@octokit/rest"; export type GitHubIssue = RestEndpointMethodTypes["issues"]["get"]["response"]["data"]; export type GitHubPullRequest = RestEndpointMethodTypes["pulls"]["get"]["response"]["data"]; -export type GitHubComment = RestEndpointMethodTypes["issues"]["listComments"]["response"]["data"][0]; +export type GitHubIssueComment = RestEndpointMethodTypes["issues"]["listComments"]["response"]["data"][0]; export type GitHubLabel = RestEndpointMethodTypes["issues"]["listLabelsOnIssue"]["response"]["data"][0]; export type GitHubIssueEvent = RestEndpointMethodTypes["issues"]["listEvents"]["response"]["data"][0]; export type GitHubTimelineEvent = RestEndpointMethodTypes["issues"]["listEventsForTimeline"]["response"]["data"][0]; export type GitHubRepository = RestEndpointMethodTypes["repos"]["get"]["response"]["data"]; export type GitHubUser = RestEndpointMethodTypes["users"]["getByUsername"]["response"]["data"]; export type GitHubPullRequestReview = RestEndpointMethodTypes["pulls"]["listReviews"]["response"]["data"][0]; -export type GitHubPullRequestComment = RestEndpointMethodTypes["pulls"]["listReviewComments"]["response"]["data"][0]; +export type GitHubPullRequestReviewComment = RestEndpointMethodTypes["pulls"]["listReviewComments"]["response"]["data"][0]; type LinkPullRequestDetail = { url: "https://api.github.com/repos/ubiquibot/comment-incentives/pulls/25"; diff --git a/src/start.ts b/src/start.ts index d4cc92c7..7efc77d4 100644 --- a/src/start.ts +++ b/src/start.ts @@ -1,5 +1,14 @@ import { getOctokitInstance } from "./get-authentication-token"; -import { GitHubIssue, GitHubPullRequest, GitHubPullRequestReview, GitHubTimelineEvent, GitHubUser } from "./github-types"; +import { + GitHubIssue, + GitHubIssueComment, + GitHubIssueEvent, + GitHubPullRequest, + GitHubPullRequestReview, + GitHubPullRequestReviewComment, + GitHubTimelineEvent, + GitHubUser, +} from "./github-types"; // async function main(gitHubIssueUrl: GitHubIssue["html_url"]) { // const issueParams = parseGitHubUrl(gitHubIssueUrl); @@ -58,12 +67,12 @@ export async function getPullRequest(pullParams: PullParams): Promise { const octokit = getOctokitInstance(); return await octokit.paginate(octokit.issues.listEvents.endpoint.merge(issueParams)); } -export async function getIssueComments(issueParams: IssueParams) { +export async function getIssueComments(issueParams: IssueParams): Promise { const octokit = getOctokitInstance(); return await octokit.paginate(octokit.issues.listComments.endpoint.merge(issueParams)); } @@ -71,7 +80,7 @@ export async function getPullRequestReviews(pullParams: PullParams): Promise { const octokit = getOctokitInstance(); return await octokit.paginate(octokit.pulls.listReviewComments.endpoint.merge(pullParams)); }