-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathGitHubRepository.groovy
149 lines (131 loc) · 4.17 KB
/
GitHubRepository.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
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
//
// Model a remote git repository
//
package com.redhat.art
import com.redhat.art.Version
class GitHubRepository {
String owner
String project
String branch
String path
String package_name
String password
def pipeline
GitHubRepository(String owner, String project, String branch=null, String package_name=null) {
this.owner = owner
this.project = project
this.branch = branch
this.path = project
this.package_name = package_name ? package_name : project
this.password = null
this.pipeline = pipeline
}
GitHubRepository(Map init) {
this.owner = init.owner
this.project = init.project
this.branch = init.branch
this.path = init.path ? init.path : this.project
this.package_name = init.package_name ? init.package_name : this.project
this.password = init.password
this.pipeline = init.pipeline
}
def getRemote() {
return "[email protected]:${this.owner}/${this.project}.git"
}
def getUrl() {
return "https://github.com/${this.owner}/${this.project}.git"
}
def getSpecfile() {
return package_name + ".spec"
}
def getSpecpath() {
return [path, specfile].join('/')
}
/*
* Retrive the branch list from a remote repository
* @param repo_url a git@ repository URL.
* @param pattern a matching pattern for the branch list. Only return matching branches
*
* @return a list of branch names. Removes the leading ref path
*
* Requires SSH_AGENT to have set a key for access to the remote repository
*/
def branches(pattern="") {
def branch_text = pipeline.sh(
returnStdout: true,
script: [
"git ls-remote ${this.remote} ${pattern}",
"awk '{print \$2}'",
"cut -d/ -f3"
].join(" | ")
)
return branch_text.tokenize("\n")
}
/**
* Retrive a list of release numbers from the OCP remote repository
* @param repo_url a git@ repository URL.
* @return a list of OSE release numbers.
*
* Get the branch names beginning with 'enterprise-'.
* Extract the release number string from each branch release name
* Sort in version order (compare fields as integers, not strings)
* Requires SSH_AGENT to have set a key for access to the remote repository
*/
def releases(pattern="enterprise-") {
// too clever: chain - get branch names, remove prefix, suffix
def r = this.branches(pattern + '*')
.collect { it - pattern }
.findAll { it =~ /^\d+((\.\d+)*)$/ }
.collect { new Version(it) }
.sort()
return r
}
def clone() {
pipeline.sh(
returnStdout: false,
script: [
"git clone",
branch ? "--branch ${branch}" : "",
remote,
path
].join(' ')
)
pipeline.echo("Cloning repo ${remote}")
}
def addRemote(remote_name, remote_project) {
// git remote add ${remote_name} ${remote_spec}
def remote_spec = "[email protected]:${owner}/${remote_project}.git"
pipeline.dir(path) {
pipeline.sh(
script: "git remote add ${remote_name} ${remote_spec} --no-tags"
)
}
}
def fetch(remote_name) {
pipeline.dir(path) {
pipeline.sh(
script: "git fetch ${remote_name}"
)
}
}
def merge(String remote_name, String remote_branch) {
pipeline.dir(path) {
pipeline.sh(
script: "git merge ${remote_name}/${remote_branch}"
)
}
}
def config(String key, String value) {
pipeline.dir(path) {
pipeline.sh "git config ${key} ${value}"
}
}
def set_attribute(String filename, String attrname, String attrvalue) {
pipeline.dir(path) {
def gitattributes = ".gitattributes"
pipeline.sh(
script: "echo '${filename} ${attrname}=${attrvalue}' >> ${gitattributes}"
)
}
}
}