From a853e38a6ff82b94ea7f9e43288ad9a6123a2f3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robin=20M=C3=BCller?= Date: Thu, 10 Mar 2016 21:03:43 +0100 Subject: [PATCH] Move migration of jobs to GitLabPushTrigger to remove deprecated getters --- .../gitlabjenkins/GitLabPushTrigger.java | 97 +++++++++---------- .../connection/GitLabConnectionConfig.java | 22 ----- .../GitLabCommitStatusPublisher.java | 22 ----- 3 files changed, 46 insertions(+), 95 deletions(-) diff --git a/src/main/java/com/dabsquared/gitlabjenkins/GitLabPushTrigger.java b/src/main/java/com/dabsquared/gitlabjenkins/GitLabPushTrigger.java index 6d7c755c9..7dbfc1489 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/GitLabPushTrigger.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/GitLabPushTrigger.java @@ -1,7 +1,11 @@ package com.dabsquared.gitlabjenkins; +import com.dabsquared.gitlabjenkins.connection.GitLabConnection; +import com.dabsquared.gitlabjenkins.connection.GitLabConnectionConfig; +import com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty; import com.dabsquared.gitlabjenkins.model.MergeRequestHook; import com.dabsquared.gitlabjenkins.model.PushHook; +import com.dabsquared.gitlabjenkins.publisher.GitLabCommitStatusPublisher; import com.dabsquared.gitlabjenkins.trigger.branch.ProjectBranchesProvider; import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilter; import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilterFactory; @@ -39,6 +43,7 @@ import org.kohsuke.stapler.StaplerRequest; import java.io.IOException; +import java.util.ArrayList; import static com.dabsquared.gitlabjenkins.trigger.filter.BranchFilterConfig.BranchFilterConfigBuilder.branchFilterConfig; @@ -89,31 +94,6 @@ public GitLabPushTrigger(boolean triggerOnPush, boolean triggerOnMergeRequest, S this.acceptMergeRequestOnSuccess = acceptMergeRequestOnSuccess; } - @Initializer(after = InitMilestone.JOB_LOADED) - public static void migrate() throws IOException { - for (AbstractProject project : Jenkins.getInstance().getAllItems(AbstractProject.class)) { - GitLabPushTrigger trigger = project.getTrigger(GitLabPushTrigger.class); - if (trigger != null) { - if (trigger.branchFilter == null) { - String name = StringUtils.isNotEmpty(trigger.branchFilterName) ? trigger.branchFilterName : "All"; - trigger.branchFilter = BranchFilterFactory.newBranchFilter(branchFilterConfig() - .withIncludeBranchesSpec(trigger.includeBranchesSpec) - .withExcludeBranchesSpec(trigger.excludeBranchesSpec) - .withTargetBranchRegex(trigger.targetBranchRegex) - .build(BranchFilterType.valueOf(name))); - } - if (trigger.pushHookTriggerHandler == null) { - trigger.pushHookTriggerHandler = PushHookTriggerHandlerFactory.newPushHookTriggerHandler(trigger.triggerOnPush); - } - if (trigger.mergeRequestHookTriggerHandler == null) { - trigger.mergeRequestHookTriggerHandler = - MergeRequestHookTriggerHandlerFactory.newMergeRequestHookTriggerHandler(trigger.triggerOnMergeRequest); - } - project.save(); - } - } - } - public boolean getTriggerOnPush() { return pushHookTriggerHandler.isEnabled(); } @@ -142,22 +122,6 @@ public boolean getAcceptMergeRequestOnSuccess() { return acceptMergeRequestOnSuccess; } - /** - * @deprecated see {@link com.dabsquared.gitlabjenkins.publisher.GitLabCommitStatusPublisher} - */ - @Deprecated - public boolean getAddCiMessage() { - return addCiMessage; - } - - /** - * @deprecated see {@link com.dabsquared.gitlabjenkins.publisher.GitLabCommitStatusPublisher} - */ - @Deprecated - public void setAddCiMessage(boolean addCiMessage) { - this.addCiMessage = addCiMessage; - } - @Override public boolean getCiSkip() { return ciSkip; @@ -181,6 +145,7 @@ public void onPost(final MergeRequestHook hook) { @Extension public static class DescriptorImpl extends TriggerDescriptor { + private boolean jobsMigrated = false; private String gitlabApiToken; private String gitlabHostUrl = ""; private boolean ignoreCertificateErrors = false; @@ -263,17 +228,47 @@ public FormValidation doCheckIncludeBranchesSpec(@AncestorInPath final Job public FormValidation doCheckExcludeBranchesSpec(@AncestorInPath final Job project, @QueryParameter final String value) { return ProjectBranchesProvider.instance().doCheckBranchesSpec(project, value); } + } - public String getGitlabApiToken() { - return gitlabApiToken; - } - - public String getGitlabHostUrl() { - return gitlabHostUrl; - } - - public boolean getIgnoreCertificateErrors() { - return ignoreCertificateErrors; + @Initializer(after = InitMilestone.JOB_LOADED) + public static void migrateJobs() throws IOException { + GitLabPushTrigger.DescriptorImpl oldConfig = Trigger.all().get(GitLabPushTrigger.DescriptorImpl.class); + if (!oldConfig.jobsMigrated) { + GitLabConnectionConfig gitLabConfig = (GitLabConnectionConfig) Jenkins.getInstance().getDescriptor(GitLabConnectionConfig.class); + gitLabConfig.getConnections().add(new GitLabConnection(oldConfig.gitlabHostUrl, + oldConfig.gitlabHostUrl, + oldConfig.gitlabApiToken, + oldConfig.ignoreCertificateErrors)); + + String defaultConnectionName = gitLabConfig.getConnections().get(0).getName(); + for (AbstractProject project : Jenkins.getInstance().getAllItems(AbstractProject.class)) { + GitLabPushTrigger trigger = project.getTrigger(GitLabPushTrigger.class); + if (trigger != null) { + if (trigger.addCiMessage) { + project.getPublishersList().add(new GitLabCommitStatusPublisher()); + } + if (trigger.branchFilter == null) { + String name = StringUtils.isNotEmpty(trigger.branchFilterName) ? trigger.branchFilterName : "All"; + trigger.branchFilter = BranchFilterFactory.newBranchFilter(branchFilterConfig() + .withIncludeBranchesSpec(trigger.includeBranchesSpec) + .withExcludeBranchesSpec(trigger.excludeBranchesSpec) + .withTargetBranchRegex(trigger.targetBranchRegex) + .build(BranchFilterType.valueOf(name))); + } + if (trigger.pushHookTriggerHandler == null) { + trigger.pushHookTriggerHandler = PushHookTriggerHandlerFactory.newPushHookTriggerHandler(trigger.triggerOnPush); + } + if (trigger.mergeRequestHookTriggerHandler == null) { + trigger.mergeRequestHookTriggerHandler = + MergeRequestHookTriggerHandlerFactory.newMergeRequestHookTriggerHandler(trigger.triggerOnMergeRequest); + } + project.addProperty(new GitLabConnectionProperty(defaultConnectionName)); + project.save(); + } + } + gitLabConfig.save(); + oldConfig.jobsMigrated = true; + oldConfig.save(); } } } diff --git a/src/main/java/com/dabsquared/gitlabjenkins/connection/GitLabConnectionConfig.java b/src/main/java/com/dabsquared/gitlabjenkins/connection/GitLabConnectionConfig.java index 239441d23..660a01b4f 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/connection/GitLabConnectionConfig.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/connection/GitLabConnectionConfig.java @@ -29,7 +29,6 @@ public class GitLabConnectionConfig extends GlobalConfiguration { private List connections = new ArrayList(); private transient Map connectionMap = new HashMap(); private transient Map clients = new HashMap(); - private boolean migrationFinished = false; public GitLabConnectionConfig() { load(); @@ -110,25 +109,4 @@ public static boolean checkConnection(String token, String url, boolean ignoreCe testApi.getProjects(); return true; } - - @Initializer(after = InitMilestone.JOB_LOADED) - public static void migrate() throws IOException { - GitLabConnectionConfig gitLabConfig = (GitLabConnectionConfig) Jenkins.getInstance().getDescriptor(GitLabConnectionConfig.class); - if (!gitLabConfig.migrationFinished) { - GitLabPushTrigger.DescriptorImpl oldConfig = Trigger.all().get(GitLabPushTrigger.DescriptorImpl.class); - gitLabConfig.connections = new ArrayList(); - gitLabConfig.connections.add(new GitLabConnection(oldConfig.getGitlabHostUrl(), oldConfig.getGitlabHostUrl(), oldConfig.getGitlabApiToken(), - oldConfig.getIgnoreCertificateErrors())); - - String defaultConnectionName = gitLabConfig.connections.get(0).getName(); - for (AbstractProject project : Jenkins.getInstance().getAllItems(AbstractProject.class)) { - if (project.getTrigger(GitLabPushTrigger.class) != null) { - project.addProperty(new GitLabConnectionProperty(defaultConnectionName)); - project.save(); - } - } - gitLabConfig.migrationFinished = true; - gitLabConfig.save(); - } - } } diff --git a/src/main/java/com/dabsquared/gitlabjenkins/publisher/GitLabCommitStatusPublisher.java b/src/main/java/com/dabsquared/gitlabjenkins/publisher/GitLabCommitStatusPublisher.java index d844b955f..f2e98b584 100644 --- a/src/main/java/com/dabsquared/gitlabjenkins/publisher/GitLabCommitStatusPublisher.java +++ b/src/main/java/com/dabsquared/gitlabjenkins/publisher/GitLabCommitStatusPublisher.java @@ -1,11 +1,8 @@ package com.dabsquared.gitlabjenkins.publisher; -import com.dabsquared.gitlabjenkins.GitLabPushTrigger; import com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty; import hudson.Extension; import hudson.Launcher; -import hudson.init.InitMilestone; -import hudson.init.Initializer; import hudson.model.AbstractBuild; import hudson.model.AbstractProject; import hudson.model.BuildListener; @@ -127,28 +124,9 @@ private String retrieveProjectId(String remoteUrl) throws URISyntaxException { return projectId; } - @Initializer(after = InitMilestone.JOB_LOADED) - public static void migrateJobs() throws IOException { - DescriptorImpl descriptor = (DescriptorImpl) Jenkins.getInstance().getDescriptor(GitLabCommitStatusPublisher.class); - if (!descriptor.migrationFinished) { - for (AbstractProject project : Jenkins.getInstance().getAllItems(AbstractProject.class)) { - GitLabPushTrigger trigger = project.getTrigger(GitLabPushTrigger.class); - if (trigger != null && trigger.getAddCiMessage()) { - project.getPublishersList().add(new GitLabCommitStatusPublisher()); - trigger.setAddCiMessage(false); - project.save(); - } - } - descriptor.migrationFinished = true; - descriptor.save(); - } - } - @Extension public static class DescriptorImpl extends BuildStepDescriptor { - private boolean migrationFinished = false; - @Override public boolean isApplicable(Class aClass) { return true;