jenkinsci / bitbucket-branch-source-plugin

Bitbucket Branch Source Plugin
https://plugins.jenkins.io/cloudbees-bitbucket-branch-source
MIT License
217 stars 349 forks source link

Expose Bitbucket repository slug #809

Open ardrabczyk opened 4 months ago

ardrabczyk commented 4 months ago

What feature do you want to see added?

I'm switching away from Bamboo and I'm looking for an equivalent of bamboo_planRepository_1_name. It would be easier to have a ready to use variable than to parse GIT_URL manually.

Upstream changes

No response

Are you interested in contributing this feature?

No response

ardrabczyk commented 4 months ago

I can do as they do here https://github.com/rtyler/jdp/blob/main/data/valid/generated-parallel-wrapped-by-declarative/Jenkinsfile:

    script {
        def jobs = Jenkins.instance.getAllItems()
        jobs.each { j ->
        def scmsrc = []
        if (j instanceof org.jenkinsci.plugins.workflow.job.WorkflowJob) {
            scmsrc = j.getSCMs()
        } else {
            try {
            scmsrc = j.SCMSources
            } catch (Exception eSCMsrc) {
            echo "No supported SCM source variable found: " + eSCMsrc;
            }
        }
        if (scmsrc.size() >0)
            for (scm in scmsrc) {
            String url = ""
            if (scm instanceof com.cloudbees.jenkins.plugins.bitbucket.BitbucketSCMSource) {
            echo "BitbucketSCM...";
            echo scm.getRepoOwner()
            echo scm.getRepository()
            }
        }

        }
    }

but it requires approving a lot of scripts and is hard to use.

ardrabczyk commented 4 months ago

It could be simplified, complete Jenkinsfile could look like this:

def determineRepoSlug() {
    def a = Jenkins.instance.getItem("${env.JOB_NAME}".tokenize("/")[0])
    return a.SCMSources[0].getRepoOwner() + "/" + a.SCMSources[0].getRepository()
}

pipeline {
    agent {
    docker {
        image 'ubuntu'
        label 'ard'
    }
    }

    environment {
        BITBUCKET_REPO_SLUG="${determineRepoSlug()}"
    }

    stages {
        stage('build') {
            steps {
        sh "env"
        }
    }
    }
}

Nonetheless a variable exposed by the plugin would be useful.