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
128 lines (120 loc) · 3.13 KB
/
git-utils.js
File metadata and controls
128 lines (120 loc) · 3.13 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
#!/usr/bin/env node
import xGithub from './github.js'
const github = xGithub()
// https://docs.github.com/rest/reference/git#get-a-reference
export 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
export 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
export 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
export 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
export 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
export 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
export 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)
}
}
export 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)
}
}