jenkinsci / violation-comments-to-stash-plugin

Comments Bitbucket Server (or Stash) pull requests with static code analyzer findings.
https://plugins.jenkins.io/violation-comments-to-stash
MIT License
43 stars 16 forks source link

Possibility to use scm variable in multibranch pipeline #28

Open testuser7 opened 7 years ago

testuser7 commented 7 years ago

It will be nice if we can use scm variable in multibranch pipeline jenkinsfile.

tomasbjerre commented 7 years ago

Do you have an example of where it does not work?

testuser7 commented 7 years ago

For now I need to manually specify projectKey and repoSlug:

                if (env.CHANGE_ID) {
                    step([$class: 'ViolationsToBitbucketServerRecorder',
                          config: [useUsernamePasswordCredentials        : true,
                                   projectKey                            : 'prokey',
                                   repoSlug                              : 'repository-name',
                                   pullRequestId                         : env.CHANGE_ID,
                                   commentOnlyChangedContent             : false,
                                   commentOnlyChangedContentContext      : 0,
                                   createCommentWithAllSingleFileComments: false,
                                   createSingleFileComments              : true,
                                   violationConfigs                      : [[pattern : '.*/checkstyle-result\\.xml$',
                                                                             reporter: 'CHECKSTYLE']]]])
                }

Simplified configuration example:

                if (env.CHANGE_ID) {
                    step([$class: 'ViolationsToBitbucketServerRecorder',
                          config: [repositoryConfig                      : scm,
                                   pullRequestId                         : env.CHANGE_ID,
                                   commentOnlyChangedContent             : false,
                                   commentOnlyChangedContentContext      : 0,
                                   createCommentWithAllSingleFileComments: false,
                                   createSingleFileComments              : true,
                                   violationConfigs                      : [[pattern : '.*/checkstyle-result\\.xml$',
                                                                             reporter: 'CHECKSTYLE']]]])
                }
tomasbjerre commented 7 years ago

Are you sure scm has these values? And if it does, cant you do something like this?

projectKey                            : env.repositoryConfig.projectKey,
testuser7 commented 7 years ago

Yes, I can simply use: checkout scm to checkout repository. Method scm.getKey() return full repository url.

There is no any environment variables containing project key or repository name.

tomasbjerre commented 7 years ago

Perhaps if you use https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket and let it provide these variables for you? Then I think you can do:

projectKey                            : env.PULL_REQUEST_TO_REPO_PROJECT_KEY,

If you parameterize the job with a String parameter like PULL_REQUEST_TO_REPO_PROJECT_KEY

testuser7 commented 7 years ago

Right now I'm using Bitbucket Branch Source Plugin with Post Webhooks for Bitbucket. Direct usage of scm variable will be most reasonable.

tomasbjerre commented 7 years ago

Perhaps you can get the project key from getRepoOwner and repository from getRepository. But I'm just guessing here =)

testuser7 commented 7 years ago

I'm not sure if I can access this methods from pipeline groovy script.

jansohn commented 6 years ago

I'd also appreciate this feature.

jetersen commented 6 years ago

Definitely possible to get the ID and even more: https://github.com/jenkinsci/office-365-connector-plugin/blob/53f1823060c4a7aba72294a08c0efba6751664d5/src/main/java/jenkins/plugins/office365connector/ActionableBuilder.java#L59-L89

Here you get the full URL so ya to the PR even String urlString = oma.getObjectUrl();

amandel commented 5 years ago

For me the following pipeline snipped could collect all needed data:

@NonCPS
void publishBuildResults() {
  try {
    def gitUrl = scmVars.GIT_URL_1
    // https://myserver.example.com/bitbucket/scm/SETOOLS/setools-sbs-bitbucket.git
    def urlMatcher = gitUrl =~ '^(https?://[^/]*/bitbucket)/scm/([^/]*)/([^/]*).git$'
    if (urlMatcher) {
      def bitbucketServer = urlMatcher[0][1]
      def projectKey = urlMatcher[0][2]
      def repoSlug = urlMatcher[0][3]
      def changeId = pipeline.env.CHANGE_ID;
      def credentialIdTemp = pipeline.scm.getUserRemoteConfigs()[0].credentialsId;
      pipeline.ViolationsToBitbucketServer([
          bitbucketServerUrl                    : bitbucketServer,
          commentOnlyChangedContent             : true,
          commentOnlyChangedContentContext      : 5,
          createCommentWithAllSingleFileComments: false,
          createSingleFileComments              : true,
          maxNumberOfViolations                 : 99999,
          keepOldComments                       : true,
          projectKey                            : projectKey,
          pullRequestId                         : changeId,
          repoSlug                              : repoSlug,
          credentialsId                         : credentialIdTemp,
          commentTemplate                       : """
**{{violation.severity}}**: {{violation.message}}
*Reporter: {{violation.reporter}}{{#violation.rule}} //  Rule: {{violation.rule}}{{/violation.rule}}*
""",
          violationConfigs            : [
              // Many more formats available, check https://github.com/tomasbjerre/violations-lib
              [parser: 'SONAR', pattern: '.*/target/sonar/sonar-report.json', reporter: 'SONAR']
          ]
      ])
    } else {
      pipeline.echo("Does not look like a bitbucket server - think about migrating?")
    }
  } catch (error) {
    // do not leak errors but report them.
    pipeline.echo("Error during result publish: " + error)
  }
}
robross0606 commented 3 years ago

From what I can tell, this is really more the fault of Bitbucket Branch Source Plugin because it doesn't expose the Bitbucket JSON payload data (project, slug, etc.) from the webhook trigger into the pipeline.