UNPKG

ds-sfcoe-ailabs

Version:

AI-powered code review tool with static analysis integration for comprehensive code quality assessment.

127 lines (126 loc) 4.9 kB
import GitProvider from './gitProvider.js'; import { PullRequestComment } from './index.js'; /** * GitHub implementation of Git provider for pull request operations * * Provides specific implementation for GitHub's API to handle pull request comments, * file changes, and repository interactions. * * @example * ```typescript * const github = new GitHub('owner', 'repo', 'token'); * await github.upsertPRComment({ * pullRequestId: 123, * comment: 'Review complete', * commitSha: 'abc123' * }); * ``` */ export default class GitHub extends GitProvider { /** * Check if a comment was created by this bot using multiple identification methods * * Identification Methods (in order of priority): * 1. Hidden HTML Comment: <!-- AI-PR-Review-Bot: ds-sfcoe-ailabs --> (backward compatibility) * 2. Visible Bot Header: Comments starting with "🤖 **AI Code Quality Review Bot**" * 3. Footer Signature: "*Generated using the npm package ds-sfcoe-ailabs" * * @param comment - GitHub comment object * @returns true if comment was created by this bot */ private static isBotComment; /** * Create a PR comment using GitHub API * * @param url - The GitHub API URL for creating comments * @param headers - HTTP headers including authorization * @param prComment - The pull request comment data to post * @param isGeneralComment - Whether this is a general comment (Issues API) or line-specific (Pull Requests API) * @returns Promise that resolves when comment is created * @throws Error if GitHub API request fails */ private static createComment; /** * Upserts a pull request comment to GitHub. * This method handles both general comments and line-specific comments. * It checks for existing bot comments and updates them if found, or creates a new comment if not. * * @param prComment - The pull request comment data to post * @returns Promise that resolves when the comment is successfully upserted * @throws Error if GitHub API request fails or PR is not found * @example * ```typescript * await github.upsertPRComment({ * pullRequestId: 123, * message: 'Code review completed successfully', * startLine: 10, * endLine: 15, * filePath: 'src/index.ts' * }); * ``` */ upsertPRComment(prComment: PullRequestComment): Promise<void>; /** * Delete an existing bot comment from the PR * * @param prNumber - Pull request number * @param isGeneralComment - Whether to look for general or line-specific comments * @returns Promise that resolves when comment is deleted, or null if no comment found */ deleteBotComment(prNumber: string, isGeneralComment?: boolean): Promise<void>; /** * Get the appropriate GitHub API URL for PR comments * * @param prNumber - Pull request number * @param isGeneralComment - Whether this is a general comment (issues API) or line-specific (pulls API) * @param commentId - Optional comment ID for update operations * @returns The GitHub API URL for the comment operation * @example * ```typescript * const url = github.getCommentUrl('123', true); * // Returns: 'https://api.github.com/repos/owner/repo/issues/123/comments' * ``` */ private getCommentUrl; /** * Get the standard headers for GitHub API requests * * @returns Headers object with authorization and API version * @example * ```typescript * const headers = github.getApiHeaders(); * // Returns: { Authorization: 'token xyz', 'X-GitHub-Api-Version': '2022-11-28', ... } * ``` */ private getApiHeaders; /** * Find existing bot comment in the PR using multiple identification methods * * @param prNumber - Pull request number * @param isGeneralComment - Whether to look for general or line-specific comments * @returns Promise that resolves to the comment ID if found, null otherwise * @throws Error if GitHub API request fails * @example * ```typescript * const commentId = await github.findExistingBotComment('123', true); * if (commentId) { * console.log(`Found existing comment: ${commentId}`); * } * ``` */ private findExistingBotComment; /** * Update an existing comment * * @param commentId - The ID of the comment to update * @param body - The new comment body * @param isGeneralComment - Whether this is a general or line-specific comment * @returns Promise that resolves when comment is updated * @throws Error if GitHub API request fails * @example * ```typescript * await github.updateExistingComment(123, 'Updated review', true); * ``` */ private updateExistingComment; }