Skip to content

Commit

Permalink
Credentials #10 #11
Browse files Browse the repository at this point in the history
 * Also adding better logging.
  • Loading branch information
tomasbjerre committed Aug 2, 2016
1 parent ade15ef commit 0b3f20a
Show file tree
Hide file tree
Showing 13 changed files with 309 additions and 93 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Changelog of Violation Comments to Bitbucket Server Plugin.
**doc**


[84cd98689620206](https://github.com/jenkinsci/violation-comments-to-stash-plugin/commit/84cd98689620206) Tomas Bjerre *2016-07-30 11:53:48*
[ade15ef47ef3ea3](https://github.com/jenkinsci/violation-comments-to-stash-plugin/commit/ade15ef47ef3ea3) Tomas Bjerre *2016-07-30 11:58:04*


## 1.16
Expand Down
22 changes: 20 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand Down Expand Up @@ -168,10 +169,27 @@ Changelog of Violation Comments to Bitbucket Server Plugin.
<artifactId>maven-plugin</artifactId>
<version>2.9</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>credentials</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>se.bjurr.violations</groupId>
<artifactId>violation-comments-to-bitbucket-server-lib</artifactId>
<version>1.1</version>
<version>1.3</version>
</dependency>
<dependency>
<groupId>uk.co.jemos.podam</groupId>
<artifactId>podam</artifactId>
<version>6.0.2.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<!-- test // -->
Expand Down
30 changes: 0 additions & 30 deletions src/main/java/org/jenkinsci/plugins/jvctb/JvctbLogger.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@
import static org.jenkinsci.plugins.jvctb.config.ViolationsToBitbucketServerConfigHelper.FIELD_PULLREQUESTID;
import static org.jenkinsci.plugins.jvctb.config.ViolationsToBitbucketServerConfigHelper.FIELD_REPOSLUG;
import static org.jenkinsci.plugins.jvctb.config.ViolationsToBitbucketServerConfigHelper.FIELD_USERNAME;
import static org.jenkinsci.plugins.jvctb.config.ViolationsToBitbucketServerConfigHelper.FIELD_USERNAMEPASSWORDCREDENTIALSID;
import static org.jenkinsci.plugins.jvctb.config.ViolationsToBitbucketServerConfigHelper.FIELD_USEUSERNAMEPASSWORD;
import static org.jenkinsci.plugins.jvctb.config.ViolationsToBitbucketServerConfigHelper.FIELD_USEUSERNAMEPASSWORDCREDENTIALS;
import static org.jenkinsci.plugins.jvctb.config.ViolationsToBitbucketServerConfigHelper.createNewConfig;
import hudson.model.AbstractProject;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Publisher;
import hudson.util.ListBoxModel;

import java.util.List;

import net.sf.json.JSONObject;

import org.jenkinsci.plugins.jvctb.config.CredentialsHelper;
import org.jenkinsci.plugins.jvctb.config.ViolationsToBitbucketServerConfig;
import org.kohsuke.stapler.StaplerRequest;

Expand All @@ -32,6 +37,10 @@ public ViolationsToBitbucketServerDescriptor() {
}
}

public ListBoxModel doFillUsernamePasswordCredentialsIdItems() {
return CredentialsHelper.doFillUsernamePasswordCredentialsIdItems();
}

@Override
public String getDisplayName() {
return "Report Violations to Bitbucket Server";
Expand Down Expand Up @@ -62,10 +71,17 @@ public Publisher newInstance(StaplerRequest req, JSONObject formData) throws hud
config.setRepoSlug(formData.getString(FIELD_REPOSLUG));
config.setProjectKey(formData.getString(FIELD_PROJECTKEY));
config.setPullRequestId(formData.getString(FIELD_PULLREQUESTID));

config.setUseUsernamePassword(formData.getBoolean(FIELD_USEUSERNAMEPASSWORD));
config.setUsername(formData.getString(FIELD_USERNAME));
config.setPassword(formData.getString(FIELD_PASSWORD));

config.setUseUsernamePasswordCredentials(formData.getBoolean(FIELD_USEUSERNAMEPASSWORDCREDENTIALS));
config.setUsernamePasswordCredentialsId(formData.getString(FIELD_USERNAMEPASSWORDCREDENTIALSID));

config.setCreateCommentWithAllSingleFileComments(formData.getString(FIELD_CREATECOMMENTWITHALLSINGLEFILECOMMENTS)
.equalsIgnoreCase("true"));

config.setCreateSingleFileComments(formData.getString(FIELD_CREATESINGLEFILECOMMENTS).equalsIgnoreCase("true"));
int i = 0;
for (String pattern : (List<String>) formData.get(FIELD_PATTERN)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package org.jenkinsci.plugins.jvctb;

import hudson.Extension;
import hudson.util.ListBoxModel;

import java.io.Serializable;

import jenkins.model.GlobalConfiguration;
import net.sf.json.JSONObject;

import org.jenkinsci.plugins.jvctb.config.CredentialsHelper;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.StaplerRequest;

Expand Down Expand Up @@ -34,6 +36,7 @@ public static Optional<ViolationsToBitbucketServerGlobalConfiguration> get() {
public String projectKey;
public String repoSlug;
public String username;
private String usernamePasswordCredentialsId;

public ViolationsToBitbucketServerGlobalConfiguration() {
load();
Expand All @@ -46,6 +49,10 @@ public boolean configure(StaplerRequest req, JSONObject json) throws FormExcepti
return true;
}

public ListBoxModel doFillUsernamePasswordCredentialsIdItems() {
return CredentialsHelper.doFillUsernamePasswordCredentialsIdItems();
}

public String getBitbucketServerUrl() {
return this.bitbucketServerUrl;
}
Expand All @@ -66,6 +73,10 @@ public String getUsername() {
return this.username;
}

public String getUsernamePasswordCredentialsId() {
return this.usernamePasswordCredentialsId;
}

@DataBoundSetter
public void setBitbucketServerUrl(String bitbucketServerUrl) {
this.bitbucketServerUrl = bitbucketServerUrl;
Expand All @@ -90,4 +101,16 @@ public void setRepoSlug(String repoSlug) {
public void setUsername(String username) {
this.username = username;
}

public void setUsernamePasswordCredentialsId(String usernamePasswordCredentialsId) {
this.usernamePasswordCredentialsId = usernamePasswordCredentialsId;
}

@Override
public String toString() {
return "ViolationsToBitbucketServerGlobalConfiguration [bitbucketServerUrl=" + this.bitbucketServerUrl
+ ", password=" + this.password + ", projectKey=" + this.projectKey + ", repoSlug=" + this.repoSlug + ", username="
+ this.username + ", usernamePasswordCredentialsId=" + this.usernamePasswordCredentialsId + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ public ViolationsToBitbucketServerRecorder() {
@DataBoundConstructor
public ViolationsToBitbucketServerRecorder(boolean createSingleFileComments,
boolean createCommentWithAllSingleFileComments, String repositoryName, String repositoryOwner, String password,
String username, String pullRequestId, String bitbucketServerUrl, List<ViolationConfig> violationConfigs) {
String username, String pullRequestId, String bitbucketServerUrl, List<ViolationConfig> violationConfigs,
String usernamePasswordCredentialsId, boolean useUsernamePasswordCredentials, boolean useUsernamePassword) {

this.config = new ViolationsToBitbucketServerConfig(createSingleFileComments, createCommentWithAllSingleFileComments,
repositoryName, repositoryOwner, password, username, pullRequestId, bitbucketServerUrl, violationConfigs);
repositoryName, repositoryOwner, password, username, pullRequestId, bitbucketServerUrl, violationConfigs,
usernamePasswordCredentialsId, useUsernamePasswordCredentials, useUsernamePassword);
}

public ViolationsToBitbucketServerConfig getConfig() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.jenkinsci.plugins.jvctb.config;

import static com.cloudbees.plugins.credentials.CredentialsMatchers.allOf;
import static com.cloudbees.plugins.credentials.CredentialsMatchers.firstOrNull;
import static com.cloudbees.plugins.credentials.CredentialsMatchers.withId;
import static com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials;
import static com.google.common.base.Optional.absent;
import static com.google.common.base.Optional.fromNullable;
import static com.google.common.base.Strings.isNullOrEmpty;
import static hudson.security.ACL.SYSTEM;
import hudson.model.ItemGroup;
import hudson.util.ListBoxModel;

import java.util.List;

import org.acegisecurity.Authentication;

import com.cloudbees.plugins.credentials.common.AbstractIdCredentialsListBoxModel;
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import com.cloudbees.plugins.credentials.common.StandardUsernameListBoxModel;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import com.google.common.base.Optional;

public class CredentialsHelper {
public static ListBoxModel doFillUsernamePasswordCredentialsIdItems() {
List<StandardUsernamePasswordCredentials> credentials = getAllCredentials();
AbstractIdCredentialsListBoxModel<StandardUsernameListBoxModel, StandardUsernameCredentials> listBoxModel = new StandardUsernameListBoxModel()
.includeEmptyValue();
for (StandardUsernamePasswordCredentials credential : credentials) {
listBoxModel.with(credential);
}
return listBoxModel;
}

public static Optional<StandardUsernamePasswordCredentials> findCredentials(String usernamePasswordCredentialsId) {
if (isNullOrEmpty(usernamePasswordCredentialsId)) {
return absent();
}

return fromNullable(firstOrNull(getAllCredentials(), allOf(withId(usernamePasswordCredentialsId))));
}

public static List<StandardUsernamePasswordCredentials> getAllCredentials() {
Class<StandardUsernamePasswordCredentials> type = StandardUsernamePasswordCredentials.class;
ItemGroup<?> itemGroup = null;
Authentication authentication = SYSTEM;
DomainRequirement domainRequirement = null;

return lookupCredentials(type, itemGroup, authentication, domainRequirement);
}

}
Loading

0 comments on commit 0b3f20a

Please sign in to comment.