Skip to content

Commit

Permalink
Personal Access Tokens #47
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Feb 10, 2018
1 parent 53f9e8d commit 041d262
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 91 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ git --no-pager log --max-count=10 --graph --abbrev-commit
repoSlug("\$PULL_REQUEST_TO_REPO_SLUG")
pullRequestId("\$PULL_REQUEST_ID")
usernamePasswordCredentialsId('bitbucketservercredentials')
usernamePasswordCredentialsId('bitbucketservercredentials'), // You only need one of these
personalAccessTokenId('bitbucketservertextcredentials'),
minSeverity('INFO')
createSingleFileComments(true)
Expand Down Expand Up @@ -242,7 +243,10 @@ node {
projectKey: 'PROJ', // Use environment variable here
pullRequestId: '1', // Use environment variable here
repoSlug: 'violations-test', // Use environment variable here
usernamePasswordCredentialsId: 'bitbucketservercredentials', //Create a Username/Passwords credential with this ID
usernamePasswordCredentialsId: 'theid', // You only need one of these
personalAccessTokenId: 'theid',
violationConfigs: [
// Many more formats available, check https://github.com/tomasbjerre/violations-lib
[parser: 'FINDBUGS', pattern: '.*/findbugs/.*\\.xml\$', reporter: 'Findbugs'],
Expand Down
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,18 @@ Changelog of Violation Comments to Bitbucket Server Plugin.
<dependency>
<groupId>se.bjurr.violations</groupId>
<artifactId>violation-comments-to-bitbucket-server-lib</artifactId>
<version>1.45</version>
<version>1.47</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plain-credentials</artifactId>
<version>1.2</version>
</dependency>

<!-- test // -->
<dependency>
Expand Down
7 changes: 5 additions & 2 deletions sandbox/violations-test.jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ node {
createCommentWithAllSingleFileComments: false,
createSingleFileComments: true,
projectKey: 'PROJ',
repoSlug: 'violations-test',
usernamePasswordCredentialsId: 'bitbucketservercredentials'
repoSlug: 'violations-test',

usernamePasswordCredentialsId: 'bitbucketservercredentials', // You only need one of these
personalAccessTokenId: 'personalAccessTokenId',

pullRequestId: '1',
minSeverity: 'INFO',
keepOldComments: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package org.jenkinsci.plugins.jvctb;

import static org.jenkinsci.plugins.jvctb.config.CredentialsHelper.migrateCredentials;

import java.io.Serializable;

import com.google.common.base.Optional;
import hudson.Extension;
import hudson.util.ListBoxModel;
import jenkins.model.GlobalConfiguration;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.jvctb.config.CredentialsHelper;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.StaplerRequest;
import se.bjurr.violations.lib.model.SEVERITY;
import se.bjurr.violations.lib.reports.Parser;

import static org.jenkinsci.plugins.jvctb.config.CredentialsHelper.migrateCredentials;
import com.google.common.base.Optional;

import hudson.Extension;
import hudson.util.ListBoxModel;
import jenkins.model.GlobalConfiguration;
import net.sf.json.JSONObject;
import se.bjurr.violations.lib.model.SEVERITY;

/** Created by magnayn on 07/04/2016. */
@Extension
Expand All @@ -42,22 +43,23 @@ public static Optional<ViolationsToBitbucketServerGlobalConfiguration> get() {
@Deprecated private transient String username;
private String usernamePasswordCredentialsId;
private SEVERITY minSeverity = SEVERITY.INFO;
private String personalAccessTokenId;

public ViolationsToBitbucketServerGlobalConfiguration() {
load();
}

@Override
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
public boolean configure(final StaplerRequest req, final JSONObject json) throws FormException {
req.bindJSON(this, json);
save();
return true;
}

@Restricted(NoExternalUse.class)
public ListBoxModel doFillMinSeverityItems() {
ListBoxModel items = new ListBoxModel();
for (SEVERITY severity : SEVERITY.values()) {
final ListBoxModel items = new ListBoxModel();
for (final SEVERITY severity : SEVERITY.values()) {
items.add(severity.name());
}
return items;
Expand All @@ -67,6 +69,10 @@ public ListBoxModel doFillUsernamePasswordCredentialsIdItems() {
return CredentialsHelper.doFillUsernamePasswordCredentialsIdItems();
}

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

public String getBitbucketServerUrl() {
return this.bitbucketServerUrl;
}
Expand All @@ -84,7 +90,7 @@ public String getUsernamePasswordCredentialsId() {
}

@DataBoundSetter
public void setMinSeverity(SEVERITY minSeverity) {
public void setMinSeverity(final SEVERITY minSeverity) {
this.minSeverity = minSeverity;
}

Expand All @@ -93,24 +99,34 @@ public SEVERITY getMinSeverity() {
}

@DataBoundSetter
public void setBitbucketServerUrl(String bitbucketServerUrl) {
public void setBitbucketServerUrl(final String bitbucketServerUrl) {
this.bitbucketServerUrl = bitbucketServerUrl;
}

@DataBoundSetter
public void setProjectKey(String projectKey) {
public void setProjectKey(final String projectKey) {
this.projectKey = projectKey;
}

@DataBoundSetter
public void setRepoSlug(String repoSlug) {
public void setRepoSlug(final String repoSlug) {
this.repoSlug = repoSlug;
}

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

@DataBoundSetter
public void setPersonalAccessTokenId(final String personalAccessTokenId) {
this.personalAccessTokenId = personalAccessTokenId;
}

public String getPersonalAccessTokenId() {
return personalAccessTokenId;
}

private Object readResolve() {
if (StringUtils.isBlank(usernamePasswordCredentialsId)
&& username != null
Expand All @@ -126,6 +142,8 @@ public int hashCode() {
int result = 1;
result = prime * result + (bitbucketServerUrl == null ? 0 : bitbucketServerUrl.hashCode());
result = prime * result + (minSeverity == null ? 0 : minSeverity.hashCode());
result =
prime * result + (personalAccessTokenId == null ? 0 : personalAccessTokenId.hashCode());
result = prime * result + (projectKey == null ? 0 : projectKey.hashCode());
result = prime * result + (repoSlug == null ? 0 : repoSlug.hashCode());
result =
Expand All @@ -137,7 +155,7 @@ public int hashCode() {
}

@Override
public boolean equals(Object obj) {
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
Expand All @@ -147,7 +165,7 @@ public boolean equals(Object obj) {
if (getClass() != obj.getClass()) {
return false;
}
ViolationsToBitbucketServerGlobalConfiguration other =
final ViolationsToBitbucketServerGlobalConfiguration other =
(ViolationsToBitbucketServerGlobalConfiguration) obj;
if (bitbucketServerUrl == null) {
if (other.bitbucketServerUrl != null) {
Expand All @@ -159,6 +177,13 @@ public boolean equals(Object obj) {
if (minSeverity != other.minSeverity) {
return false;
}
if (personalAccessTokenId == null) {
if (other.personalAccessTokenId != null) {
return false;
}
} else if (!personalAccessTokenId.equals(other.personalAccessTokenId)) {
return false;
}
if (projectKey == null) {
if (other.projectKey != null) {
return false;
Expand Down Expand Up @@ -195,6 +220,8 @@ public String toString() {
+ usernamePasswordCredentialsId
+ ", minSeverity="
+ minSeverity
+ ", personalAccessTokenId="
+ personalAccessTokenId
+ "]";
}
}
Original file line number Diff line number Diff line change
@@ -1,48 +1,63 @@
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 java.util.List;

import org.acegisecurity.Authentication;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.plaincredentials.StringCredentials;

import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
import com.cloudbees.plugins.credentials.common.AbstractIdCredentialsListBoxModel;
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
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.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
import com.google.common.base.Optional;

import hudson.model.ItemGroup;
import hudson.security.ACL;
import hudson.util.ListBoxModel;
import hudson.util.Secret;
import jenkins.model.Jenkins;
import org.acegisecurity.Authentication;
import org.apache.commons.lang.StringUtils;

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;

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

public static ListBoxModel doFillPersonalAccessTokenIdItems() {
final List<StringCredentials> credentials = getAllCredentials(StringCredentials.class);
final ListBoxModel listBoxModel =
new StandardListBoxModel() //
.includeEmptyValue() //
.withAll(credentials);
return listBoxModel;
}

public static Optional<StandardUsernamePasswordCredentials> findCredentials(
String usernamePasswordCredentialsId) {
final String usernamePasswordCredentialsId) {
if (isNullOrEmpty(usernamePasswordCredentialsId)) {
return absent();
}
Expand All @@ -51,27 +66,45 @@ public static Optional<StandardUsernamePasswordCredentials> findCredentials(
firstOrNull(getAllCredentials(), allOf(withId(usernamePasswordCredentialsId))));
}

public static Optional<StringCredentials> findStringCredentials(final String stringCredentials) {
if (isNullOrEmpty(stringCredentials)) {
return absent();
}

return fromNullable(
firstOrNull(getAllCredentials(StringCredentials.class), allOf(withId(stringCredentials))));
}

private static <C extends Credentials> List<C> getAllCredentials(final Class<C> type) {
final ItemGroup<?> itemGroup = null;
final Authentication authentication = SYSTEM;
final DomainRequirement domainRequirement = null;

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

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

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

public static String migrateCredentials(String username, String password) {
public static String migrateCredentials(final String username, final String password) {
String credentialsId = null;
DomainRequirement domainRequirement = null;
List<StandardUsernamePasswordCredentials> credentials =
final DomainRequirement domainRequirement = null;
final List<StandardUsernamePasswordCredentials> credentials =
CredentialsMatchers.filter(
CredentialsProvider.lookupCredentials(
StandardUsernamePasswordCredentials.class,
Jenkins.getInstance(),
ACL.SYSTEM,
domainRequirement),
CredentialsMatchers.withUsername(username));
for (StandardUsernamePasswordCredentials cred : credentials) {
for (final StandardUsernamePasswordCredentials cred : credentials) {
if (StringUtils.equals(password, Secret.toString(cred.getPassword()))) {
// If some credentials have the same username/password, use those.
credentialsId = cred.getId();
Expand All @@ -81,7 +114,7 @@ public static String migrateCredentials(String username, String password) {
if (StringUtils.isBlank(credentialsId)) {
// If we couldn't find any existing credentials,
// create new credentials with the principal and secret and use it.
StandardUsernamePasswordCredentials newCredentials =
final StandardUsernamePasswordCredentials newCredentials =
new UsernamePasswordCredentialsImpl(
CredentialsScope.SYSTEM,
null,
Expand Down
Loading

0 comments on commit 041d262

Please sign in to comment.