Skip to content

Commit bcff70f

Browse files
author
Nick Griffiths
committed
[FIXED JENKINS-51519] Ignore archived repos.
When "Exclude archived repositories" is added to the list of behaviours in the settings, repositories that have been archived in GitHub will not be processed. This is achieved using `witness.record(repo.getName(), false)`.
1 parent 3059575 commit bcff70f

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 ExcludeArchivedTrait extends SCMNavigatorTrait {
17+
18+
/**
19+
* Constructor for stapler.
20+
*/
21+
@DataBoundConstructor
22+
public ExcludeArchivedTrait() {
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.setExcludeArchived(true);
33+
}
34+
35+
/**
36+
* Excluded archived repositories filter
37+
*/
38+
@Symbol("archivedRepositoriesFilter")
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.ExcludeArchivedTrait_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.isExcludeArchived()) {
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.isExcludeArchived()) {
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.isExcludeArchived()) {
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.isExcludeArchived()) {
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.isExcludeArchived()) {
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.isExcludeArchived()) {
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 excludeArchived;
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 isExcludeArchived() {
76+
return excludeArchived;
77+
}
78+
79+
/**
80+
* @param excludeArchived Set true to exclude archived repositories
81+
*/
82+
public void setExcludeArchived(boolean excludeArchived) {
83+
this.excludeArchived = excludeArchived;
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+
ExcludeArchivedTrait.displayName=Exclude archived repositories
6566

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

0 commit comments

Comments
 (0)