From 8c9922c5f79bc3d54e39045f951940810790d7d8 Mon Sep 17 00:00:00 2001 From: Michael Kriese Date: Mon, 1 Nov 2021 08:30:24 +0100 Subject: [PATCH] feat: add archived filter (JENKINS-64353) (#27) * 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 --- .gitignore | 4 + .../ExcludeArchivedRepositoriesTrait.java | 77 +++++++++++++++++++ .../plugin/gitea/GiteaSCMNavigator.java | 11 ++- .../gitea/GiteaSCMNavigatorContext.java | 21 +++++ .../gitea/client/api/GiteaRepository.java | 12 ++- .../gitea/ExcludeArchivedTrait/config.jelly | 5 ++ .../plugin/gitea/GiteaSCMNavigator/help.html | 3 + .../plugin/gitea/Messages.properties | 1 + .../impl/DefaultGiteaConnectionTest.java | 2 +- ...ultGiteaConnection_PagedRequests_Test.java | 2 +- 10 files changed, 131 insertions(+), 7 deletions(-) create mode 100644 src/main/java/org/jenkinsci/plugin/gitea/ExcludeArchivedRepositoriesTrait.java create mode 100644 src/main/resources/org/jenkinsci/plugin/gitea/ExcludeArchivedTrait/config.jelly create mode 100644 src/main/resources/org/jenkinsci/plugin/gitea/GiteaSCMNavigator/help.html diff --git a/.gitignore b/.gitignore index 433afac..ee38530 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,8 @@ work*/ .project build/ +# vscode +bin/ +/.factorypath + /nb-configuration.xml diff --git a/src/main/java/org/jenkinsci/plugin/gitea/ExcludeArchivedRepositoriesTrait.java b/src/main/java/org/jenkinsci/plugin/gitea/ExcludeArchivedRepositoriesTrait.java new file mode 100644 index 0000000..2bf999a --- /dev/null +++ b/src/main/java/org/jenkinsci/plugin/gitea/ExcludeArchivedRepositoriesTrait.java @@ -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 getContextClass() { + return GiteaSCMNavigatorContext.class; + } + + @Nonnull + @Override + public String getDisplayName() { + return Messages.ExcludeArchivedRepositoriesTrait_displayName(); + } + } +} \ No newline at end of file diff --git a/src/main/java/org/jenkinsci/plugin/gitea/GiteaSCMNavigator.java b/src/main/java/org/jenkinsci/plugin/gitea/GiteaSCMNavigator.java index e824909..a90ec92 100644 --- a/src/main/java/org/jenkinsci/plugin/gitea/GiteaSCMNavigator.java +++ b/src/main/java/org/jenkinsci/plugin/gitea/GiteaSCMNavigator.java @@ -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 repositories = c.fetchRepositories(giteaOwner); @@ -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() { diff --git a/src/main/java/org/jenkinsci/plugin/gitea/GiteaSCMNavigatorContext.java b/src/main/java/org/jenkinsci/plugin/gitea/GiteaSCMNavigatorContext.java index 616e8d5..aab3c3d 100644 --- a/src/main/java/org/jenkinsci/plugin/gitea/GiteaSCMNavigatorContext.java +++ b/src/main/java/org/jenkinsci/plugin/gitea/GiteaSCMNavigatorContext.java @@ -29,6 +29,27 @@ import jenkins.scm.api.trait.SCMNavigatorContext; public class GiteaSCMNavigatorContext extends SCMNavigatorContext { + + /** + * 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) { diff --git a/src/main/java/org/jenkinsci/plugin/gitea/client/api/GiteaRepository.java b/src/main/java/org/jenkinsci/plugin/gitea/client/api/GiteaRepository.java index 8c8972c..97ac85c 100644 --- a/src/main/java/org/jenkinsci/plugin/gitea/client/api/GiteaRepository.java +++ b/src/main/java/org/jenkinsci/plugin/gitea/client/api/GiteaRepository.java @@ -42,6 +42,7 @@ public class GiteaRepository extends GiteaObject { private boolean fork; private boolean empty; private boolean mirror; + private boolean archived; private String htmlUrl; private String sshUrl; private String cloneUrl; @@ -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, @@ -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; @@ -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; } diff --git a/src/main/resources/org/jenkinsci/plugin/gitea/ExcludeArchivedTrait/config.jelly b/src/main/resources/org/jenkinsci/plugin/gitea/ExcludeArchivedTrait/config.jelly new file mode 100644 index 0000000..6ce4f1b --- /dev/null +++ b/src/main/resources/org/jenkinsci/plugin/gitea/ExcludeArchivedTrait/config.jelly @@ -0,0 +1,5 @@ + + + \ No newline at end of file diff --git a/src/main/resources/org/jenkinsci/plugin/gitea/GiteaSCMNavigator/help.html b/src/main/resources/org/jenkinsci/plugin/gitea/GiteaSCMNavigator/help.html new file mode 100644 index 0000000..a7d5764 --- /dev/null +++ b/src/main/resources/org/jenkinsci/plugin/gitea/GiteaSCMNavigator/help.html @@ -0,0 +1,3 @@ +
+ Exclude Gitea repositories that have been archived. If set, no jobs will be created for archived repositories. +
diff --git a/src/main/resources/org/jenkinsci/plugin/gitea/Messages.properties b/src/main/resources/org/jenkinsci/plugin/gitea/Messages.properties index a486a5c..19ab5ad 100644 --- a/src/main/resources/org/jenkinsci/plugin/gitea/Messages.properties +++ b/src/main/resources/org/jenkinsci/plugin/gitea/Messages.properties @@ -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 diff --git a/src/test/java/org/jenkinsci/plugin/gitea/client/impl/DefaultGiteaConnectionTest.java b/src/test/java/org/jenkinsci/plugin/gitea/client/impl/DefaultGiteaConnectionTest.java index a4dac5c..12597a9 100644 --- a/src/test/java/org/jenkinsci/plugin/gitea/client/impl/DefaultGiteaConnectionTest.java +++ b/src/test/java/org/jenkinsci/plugin/gitea/client/impl/DefaultGiteaConnectionTest.java @@ -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 diff --git a/src/test/java/org/jenkinsci/plugin/gitea/client/impl/DefaultGiteaConnection_PagedRequests_Test.java b/src/test/java/org/jenkinsci/plugin/gitea/client/impl/DefaultGiteaConnection_PagedRequests_Test.java index 1962ba1..4d124d4 100644 --- a/src/test/java/org/jenkinsci/plugin/gitea/client/impl/DefaultGiteaConnection_PagedRequests_Test.java +++ b/src/test/java/org/jenkinsci/plugin/gitea/client/impl/DefaultGiteaConnection_PagedRequests_Test.java @@ -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