Skip to content

Commit b953513

Browse files
authored
Merge pull request #274 from dmwgroup/JENKINS-51519
[FIXED JENKINS-51519] Ignore archived repos.
2 parents 089d562 + 8a3929c commit b953513

21 files changed

+1299
-17
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 have been archived.
15+
*/
16+
public class ExcludeArchivedRepositoriesTrait extends SCMNavigatorTrait {
17+
18+
/**
19+
* Constructor for stapler.
20+
*/
21+
@DataBoundConstructor
22+
public ExcludeArchivedRepositoriesTrait() {
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.setExcludeArchivedRepositories(true);
33+
}
34+
35+
/**
36+
* Exclude archived repositories filter
37+
*/
38+
@Symbol("gitHubExcludeArchivedRepositories")
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.ExcludeArchivedRepositoriesTrait_displayName();
52+
}
53+
}
54+
}

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

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -951,7 +951,14 @@ public void visitSources(SCMSourceObserver observer) throws IOException, Interru
951951
if (!repo.getOwnerName().equals(repoOwner)) {
952952
continue; // ignore repos in other orgs when using GHMyself
953953
}
954-
if (request.process(repo.getName(), sourceFactory, null, witness)) {
954+
955+
if (repo.isArchived() && gitHubSCMNavigatorContext.isExcludeArchivedRepositories()) {
956+
witness.record(repo.getName(), false);
957+
listener.getLogger()
958+
.println(GitHubConsoleNote.create(System.currentTimeMillis(), String.format(
959+
"Skipping repository %s because it is archived", repo.getName())));
960+
961+
} else if (request.process(repo.getName(), sourceFactory, null, witness)) {
955962
listener.getLogger()
956963
.println(GitHubConsoleNote.create(System.currentTimeMillis(), String.format(
957964
"%d repositories were processed (query completed)", witness.getCount()
@@ -979,7 +986,14 @@ public void visitSources(SCMSourceObserver observer) throws IOException, Interru
979986
}
980987
for (GHRepository repo : repositories) {
981988
Connector.checkApiRateLimit(listener, github);
982-
if (request.process(repo.getName(), sourceFactory, null, witness)) {
989+
990+
if (repo.isArchived() && gitHubSCMNavigatorContext.isExcludeArchivedRepositories()) {
991+
witness.record(repo.getName(), false);
992+
listener.getLogger()
993+
.println(GitHubConsoleNote.create(System.currentTimeMillis(), String.format(
994+
"Skipping repository %s because it is archived", repo.getName())));
995+
996+
} else if (request.process(repo.getName(), sourceFactory, null, witness)) {
983997
listener.getLogger().println(GitHubConsoleNote.create(System.currentTimeMillis(), String.format(
984998
"%d repositories were processed (query completed)", witness.getCount()
985999
)));
@@ -1003,7 +1017,14 @@ public void visitSources(SCMSourceObserver observer) throws IOException, Interru
10031017
Connector.checkApiRateLimit(listener, github);
10041018
for (GHRepository repo : user.listRepositories(100)) {
10051019
Connector.checkApiRateLimit(listener, github);
1006-
if (request.process(repo.getName(), sourceFactory, null, witness)) {
1020+
1021+
if (repo.isArchived() && gitHubSCMNavigatorContext.isExcludeArchivedRepositories()) {
1022+
witness.record(repo.getName(), false);
1023+
listener.getLogger()
1024+
.println(GitHubConsoleNote.create(System.currentTimeMillis(), String.format(
1025+
"Skipping repository %s because it is archived", repo.getName())));
1026+
1027+
} else if (request.process(repo.getName(), sourceFactory, null, witness)) {
10071028
listener.getLogger()
10081029
.println(GitHubConsoleNote.create(System.currentTimeMillis(), String.format(
10091030
"%d repositories were processed (query completed)", witness.getCount()
@@ -1072,9 +1093,9 @@ public void visitSource(String sourceName, SCMSourceObserver observer)
10721093
throw new AbortException(message);
10731094
}
10741095

1075-
GitHubSCMNavigatorRequest request = new GitHubSCMNavigatorContext()
1076-
.withTraits(traits)
1077-
.newRequest(this, observer);
1096+
GitHubSCMNavigatorContext gitHubSCMNavigatorContext = new GitHubSCMNavigatorContext().withTraits(traits);
1097+
GitHubSCMNavigatorRequest request = gitHubSCMNavigatorContext.newRequest(this, observer);
1098+
10781099
try {
10791100
SourceFactory sourceFactory = new SourceFactory(request);
10801101
WitnessImpl witness = new WitnessImpl(listener);
@@ -1094,7 +1115,14 @@ public void visitSource(String sourceName, SCMSourceObserver observer)
10941115
listener.getLogger().format("Looking up %s repository of myself %s%n%n", sourceName, repoOwner);
10951116
GHRepository repo = myself.getRepository(sourceName);
10961117
if (repo != null && repo.getOwnerName().equals(repoOwner)) {
1097-
if (request.process(repo.getName(), sourceFactory, null, witness)) {
1118+
1119+
if (repo.isArchived() && gitHubSCMNavigatorContext.isExcludeArchivedRepositories()) {
1120+
witness.record(repo.getName(), false);
1121+
listener.getLogger()
1122+
.println(GitHubConsoleNote.create(System.currentTimeMillis(), String.format(
1123+
"Skipping repository %s because it is archived", repo.getName())));
1124+
1125+
} else if (request.process(repo.getName(), sourceFactory, null, witness)) {
10981126
listener.getLogger()
10991127
.println(GitHubConsoleNote.create(System.currentTimeMillis(), String.format(
11001128
"%d repositories were processed (query completed)", witness.getCount()
@@ -1117,7 +1145,14 @@ public void visitSource(String sourceName, SCMSourceObserver observer)
11171145
.format("Looking up %s repository of organization %s%n%n", sourceName, repoOwner);
11181146
GHRepository repo = org.getRepository(sourceName);
11191147
if (repo != null) {
1120-
if (request.process(repo.getName(), sourceFactory, null, witness)) {
1148+
1149+
if (repo.isArchived() && gitHubSCMNavigatorContext.isExcludeArchivedRepositories()) {
1150+
witness.record(repo.getName(), false);
1151+
listener.getLogger()
1152+
.println(GitHubConsoleNote.create(System.currentTimeMillis(), String.format(
1153+
"Skipping repository %s because it is archived", repo.getName())));
1154+
1155+
} else if (request.process(repo.getName(), sourceFactory, null, witness)) {
11211156
listener.getLogger()
11221157
.println(GitHubConsoleNote.create(System.currentTimeMillis(), String.format(
11231158
"%d repositories were processed (query completed)", witness.getCount()
@@ -1142,7 +1177,14 @@ public void visitSource(String sourceName, SCMSourceObserver observer)
11421177
listener.getLogger().format("Looking up %s repository of user %s%n%n", sourceName, repoOwner);
11431178
GHRepository repo = user.getRepository(sourceName);
11441179
if (repo != null) {
1145-
if (request.process(repo.getName(), sourceFactory, null, witness)) {
1180+
1181+
if (repo.isArchived() && gitHubSCMNavigatorContext.isExcludeArchivedRepositories()) {
1182+
witness.record(repo.getName(), false);
1183+
listener.getLogger()
1184+
.println(GitHubConsoleNote.create(System.currentTimeMillis(), String.format(
1185+
"Skipping repository %s because it is archived", repo.getName())));
1186+
1187+
} else if (request.process(repo.getName(), sourceFactory, null, witness)) {
11461188
listener.getLogger()
11471189
.println(GitHubConsoleNote.create(System.currentTimeMillis(), String.format(
11481190
"%d repositories were processed (query completed)", witness.getCount()

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
@@ -40,6 +40,11 @@ public class GitHubSCMNavigatorContext extends SCMNavigatorContext<GitHubSCMNavi
4040
*/
4141
private String teamSlug = "";
4242

43+
/**
44+
* If true, archived repositories will be ignored.
45+
*/
46+
private boolean excludeArchivedRepositories;
47+
4348
/**
4449
* {@inheritDoc}
4550
*/
@@ -63,4 +68,18 @@ void setTeamSlug(String teamSlug) {
6368
public String getTeamSlug() {
6469
return teamSlug;
6570
}
71+
72+
/**
73+
* @return True if archived repositories should be ignored, false if they should be included.
74+
*/
75+
public boolean isExcludeArchivedRepositories() {
76+
return excludeArchivedRepositories;
77+
}
78+
79+
/**
80+
* @param excludeArchivedRepositories Set true to exclude archived repositories
81+
*/
82+
public void setExcludeArchivedRepositories(boolean excludeArchivedRepositories) {
83+
this.excludeArchivedRepositories = excludeArchivedRepositories;
84+
}
6685
}
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 have been archived. If set, no jobs will be created for archived 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
@@ -62,6 +62,7 @@ SSHCheckoutTrait.missingCredentials=The currently configured credentials cannot
6262
SSHCheckoutTrait.useAgentKey=- use build agent''s key -
6363
TagDiscoveryTrait.authorityDisplayName=Trust origin tags
6464
TagDiscoveryTrait.displayName=Discover tags
65+
ExcludeArchivedRepositoriesTrait.displayName=Exclude archived repositories
6566

6667
GitHubSCMNavigator.general=General
6768
GitHubSCMNavigator.withinRepository=Within repository

0 commit comments

Comments
 (0)