Skip to content
Merged
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
30 changes: 19 additions & 11 deletions src/common/github/GitHubClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import IGitHubClient, {
PullRequestFile
} from "./IGitHubClient"

const GITHUB_API_VERSION = { "X-GitHub-Api-Version": "2026-03-10" } as const

interface IGitHubOAuthTokenDataSource {
getOAuthToken(): Promise<{ accessToken: string }>
}
Expand All @@ -26,7 +28,7 @@ type InstallationAuthenticator = (installationId: number) => Promise<{token: str
export default class GitHubClient implements IGitHubClient {
private readonly oauthTokenDataSource: IGitHubOAuthTokenDataSource
private readonly installationAuthenticator: InstallationAuthenticator

constructor(config: {
appId: string
clientId: string
Expand All @@ -45,46 +47,49 @@ export default class GitHubClient implements IGitHubClient {
return await appAuth({ type: "installation", installationId })
}
}

async graphql(request: GraphQLQueryRequest): Promise<GraphQlQueryResponse> {
const oauthToken = await this.oauthTokenDataSource.getOAuthToken()
const octokit = new Octokit({ auth: oauthToken.accessToken })
return await octokit.graphql(request.query, request.variables)
}

async getRepositoryContent(request: GetRepositoryContentRequest): Promise<RepositoryContent> {
const oauthToken = await this.oauthTokenDataSource.getOAuthToken()
const octokit = new Octokit({ auth: oauthToken.accessToken })
const response = await octokit.rest.repos.getContent({
owner: request.repositoryOwner,
repo: request.repositoryName,
path: request.path,
ref: request.ref
ref: request.ref,
headers: GITHUB_API_VERSION
})
const item = response.data as GitHubContentItem
return { downloadURL: item.download_url }
}

async getPullRequestFiles(request: GetPullRequestFilesRequest): Promise<PullRequestFile[]> {
const auth = await this.installationAuthenticator(request.appInstallationId)
const octokit = new Octokit({ auth: auth.token })
const files = await octokit.paginate(octokit.rest.pulls.listFiles, {
owner: request.repositoryOwner,
repo: request.repositoryName,
pull_number: request.pullRequestNumber,
headers: GITHUB_API_VERSION
})
return files.map(file => {
return { filename: file.filename, status: file.status }
})
}

async getPullRequestComments(request: GetPullRequestCommentsRequest): Promise<PullRequestComment[]> {
const auth = await this.installationAuthenticator(request.appInstallationId)
const octokit = new Octokit({ auth: auth.token })
const comments = await octokit.paginate(octokit.rest.issues.listComments, {
owner: request.repositoryOwner,
repo: request.repositoryName,
issue_number: request.pullRequestNumber,
headers: GITHUB_API_VERSION
})
const result: PullRequestComment[] = []
for await (const comment of comments) {
Expand All @@ -99,26 +104,28 @@ export default class GitHubClient implements IGitHubClient {
}
return result
}

async addCommentToPullRequest(request: AddCommentToPullRequestRequest): Promise<void> {
const auth = await this.installationAuthenticator(request.appInstallationId)
const octokit = new Octokit({ auth: auth.token })
await octokit.rest.issues.createComment({
owner: request.repositoryOwner,
repo: request.repositoryName,
issue_number: request.pullRequestNumber,
body: request.body
body: request.body,
headers: GITHUB_API_VERSION
})
}

async updatePullRequestComment(request: UpdatePullRequestCommentRequest): Promise<void> {
const auth = await this.installationAuthenticator(request.appInstallationId)
const octokit = new Octokit({ auth: auth.token })
await octokit.rest.issues.updateComment({
comment_id: request.commentId,
owner: request.repositoryOwner,
repo: request.repositoryName,
body: request.body
body: request.body,
headers: GITHUB_API_VERSION
})
}

Expand All @@ -128,7 +135,8 @@ export default class GitHubClient implements IGitHubClient {
const response = await octokit.rest.repos.compareCommitsWithBasehead({
owner: request.repositoryOwner,
repo: request.repositoryName,
basehead: `${request.baseRefOid}...${request.headRefOid}`
basehead: `${request.baseRefOid}...${request.headRefOid}`,
headers: GITHUB_API_VERSION
})
return { mergeBaseSha: response.data.merge_base_commit.sha }
}
Expand Down
Loading