forked from badges/shields
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-common-fetch.js
More file actions
78 lines (71 loc) · 2.21 KB
/
Copy pathgithub-common-fetch.js
File metadata and controls
78 lines (71 loc) · 2.21 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
import Joi from 'joi'
import { InvalidResponse } from '../index.js'
import { errorMessagesFor } from './github-helpers.js'
const issueSchema = Joi.object({
head: Joi.object({
sha: Joi.string().required(),
}).required(),
}).required()
async function fetchIssue(serviceInstance, { user, repo, number }) {
return serviceInstance._requestJson({
schema: issueSchema,
url: `/repos/${user}/${repo}/pulls/${number}`,
errorMessages: errorMessagesFor('pull request or repo not found'),
})
}
const contentSchema = Joi.object({
// https://github.com/hapijs/joi/issues/1430
content: Joi.string().required(),
encoding: Joi.equal('base64').required(),
}).required()
async function fetchRepoContent(
serviceInstance,
{ user, repo, branch = 'HEAD', filename }
) {
const errorMessages = errorMessagesFor(
`repo not found, branch not found, or ${filename} missing`
)
if (serviceInstance.staticAuthConfigured) {
const { content } = await serviceInstance._requestJson({
schema: contentSchema,
url: `/repos/${user}/${repo}/contents/${filename}`,
options: { qs: { ref: branch } },
errorMessages,
})
try {
return Buffer.from(content, 'base64').toString('utf-8')
} catch (e) {
throw new InvalidResponse({ prettyMessage: 'undecodable content' })
}
} else {
const { buffer } = await serviceInstance._request({
url: `https://raw.githubusercontent.com/${user}/${repo}/${branch}/${filename}`,
errorMessages,
})
return buffer
}
}
async function fetchJsonFromRepo(
serviceInstance,
{ schema, user, repo, branch = 'HEAD', filename }
) {
if (serviceInstance.staticAuthConfigured) {
const buffer = await fetchRepoContent(serviceInstance, {
user,
repo,
branch,
filename,
})
const json = serviceInstance._parseJson(buffer)
return serviceInstance.constructor._validate(json, schema)
} else {
return serviceInstance._requestJson({
schema,
url: `https://raw.githubusercontent.com/${user}/${repo}/${branch}/${filename}`,
errorMessages: errorMessagesFor(
`repo not found, branch not found, or ${filename} missing`
),
})
}
}
export { fetchIssue, fetchRepoContent, fetchJsonFromRepo }