Skip to content

Commit

Permalink
feat(get-activity): properly fetches everything according to test
Browse files Browse the repository at this point in the history
  • Loading branch information
0x4007 committed Feb 27, 2024
1 parent f5104e1 commit 6e067f7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 37 deletions.
3 changes: 1 addition & 2 deletions src/get-activity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});

Expand Down
73 changes: 44 additions & 29 deletions src/get-activity.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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> | GitHubIssue | null = null;
events: Promise<GitHubIssueEvent[]> | GitHubIssueEvent[] | null = null;
comments: Promise<GitHubIssueComment[]> | GitHubIssueComment[] | null = null;
linkedReviews: Promise<Review[]> | 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<Review[]> {
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<GitHubPullRequest> | null = null;
reviews: Promise<GitHubPullRequestReview[]> | null = null;
reviewComments: Promise<GitHubPullRequestReviewComment[]> | null = null;

constructor(private _pullParams: PullParams) {}

async init() {
this.self = getPullRequest(this._pullParams);
this.reviews = getPullRequestReviews(this._pullParams);
this.reviewComments = getPullRequestReviewComments(this._pullParams);
}
}
4 changes: 2 additions & 2 deletions src/github-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
17 changes: 13 additions & 4 deletions src/start.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -58,20 +67,20 @@ export async function getPullRequest(pullParams: PullParams): Promise<GitHubPull
return (await octokit.pulls.get(pullParams)).data;
}

export async function getIssueEvents(issueParams: IssueParams) {
export async function getIssueEvents(issueParams: IssueParams): Promise<GitHubIssueEvent[]> {
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<GitHubIssueComment[]> {
const octokit = getOctokitInstance();
return await octokit.paginate(octokit.issues.listComments.endpoint.merge(issueParams));
}
export async function getPullRequestReviews(pullParams: PullParams): Promise<GitHubPullRequestReview[]> {
const octokit = getOctokitInstance();
return await octokit.paginate(octokit.pulls.listReviews.endpoint.merge(pullParams));
}
export async function getPullRequestReviewComments(pullParams: PullParams) {
export async function getPullRequestReviewComments(pullParams: PullParams): Promise<GitHubPullRequestReviewComment[]> {
const octokit = getOctokitInstance();
return await octokit.paginate(octokit.pulls.listReviewComments.endpoint.merge(pullParams));
}
Expand Down

0 comments on commit 6e067f7

Please sign in to comment.