Skip to content

Commit

Permalink
Move migration of jobs to GitLabPushTrigger to remove deprecated getters
Browse files Browse the repository at this point in the history
  • Loading branch information
coder-hugo committed Mar 12, 2016
1 parent 0f36541 commit a853e38
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 95 deletions.
97 changes: 46 additions & 51 deletions src/main/java/com/dabsquared/gitlabjenkins/GitLabPushTrigger.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class GitLabConnectionConfig extends GlobalConfiguration {
private List<GitLabConnection> connections = new ArrayList<GitLabConnection>();
private transient Map<String, GitLabConnection> connectionMap = new HashMap<String, GitLabConnection>();
private transient Map<String, GitlabAPI> clients = new HashMap<String, GitlabAPI>();
private boolean migrationFinished = false;

public GitLabConnectionConfig() {
load();
Expand Down Expand Up @@ -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<GitLabConnection>();
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();
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Publisher> {

private boolean migrationFinished = false;

@Override
public boolean isApplicable(Class<? extends AbstractProject> aClass) {
return true;
Expand Down

0 comments on commit a853e38

Please sign in to comment.