
com.github.nfalco79.jenkins.plugins.bitbucket.steps.CommitsStep Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bitbucket-steps Show documentation
Show all versions of bitbucket-steps Show documentation
Steps to query details about repositories or handle team/user information in bitbucket cloud.
The newest version!
/*
* Copyright 2023 Nikolas Falco
*
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.github.nfalco79.jenkins.plugins.bitbucket.steps;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.jenkinsci.plugins.workflow.steps.Step;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.jenkinsci.plugins.workflow.steps.SynchronousStepExecution;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import com.github.nfalco79.bitbucket.client.BitbucketCloudClient;
import com.github.nfalco79.bitbucket.client.Credentials;
import com.github.nfalco79.bitbucket.client.Credentials.CredentialsBuilder;
import com.github.nfalco79.bitbucket.client.model.Commit;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.Util;
import hudson.model.Failure;
import hudson.model.Run;
import hudson.util.FormValidation;
public class CommitsStep extends Step {
private String credentialsId;
private Integer prId;
private final String workspace;
private final String repository;
@DataBoundConstructor
public CommitsStep(@NonNull String workspace, @NonNull String repository) {
this.workspace = Util.fixEmptyAndTrim(workspace);
if (workspace == null) {
throw new Failure(Messages.Steps_requiredParameter("Workspace/Owner"));
}
this.repository = Util.fixEmptyAndTrim(repository);
if (repository == null) {
throw new Failure(Messages.Steps_requiredParameter("Repository"));
}
}
public String getWorkspace() {
return workspace;
}
public String getRepository() {
return repository;
}
public String getCredentialsId() {
return credentialsId;
}
@DataBoundSetter
public void setCredentialsId(String credentialsId) {
this.credentialsId = Util.fixEmptyAndTrim(credentialsId);
}
@Override
public StepExecution start(StepContext context) throws Exception {
return new CommitsStepExecution(this, context);
}
public Integer getPrId() {
return prId;
}
@DataBoundSetter
public void setPrId(Integer prId) {
this.prId = prId;
}
private static class CommitsStepExecution extends SynchronousStepExecution> {
private static final long serialVersionUID = 1L;
private transient final CommitsStep step;
private CommitsStepExecution(CommitsStep step, StepContext context) {
super(context);
this.step = step;
}
@Override
protected List run() throws Exception {
Credentials credentials = null;
if (step.credentialsId != null) {
String credentialsId = step.credentialsId;
Run, ?> run = getContext().get(Run.class);
StandardUsernamePasswordCredentials c = CredentialsProvider.findCredentialById(credentialsId, StandardUsernamePasswordCredentials.class, run);
if (c != null) {
credentials = CredentialsBuilder.build(c.getUsername(), c.getPassword().getPlainText());
}
}
if (step.prId == null) {
// generic commit not yet supported
throw new Failure(Messages.Steps_requiredParameter("Pull request identifier"));
}
try (BitbucketCloudClient client = new BitbucketCloudClient(credentials)) {
if (step.prId != null) {
return client.getPullRequestCommits(step.workspace, step.repository, step.prId, true);
} else {
return null; // not yet supported
}
}
}
}
@Extension
public static class DescriptorImpl extends AbstractBitbucketStepDescriptor {
@Override
public String getFunctionName() {
return "bitbucketCommits";
}
@NonNull
@Override
public String getDisplayName() {
return Messages.CommitStep_displayName();
}
@Override
public Set extends Class>> getRequiredContext() {
Set> context = new HashSet<>();
Collections.addAll(context, Run.class);
return Collections.unmodifiableSet(context);
}
public FormValidation doCheckWorkspace(@QueryParameter String workspace) {
return FormValidationBuilder.checkWorkspace(workspace);
}
public FormValidation doCheckRepository(@QueryParameter String repository) {
return FormValidationBuilder.checkRepository(repository);
}
public FormValidation doCheckPrId(@QueryParameter Integer prId) {
return FormValidationBuilder.checkPrId(false, prId);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy