-
Notifications
You must be signed in to change notification settings - Fork 73
/
jenkins.js
61 lines (50 loc) · 1.81 KB
/
jenkins.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
'use strict'
const gitHelpers = require('../lib/git-helpers')
const env = process.env
// Jenkins reports the branch name and Git URL in a couple of different places depending on use of the new
// pipeline vs. older job types.
const origBranch = env.CHANGE_TARGET || env.CHANGE_BRANCH || env.BRANCH_NAME || env.GIT_BRANCH
function getGitURL () {
// CHANGE_URL gives us something like: 'https://github.com/owner/repo/pull/4',
// here we convert that to 'https://github.com/owner/repo.git'
if (env.CHANGE_URL) {
const index = env.CHANGE_URL.indexOf('/pull/')
if (index !== -1) {
return env.CHANGE_URL.slice(0, index) + '.git'
}
}
// Try falling back to another var or query git's config ourselves
const gitUrl = env.GIT_URL || gitHelpers.getGitURL()
if (gitUrl) {
return gitUrl
}
// fall back to trying url from package.json
const relative = require('require-relative')
const pkg = relative('./package.json')
if (typeof pkg.repository === 'string') {
return pkg.repository
}
if (typeof pkg.repository.url === 'string') {
return pkg.repository.url
}
return ''
}
function getRepoSlug () {
const gitURL = getGitURL()
if (gitURL) {
return gitHelpers.getRepoSlug(gitURL)
}
console.warn('Failed to extract repoSlug, pushes will probably fail.')
console.warn('Set repository.url with the repo GitHub url in package.json.')
return env.CI_REPO_NAME
}
// Different Jenkins plugins format the branch name differently
const matchesGreenkeeper = origBranch.match(/greenkeeper.*/)
const branchName = matchesGreenkeeper ? matchesGreenkeeper[0] : origBranch
module.exports = {
gitUrl: getGitURL(),
branchName,
repoSlug: getRepoSlug(),
correctBuild: true, // assuming this is always the correct build to update the lockfile
uploadBuild: true // assuming 1 build per branch/PR
}