Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compare coverage against PR base branch instead of master #67

Open
jaredj opened this issue Dec 27, 2018 · 8 comments
Open

Compare coverage against PR base branch instead of master #67

jaredj opened this issue Dec 27, 2018 · 8 comments

Comments

@jaredj
Copy link

jaredj commented Dec 27, 2018

Not all of our pull requests are opened against a single branch; sometimes we create pull requests against release/hotfix branches. When we do, the coverage change report is not meaningful. What would it take to always compare coverage in a PR branch against the PR's base branch instead?

@terma
Copy link
Contributor

terma commented Dec 28, 2018

I think that smt what we can do, do you know how to get base branch info from github pr?

@jaredj
Copy link
Author

jaredj commented Dec 28, 2018

@terma I don't know offhand but I can research it next week. if I have the time and can get with the program maybe I can make a PR too but no promises :)

@terma
Copy link
Contributor

terma commented Dec 29, 2018

@jaredj, thx, any info about that will be appreciated.

@jaredj
Copy link
Author

jaredj commented Dec 30, 2018

the base branch can be retrieved with the github API:

https://developer.github.com/v3/pulls/#get-a-single-pull-request

in the bit of example response they give the base branch happens to be master:

  "base": {
    "label": "master",
    "ref": "master",
    "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e",
    "user": {
      "login": "octocat",
      "id": 1,
      "node_id": "MDQ6VXNlcjE=",
      "avatar_url": "https://github.com/images/error/octocat_happy.gif",
      "gravatar_id": "",
      "url": "https://api.github.com/users/octocat",
      "html_url": "https://github.com/octocat",
      "followers_url": "https://api.github.com/users/octocat/followers",
      "following_url": "https://api.github.com/users/octocat/following{/other_user}",
      "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/octocat/subscriptions",
      "organizations_url": "https://api.github.com/users/octocat/orgs",
      "repos_url": "https://api.github.com/users/octocat/repos",
      "events_url": "https://api.github.com/users/octocat/events{/privacy}",
      "received_events_url": "https://api.github.com/users/octocat/received_events",
      "type": "User",
      "site_admin": false
    },

I think ref is what we want.

I'm not sure I'm reading it right but it seems like you're using GHPullRequest.java from the github java API in which case it's maybe as simple as just doing something like:

pr.getHead().getRef()

@dmotpan
Copy link
Collaborator

dmotpan commented Dec 31, 2018

@jaredj can you try to use the following params in your pipeline and check if you achieve the expected result?
scmVars: [GIT_URL: fullBranchUrl(env.BRANCH_NAME)]]) - for MasterCoverageAction
scmVars: [GIT_URL: fullBranchUrl(env.CHANGE_TARGET)]]) - for CompareCoverageAction

def fullBranchUrl(branchName) { return "${scm.getUserRemoteConfigs()[0].getUrl()}/tree/$branchName" }

@anton-yurchenko
Copy link

anton-yurchenko commented Feb 25, 2019

@dmotpan, works as expected.

Result:

screen shot 2019-02-25 at 9 09 42

Pipeline:

def fullBranchUrl(branchName) { return "${scm.getUserRemoteConfigs()[0].getUrl()}/tree/$branchName" }

pipeline {
  agent none
  stages {
    stage('Code Coverage') {
      agent {node 'nodejs'}
      steps {
        sh 'npm run coverage'
      }
      post {
        success {
          script {
            // commit: record code coverage
            if (env.CHANGE_ID == null) {
              currentBuild.result = 'SUCCESS'
              step([$class: 'MasterCoverageAction', scmVars: [GIT_URL: fullBranchUrl(env.BRANCH_NAME)]])
            }
            // pull request: compare code coverage
            else if (env.CHANGE_ID != null) {
              currentBuild.result = 'SUCCESS'
              step([$class: 'CompareCoverageAction', publishResultAs: 'statusCheck', scmVars: [GIT_URL: fullBranchUrl(env.CHANGE_TARGET)]])
            }
          }
        }
        cleanup {
          cleanWs()
        }
      }
    }
  }
}

@jaredj
Copy link
Author

jaredj commented Feb 25, 2019

Yes, it works for me too! Sorry for the late reply.

Note that (not a big deal) it still calls the comparison branch "master" in the status check even though it's not

@toanlakaplan
Copy link

Hi @terma
Thanks for your support,
How about the progress of this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants