-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
150 lines (126 loc) · 3.87 KB
/
index.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
const fs = require('fs')
const yaml = require('js-yaml')
const merge = require('lodash.merge')
const ora = require('ora')()
const axios = require('axios')
const Octokit = require('@octokit/rest')
function __asyncIter(iterable, fn) {
return iterable.reduce(async (previousPromise, current)=> {
let previous = await previousPromise
let result = await fn(current, previous)
return [...previous, result]
}, Promise.resolve([]))
}
async function run(_confPath) {
const settings = yaml.safeLoad(fs.readFileSync(_confPath, 'utf8'));
const name = settings.name
const circle_token = settings.secrets.circle_ci
const github_token = settings.secrets.github
const octokit = Octokit({
auth: github_token,
previews: ['luke-cage-preview', 'mercy-preview']
})
let owner = settings.org
if (!owner) {
ora.start('Finding current user')
owner = (await octokit.users.getAuthenticated()).data.login
ora.succeed()
}
ora.start(`Create the repo "${owner}/${name}"`)
let repo = await octokit.repos.createInOrg({
org: owner,
name,
description: settings.description,
homepage: settings.homepage,
private: settings.private,
auto_init: true,
has_issues: settings.feats.issues,
has_projects: settings.feats.projects,
has_wiki: settings.feats.wiki,
allow_merge_commit: settings.pull_requests.merge,
allow_rebase_merge: settings.pull_requests.rebase,
allow_squash_merge: settings.pull_requests.squash
})
ora.succeed()
if (settings.org) {
let { org } = settings
let teamsIDs = await __asyncIter(settings.teams, async function(team) {
let [
team_slug,
permission = 'pull'
] = team.split(':')
ora.start(`Find team: ${team_slug}`)
let {
data: { name, id }
} = await octokit.teams.getByName({ org, team_slug })
ora.succeed()
return { team_name: name, team_id: id, permission }
})
await __asyncIter(teamsIDs, async function({ team_id, permission = 'pull', team_name }) {
ora.start(`Give team ${team_name} "${permission}" authorization`)
await octokit.teams.addOrUpdateRepo({
team_id,
owner: org,
repo: name,
permission
})
ora.succeed()
})
}
if (settings.topics) {
ora.start('Add topics to the repository')
await octokit.repos.replaceTopics({
owner,
repo: name,
names: settings.topics
})
ora.succeed()
}
if (settings.protected_branches) {
__asyncIter(settings.protected_branches, async function({ branch, protections }) {
let conf = merge(
{
required_status_checks: { strict: true, contexts: [] },
enforce_admins: true,
required_pull_request_reviews: { required_approving_review_count: 1 },
restrictions: null
},
protections,
{ owner, repo: name, branch },
)
ora.start(`Apply protections for branch "${branch}"`)
await octokit.repos.updateBranchProtection(conf)
ora.succeed()
})
}
if (settings.security.vulnerability_alerts) {
ora.start('Enable vulnerability alerts')
await octokit.repos.enableVulnerabilityAlerts({
owner,
repo: name
})
ora.succeed()
}
if (settings.integrations.CircleCI) {
ora.start('Activate CircleCI')
let url = `https://circleci.com/api/v1.1/project/github/${owner}/${name}/follow?circle-token=${circle_token}`
await axios.post(url)
ora.succeed()
}
}
// async function rollback() {
// ora.warn(`Deleting repo ${name}`)
// await octokit.repos.delete({ owner: owner, repo: name })
// }
module.exports = async (conf)=> {
try {
await run(conf)
} catch (e) {
ora.isSpinning && ora.fail()
e.message && ora.fail(`Error: ${e.message}`);
(e.errors || [])
.map((e)=> e.message)
.forEach((m)=> ora.fail(`Error: ${m}`))
}
ora.stop()
}