forked from Risk-DAO/bad-debt-leaderboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
githubClient.js
85 lines (77 loc) · 2.39 KB
/
githubClient.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
require('dotenv').config()
const { Octokit } = require('octokit')
const base64 = require('base-64')
const { default: axios } = require('axios')
const {retry} = require('./utils')
const IS_STAGING = process.env.STAGING_ENV && process.env.STAGING_ENV.toLowerCase() == 'true';
const REPO_PATH = IS_STAGING ? 'bad-debt-staging' : 'bad-debt'
const octokit = new Octokit({
auth: process.env.GH_TOKEN
})
const getSha = async (fileName, day) => {
try{
const res = await octokit.request(`Get /repos/{owner}/{repo}/contents/${REPO_PATH}/${day || 'latest'}/{path}`, {
owner: 'Risk-DAO',
repo: 'simulation-results',
path: `${fileName}`,
})
return res.data.sha
} catch (err) {
return null
}
}
const getDay = () => {
const dateObj = new Date();
const month = dateObj.getUTCMonth() + 1; //months from 1-12
const day = dateObj.getUTCDate();
const year = dateObj.getUTCFullYear();
return day + '.' + month + '.' + year
}
const uploadJsonFile = async (jsonString, fileName, day) => {
try {
const sha = await getSha(fileName, day)
if(!day){
await uploadJsonFile(jsonString, fileName, getDay())
}
return octokit.request(`PUT /repos/{owner}/{repo}/contents/${REPO_PATH}/${day || 'latest'}/{path}`, {
owner: 'Risk-DAO',
repo: 'simulation-results',
path: `${fileName}`,
message: `bad-debt push ${new Date().toString()}`,
sha,
committer: {
name: process.env.GH_HANDLE,
email: 'octocat@github.com'
},
content: base64.encode(jsonString)
})
} catch(err) {
console.error('failed to upload to github')
console.error(err)
}
}
const listJsonFiles = async () => {
try{
const res = await octokit.request(`Get /repos/{owner}/{repo}/contents/${REPO_PATH}/latest`, {
owner: 'Risk-DAO',
repo: 'simulation-results',
})
return res.data.map(o => o.name)
} catch (err) {
return []
}
}
const getJsonFile = async (fileName) => {
try{
const {data} = await axios.get(`https://raw.githubusercontent.com/Risk-DAO/simulation-results/main/${REPO_PATH}/latest/${encodeURIComponent(fileName)}`)
return data
} catch (err) {
console.error(err)
return null
}
}
module.exports = {
uploadJsonFile: (...arguments) => retry(uploadJsonFile, arguments),
listJsonFiles: (...arguments) => retry(listJsonFiles, arguments),
getJsonFile: (...arguments) => retry(getJsonFile, arguments),
}