Skip to content

Commit

Permalink
feat: add archived filter (JENKINS-64353) (#27)
Browse files Browse the repository at this point in the history
* feat: add archived filter

* Update src/main/resources/org/jenkinsci/plugin/gitea/GiteaSCMNavigator/help.html

* chore: ignore files

* chore: fix test

* Update src/main/java/org/jenkinsci/plugin/gitea/ExcludeArchivedRepositoriesTrait.java
  • Loading branch information
viceice authored Nov 1, 2021
1 parent 401a42b commit 8c9922c
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ work*/
.project
build/

# vscode
bin/
/.factorypath

/nb-configuration.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* The MIT License
*
* Copyright (c) 2021, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jenkinsci.plugin.gitea;

import hudson.Extension;
import jenkins.scm.api.trait.SCMNavigatorContext;
import jenkins.scm.api.trait.SCMNavigatorTrait;
import jenkins.scm.api.trait.SCMNavigatorTraitDescriptor;
import jenkins.scm.impl.trait.Selection;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;

import javax.annotation.Nonnull;

/**
* A {@link Selection} trait that will restrict the discovery of repositories that have been archived.
*/
public class ExcludeArchivedRepositoriesTrait extends SCMNavigatorTrait {

/**
* Constructor for stapler.
*/
@DataBoundConstructor
public ExcludeArchivedRepositoriesTrait() {
}

/**
* {@inheritDoc}
*/
@Override
protected void decorateContext(SCMNavigatorContext<?, ?> context) {
super.decorateContext(context);
GiteaSCMNavigatorContext ctx = (GiteaSCMNavigatorContext) context;
ctx.setExcludeArchivedRepositories(true);
}

/**
* Exclude archived repositories filter
*/
@Symbol("giteaExcludeArchivedRepositories")
@Extension
@Selection
public static class DescriptorImpl extends SCMNavigatorTraitDescriptor {

@Override
public Class<? extends SCMNavigatorContext> getContextClass() {
return GiteaSCMNavigatorContext.class;
}

@Nonnull
@Override
public String getDisplayName() {
return Messages.ExcludeArchivedRepositoriesTrait_displayName();
}
}
}
11 changes: 7 additions & 4 deletions src/main/java/org/jenkinsci/plugin/gitea/GiteaSCMNavigator.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,8 @@ protected String id() {

@Override
public void visitSources(@NonNull final SCMSourceObserver observer) throws IOException, InterruptedException {
try (GiteaSCMNavigatorRequest request = new GiteaSCMNavigatorContext()
.withTraits(traits)
.newRequest(this, observer);
GiteaSCMNavigatorContext context = new GiteaSCMNavigatorContext().withTraits(traits);
try (GiteaSCMNavigatorRequest request = context.newRequest(this, observer);
GiteaConnection c = gitea(observer.getContext()).open()) {
giteaOwner = c.fetchOwner(repoOwner);
List<GiteaRepository> repositories = c.fetchRepositories(giteaOwner);
Expand All @@ -162,7 +161,11 @@ public void visitSources(@NonNull final SCMSourceObserver observer) throws IOExc
observer.getListener().getLogger().format("%n Ignoring empty repository %s%n",
HyperlinkNote.encodeTo(r.getHtmlUrl(), r.getName()));
continue;
}
} else if (r.isArchived() && context.isExcludeArchivedRepositories()) {
observer.getListener().getLogger().format("%n Skipping repository %s because it is archived", r.getName());
continue;

}
observer.getListener().getLogger().format("%n Checking repository %s%n",
HyperlinkNote.encodeTo(r.getHtmlUrl(), r.getName()));
if (request.process(r.getName(), new SCMNavigatorRequest.SourceLambda() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,27 @@
import jenkins.scm.api.trait.SCMNavigatorContext;

public class GiteaSCMNavigatorContext extends SCMNavigatorContext<GiteaSCMNavigatorContext, GiteaSCMNavigatorRequest> {

/**
* If true, archived repositories will be ignored.
*/
private boolean excludeArchivedRepositories;


/**
* @return True if archived repositories should be ignored, false if they should be included.
*/
public boolean isExcludeArchivedRepositories() {
return excludeArchivedRepositories;
}

/**
* @param excludeArchivedRepositories Set true to exclude archived repositories
*/
public void setExcludeArchivedRepositories(boolean excludeArchivedRepositories) {
this.excludeArchivedRepositories = excludeArchivedRepositories;
}

@NonNull
@Override
public GiteaSCMNavigatorRequest newRequest(@NonNull SCMNavigator navigator, @NonNull SCMSourceObserver observer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class GiteaRepository extends GiteaObject<GiteaRepository> {
private boolean fork;
private boolean empty;
private boolean mirror;
private boolean archived;
private String htmlUrl;
private String sshUrl;
private String cloneUrl;
Expand All @@ -59,7 +60,7 @@ public GiteaRepository() {
}

public GiteaRepository(GiteaOwner owner, GiteaRepository parent, String name, String fullName,
String description, boolean _private, boolean fork, boolean empty, boolean mirror,
String description, boolean _private, boolean fork, boolean empty, boolean mirror, boolean archived,
String htmlUrl, String sshUrl, String cloneUrl, String website, long starsCount,
long forksCount,
long watchersCount, long openIssuesCount, String defaultBranch,
Expand All @@ -73,6 +74,7 @@ public GiteaRepository(GiteaOwner owner, GiteaRepository parent, String name, St
this.fork = fork;
this.empty = empty;
this.mirror = mirror;
this.archived = archived;
this.htmlUrl = htmlUrl;
this.sshUrl = sshUrl;
this.cloneUrl = cloneUrl;
Expand Down Expand Up @@ -166,6 +168,14 @@ public void setMirror(boolean mirror) {
this.mirror = mirror;
}

public boolean isArchived() {
return archived;
}

public void setArchived(boolean archived) {
this.archived = archived;
}

public String getHtmlUrl() {
return htmlUrl;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:c="/lib/credentials"
xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form"
xmlns:f2="/org/jenkinsci/plugins/gitea/form">
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
Exclude Gitea repositories that have been archived. If set, no jobs will be created for archived repositories.
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ SSHCheckoutTrait.displayName=Checkout over SSH
SSHCheckoutTrait.incompatibleCredentials=The currently configured credentials are incompatible with this behaviour
SSHCheckoutTrait.missingCredentials=The currently configured credentials cannot be found
SSHCheckoutTrait.useAgentKey=- use build agent''s key -
ExcludeArchivedRepositoriesTrait.displayName=Exclude archived repositories
WebhookRegistrationTrait.disableHook=Disable hook management
WebhookRegistrationTrait.displayName=Override hook management
WebhookRegistrationTrait.useItemHook=Use item credentials for hook management
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void reset() {
giteaRepository = new GiteaRepository(
new GiteaOwner("", "", "", ""),
null, "", "", "",
true, false, false, false,
true, false, false, false, false,
"", "", "", "",
0L, 0L, 0L, 0L, "",
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void reset() {
giteaRepository = new GiteaRepository(
new GiteaOwner("", "", "", ""),
null, "", "", "",
true, false, false, false,
true, false, false, false, false,
"", "", "", "",
0L, 0L, 0L, 0L, "",
null
Expand Down

0 comments on commit 8c9922c

Please sign in to comment.