Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a comment in github pull request in jenkins

Tags:

I am using Git pull request builder plugin in Jenkins to link the Pull requests to Jenkins. Jobs are triggered with hooks and everything runs smoothly.

Nevertheless, at the end of the build, I'd like to add a comment when there's a fail/success or an error.

I tried using pipeline and use

pullRequest.comment('This is a comment sent from the Pipeline')

But it doesn't recognize it (I installed pipeline for github):

groovy.lang.MissingPropertyException: No such property: pullRequest for class: groovy.lang.Binding
  at groovy.lang.Binding.getVariable(Binding.java:63)
  at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:242)
  at org.kohsuke.groovy.sandbox.impl.Checker$6.call(Checker.java:288)
  at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:292)
  at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:268)
  at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetProperty(Checker.java:268)
  at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getProperty(SandboxInvoker.java:29)
  at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
  at WorkflowScript.run(WorkflowScript:5)
  at ___cps.transform___(Native Method)
  at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.get(PropertyishBlock.java:74)
  at com.cloudbees.groovy.cps.LValueBlock$GetAdapter.receive(LValueBlock.java:30)
  at com.cloudbees.groovy.cps.impl.PropertyishBlock$ContinuationImpl.fixName(PropertyishBlock.java:66)
  at sun.reflect.GeneratedMethodAccessor446.invoke(Unknown Source)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at com.cloudbees.groovy.cps.impl.ContinuationPtr$ContinuationImpl.receive(ContinuationPtr.java:72)
  at com.cloudbees.groovy.cps.impl.ConstantBlock.eval(ConstantBlock.java:21)
  at com.cloudbees.groovy.cps.Next.step(Next.java:83)
  at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:174)
  at com.cloudbees.groovy.cps.Continuable$1.call(Continuable.java:163)
  at org.codehaus.groovy.runtime.GroovyCategorySupport$ThreadCategoryInfo.use(GroovyCategorySupport.java:122)
  at org.codehaus.groovy.runtime.GroovyCategorySupport.use(GroovyCategorySupport.java:261)
  at com.cloudbees.groovy.cps.Continuable.run0(Continuable.java:163)
  at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.access$101(SandboxContinuable.java:34)
  at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.lambda$run0$0(SandboxContinuable.java:59)
  at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)
  at org.jenkinsci.plugins.workflow.cps.SandboxContinuable.run0(SandboxContinuable.java:58)
  at org.jenkinsci.plugins.workflow.cps.CpsThread.runNextChunk(CpsThread.java:174)
  at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:332)
  at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:83)
  at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:244)
  at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:232)
  at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64)
  at java.util.concurrent.FutureTask.run(FutureTask.java:266)
  at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:131)
  at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
  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

With the freestyle job:

java.lang.NullPointerException
  at org.jenkinsci.plugins.github.pullrequest.utils.JobHelper.getGhRepositoryFromPRTrigger(JobHelper.java:162)
  at org.jenkinsci.plugins.github.pullrequest.utils.JobHelper.getGhPullRequest(JobHelper.java:178)
  at org.jenkinsci.plugins.github.pullrequest.publishers.impl.GitHubPRCommentPublisher.perform(GitHubPRCommentPublisher.java:72)
  at hudson.tasks.BuildStepCompatibilityLayer.perform(BuildStepCompatibilityLayer.java:81)
  at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
  at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:744)
  at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:690)
  at hudson.model.Build$BuildExecution.post2(Build.java:186)
  at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:635)
  at hudson.model.Run.execute(Run.java:1752)
  at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
  at hudson.model.ResourceController.execute(ResourceController.java:97)
  at hudson.model.Executor.run(Executor.java:429)
Build step 'GitHub PR: post comment' marked build as failure

Now I don't really know where to look for

like image 587
David Bensoussan Avatar asked May 23 '18 12:05

David Bensoussan


People also ask

How do I add a comment to a GitHub pull request?

Adding line comments to a pull requestUnder your repository name, click Pull requests. In the list of pull requests, click the pull request where you'd like to leave line comments. On the pull request, click Files changed. Hover over the line of code where you'd like to add a comment, and click the blue comment icon.

What is a pull request comment?

A pull request (PR) is a process when new code is reviewed before it should be merged to develop a branch or master branch in a Git repository like GitHub. The author creates a PR, while a reviewer reviews a PR. A PR could be time-consuming, annoying, or even nerve-racking if done wrong.


1 Answers

I couldn't find a plugin doing it, so I did using curl. Here is my groovy function

def call(def text_pr) {
  def repository_url = scm.userRemoteConfigs[0].url
  def repository_name = repository_url.replace("[email protected]:","").replace(".git","")

  withCredentials([string(credentialsId: '<YOUR-TOKEN-ID>', variable: 'GITHUB_TOKEN')]) {
    sh "curl -s -H \"Authorization: token ${GITHUB_TOKEN}\" -X POST -d '{\"body\": \"${text_pr}\"}' \"https://api.github.com/repos/${repository_name}/issues/${ghprbPullId}/comments\""
  }
}
like image 193
David Bensoussan Avatar answered Sep 28 '22 18:09

David Bensoussan