-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
87 lines (78 loc) · 2.47 KB
/
utils.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
require('dotenv').config({ path: __dirname + '/.env' })
const { writeFile } = require('fs/promises')
const path = require('path')
const fetch = require('node-fetch')
const config = require('./config.json')
const git = require('simple-git')()
const GH_API_BASE = 'https://api.github.com'
const ghAuthHeaders = {
Authorization: `token ${process.env.GH_TOKEN}`
}
function filterAndProcessRepos (repos) {
return repos
.filter(repo => !repo.fork)
.map(({
description,
full_name: name,
ssh_url: cloneUrl,
hooks_url: hookUrl
}) => ({ name, description, hookUrl, cloneUrl }))
}
function getPublicRepoParams () {
const searchParams = new URLSearchParams()
for (const [key, value] of Object.entries(config.githubRepoListParams)) {
searchParams.set(key, value)
}
return searchParams
}
async function getPublicRepos () {
const searchParams = getPublicRepoParams()
const response = await fetch(`${GH_API_BASE}/user/repos?${searchParams}`, { headers: ghAuthHeaders })
const json = await response.json()
return filterAndProcessRepos(json)
}
async function createMetaList (repos) {
console.log('[INFO]', 'Creating list of cloned directories')
const list = JSON.stringify(repos.map(repo => repo.name))
const filePath = path.join(config.repositoriesPath, 'repositories.json')
await writeFile(filePath, list, 'utf-8')
return repos
}
async function cloneToPath (repo, log = false) {
if (log) console.log('[INFO] Cloning %s', repo.name)
const remotePath = repo.cloneUrl
const localPath = path.join(config.repositoriesPath, repo.name)
await git.clone(remotePath, localPath)
return localPath
}
async function updateDescription (localPath, repo, log = false) {
if (log) console.log('[INFO] Adding description for %s', repo.name)
const descriptionFilePath = path.join(localPath, '.git', 'description')
await writeFile(descriptionFilePath, repo.description || '', 'utf-8')
}
async function createWebhook (repo, log = false) {
if (log) console.log('[INFO] Creating webhook for %s', repo.name)
const requestBody = {
config: {
url: config.webhookBase + '/push',
content_type: 'json',
secret: process.env.WEBHOOK_SECRET
}
}
await fetch(repo.hookUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...ghAuthHeaders
},
body: JSON.stringify(requestBody)
})
}
module.exports = {
ghAuthHeaders,
getPublicRepos,
createMetaList,
cloneToPath,
updateDescription,
createWebhook
}