-
Notifications
You must be signed in to change notification settings - Fork 401
/
Copy pathGitHubRepositoryNameContributor.java
243 lines (224 loc) · 8.83 KB
/
GitHubRepositoryNameContributor.java
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package com.cloudbees.jenkins;
import hudson.EnvVars;
import hudson.Extension;
import hudson.ExtensionList;
import hudson.ExtensionPoint;
import hudson.Util;
import hudson.model.AbstractProject;
import hudson.model.EnvironmentContributor;
import hudson.model.Item;
import hudson.model.Job;
import hudson.model.TaskListener;
import hudson.plugins.git.BranchSpec;
import hudson.plugins.git.GitSCM;
import hudson.scm.SCM;
import jenkins.model.Jenkins;
import jenkins.triggers.SCMTriggerItem;
import jenkins.triggers.SCMTriggerItem.SCMTriggerItems;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
/**
* Extension point that associates {@link GitHubRepositoryName}s to a project.
*
* @author Kohsuke Kawaguchi
* @since 1.7
*/
public abstract class GitHubRepositoryNameContributor implements ExtensionPoint {
private static final Logger LOGGER = LoggerFactory.getLogger(GitHubRepositoryNameContributor.class);
/**
* Looks at the definition of {@link AbstractProject} and list up the related github repositories,
* then puts them into the collection.
*
* @deprecated Use {@link #parseAssociatedNames(Item, Collection)}
*/
@Deprecated
public void parseAssociatedNames(AbstractProject<?, ?> job, Collection<GitHubRepositoryName> result) {
parseAssociatedNames((Item) job, result);
}
/**
* Looks at the definition of {@link Job} and list up the related github repositories,
* then puts them into the collection.
* @deprecated Use {@link #parseAssociatedNames(Item, Collection)}
*/
@Deprecated
public /*abstract*/ void parseAssociatedNames(Job<?, ?> job, Collection<GitHubRepositoryName> result) {
parseAssociatedNames((Item) job, result);
}
/**
* Looks at the definition of {@link Item} and list up the related github repositories,
* then puts them into the collection.
* @param item the item.
* @param result the collection to add repository names to
* @since 1.25.0
*/
@SuppressWarnings("deprecation")
public /*abstract*/ void parseAssociatedNames(Item item, Collection<GitHubRepositoryName> result) {
if (Util.isOverridden(
GitHubRepositoryNameContributor.class,
getClass(),
"parseAssociatedNames",
Job.class,
Collection.class
)) {
// if this impl is legacy, it cannot contribute to non-jobs, so not an error
if (item instanceof Job) {
parseAssociatedNames((Job<?, ?>) item, result);
}
} else if (Util.isOverridden(
GitHubRepositoryNameContributor.class,
getClass(),
"parseAssociatedNames",
AbstractProject.class,
Collection.class
)) {
// if this impl is legacy, it cannot contribute to non-projects, so not an error
if (item instanceof AbstractProject) {
parseAssociatedNames((AbstractProject<?, ?>) item, result);
}
} else {
throw new AbstractMethodError("you must override the new overload of parseAssociatedNames");
}
}
/**
* Looks at the definition of {@link Item} and list up its branches, then puts them into
* the collection.
*/
public /*abstract*/ void parseAssociatedBranches(Item item, Collection<GitHubBranch> result) {
if (Util.isOverridden(
GitHubRepositoryNameContributor.class,
getClass(),
"parseAssociatedBranches",
Job.class,
Collection.class
)) {
// if this impl is legacy, it cannot contribute to non-jobs, so not an error
if (item instanceof Job) {
parseAssociatedBranches((Job<?, ?>) item, result);
}
} else if (Util.isOverridden(
GitHubRepositoryNameContributor.class,
getClass(),
"parseAssociatedBranches",
AbstractProject.class,
Collection.class
)) {
// if this impl is legacy, it cannot contribute to non-projects, so not an error
if (item instanceof AbstractProject) {
parseAssociatedBranches((AbstractProject<?, ?>) item, result);
}
} else {
throw new AbstractMethodError("you must override the new overload of parseAssociatedBranches");
}
};
public static ExtensionList<GitHubRepositoryNameContributor> all() {
return Jenkins.getInstance().getExtensionList(GitHubRepositoryNameContributor.class);
}
/**
* @deprecated Use {@link #parseAssociatedNames(Job)}
*/
@Deprecated
public static Collection<GitHubRepositoryName> parseAssociatedNames(AbstractProject<?, ?> job) {
return parseAssociatedNames((Item) job);
}
/**
* @deprecated Use {@link #parseAssociatedNames(Item)}
*/
@Deprecated
public static Collection<GitHubRepositoryName> parseAssociatedNames(Job<?, ?> job) {
return parseAssociatedNames((Item) job);
}
public static Collection<GitHubRepositoryName> parseAssociatedNames(Item item) {
Set<GitHubRepositoryName> names = new HashSet<GitHubRepositoryName>();
for (GitHubRepositoryNameContributor c : all()) {
c.parseAssociatedNames(item, names);
}
return names;
}
/**
* @deprecated Use {@link #parseAssociatedBranches(Job)}
*/
@Deprecated
public static Collection<GitHubBranch> parseAssociatedBranches(AbstractProject<?, ?> job) {
return parseAssociatedBranches((Item) job);
}
/**
* @deprecated Use {@link #parseAssociatedBranches(Item)}
*/
@Deprecated
public static Collection<GitHubBranch> parseAssociatedBranches(Job<?, ?> job) {
return parseAssociatedBranches((Item) job);
}
public static Collection<GitHubBranch> parseAssociatedBranches(Item item) {
Set<GitHubBranch> names = new HashSet<GitHubBranch>();
for (GitHubRepositoryNameContributor c : all()) {
c.parseAssociatedBranches(item, names);
}
return names;
}
/**
* Default implementation that looks at SCMs
*/
@Extension
public static class FromSCM extends GitHubRepositoryNameContributor {
@Override
public void parseAssociatedNames(Item item, Collection<GitHubRepositoryName> result) {
SCMTriggerItem triggerItem = SCMTriggerItems.asSCMTriggerItem(item);
EnvVars envVars = item instanceof Job ? buildEnv((Job) item) : new EnvVars();
if (triggerItem != null) {
for (SCM scm : triggerItem.getSCMs()) {
addRepositories(scm, envVars, result);
}
}
}
@Override
public void parseAssociatedBranches(Item item, Collection<GitHubBranch> result) {
SCMTriggerItem triggerItem = SCMTriggerItems.asSCMTriggerItem(item);
if (triggerItem != null) {
for (SCM scm : triggerItem.getSCMs()) {
addBranches(scm, result);
}
}
}
protected EnvVars buildEnv(Job<?, ?> job) {
EnvVars env = new EnvVars();
for (EnvironmentContributor contributor : EnvironmentContributor.all()) {
try {
contributor.buildEnvironmentFor(job, env, TaskListener.NULL);
} catch (Exception e) {
LOGGER.debug("{} failed to build env ({}), skipping", contributor.getClass(), e.getMessage(), e);
}
}
return env;
}
protected static void addRepositories(SCM scm, EnvVars env, Collection<GitHubRepositoryName> r) {
if (scm instanceof GitSCM) {
GitSCM git = (GitSCM) scm;
for (RemoteConfig rc : git.getRepositories()) {
for (URIish uri : rc.getURIs()) {
String url = env.expand(uri.toString());
GitHubRepositoryName repo = GitHubRepositoryName.create(url);
if (repo != null) {
r.add(repo);
}
}
}
}
}
protected static void addBranches(SCM scm, Collection<GitHubBranch> r) {
if (scm instanceof GitSCM) {
GitSCM git = (GitSCM) scm;
for (BranchSpec branch : git.getBranches()) {
GitHubBranch gitHubBranch = GitHubBranch.create(branch);
if (gitHubBranch != null) {
r.add(gitHubBranch);
}
}
}
}
}
}