jenkinsci / github-pr-coverage-status-plugin

Nice test coverage icon for your pull requests just from Jenkins
https://plugins.jenkins.io/github-pr-coverage-status/
Apache License 2.0
93 stars 101 forks source link

Injecting GIT_URL and CHANGE_URL #39

Open oz123 opened 7 years ago

oz123 commented 7 years ago

Trying to make use of the hint you gave in README, I am still getting an error:

java.lang.UnsupportedOperationException: Can't find GIT_URL or CHANGE_URL in envs: {BRANCH_NAME=master, BUILD_DISPLAY_NAME=#31, BUILD_ID=31, BUILD_NUMBER=31, BUILD_TAG=jenkins-testci-master-31, BUILD_URL=https://jnkns-ci.tmh.cloud/job/testci/job/master/31/, CLASSPATH=, HUDSON_HOME=/var/jenkins_home, HUDSON_SERVER_COOKIE=01f6aedeea333d1f, HUDSON_URL=https://jnkns-ci.tmh.cloud/, JENKINS_HOME=/var/jenkins_home, JENKINS_SERVER_COOKIE=01f6aedeea333d1f, JENKINS_URL=https://jnkns-ci.tmh.cloud/, JOB_BASE_NAME=master, JOB_DISPLAY_URL=https://jnkns-ci.tmh.cloud/job/testci/job/master/display/redirect, JOB_NAME=testci/master, JOB_URL=https://jnkns-ci.tmh.cloud/job/testci/job/master/, RUN_CHANGES_DISPLAY_URL=https://jnkns-ci.tmh.cloud/job/testci/job/master/31/display/redirect?page=changes, RUN_DISPLAY_URL=https://jnkns-ci.tmh.cloud/job/testci/job/master/31/display/redirect}
    at com.github.terma.jenkins.githubprcoveragestatus.PrIdAndUrlUtils.getGitUrl(PrIdAndUrlUtils.java:85)
    at com.github.terma.jenkins.githubprcoveragestatus.MasterCoverageAction.perform(MasterCoverageAction.java:71)
    at org.jenkinsci.plugins.workflow.steps.CoreStep$Execution.run(CoreStep.java:80)
    at org.jenkinsci.plugins.workflow.steps.CoreStep$Execution.run(CoreStep.java:67)
    at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution$1$1.call(SynchronousNonBlockingStepExecution.java:49)
    at hudson.security.ACL.impersonate(ACL.java:260)
    at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution$1.run(SynchronousNonBlockingStepExecution.java:46)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Finished: FAILURE

This is my pipeline:

pipeline {

  agent any

  options {
    skipDefaultCheckout()
  }
  environment {
    // calling credentials() actually sets 3 environment variables
    // GIT_HUB with  <username>:<password>
    // GIT_HUB_USER with <username>
    // GIT_HUB_PSW with <password>

    GIT_HUB = credentials('xxxx')
    DOCKER_REPO  = 'mobilityhouse'
    DOCKER_HUB   = credentials('xxxx')
    CHANGE_URL = "https://$GIT_HUB@github.com/mobilityhouse/testci.git"
  }

  stages{
    stage('Checkout') {
      steps {
        script {
          checkout scm
          result = sh (script: "git log -1 | grep '.*\\[ci skip\\].*\\|.*\\[skip ci\\].*'", returnStatus: true)
          if (result == 0) {
              currentBuild.result = 'NOT_BUILT'
              error("SKIPPING BUILD ...")
          }
        }
      }
    }
    // slaves are built on top of Debian ... or at least I thought so ...
    stage('Prepare Environment') {
      steps {
        sh 'pip3 install -r requirements_dev.txt'
        script {
          currentBuild.result = 'SUCCESS'
        }
      }
    }

    /* currently it's not possible to run the tests in a docker
       and get the xml results ...*/
    stage('Coverage & Tests') {
      steps {
        sh 'pip3 install -e .'
        sh 'make coverage-xml'
        script {
          currentBuild.result = 'SUCCESS'
          // sh(script: 'export GIT_URL="https://$GIT_HUB@github.com/mobilityhouse/testci.git"')
          env.CHANGE_URL = "https://$GIT_HUB@github.com/mobilityhouse/testci.git"
        }
      }
    }

    /* stage('Test') {
      steps {
        echo 'hl'
      }
    post {
      success {
        echo 'This works!'
        }
      }
    }

    stage('Fail') {
      steps {
        sh 'ls -l /jkk'
      }
    post {
      success {
        echo 'does will never work'
        }
      failure {
        echo 'sigh!'
        }
      }

    } */
  }

    post {
        success {
              step([$class: 'MasterCoverageAction'])
              step([$class: 'CompareCoverageAction'])

        }
    }

}

