forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-utils.js
More file actions
137 lines (128 loc) · 3.19 KB
/
git-utils.js
File metadata and controls
137 lines (128 loc) · 3.19 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
const github = require('./github')()
// https://docs.github.com/rest/reference/git#get-a-reference
async function getCommitSha (owner, repo, ref) {
try {
const { data } = await github.git.getRef({
owner,
repo,
ref
})
return data.object.sha
} catch (err) {
console.log('error getting tree')
throw (err)
}
}
// https://docs.github.com/rest/reference/git#list-matching-references
async function listMatchingRefs (owner, repo, ref) {
try {
// if the ref is found, this returns an array of objects;
// if the ref is not found, this returns an empty array
const { data } = await github.git.listMatchingRefs({
owner,
repo,
ref
})
return data
} catch (err) {
console.log('error getting tree')
throw (err)
}
}
// https://docs.github.com/rest/reference/git#get-a-commit
async function getTreeSha (owner, repo, commitSha) {
try {
const { data } = await github.git.getCommit({
owner,
repo,
commit_sha: commitSha
})
return data.tree.sha
} catch (err) {
console.log('error getting tree')
throw (err)
}
}
// https://docs.github.com/rest/reference/git#get-a-tree
async function getTree (owner, repo, ref, allowedPaths = []) {
const commitSha = await getCommitSha(owner, repo, ref)
const treeSha = await getTreeSha(owner, repo, commitSha)
try {
const { data } = await github.git.getTree({
owner,
repo,
tree_sha: treeSha,
recursive: 1
})
// only return files that match the patterns in allowedPaths
// skip actions/changes files
return data.tree
} catch (err) {
console.log('error getting tree')
throw (err)
}
}
// https://docs.github.com/rest/reference/git#get-a-blob
async function getContentsForBlob (owner, repo, blob) {
const { data } = await github.git.getBlob({
owner,
repo,
file_sha: blob.sha
})
// decode blob contents
return Buffer.from(data.content, 'base64')
}
// https://docs.github.com/rest/reference/repos#get-repository-content
async function getContents (owner, repo, ref, path) {
try {
const { data } = await github.repos.getContent({
owner,
repo,
ref,
path
})
// decode contents
return Buffer.from(data.content, 'base64').toString()
} catch (err) {
console.log(`error getting ${path} from ${owner}/${repo} at ref ${ref}`)
throw (err)
}
}
// https://docs.github.com/en/rest/reference/pulls#list-pull-requests
async function listPulls (owner, repo) {
try {
const { data } = await github.pulls.list({
owner,
repo,
per_page: 100
})
return data
} catch (err) {
console.log(`error listing pulls in ${owner}/${repo}`)
throw (err)
}
}
async function createIssueComment (owner, repo, pullNumber, body) {
try {
const { data } = await github.issues.createComment({
owner,
repo,
issue_number: pullNumber,
body
})
return data
} catch (err) {
console.log(`error creating a review comment on PR ${pullNumber} in ${owner}/${repo}`)
throw (err)
}
}
module.exports = {
getTree,
getTreeSha,
getCommitSha,
getContentsForBlob,
getContents,
listMatchingRefs,
listPulls,
createIssueComment
}