Skip to content

Commit f7fc17a

Browse files
authored
Providers dsl (#222)
* Remove deleted branches from local repo. * WIP * Fix * Fix checkstyle * fixup * Docs * Repoprovider dsl for PR
1 parent 1b71afc commit f7fc17a

23 files changed

Lines changed: 277 additions & 16 deletions

File tree

github-pullrequest-plugin/src/main/java/com/github/kostyasha/github/integration/branch/GitHubBranchTrigger.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
import java.io.IOException;
3232
import java.util.ArrayList;
3333
import java.util.Date;
34+
import java.util.Iterator;
3435
import java.util.LinkedHashSet;
3536
import java.util.List;
37+
import java.util.Map;
3638
import java.util.Objects;
3739
import java.util.Set;
3840
import java.util.stream.Collectors;
@@ -279,11 +281,25 @@ private List<GitHubBranchCause> checkBranches(Set<GHBranch> remoteBranches,
279281
return causes;
280282
}
281283

282-
private void updateLocalRepository(Set<GHBranch> remoteBranches, GitHubBranchRepository localRepository) {
284+
public static void updateLocalRepository(Set<GHBranch> remoteBranches, GitHubBranchRepository localRepository) {
283285
long count = remoteBranches.stream()
284286
.map(updateLocalRepo(localRepository))
285287
.count();
288+
286289
LOG.trace("Updated local branch details with [{}] repositories.", count);
290+
final Map<String, GitHubBranch> localBranches = localRepository.getBranches();
291+
final Iterator<String> iterator = localBranches.keySet().iterator();
292+
while (iterator.hasNext()) {
293+
final String localBranch = iterator.next();
294+
final boolean present = remoteBranches.stream()
295+
.filter(br -> br.getName().equals(localBranch))
296+
.findFirst()
297+
.isPresent();
298+
if (!present) {
299+
LOG.debug("Removing {}, from localBranches", localBranch);
300+
iterator.remove();
301+
}
302+
}
287303
}
288304

289305
private static boolean isSupportedTriggerMode(GitHubPRTriggerMode mode) {

github-pullrequest-plugin/src/main/java/com/github/kostyasha/github/integration/branch/dsl/context/GitHubBranchTriggerDslContext.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.github.kostyasha.github.integration.branch.dsl.context;
22

33
import com.github.kostyasha.github.integration.branch.dsl.context.events.GitHubBranchEventsDslContext;
4+
import com.github.kostyasha.github.integration.generic.dsl.repoproviders.GitHubRepoProvidersDslContext;
45
import com.github.kostyasha.github.integration.branch.events.GitHubBranchEvent;
6+
import com.github.kostyasha.github.integration.generic.GitHubRepoProvider;
7+
import com.github.kostyasha.github.integration.generic.repoprovider.GitHubPluginRepoProvider;
58
import javaposse.jobdsl.dsl.Context;
69
import javaposse.jobdsl.plugin.ContextExtensionPoint;
710
import org.jenkinsci.plugins.github.pullrequest.GitHubPRTriggerMode;
@@ -11,6 +14,8 @@
1114
import java.util.Arrays;
1215
import java.util.List;
1316

17+
import static org.codehaus.groovy.runtime.InvokerHelper.asList;
18+
1419
/**
1520
* @author Kanstantsin Shautsou
1621
*/
@@ -23,6 +28,7 @@ public class GitHubBranchTriggerDslContext implements Context {
2328

2429
private List<String> whitelistedBranches = new ArrayList<>();
2530
private List<GitHubBranchEvent> events = new ArrayList<>();
31+
private List<GitHubRepoProvider> repoProviders = new ArrayList<>(asList(new GitHubPluginRepoProvider()));
2632

2733

2834
public void cron(String cron) {
@@ -56,6 +62,13 @@ public void events(Runnable closure) {
5662
events.addAll(eventsContext.events());
5763
}
5864

65+
public void repoProviders(Runnable closure) {
66+
GitHubRepoProvidersDslContext repoProvidersContext = new GitHubRepoProvidersDslContext();
67+
ContextExtensionPoint.executeInContext(closure, repoProvidersContext);
68+
69+
repoProviders.addAll(repoProvidersContext.repoProviders());
70+
}
71+
5972
public void whitelistedBranches(String... branches) {
6073
whitelistedBranches.addAll(Arrays.asList(branches));
6174
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.github.kostyasha.github.integration.generic.dsl.repoproviders;
2+
3+
import com.github.kostyasha.github.integration.generic.repoprovider.GHPermission;
4+
import javaposse.jobdsl.dsl.Context;
5+
6+
/**
7+
* @author Kanstantsin Shautsou
8+
*/
9+
public class GHRepoPermissionDslContext implements Context {
10+
private GHPermission permission = GHPermission.ADMIN;
11+
12+
public void admin() {
13+
permission = GHPermission.ADMIN;
14+
}
15+
16+
public void pull() {
17+
permission = GHPermission.PULL;
18+
}
19+
20+
public void push() {
21+
permission = GHPermission.PUSH;
22+
}
23+
24+
public GHPermission permission() {
25+
return permission;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.github.kostyasha.github.integration.generic.dsl.repoproviders;
2+
3+
import com.github.kostyasha.github.integration.generic.repoprovider.GitHubPluginRepoProvider;
4+
import javaposse.jobdsl.dsl.Context;
5+
import javaposse.jobdsl.plugin.ContextExtensionPoint;
6+
7+
/**
8+
* @author Kanstantsin Shautsou
9+
*/
10+
public class GitHubPluginRepoProviderDslContext implements Context {
11+
12+
private GitHubPluginRepoProvider provider = new GitHubPluginRepoProvider();
13+
14+
public void cacheConnection(boolean cacheConnection) {
15+
provider.setCacheConnection(cacheConnection);
16+
}
17+
18+
public void manageHooks(boolean manageHooks) {
19+
provider.setManageHooks(manageHooks);
20+
}
21+
22+
public void permission(Runnable closure) {
23+
GHRepoPermissionDslContext permissionContext = new GHRepoPermissionDslContext();
24+
ContextExtensionPoint.executeInContext(closure, permissionContext);
25+
26+
provider.setRepoPermission(permissionContext.permission());
27+
}
28+
29+
public GitHubPluginRepoProvider getProvider() {
30+
return provider;
31+
}
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.github.kostyasha.github.integration.generic.dsl.repoproviders;
2+
3+
import com.github.kostyasha.github.integration.generic.GitHubRepoProvider;
4+
import javaposse.jobdsl.dsl.Context;
5+
import javaposse.jobdsl.plugin.ContextExtensionPoint;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
/**
11+
* @author Kanstantsin Shautsou
12+
*/
13+
public class GitHubRepoProvidersDslContext implements Context {
14+
15+
private List<GitHubRepoProvider> repoProviders = new ArrayList<>();
16+
17+
public void gitHubPlugin(Runnable closure) {
18+
GitHubPluginRepoProviderDslContext githubPluginContext = new GitHubPluginRepoProviderDslContext();
19+
ContextExtensionPoint.executeInContext(closure, githubPluginContext);
20+
21+
repoProviders.add(githubPluginContext.getProvider());
22+
}
23+
24+
public List<GitHubRepoProvider> repoProviders() {
25+
return repoProviders;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.github.kostyasha.github.integration.generic.repoprovider;
2+
3+
/**
4+
* @author Kanstantsin Shautsou
5+
*/
6+
public enum GHPermission {
7+
ADMIN, PUSH, PULL
8+
}

github-pullrequest-plugin/src/main/java/com/github/kostyasha/github/integration/generic/repoprovider/GitHubPluginRepoProvider.java

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.github.kostyasha.github.integration.generic.GitHubTrigger;
77
import com.google.common.base.Optional;
88
import hudson.Extension;
9+
import org.apache.commons.lang3.BooleanUtils;
910
import org.jenkinsci.plugins.github.GitHubPlugin;
1011
import org.jenkinsci.plugins.github.internal.GHPluginConfigException;
1112
import org.jenkinsci.plugins.github.util.misc.NullSafePredicate;
@@ -39,6 +40,10 @@ public class GitHubPluginRepoProvider extends GitHubRepoProvider {
3940
// possible cache connection/repo here
4041
protected Boolean cacheConnection = true;
4142

43+
private Boolean manageHooks = true;
44+
45+
private GHPermission repoPermission = GHPermission.ADMIN;
46+
4247
private transient GHRepository remoteRepository;
4348
private transient GitHub gitHub;
4449

@@ -55,6 +60,24 @@ public void setCacheConnection(boolean cacheConnection) {
5560
this.cacheConnection = cacheConnection;
5661
}
5762

63+
public boolean isManageHooks() {
64+
return BooleanUtils.isTrue(manageHooks);
65+
}
66+
67+
@DataBoundSetter
68+
public void setManageHooks(boolean manageHooks) {
69+
this.manageHooks = manageHooks;
70+
}
71+
72+
public GHPermission getRepoPermission() {
73+
return repoPermission;
74+
}
75+
76+
@DataBoundSetter
77+
public void setRepoPermission(GHPermission repoPermission) {
78+
this.repoPermission = repoPermission;
79+
}
80+
5881
@Override
5982
public void registerHookFor(GitHubTrigger trigger) {
6083
GitHubWebHook.get().registerHookFor(trigger.getJob());
@@ -63,7 +86,7 @@ public void registerHookFor(GitHubTrigger trigger) {
6386
@Override
6487
public boolean isManageHooks(GitHubTrigger trigger) {
6588
// not exact @see https://github.com/jenkinsci/github-plugin/pull/149
66-
return GitHubPlugin.configuration().isManageHooks();
89+
return isManageHooks() && GitHubPlugin.configuration().isManageHooks();
6790
}
6891

6992
@Nonnull
@@ -76,24 +99,31 @@ public synchronized GitHub getGitHub(GitHubTrigger trigger) {
7699
final GitHubRepositoryName repoFullName = trigger.getRepoFullName();
77100

78101
Optional<GitHub> client = from(GitHubPlugin.configuration().findGithubConfig(withHost(repoFullName.getHost())))
79-
.firstMatch(withAdminAccess(repoFullName));
102+
.firstMatch(withPermission(repoFullName, getRepoPermission()));
80103
if (client.isPresent()) {
81104
gitHub = client.get();
82105
return gitHub;
83106
}
84107

85108
throw new GHPluginConfigException("GitHubPluginRepoProvider can't find appropriate client for github repo " +
86-
"<%s>. Probably you didn't configure 'GitHub Plugin' global 'GitHub Server Settings'.or there is no tokens" +
87-
"with admin access to this repo.",
88-
repoFullName.toString());
109+
"<%s>. Probably you didn't configure 'GitHub Plugin' global 'GitHub Server Settings' or there is no tokens" +
110+
"with %s access to this repository.",
111+
repoFullName.toString(), getRepoPermission());
89112
}
90113

91-
private NullSafePredicate<GitHub> withAdminAccess(final GitHubRepositoryName name) {
114+
private NullSafePredicate<GitHub> withPermission(final GitHubRepositoryName name, GHPermission permission) {
92115
return new NullSafePredicate<GitHub>() {
93116
@Override
94117
protected boolean applyNullSafe(@Nonnull GitHub gh) {
95118
try {
96-
return gh.getRepository(name.getUserName() + "/" + name.getRepositoryName()).hasAdminAccess();
119+
final GHRepository repo = gh.getRepository(name.getUserName() + "/" + name.getRepositoryName());
120+
if (permission == GHPermission.ADMIN) {
121+
return repo.hasAdminAccess();
122+
} else if (permission == GHPermission.PUSH) {
123+
return repo.hasPushAccess();
124+
} else {
125+
return repo.hasPullAccess();
126+
}
97127
} catch (IOException e) {
98128
return false;
99129
}
@@ -120,6 +150,8 @@ public synchronized GHRepository getGHRepository(GitHubTrigger trigger) {
120150

121151
protected Object readResolve() {
122152
if (isNull(cacheConnection)) cacheConnection = true;
153+
if (isNull(repoPermission)) repoPermission = GHPermission.ADMIN;
154+
if (isNull(manageHooks)) manageHooks = true;
123155
return this;
124156
}
125157

github-pullrequest-plugin/src/main/java/org/jenkinsci/plugins/github/pullrequest/dsl/context/GitHubPRTriggerDslContext.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.jenkinsci.plugins.github.pullrequest.dsl.context;
22

3+
import com.github.kostyasha.github.integration.generic.dsl.repoproviders.GitHubRepoProvidersDslContext;
4+
import com.github.kostyasha.github.integration.generic.GitHubRepoProvider;
5+
import com.github.kostyasha.github.integration.generic.repoprovider.GitHubPluginRepoProvider;
36
import javaposse.jobdsl.dsl.Context;
47
import javaposse.jobdsl.plugin.ContextExtensionPoint;
58
import org.jenkinsci.plugins.github.pullrequest.GitHubPRTriggerMode;
@@ -9,6 +12,8 @@
912
import java.util.ArrayList;
1013
import java.util.List;
1114

15+
import static org.codehaus.groovy.runtime.InvokerHelper.asList;
16+
1217
/**
1318
* @author lanwen (Merkushev Kirill)
1419
*/
@@ -19,6 +24,8 @@ public class GitHubPRTriggerDslContext implements Context {
1924
private boolean cancelQueued;
2025
private boolean abortRunning;
2126
private List<GitHubPREvent> events = new ArrayList<>();
27+
private List<GitHubRepoProvider> repoProviders = new ArrayList<>(asList(new GitHubPluginRepoProvider()));
28+
2229

2330
public void cron(String cron) {
2431
this.cron = cron;
@@ -50,6 +57,13 @@ public void events(Runnable closure) {
5057
events.addAll(eventsContext.events());
5158
}
5259

60+
public void repoProviders(Runnable closure) {
61+
GitHubRepoProvidersDslContext repoProvidersContext = new GitHubRepoProvidersDslContext();
62+
ContextExtensionPoint.executeInContext(closure, repoProvidersContext);
63+
64+
repoProviders.addAll(repoProvidersContext.repoProviders());
65+
}
66+
5367
public String cron() {
5468
return cron;
5569
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
If selected, when a new PR build is triggered, if there are already running/building
2+
builds/runs for that PR on the executors, they'll be aborted in favor of this most recent build.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<div>
2+
GitHub events or statuses that should or should not trigger a build for branch.
3+
<br/>
4+
The specified events will be checked in order, and the first to match will
5+
be used, so be sure to put events/statuses that should prevent triggering a
6+
build first in order.
7+
<br/>
8+
If you want to skip PR, you must place skip events firstly.
9+
</div>

0 commit comments

Comments
 (0)