Can you explain how to inject the variables?

terma commented 7 years ago

@oz123 very good question, checking...

terma commented 7 years ago

@oz123 still no answer, I tried to debug, but totally unclear where they injected and how, so have created defect for pipeline plugin https://issues.jenkins-ci.org/browse/JENKINS-46889 let see

ichasepucks commented 7 years ago

@terma I've set GIT_URL in my environments block and right before calling CompareCoverageAction I dumped my env and see the correct value for GIT_URL. But what appears to be happening is the call to getGitUrl does not have GIT_URL in the envVars. I think you know that and that's the odd problem we are trying to understand why.

Could we add a workaround such that we can pass scmVars which contains GIT_URL and use that? If you look at how getGitUrl is implemented, if GIT_URL is not defined in envVars it'll try CHANGE_URL which, in my case, has a value, so it tries to use it. It would be nice to maybe do something like below (note untested):

final String gitUrl = envVars.get(GIT_URL_PROPERTY);
if (!gitUrl && scmVars != null && scmVars.containsKey(GIT_URL_PROPERTY)) return scmVars.get(GIT_URL_PROPERTY);

I suspect, if we do that change then we could make the following calls and it would work?

step([$class: 'CompareCoverageAction', scmVars: [GIT_URL: "git@github.com:org/repo.git"]])
step([$class: 'MasterCoverageAction', scmVars: [GIT_URL: "git@github.com:org/repo.git"]])
ichasepucks commented 7 years ago

I should note that I just tested this and using

step([$class: 'MasterCoverageAction', scmVars: [GIT_URL: "git@github.com:org/repo.git"]])

works today as the code is written, which may even solve @oz123's problem now.

My problem is that the key into the db for coverage numbers don't match unless the workaround I suggested above works. Or possibly using Sonar?

oz123 commented 7 years ago

@ichasepucks this definitely helped! The docs should be updated ...

oz123 commented 7 years ago

@ichasepucks I have to withdraw my previous comment. While you solution works for the injection itself, it still does fix the problem. The reporting still fails. see #42

ichasepucks commented 7 years ago

@oz123 yeah, I was going to say. I'm still not able to get this to work until my proposed fix comes in. Any comment @terma on this? I can try to build this and submit a PR, but I am not a Java programmer at all. However, it would be excellent if this got fixed. I found this https://issues.jenkins-ci.org/browse/JENKINS-35230 which seems very related. For some reason people think its acceptable to have those env vars not injected. I'm not sure we're ever going to get this working right until that is working or we properly read scmVars.

soliad commented 7 years ago

There is an issue the way the plugin retrieve the env variable, but doing step([$class: 'MasterCoverageAction', scmVars: [GIT_URL: env.GIT_URL]]) is working in our Github Organization folder.

But it's not working for 'CompareCoverageAction' as the CHANGE_URL (with the PR path) is taking over the scmVars parameters. Wouldn't it make sense to read from scmVars first ?

ichasepucks commented 7 years ago

@soliad yes, that's exactly what I'm proposing as well.

soliad commented 7 years ago

I propose #45 as a quick simple fix, so we just use scmVars and do not need to care about env variable propagation

oz123 commented 7 years ago

@soliad I am sorry for responding late. I am quite busy with other projects, so I can't promise to make the required changed quickly.

45 seems promising, can you also update the docs and add an example pipline?

soliad commented 7 years ago

@oz123 yes I can take a look at the doc as well