Skip to content

Commit bd2e945

Browse files
stephencKostyaSha
authored andcommitted
[FIXED JENKINS-39553] Make GitHub plugin BuildableItem aware (#153)
* [FIXED JENKINS-39533] Make GitHub plugin BuildableItem aware * Address code review comments from Oleg * not actually deprecated * Address review comments
1 parent ebfcc1b commit bd2e945

18 files changed

Lines changed: 269 additions & 93 deletions

src/main/java/com/cloudbees/jenkins/Cleaner.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.cloudbees.jenkins;
22

33
import hudson.Extension;
4-
import hudson.model.Job;
4+
import hudson.model.Item;
55
import hudson.model.PeriodicWork;
66
import jenkins.model.Jenkins;
77
import org.jenkinsci.plugins.github.GitHubPlugin;
@@ -28,7 +28,7 @@
2828
public class Cleaner extends PeriodicWork {
2929
/**
3030
* Queue contains repo names prepared to cleanup.
31-
* After configure method on job, trigger calls {@link #onStop(Job)}
31+
* After configure method on job, trigger calls {@link #onStop(Item)}
3232
* which converts to repo names with help of contributors.
3333
*
3434
* This queue is thread-safe, so any thread can write or
@@ -39,8 +39,8 @@ public class Cleaner extends PeriodicWork {
3939
/**
4040
* Called when a {@link GitHubPushTrigger} is about to be removed.
4141
*/
42-
/* package */ void onStop(Job<?, ?> job) {
43-
cleanQueue.addAll(GitHubRepositoryNameContributor.parseAssociatedNames(job));
42+
/* package */ void onStop(Item item) {
43+
cleanQueue.addAll(GitHubRepositoryNameContributor.parseAssociatedNames(item));
4444
}
4545

4646
@Override
@@ -61,8 +61,8 @@ protected void doRun() throws Exception {
6161

6262
URL url = GitHubPlugin.configuration().getHookUrl();
6363

64-
List<Job> jobs = Jenkins.getInstance().getAllItems(Job.class);
65-
List<GitHubRepositoryName> aliveRepos = from(jobs)
64+
List<Item> items = Jenkins.getInstance().getAllItems(Item.class);
65+
List<GitHubRepositoryName> aliveRepos = from(items)
6666
.filter(isAlive()) // live repos
6767
.transformAndConcat(associatedNames()).toList();
6868

src/main/java/com/cloudbees/jenkins/GitHubPushTrigger.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.jenkinsci.plugins.github.config.GitHubPluginConfig;
2828
import org.jenkinsci.plugins.github.internal.GHPluginConfigException;
2929
import org.jenkinsci.plugins.github.migration.Migrator;
30+
import org.kohsuke.accmod.Restricted;
31+
import org.kohsuke.accmod.restrictions.NoExternalUse;
3032
import org.kohsuke.stapler.AncestorInPath;
3133
import org.kohsuke.stapler.DataBoundConstructor;
3234
import org.slf4j.Logger;
@@ -368,19 +370,20 @@ private static ThreadFactory threadFactory() {
368370
}
369371

370372
/**
371-
* Checks that repo defined in this job is not in administrative monitor as failed to be registered.
373+
* Checks that repo defined in this item is not in administrative monitor as failed to be registered.
372374
* If that so, shows warning with some instructions
373375
*
374-
* @param job - to check against. Should be not null and have at least one repo defined
376+
* @param item - to check against. Should be not null and have at least one repo defined
375377
*
376378
* @return warning or empty string
377379
* @since 1.17.0
378380
*/
379381
@SuppressWarnings("unused")
380-
public FormValidation doCheckHookRegistered(@AncestorInPath Job<?, ?> job) {
381-
Preconditions.checkNotNull(job, "Job can't be null if wants to check hook in monitor");
382+
@Restricted(NoExternalUse.class) // invoked from Stapler
383+
public FormValidation doCheckHookRegistered(@AncestorInPath Item item) {
384+
Preconditions.checkNotNull(item, "Item can't be null if wants to check hook in monitor");
382385

383-
Collection<GitHubRepositoryName> repos = GitHubRepositoryNameContributor.parseAssociatedNames(job);
386+
Collection<GitHubRepositoryName> repos = GitHubRepositoryNameContributor.parseAssociatedNames(item);
384387

385388
for (GitHubRepositoryName repo : repos) {
386389
if (monitor.isProblemWith(repo)) {

src/main/java/com/cloudbees/jenkins/GitHubRepositoryNameContributor.java

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import hudson.Util;
88
import hudson.model.AbstractProject;
99
import hudson.model.EnvironmentContributor;
10+
import hudson.model.Item;
1011
import hudson.model.Job;
1112
import hudson.model.TaskListener;
1213
import hudson.plugins.git.GitSCM;
@@ -36,41 +37,57 @@ public abstract class GitHubRepositoryNameContributor implements ExtensionPoint
3637
* Looks at the definition of {@link AbstractProject} and list up the related github repositories,
3738
* then puts them into the collection.
3839
*
39-
* @deprecated Use {@link #parseAssociatedNames(Job, Collection)}
40+
* @deprecated Use {@link #parseAssociatedNames(Item, Collection)}
4041
*/
4142
@Deprecated
4243
public void parseAssociatedNames(AbstractProject<?, ?> job, Collection<GitHubRepositoryName> result) {
43-
parseAssociatedNames((Job) job, result);
44+
parseAssociatedNames((Item) job, result);
4445
}
4546

4647
/**
4748
* Looks at the definition of {@link Job} and list up the related github repositories,
4849
* then puts them into the collection.
50+
* @deprecated Use {@link #parseAssociatedNames(Item, Collection)}
4951
*/
52+
@Deprecated
5053
public /*abstract*/ void parseAssociatedNames(Job<?, ?> job, Collection<GitHubRepositoryName> result) {
51-
if (overriddenMethodHasDeprecatedSignature(job)) {
52-
parseAssociatedNames((AbstractProject) job, result);
53-
} else {
54-
throw new AbstractMethodError("you must override the new overload of parseAssociatedNames");
55-
}
54+
parseAssociatedNames((Item) job, result);
5655
}
5756

5857
/**
59-
* To select backward compatible method with old extensions
60-
* with overridden {@link #parseAssociatedNames(AbstractProject, Collection)}
61-
*
62-
* @param job - parameter to check for old class
63-
*
64-
* @return true if overridden deprecated method
58+
* Looks at the definition of {@link Item} and list up the related github repositories,
59+
* then puts them into the collection.
60+
* @param item the item.
61+
* @param result the collection to add repository names to
62+
* @since FIXME
6563
*/
66-
private boolean overriddenMethodHasDeprecatedSignature(Job<?, ?> job) {
67-
return Util.isOverridden(
64+
@SuppressWarnings("deprecation")
65+
public /*abstract*/ void parseAssociatedNames(Item item, Collection<GitHubRepositoryName> result) {
66+
if (Util.isOverridden(
67+
GitHubRepositoryNameContributor.class,
68+
getClass(),
69+
"parseAssociatedNames",
70+
Job.class,
71+
Collection.class
72+
)) {
73+
// if this impl is legacy, it cannot contribute to non-jobs, so not an error
74+
if (item instanceof Job) {
75+
parseAssociatedNames((Job<?, ?>) item, result);
76+
}
77+
} else if (Util.isOverridden(
6878
GitHubRepositoryNameContributor.class,
6979
getClass(),
7080
"parseAssociatedNames",
7181
AbstractProject.class,
7282
Collection.class
73-
) && job instanceof AbstractProject;
83+
)) {
84+
// if this impl is legacy, it cannot contribute to non-projects, so not an error
85+
if (item instanceof AbstractProject) {
86+
parseAssociatedNames((AbstractProject<?, ?>) item, result);
87+
}
88+
} else {
89+
throw new AbstractMethodError("you must override the new overload of parseAssociatedNames");
90+
}
7491
}
7592

7693
public static ExtensionList<GitHubRepositoryNameContributor> all() {
@@ -82,13 +99,21 @@ public static ExtensionList<GitHubRepositoryNameContributor> all() {
8299
*/
83100
@Deprecated
84101
public static Collection<GitHubRepositoryName> parseAssociatedNames(AbstractProject<?, ?> job) {
85-
return parseAssociatedNames((Job) job);
102+
return parseAssociatedNames((Item) job);
86103
}
87104

105+
/**
106+
* @deprecated Use {@link #parseAssociatedNames(Item)}
107+
*/
108+
@Deprecated
88109
public static Collection<GitHubRepositoryName> parseAssociatedNames(Job<?, ?> job) {
110+
return parseAssociatedNames((Item) job);
111+
}
112+
113+
public static Collection<GitHubRepositoryName> parseAssociatedNames(Item item) {
89114
Set<GitHubRepositoryName> names = new HashSet<GitHubRepositoryName>();
90115
for (GitHubRepositoryNameContributor c : all()) {
91-
c.parseAssociatedNames(job, names);
116+
c.parseAssociatedNames(item, names);
92117
}
93118
return names;
94119
}
@@ -99,11 +124,11 @@ public static Collection<GitHubRepositoryName> parseAssociatedNames(Job<?, ?> jo
99124
@Extension
100125
public static class FromSCM extends GitHubRepositoryNameContributor {
101126
@Override
102-
public void parseAssociatedNames(Job<?, ?> job, Collection<GitHubRepositoryName> result) {
103-
SCMTriggerItem item = SCMTriggerItems.asSCMTriggerItem(job);
104-
EnvVars envVars = buildEnv(job);
105-
if (item != null) {
106-
for (SCM scm : item.getSCMs()) {
127+
public void parseAssociatedNames(Item item, Collection<GitHubRepositoryName> result) {
128+
SCMTriggerItem triggerItem = SCMTriggerItems.asSCMTriggerItem(item);
129+
EnvVars envVars = item instanceof Job ? buildEnv((Job) item) : new EnvVars();
130+
if (triggerItem != null) {
131+
for (SCM scm : triggerItem.getSCMs()) {
107132
addRepositories(scm, envVars, result);
108133
}
109134
}

src/main/java/com/cloudbees/jenkins/GitHubTrigger.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import hudson.Extension;
44
import hudson.Util;
55
import hudson.model.AbstractProject;
6-
import hudson.model.Job;
6+
import hudson.model.Item;
77
import hudson.triggers.Trigger;
88
import jenkins.model.ParameterizedJobMixIn;
99

@@ -46,9 +46,9 @@ public interface GitHubTrigger {
4646
@Extension
4747
class GitHubRepositoryNameContributorImpl extends GitHubRepositoryNameContributor {
4848
@Override
49-
public void parseAssociatedNames(Job<?, ?> job, Collection<GitHubRepositoryName> result) {
50-
if (job instanceof ParameterizedJobMixIn.ParameterizedJob) {
51-
ParameterizedJobMixIn.ParameterizedJob p = (ParameterizedJobMixIn.ParameterizedJob) job;
49+
public void parseAssociatedNames(Item item, Collection<GitHubRepositoryName> result) {
50+
if (item instanceof ParameterizedJobMixIn.ParameterizedJob) {
51+
ParameterizedJobMixIn.ParameterizedJob p = (ParameterizedJobMixIn.ParameterizedJob) item;
5252
// TODO use standard method in 1.621+
5353
for (GitHubTrigger ght : Util.filter(p.getTriggers().values(), GitHubTrigger.class)) {
5454
result.addAll(ght.getGitHubRepositories());

src/main/java/com/cloudbees/jenkins/GitHubWebHook.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.google.common.base.Function;
44
import hudson.Extension;
55
import hudson.ExtensionPoint;
6+
import hudson.model.Item;
67
import hudson.model.Job;
78
import hudson.model.RootAction;
89
import hudson.model.UnprotectedRootAction;
@@ -70,21 +71,36 @@ public String getUrlName() {
7071
* {@code GitHubWebHook.get().registerHookFor(job);}
7172
*
7273
* @param job not null project to register hook for
74+
* @deprecated use {@link #registerHookFor(Item)}
7375
*/
76+
@Deprecated
7477
public void registerHookFor(Job job) {
7578
reRegisterHookForJob().apply(job);
7679
}
7780

81+
/**
82+
* If any wants to auto-register hook, then should call this method
83+
* Example code:
84+
* {@code GitHubWebHook.get().registerHookFor(item);}
85+
*
86+
* @param item not null item to register hook for
87+
* @since FIXME
88+
*/
89+
public void registerHookFor(Item item) {
90+
reRegisterHookForJob().apply(item);
91+
}
92+
7893
/**
7994
* Calls {@link #registerHookFor(Job)} for every project which have subscriber
8095
*
8196
* @return list of jobs which jenkins tried to register hook
8297
*/
83-
public List<Job> reRegisterAllHooks() {
84-
return from(getJenkinsInstance().getAllItems(Job.class))
98+
public List<Item> reRegisterAllHooks() {
99+
return from(getJenkinsInstance().getAllItems(Item.class))
85100
.filter(isBuildable())
86101
.filter(isAlive())
87-
.transform(reRegisterHookForJob()).toList();
102+
.transform(reRegisterHookForJob())
103+
.toList();
88104
}
89105

90106
/**
@@ -101,11 +117,11 @@ public void doIndex(@Nonnull @GHEventHeader GHEvent event, @Nonnull @GHEventPayl
101117
.transform(processEvent(event, payload)).toList();
102118
}
103119

104-
private Function<Job, Job> reRegisterHookForJob() {
105-
return new Function<Job, Job>() {
120+
private <T extends Item> Function<T, T> reRegisterHookForJob() {
121+
return new Function<T, T>() {
106122
@Override
107-
public Job apply(Job job) {
108-
LOGGER.debug("Calling registerHooks() for {}", notNull(job, "Job can't be null").getFullName());
123+
public T apply(T job) {
124+
LOGGER.debug("Calling registerHooks() for {}", notNull(job, "Item can't be null").getFullName());
109125

110126
// We should handle wrong url of self defined hook url here in any case with try-catch :(
111127
URL hookUrl;

src/main/java/org/jenkinsci/plugins/github/config/GitHubPluginConfig.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import hudson.Extension;
77
import hudson.XmlFile;
88
import hudson.model.Descriptor;
9-
import hudson.model.Job;
9+
import hudson.model.Item;
1010
import hudson.util.FormValidation;
1111
import jenkins.model.GlobalConfiguration;
1212
import jenkins.model.Jenkins;
@@ -181,10 +181,10 @@ public FormValidation doReRegister() {
181181
return FormValidation.warning("Works only when Jenkins manages hooks (one ore more creds specified)");
182182
}
183183

184-
List<Job> registered = GitHubWebHook.get().reRegisterAllHooks();
184+
List<Item> registered = GitHubWebHook.get().reRegisterAllHooks();
185185

186-
LOGGER.info("Called registerHooks() for {} jobs", registered.size());
187-
return FormValidation.ok("Called re-register hooks for %s jobs", registered.size());
186+
LOGGER.info("Called registerHooks() for {} items", registered.size());
187+
return FormValidation.ok("Called re-register hooks for %s items", registered.size());
188188
}
189189

190190
@SuppressWarnings("unused")

0 commit comments

Comments
 (0)