Skip to content

Fix migrating settings from plugin 0.20 to 0.21+ #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 20, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 41 additions & 17 deletions src/main/java/org/jenkinsci/plugins/GithubSecurityRealm.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,40 @@ of this software and associated documentation files (the "Software"), to deal
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import hudson.Extension;
import hudson.ProxyConfiguration;
import hudson.Util;
import hudson.model.Descriptor;
import hudson.model.listeners.ItemListener;
import hudson.model.User;
import hudson.ProxyConfiguration;
import hudson.security.GroupDetails;
import hudson.security.SecurityRealm;
import hudson.security.UserMayOrMayNotExistException;
import hudson.tasks.Mailer;
import hudson.Util;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.HashSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Set;
import jenkins.model.Jenkins;
import org.acegisecurity.Authentication;
import org.acegisecurity.AuthenticationException;
import org.acegisecurity.AuthenticationManager;
import org.acegisecurity.BadCredentialsException;
import org.acegisecurity.context.SecurityContextHolder;
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
import org.acegisecurity.userdetails.UserDetails;
import org.acegisecurity.userdetails.UserDetailsService;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
import org.apache.commons.httpclient.URIException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.jfree.util.Log;
Expand All @@ -72,17 +82,6 @@ of this software and associated documentation files (the "Software"), to deal
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataRetrievalFailureException;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this was the only file importing java.util.logging.Level.*

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;

import static java.util.logging.Level.*;

/**
*
* Implementation of the AbstractPasswordBasedSecurityRealm that uses github
Expand Down Expand Up @@ -479,7 +478,7 @@ private void fireAuthenticated(UserDetails details) {
} catch (IllegalAccessException e) {
throw (Error)new IllegalAccessError(e.getMessage()).initCause(e);
} catch (InvocationTargetException e) {
LOGGER.log(WARNING, "Failed to invoke fireAuthenticated", e);
LOGGER.log(Level.WARNING, "Failed to invoke fireAuthenticated", e);
}
}

Expand Down Expand Up @@ -663,6 +662,31 @@ public GroupDetails loadGroupByGroupname(String groupName)
}
}

/*
Migrate settings from 0.20 to 0.21+
*/
@Extension
public static final class Migrator extends ItemListener {
@SuppressWarnings("deprecation")
@Override
public void onLoaded() {
try {
Jenkins instance = Jenkins.getInstance();
if(instance.getSecurityRealm() instanceof GithubSecurityRealm) {
GithubSecurityRealm myRealm = (GithubSecurityRealm) instance.getSecurityRealm();
if(myRealm.getOauthScopes() == null) {
GithubSecurityRealm newRealm = new GithubSecurityRealm(myRealm.getGithubWebUri(), myRealm.getGithubApiUri(), myRealm.getClientID(), myRealm.getClientSecret());
instance.setSecurityRealm(newRealm);
instance.save();
}
}
}
catch(IOException e) {
LOGGER.log(Level.WARNING, "could not migrate GithubSecurityRealm", e);
}
}
}

/**
* Logger for debugging purposes.
*/
Expand Down