-
Notifications
You must be signed in to change notification settings - Fork 392
/
GitHubRepositoryNameContributor.java
164 lines (151 loc) · 5.94 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
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.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");
}
}
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;
}
/**
* 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);
}
}
}
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);
}
}
}
}
}
}
}