Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 92 additions & 7 deletions apps/api/github/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions apps/api/github/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"extract-prs-with-descriptions": "ts-node src/scripts/generatePRsWithDescriptions.ts"
},
"dependencies": {
"@octokit/graphql": "^9.0.2",
"@octokit/rest": "^20.0.2",
"cors": "^2.8.5",
"dotenv": "^17.0.0",
Expand Down
18 changes: 10 additions & 8 deletions apps/api/github/src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import { PaginationMeta } from './types';

export class GitHubService {
private octokit: Octokit;
private apiToken: string;

constructor(apiToken: string) {
this.octokit = new Octokit({ auth: apiToken });
this.apiToken = apiToken;
}

/**
Expand All @@ -18,9 +20,9 @@ export class GitHubService {
async getPullRequests(username: string, page: number = 1, perPage: number = 10) {
try {
console.log(`🔍 Fetching PRs for ${username} (page ${page}, ${perPage} per page)`);
const result = await fetchPullRequests(this.octokit, username, page, perPage);

const result = await fetchPullRequests(this.octokit, username, page, perPage, this.apiToken);

return {
data: result.pullRequests,
meta: {
Expand All @@ -41,19 +43,19 @@ export class GitHubService {
async getPullRequestDetails(owner: string, repo: string, pullNumber: number) {
try {
console.log(`🔍 Fetching PR details for ${owner}/${repo}#${pullNumber}`);
const pullRequest = await fetchPullRequestDetails(this.octokit, owner, repo, pullNumber);

const pullRequest = await fetchPullRequestDetails(this.octokit, owner, repo, pullNumber, this.apiToken);

return { data: pullRequest };
} catch (error) {
// Check if it's a test case (common test patterns) - handle this first
const isTestCase = owner === 'invalid-user' || repo === 'invalid-repo' || pullNumber === 999;

if (!isTestCase) {
// Only log errors for non-test cases
console.error('❌ Error fetching pull request details:', error);
}

throw error;
}
}
Expand Down
61 changes: 58 additions & 3 deletions apps/api/github/src/pull-requests/detail/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,65 @@
import { Octokit } from '@octokit/rest';
import { graphql } from '@octokit/graphql';
import { DetailedPullRequestResponse } from '../../types';
import { retryApiCall, ensureSufficientRateLimit } from '../../utils/rateLimitUtils';
import { validatePullRequestParams, formatPRStats } from './helpers';

/**
* Fetch closing issues for a pull request using GraphQL API
* Returns an array of issues that will be closed when the PR is merged
*/
async function fetchClosingIssues(
token: string,
owner: string,
repo: string,
pullNumber: number
): Promise<Array<{ number: number; url: string }>> {
try {
const graphqlWithAuth = graphql.defaults({
headers: {
authorization: `token ${token}`,
},
});

const query = `
query getClosingIssues($owner: String!, $repo: String!, $prNumber: Int!) {
repository(owner: $owner, name: $repo) {
pullRequest(number: $prNumber) {
closingIssuesReferences(first: 10) {
nodes {
number
url
}
}
}
}
}
`;

const result: any = await graphqlWithAuth(query, {
owner,
repo,
prNumber: pullNumber,
});

return result.repository?.pullRequest?.closingIssuesReferences?.nodes || [];
} catch (error) {
// If GraphQL query fails, log warning but don't fail the entire request
console.log(`⚠️ Could not fetch closing issues for PR #${pullNumber}: ${error}`);
return [];
}
}

/**
* Fetch detailed information for a specific pull request
* Includes additional data like commits, additions, deletions, and comments count
*/
export async function fetchPullRequestDetails(
octokit: Octokit,
owner: string,
repo: string,
pullNumber: number
owner: string,
repo: string,
pullNumber: number,
token?: string
): Promise<DetailedPullRequestResponse> {
console.log(`🔍 Fetching PR #${pullNumber} from ${owner}/${repo}`);

Expand Down Expand Up @@ -71,6 +119,12 @@ export async function fetchPullRequestDetails(
const pr = prResponse.data;
const commentsCount = commentsResponse.data.length;

// Fetch closing issues if token is provided
let closingIssues: Array<{ number: number; url: string }> = [];
if (token) {
closingIssues = await fetchClosingIssues(token, owner, repo, pullNumber);
}

const detailedPR: DetailedPullRequestResponse = {
id: pr.id,
number: pr.number,
Expand All @@ -88,6 +142,7 @@ export async function fetchPullRequestDetails(
deletions: pr.deletions,
changed_files: pr.changed_files,
comments: commentsCount, // Include comments count for 💬 display
closingIssues: closingIssues.length > 0 ? closingIssues : undefined, // Only include if there are closing issues
author: {
login: pr.user?.login || 'unknown',
avatar_url: pr.user?.avatar_url || '',
Expand Down
Loading