-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgithubapi.js
92 lines (84 loc) · 2.65 KB
/
githubapi.js
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
const github = require("@actions/github");
class GitHubAPI {
constructor(token) {
this.octokit = github.getOctokit(token);
}
async compareCommits(owner, repo, baseBranchName, headBranchName) {
try {
const { data: diff } = await this.octokit.rest.repos.compareCommits({
owner,
repo,
base: baseBranchName,
head: headBranchName,
});
return diff;
} catch (error) {
throw new Error(`Error comparing commits: ${error.message}`);
}
}
async getPullRequest(owner, repo, prNumber) {
try {
const { data: prData } = await this.octokit.rest.pulls.get({
owner,
repo,
pull_number: prNumber,
});
return prData;
} catch (error) {
throw new Error(`Error retrieving pull request: ${error.message}`);
}
}
async listFiles(owner, repo, prNumber) {
try {
const { data: changedFiles } = await this.octokit.rest.pulls.listFiles({
owner,
repo,
pull_number: prNumber,
});
return changedFiles;
} catch (error) {
throw new Error(`Error listing changed files: ${error.message}`);
}
}
async getContent(owner, repo, filePath, ref) {
try {
const { data: fileContent } = await this.octokit.rest.repos.getContent({
owner,
repo,
path: filePath,
ref,
});
return Buffer.from(fileContent.content, "base64").toString("utf-8");
} catch (error) {
throw new Error(`Error retrieving file content: ${error.message}`);
}
}
async createPRComment(owner, repo, prNumber, body) {
try {
await this.octokit.rest.issues.createComment({
owner,
repo,
issue_number: prNumber,
body,
});
} catch (error) {
throw new Error(`Error creating comment: ${error.message}`);
}
}
async createReviewComment(owner, repo, pull_number, commit_id, body, path, line) {
try {
await this.octokit.rest.pulls.createReviewComment({
owner,
repo,
pull_number,
body,
commit_id,
path,
line,
});
} catch (error) {
throw new Error(`Error creating review comment: ${error.message}`);
}
}
}
module.exports = GitHubAPI;