-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
167 lines (145 loc) · 5.14 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const fs = require('fs')
const path = require('path')
const assert = require('assert')
const _ = require('lodash')
const execa = require('execa')
const username = 'x-oauth-basic'
const token = process.env.PLUGIN_TOKEN || process.env.GH_TOKEN
const OWNER = process.env.DRONE_REPO_OWNER
const REPO = process.env.DRONE_REPO_NAME
const REPO_BASE = process.env.DRONE_REPO_BRANCH || 'master'
const PULL_REQUEST_NR = process.env.DRONE_PULL_REQUEST
const CURRENT_BRANCH = process.env.DRONE_COMMIT_BRANCH
const DEPTH = Math.max(process.env.PLUGIN_DEPTH, 1) || 1
const DOWNSTREAMS = (process.env.PLUGIN_DOWNSTREAMS || '').split(/(, )/).filter(Boolean)
const INTEGRATION_FILE = process.env.PLUGIN_FILE || 'livingdocs-integration.json' // eslint-disable-line max-len
const REMOTE_FILE = ['true', true].includes(process.env.PLUGIN_REMOTE)
const LOCAL_INTEGRATION_FILE_PATH = !REMOTE_FILE && path.resolve(INTEGRATION_FILE)
const CWD = process.env.PLUGIN_CWD || process.cwd()
assert(token, 'The variable GH_TOKEN is required.')
assert(OWNER, 'The variable DRONE_REPO_OWNER is required.')
assert(REPO, 'The variable DRONE_REPO_NAME is required.')
assert(CURRENT_BRANCH, 'The variable DRONE_COMMIT_BRANCH is required.')
const {Octokit} = require('@octokit/rest')
const octokit = new Octokit({
auth: token
})
fs.writeFileSync(`${process.env.HOME}/.netrc`, [
`machine github.com`,
`login ${username}`,
`password ${token}`
].join('\n'))
async function getIntegrationFile () {
let fileContent
if (LOCAL_INTEGRATION_FILE_PATH) {
fileContent = fs.readFileSync(LOCAL_INTEGRATION_FILE_PATH, 'utf8')
} else {
const resp = await octokit.rest.repos.getContent({
owner: OWNER,
repo: REPO,
ref: REPO_BASE,
path: INTEGRATION_FILE
})
fileContent = Buffer.from(resp.data.content, 'base64').toString()
}
return normalizeLegacyIntegrationFile(JSON.parse(fileContent))
}
function normalizeLegacyIntegrationFile (json) {
const normalized = {}
for (const name in json) {
const orig = json[name]
if (orig.repository) {
normalized[name] = orig
continue
}
normalized[name] = {
repository: orig.default.downstream['repository'],
defaultBranch: orig.default.downstream['integration-branch'],
customBranches: _.mapValues(_.keyBy(orig.custom, 'base-branch'), (c) => {
return c.downstream['integration-branch']
})
}
}
return normalized
}
async function extractTargetBranches (integrationFile) {
const downstreams = {}
const prBaseBranch = PULL_REQUEST_NR && await getPRBase()
const isGreenkeeper = /^greenkeeper\//.test(CURRENT_BRANCH)
for (const name in integrationFile) {
// Only include whitelisted downstreams
if (DOWNSTREAMS.length && !DOWNSTREAMS.includes(name)) continue
const downstreamConfig = integrationFile[name]
const repo = downstreamConfig.repository
const downstream = {name, repo}
downstreams[name] = downstream
if (CURRENT_BRANCH === REPO_BASE || isGreenkeeper) {
const defaultBranch = isGreenkeeper ? 'greenkeeper' : 'base'
const {branch, cause} = await fallbackDefault(defaultBranch, downstreamConfig)
downstream.branch = branch
downstream.cause = cause
continue
}
if (await hasBranch(downstreamConfig.repository, CURRENT_BRANCH)) {
downstream.branch = CURRENT_BRANCH
downstream.cause = 'current'
continue
}
const customBranch = (downstreamConfig.customBranches || {})[prBaseBranch]
if (customBranch && await hasBranch(downstreamConfig.repository, customBranch)) {
downstream.branch = customBranch
downstream.cause = 'custom'
continue
}
const {branch, cause} = await fallbackDefault('default', downstreamConfig)
downstream.branch = branch
downstream.cause = cause
}
return downstreams
}
async function getPRBase () {
const resp = await octokit.rest.pulls.get({
owner: OWNER, repo: REPO, pull_number: PULL_REQUEST_NR
})
return resp.data.base.ref
}
async function hasBranch (repository, branch) {
if (!branch) return false
const [owner, repo] = repository.split('/')
try {
await octokit.rest.repos.getBranch({owner, repo, branch})
return true
} catch (err) {
if ([404, '404'].includes(err.status)) return false
throw err
}
}
async function fallbackDefault (cause, downstreamConfig) {
const hasDefault = await hasBranch(downstreamConfig.repository, downstreamConfig.defaultBranch)
return {
repo: downstreamConfig.repository,
branch: hasDefault ? downstreamConfig.defaultBranch : 'master',
cause
}
}
async function cloneAll (targets) {
await Promise.all(Object.values(targets).map(async (target) => {
const dir = path.join(CWD, target.name)
await execa('rm', ['-Rf', `${dir}`])
await execa('mkdir', ['-p', `${dir}`])
await execa(`git`, [
`clone`,
`--branch`, target.branch,
`--depth`, DEPTH,
`https://github.com/${target.repo}.git`,
`${dir}`
])
}))
return targets
}
async function execute () {
const integrationFile = await getIntegrationFile()
const targets = await extractTargetBranches(integrationFile)
return cloneAll(targets)
}
module.exports = {execute, getIntegrationFile}