forked from kiegroup/jenkins-pipeline-shared-libraries
/
githubscm.groovy
85 lines (77 loc) · 3.8 KB
/
githubscm.groovy
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
def resolveRepository(String repository, String author, String branches, boolean ignoreErrors) {
return resolveScm(
source: github(
credentialsId: 'kie-ci',
repoOwner: author,
repository: repository,
traits: [[$class: 'org.jenkinsci.plugins.github_branch_source.BranchDiscoveryTrait', strategyId: 3],
[$class: 'org.jenkinsci.plugins.github_branch_source.OriginPullRequestDiscoveryTrait', strategyId: 1],
[$class: 'org.jenkinsci.plugins.github_branch_source.ForkPullRequestDiscoveryTrait', strategyId: 1, trust: [$class: 'TrustPermission']]]),
ignoreErrors: ignoreErrors,
targets: [branches])
}
def checkoutIfExists(String repository, String author, String branches, String defaultAuthor, String defaultBranches, boolean mergeTarget = false) {
def sourceAuthor = author
// Checks source group and branch (for cases where the branch has been created in the author's forked project)
def repositoryScm = getRepositoryScm(repository, author, branches)
if (repositoryScm == null) {
// Checks target group and and source branch (for cases where the branch has been created in the target project itself
repositoryScm = getRepositoryScm(repository, defaultAuthor, branches)
sourceAuthor = repositoryScm ? defaultAuthor : author
}
if (repositoryScm != null) {
if(mergeTarget) {
mergeSourceIntoTarget(repository, sourceAuthor, branches, defaultAuthor, defaultBranches)
} else {
checkout repositoryScm
}
} else {
checkout(resolveRepository(repository, defaultAuthor, defaultBranches, false))
}
}
def getRepositoryScm(String repository, String author, String branches) {
println "[INFO] Resolving repository ${repository} author ${author} branches ${branches}"
def repositoryScm = null
try {
repositoryScm = resolveRepository(repository, author, branches, true)
} catch (Exception ex) {
println "[WARNING] Branches [${branches}] from repository ${repository} not found in ${author} organisation."
}
return repositoryScm
}
def mergeSourceIntoTarget(String repository, String sourceAuthor, String sourceBranches, String targetAuthor, String targetBranches) {
println "[INFO] Merging source [${repository}/${sourceAuthor}:${sourceBranches}] into target [${repository}/${targetAuthor}:${targetBranches}]..."
checkout(resolveRepository(repository, targetAuthor, targetBranches, false))
def targetCommit = getCommit()
try {
sh "git pull git://github.com/${sourceAuthor}/${repository} ${sourceBranches}"
} catch (Exception e) {
println """
-------------------------------------------------------------
[ERROR] Can't merge source into Target. Please rebase PR branch.
-------------------------------------------------------------
Source: git://github.com/${sourceAuthor}/${repository} ${sourceBranches}
Target: ${targetCommit}
-------------------------------------------------------------
"""
throw e;
}
def mergedCommit = getCommit()
println """
-------------------------------------------------------------
[INFO] Source merged into Target
-------------------------------------------------------------
Target: ${targetCommit}
Produced: ${mergedCommit}
-------------------------------------------------------------
"""
}
def getCommit() {
return sh(returnStdout: true, script: 'git log --oneline -1').trim()
}
def getBranch() {
return sh(returnStdout: true, script: 'git branch --all --contains HEAD').trim()
}
def getRemoteInfo(String remoteName, String configName) {
return sh(returnStdout: true, script: "git config --get remote.${remoteName}.${configName}").trim()
}