-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathgithub-service.ts
More file actions
153 lines (132 loc) · 4.16 KB
/
github-service.ts
File metadata and controls
153 lines (132 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* GitHub API service for PR information extraction
*/
import { Octokit } from '@octokit/rest';
import { PullRequestInfo, PullRequestFile, PullRequestDiff } from '../types/github.js';
import { TEMPLATE_CONFIG, ERROR } from '../config/constants.js';
import { logger } from '../utils/logger.js';
export class GitHubService {
private octokit: Octokit;
private owner: string;
private repo: string;
constructor(config: { token: string; owner: string; repo: string }) {
this.octokit = new Octokit({ auth: config.token });
this.owner = config.owner;
this.repo = config.repo;
}
async getPullRequest(pullNumber: number): Promise<PullRequestInfo> {
try {
logger.debug(`Fetching PR ${pullNumber} from ${this.owner}/${this.repo}`);
const { data } = await this.octokit.rest.pulls.get({
owner: this.owner,
repo: this.repo,
pull_number: pullNumber,
});
return {
number: data.number,
title: data.title,
body: data.body,
state: data.state,
user: { login: data.user?.login || 'unknown' },
head: {
ref: data.head.ref,
sha: data.head.sha,
repo: {
full_name: data.head.repo.full_name,
name: data.head.repo.name,
owner: {
login: data.head.repo.owner.login,
},
},
},
base: {
ref: data.base.ref,
sha: data.base.sha,
repo: {
full_name: data.base.repo.full_name,
name: data.base.repo.name,
owner: {
login: data.base.repo.owner.login,
},
},
},
};
} catch (error) {
logger.error(`${ERROR.GITHUB.API_ERROR}: Failed to fetch PR ${pullNumber}`, error);
throw error;
}
}
async getPullRequestFiles(pullNumber: number): Promise<PullRequestFile[]> {
try {
logger.debug(`Fetching files for PR ${pullNumber}`);
const allFiles: PullRequestFile[] = [];
let page = 1;
const perPage = 100; // GitHub's maximum per page
while (true) {
logger.debug(`Fetching PR files page ${page}`, {
pullNumber,
page,
perPage,
});
const { data } = await this.octokit.rest.pulls.listFiles({
owner: this.owner,
repo: this.repo,
pull_number: pullNumber,
per_page: perPage,
page,
});
// Map and add files from this page
const pageFiles = data.map(file => ({
filename: file.filename,
status: file.status,
additions: file.additions,
deletions: file.deletions,
changes: file.changes,
}));
allFiles.push(...pageFiles);
logger.debug(`Fetched ${pageFiles.length} files from page ${page}`, {
pullNumber,
page,
filesOnPage: pageFiles.length,
totalFilesSoFar: allFiles.length,
});
// If we got fewer files than the page size, we've reached the end
if (data.length < perPage) {
break;
}
page++;
}
logger.info(`Successfully fetched all PR files`, {
pullNumber,
totalFiles: allFiles.length,
totalPages: page,
});
return allFiles;
} catch (error) {
logger.error(`${ERROR.GITHUB.API_ERROR}: Failed to fetch PR files`, error);
throw error;
}
}
async getPullRequestDiff(pullNumber: number): Promise<PullRequestDiff> {
try {
logger.debug(`Fetching diff for PR ${pullNumber}`);
const { data } = await this.octokit.rest.pulls.get({
owner: this.owner,
repo: this.repo,
pull_number: pullNumber,
mediaType: { format: 'diff' },
});
const content = data as unknown as string;
const size = Buffer.byteLength(content, 'utf8');
const truncated = size > TEMPLATE_CONFIG.MAX_DIFF_SIZE;
return {
content: truncated ? content.substring(0, TEMPLATE_CONFIG.MAX_DIFF_SIZE) : content,
size,
truncated,
};
} catch (error) {
logger.error(`${ERROR.GITHUB.API_ERROR}: Failed to fetch PR diff`, error);
throw error;
}
}
}