Answers: <p>I guess the simplest and direct solution would be to use GitHub API: <a href="https://docs.github.com/en/rest/reference/pulls#create-a-review-for-a-pull-request" rel="noreferrer">https://docs.github.com/en/rest/reference/pulls#create-a-review-for-a-pull-request</a></p>
  <p>But for the requirement you have, PR checks is usually the way to go and you have a plugin that does the API communication for you: <a href="https://www.jenkins.io/doc/pipeline/steps/pipeline-githubnotify-step/" rel="noreferrer">https://www.jenkins.io/doc/pipeline/steps/pipeline-githubnotify-step/</a></p>
  
Answers: CC BY-SA 4.0
Answers: 2020-07-10T09:03:10.117Z
Answers: Thanks @froblesmartin - your consideration helped me to understand that indeed the best way to go about doing this was to use the API directly.
Answers: Bruce Becker
Answers: CC BY-SA 4.0
Answers: 2020-07-10T09:05:55.787Z
Answers: You are welcome! Glad to help :)
Answers: froblesmartin
Answers: CC BY-SA 4.0
Answers: 2020-07-10T15:43:40.873Z
Answers: I have a few examples of using the api in a step - would you mind if I edit your answer to add them?
Answers: Bruce Becker
Answers: CC BY-SA 4.0
Answers: 2020-07-13T06:51:28.763Z
Answers: @BruceBecker No problem! Sharing knowledge is always welcome!
Answers: froblesmartin
Answers: CC BY-SA 4.0
Answers: 2020-07-09T08:15:24.780Z
Answers: 2020-07-10T09:02:19.127Z
Answers: 2020-07-10T09:02:19.127Z
Answers: froblesmartin
Body: <p>I have a Jenkins instance, with credentials for a &quot;robot&quot; github account, which we use to interact with the GitHub API as a team. Typically, <strong>jobs are defined as declarative pipelines</strong>. I am looking for the correct syntax to perform the following steps:</p>
  <ol>
  <li>At the start of the run, create a review</li>
  <li>At the end of each stage, post a review comment, with the status of the stage, <em>e.g.</em> &quot;build passed&quot;.</li>
  <li>If all of the stages pass, approve the pull request.</li>
  </ol>
  <p>I can allow scripted steps, but the pipeline as a whole should be declarative. No shared libraries, and preferably only using commonly-used plugins.</p>
  <p><em>(update with what I have tried)</em></p>
  <p>I have tried using <a href="https://github.com/jenkinsci/pipeline-github-plugin#review" rel="noreferrer">the pipeline github plugin's review method</a>:</p>
  <pre><code>pullRequest.review('APPROVE')
  </code></pre>
  <p>naively, but this throws errors.</p>
  <p>What would be the correct syntax here?</p>
  
Comments: CC BY-SA 4.0
Comments: 2020-07-09T08:13:52.157Z
Comments: Why don't you just use GitHub checks on PRs? https://www.jenkins.io/doc/pipeline/steps/pipeline-githubnotify-step/
Comments: froblesmartin
Comments: CC BY-SA 4.0
Comments: 2020-07-09T08:23:50.997Z
Comments: That notifies GitHub of the _status_ of a pull request, not the outcome of a code review. Ie, it says to github "I have tested this commit and it's ok", not "I have conducted a review and I approve this pull request". The difference is that they use different parts of the GitHub API.
Comments: Bruce Becker
Comments: CC BY-SA 4.0
Comments: 2020-07-09T10:49:18.543Z
Comments: Yes I know, but what is your requirement exactly? If you need to ensure that something, executed by Jenkins, finishes fine and notify about that to a PR, that's what PR checks are meant for, and you can configure branch protection to not allow merges without the required checks green. Then why do you want an automated process to allow or not a PR to be merged, through a review instead of a check?
Comments: froblesmartin
Tags: <jenkins><jenkins-plugins><jenkinsfile><github>
Title: How to have jenkins add a review to a pull request
UserDisplayName: Bruce Becker
ThreadText: # How to have jenkins add a review to a pull request
  
  (Asked by: username_0 on 2020-07-09)
  
  I have a Jenkins instance, with credentials for a "robot" github account, which we use to interact with the GitHub API as a team. Typically, **jobs are defined as declarative pipelines**. I am looking for the correct syntax to perform the following steps:
  
  1. At the start of the run, create a review
  2. At the end of each stage, post a review comment, with the status of the stage, *e.g.* "build passed".
  3. If all of the stages pass, approve the pull request.
  
  I can allow scripted steps, but the pipeline as a whole should be declarative. No shared libraries, and preferably only using commonly-used plugins.
  
  *(update with what I have tried)*
  
  I have tried using [the pipeline github plugin's review method](https://github.com/jenkinsci/pipeline-github-plugin#review):
  
  ```
  pullRequest.review('APPROVE')
  
  ```
  
  naively, but this throws errors.
  
  What would be the correct syntax here?
  
  **Tags:** jenkins, jenkins-plugins, jenkinsfile, github
  
  > **Comments:**
  > **username_1**: Why don't you just use GitHub checks on PRs? https://www.jenkins.io/doc/pipeline/steps/pipeline-githubnotify-step/  
  > **username_0**: That notifies GitHub of the _status_ of a pull request, not the outcome of a code review. Ie, it says to github "I have tested this commit and it's ok", not "I have conducted a review and I approve this pull request". The difference is that they use different parts of the GitHub API.  
  > **username_1**: Yes I know, but what is your requirement exactly? If you need to ensure that something, executed by Jenkins, finishes fine and notify about that to a PR, that's what PR checks are meant for, and you can configure branch protection to not allow merges without the required checks green. Then why do you want an automated process to allow or not a PR to be merged, through a review instead of a check?  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-07-09. Score: 7 | Accepted answer
  
  I guess the simplest and direct solution would be to use GitHub API: <https://docs.github.com/en/rest/reference/pulls#create-a-review-for-a-pull-request>
  
  But for the requirement you have, PR checks is usually the way to go and you have a plugin that does the API communication for you: <https://www.jenkins.io/doc/pipeline/steps/pipeline-githubnotify-step/>
  
  > **Comments:**
  > **username_0**: Thanks @username_1 - your consideration helped me to understand that indeed the best way to go about doing this was to use the API directly.  
  > **username_1**: You are welcome! Glad to help :)  
  > **username_0**: I have a few examples of using the api in a step - would you mind if I edit your answer to add them?  
  > **username_1**: @username_0 No problem! Sharing knowledge is always welcome!  
  
  ---
  
  