All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.nfalco79.jenkins.plugins.bitbucket.trait.PullRequestTargetBranchTrait Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2023 Falco Nikolas
 *
 * 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.trait;

import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;

import com.cloudbees.jenkins.plugins.bitbucket.BitbucketGitSCMBuilder;
import com.cloudbees.jenkins.plugins.bitbucket.BitbucketSCMSource;
import com.cloudbees.jenkins.plugins.bitbucket.BitbucketSCMSourceContext;
import com.cloudbees.jenkins.plugins.bitbucket.PullRequestSCMHead;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.plugins.git.GitSCM;
import hudson.scm.SCMDescriptor;
import jenkins.plugins.git.AbstractGitSCMSource;
import jenkins.plugins.git.GitSCMBuilder;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.SCMSource;
import jenkins.scm.api.trait.SCMBuilder;
import jenkins.scm.api.trait.SCMSourceContext;
import jenkins.scm.api.trait.SCMSourceTrait;
import jenkins.scm.api.trait.SCMSourceTraitDescriptor;

/**
 * Exposes the target branch of pull request as ref specs of a
 * {@link AbstractGitSCMSource} as a {@link SCMSourceTrait}.
 *
 * @since 1.1.0
 */
public class PullRequestTargetBranchTrait extends SCMSourceTrait {

    /**
     * Constructor for stapler.
     */
    @DataBoundConstructor
    public PullRequestTargetBranchTrait() {
        // for stapler
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void decorateBuilder(SCMBuilder builder) {
        if (builder instanceof GitSCMBuilder) {
            GitSCMBuilder gitBuilder = (GitSCMBuilder) builder;
            SCMHead head = builder.head();
            if (head instanceof PullRequestSCMHead) {
                String targetBranch = ((PullRequestSCMHead) head).getTarget().getName();
                gitBuilder.withRefSpec("+refs/heads/" + targetBranch + ":refs/remotes/@{remote}/" + targetBranch);
            }
        }
    }

    /**
     * Our descriptor.
     */
    @Symbol("bitbucketPRTargetBranchRefSpec")
    @Extension
    public static class DescriptorImpl extends SCMSourceTraitDescriptor {

        /**
         * {@inheritDoc}
         */
        @NonNull
        @Override
        public String getDisplayName() {
            return Messages.PullRequestTargetBranchTrait_displayName();
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public Class getContextClass() {
            return BitbucketSCMSourceContext.class;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public Class getSourceClass() {
            return BitbucketSCMSource.class;
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean isApplicableToBuilder(@NonNull Class builderClass) {
            return BitbucketGitSCMBuilder.class.isAssignableFrom(builderClass);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public boolean isApplicableToSCM(@NonNull SCMDescriptor scm) {
            return scm instanceof GitSCM.DescriptorImpl;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy