Skip to content

Commit 187600f

Browse files
committed
Add an option to only build repositories that are private
1 parent 07b69ab commit 187600f

File tree

6 files changed

+113
-1
lines changed

6 files changed

+113
-1
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.jenkinsci.plugins.github_branch_source;
2+
3+
import hudson.Extension;
4+
import jenkins.scm.api.trait.SCMNavigatorContext;
5+
import jenkins.scm.api.trait.SCMNavigatorTrait;
6+
import jenkins.scm.api.trait.SCMNavigatorTraitDescriptor;
7+
import jenkins.scm.impl.trait.Selection;
8+
import org.jenkinsci.Symbol;
9+
import org.kohsuke.stapler.DataBoundConstructor;
10+
11+
import javax.annotation.Nonnull;
12+
13+
/**
14+
* A {@link Selection} trait that will restrict the discovery of repositories that are public.
15+
*/
16+
public class ExcludePublicRepositoriesTrait extends SCMNavigatorTrait {
17+
18+
/**
19+
* Constructor for stapler.
20+
*/
21+
@DataBoundConstructor
22+
public ExcludePublicRepositoriesTrait() {
23+
}
24+
25+
/**
26+
* {@inheritDoc}
27+
*/
28+
@Override
29+
protected void decorateContext(SCMNavigatorContext<?, ?> context) {
30+
super.decorateContext(context);
31+
GitHubSCMNavigatorContext ctx = (GitHubSCMNavigatorContext) context;
32+
ctx.setExcludePublicRepositories(true);
33+
}
34+
35+
/**
36+
* Exclude archived repositories filter
37+
*/
38+
@Symbol("gitHubExcludePublicRepositories")
39+
@Extension
40+
@Selection
41+
public static class DescriptorImpl extends SCMNavigatorTraitDescriptor {
42+
43+
@Override
44+
public Class<? extends SCMNavigatorContext> getContextClass() {
45+
return GitHubSCMNavigatorContext.class;
46+
}
47+
48+
@Nonnull
49+
@Override
50+
public String getDisplayName() {
51+
return Messages.ExcludePublicRepositoriesTrait_displayName();
52+
}
53+
}
54+
}

src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMNavigator.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,11 @@ public void visitSources(SCMSourceObserver observer) throws IOException, Interru
967967
listener.getLogger()
968968
.println(GitHubConsoleNote.create(System.currentTimeMillis(), String.format(
969969
"Skipping repository %s because it is missing one or more of the following topics: '%s'", repo.getName(), gitHubSCMNavigatorContext.getTopics())));
970+
} else if (!repo.isPrivate() && gitHubSCMNavigatorContext.isExcludePublicRepositories()) {
971+
witness.record(repo.getName(), false);
972+
listener.getLogger()
973+
.println(GitHubConsoleNote.create(System.currentTimeMillis(), String.format(
974+
"Skipping repository %s because it is public", repo.getName())));
970975
} else if (request.process(repo.getName(), sourceFactory, null, witness)) {
971976
listener.getLogger()
972977
.println(GitHubConsoleNote.create(System.currentTimeMillis(), String.format(
@@ -1009,7 +1014,14 @@ public void visitSources(SCMSourceObserver observer) throws IOException, Interru
10091014
listener.getLogger()
10101015
.println(GitHubConsoleNote.create(System.currentTimeMillis(), String.format(
10111016
"Skipping repository %s because it is missing one or more of the following topics: '%s'", repo.getName(), gitHubSCMNavigatorContext.getTopics())));
1012-
} else if (request.process(repo.getName(), sourceFactory, null, witness)) {
1017+
1018+
} else if (!repo.isPrivate() && gitHubSCMNavigatorContext.isExcludePublicRepositories()) {
1019+
witness.record(repo.getName(), false);
1020+
listener.getLogger()
1021+
.println(GitHubConsoleNote.create(System.currentTimeMillis(), String.format(
1022+
"Skipping repository %s because it is public", repo.getName())));
1023+
1024+
} else if (request.process(repo.getName(), sourceFactory, null, witness)) {
10131025
listener.getLogger().println(GitHubConsoleNote.create(System.currentTimeMillis(), String.format(
10141026
"%d repositories were processed (query completed)", witness.getCount())));
10151027
}
@@ -1150,6 +1162,12 @@ public void visitSource(String sourceName, SCMSourceObserver observer)
11501162
listener.getLogger()
11511163
.println(GitHubConsoleNote.create(System.currentTimeMillis(), String.format(
11521164
"Skipping repository %s because it is missing one or more of the following topics: '%s'", repo.getName(), gitHubSCMNavigatorContext.getTopics())));
1165+
} else if (!repo.isPrivate() && gitHubSCMNavigatorContext.isExcludePublicRepositories()) {
1166+
witness.record(repo.getName(), false);
1167+
listener.getLogger()
1168+
.println(GitHubConsoleNote.create(System.currentTimeMillis(), String.format(
1169+
"Skipping repository %s because it is public", repo.getName())));
1170+
11531171
} else if (request.process(repo.getName(), sourceFactory, null, witness)) {
11541172
listener.getLogger()
11551173
.println(GitHubConsoleNote.create(System.currentTimeMillis(), String.format(
@@ -1190,6 +1208,12 @@ public void visitSource(String sourceName, SCMSourceObserver observer)
11901208
"Skipping repository %s because it is not in team %s",
11911209
repo.getName(),
11921210
gitHubSCMNavigatorContext.getTeamSlug())));
1211+
} else if (!repo.isPrivate() && gitHubSCMNavigatorContext.isExcludePublicRepositories()) {
1212+
witness.record(repo.getName(), false);
1213+
listener.getLogger()
1214+
.println(GitHubConsoleNote.create(System.currentTimeMillis(), String.format(
1215+
"Skipping repository %s because it is public", repo.getName())));
1216+
11931217
} else if (request.process(repo.getName(), sourceFactory, null, witness)) {
11941218
listener.getLogger()
11951219
.println(GitHubConsoleNote.create(System.currentTimeMillis(), String.format(
@@ -1228,6 +1252,12 @@ public void visitSource(String sourceName, SCMSourceObserver observer)
12281252
listener.getLogger()
12291253
.println(GitHubConsoleNote.create(System.currentTimeMillis(), String.format(
12301254
"Skipping repository %s because it is missing one or more of the following topics: '%s'", repo.getName(), gitHubSCMNavigatorContext.getTopics())));
1255+
} else if (!repo.isPrivate() && gitHubSCMNavigatorContext.isExcludePublicRepositories()) {
1256+
witness.record(repo.getName(), false);
1257+
listener.getLogger()
1258+
.println(GitHubConsoleNote.create(System.currentTimeMillis(), String.format(
1259+
"Skipping repository %s because it is public", repo.getName())));
1260+
12311261
} else if (request.process(repo.getName(), sourceFactory, null, witness)) {
12321262
listener.getLogger()
12331263
.println(GitHubConsoleNote.create(System.currentTimeMillis(), String.format(

src/main/java/org/jenkinsci/plugins/github_branch_source/GitHubSCMNavigatorContext.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ public class GitHubSCMNavigatorContext extends SCMNavigatorContext<GitHubSCMNavi
5353
*/
5454
private boolean excludeArchivedRepositories;
5555

56+
/**
57+
* If true, public repositories will be ignored.
58+
*/
59+
private boolean excludePublicRepositories;
60+
5661
/**
5762
* {@inheritDoc}
5863
*/
@@ -99,10 +104,24 @@ public boolean isExcludeArchivedRepositories() {
99104
return excludeArchivedRepositories;
100105
}
101106

107+
/**
108+
* @return True if public repositories should be ignored, false if they should be included.
109+
*/
110+
public boolean isExcludePublicRepositories() {
111+
return excludePublicRepositories;
112+
}
113+
102114
/**
103115
* @param excludeArchivedRepositories Set true to exclude archived repositories
104116
*/
105117
public void setExcludeArchivedRepositories(boolean excludeArchivedRepositories) {
106118
this.excludeArchivedRepositories = excludeArchivedRepositories;
107119
}
120+
121+
/**
122+
* @param excludePublicRepositories Set true to exclude public repositories
123+
*/
124+
public void setExcludePublicRepositories(boolean excludePublicRepositories) {
125+
this.excludePublicRepositories = excludePublicRepositories;
126+
}
108127
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?jelly escape-by-default='true'?>
2+
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:c="/lib/credentials"
3+
xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form"
4+
xmlns:f2="/org/jenkinsci/plugins/github_branch_source/form">
5+
</j:jelly>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div>
2+
Exclude GitHub repositories that are public. If set, no jobs will be created for public repositories.
3+
</div>

src/main/resources/org/jenkinsci/plugins/github_branch_source/Messages.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ SSHCheckoutTrait.useAgentKey=- use build agent''s key -
6464
TagDiscoveryTrait.authorityDisplayName=Trust origin tags
6565
TagDiscoveryTrait.displayName=Discover tags
6666
ExcludeArchivedRepositoriesTrait.displayName=Exclude archived repositories
67+
ExcludePublicRepositoriesTrait.displayName=Exclude public repositories
6768

6869
GitHubSCMNavigator.general=General
6970
GitHubSCMNavigator.withinRepository=Within repository

0 commit comments

Comments
 (0)