You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
2.7 KiB

  1. package org.wikimedia.integration
  2. import java.net.URLEncoder
  3. import groovy.json.JsonOutput
  4. /**
  5. * Gerrit review that can be used to comment on a gerrit patchset
  6. *
  7. * {@code
  8. * import org.wikimedia.integration.GerritReview
  9. * import org.wikimedia.integration.GerritComment
  10. *
  11. * stage('comment') {
  12. * comment = new GerritComment(
  13. * jobName: xx,
  14. * buildNumber: xx,
  15. * jobStatus: xx,
  16. * image: xx,
  17. * tags: xx
  18. * )
  19. * GerritReview.post(this, comment)
  20. * }
  21. * }
  22. */
  23. class GerritReview implements Serializable {
  24. /**
  25. * Jenkins pipeline workflow script context.
  26. */
  27. final def workflowScript
  28. /**
  29. * Gerrit URL
  30. */
  31. final def gerritURL = 'https://gerrit.wikimedia.org/r'
  32. /**
  33. * Name of the auth credentials in jenkins to use in gerrit
  34. */
  35. final def gerritAuthCreds = 'gerrit.pipelinebot'
  36. /**
  37. * GerritComment with information for message body
  38. */
  39. final GerritComment comment
  40. GerritReview(workflowScript, GerritComment comment) {
  41. this.workflowScript = workflowScript
  42. this.comment = comment
  43. }
  44. /**
  45. * URLEncoded ZUUL_PROJECT from the environment.
  46. *
  47. * This should be set for all patchsets.
  48. */
  49. String getProject() {
  50. URLEncoder.encode(this.workflowScript.env.ZUUL_PROJECT, 'UTF-8')
  51. }
  52. /**
  53. * Return a full authorized url for a gerrit revision review.
  54. *
  55. * May return an empty string in the cases of an unexpected environment.
  56. */
  57. String getRequestURL() {
  58. def change = this.workflowScript.env.ZUUL_CHANGE
  59. def revision = this.workflowScript.env.ZUUL_PATCHSET
  60. if (! revision || ! change) { return "" }
  61. def changeId = [this.getProject(), change].join('~')
  62. [
  63. this.gerritURL,
  64. 'a/changes',
  65. changeId,
  66. 'revisions',
  67. revision,
  68. 'review',
  69. ].join('/')
  70. }
  71. /**
  72. * Format gerritcomment as a json message.
  73. */
  74. String getBody() {
  75. JsonOutput.toJson([message: this.comment.formatMessage()])
  76. }
  77. /**
  78. * Static method to POST GerritComment to a particular change.
  79. *
  80. * Uses the Gerrit RESTAPI to POST a comment on a patchset revision.
  81. *
  82. * @param workflowScript Jenkins workflow script context.
  83. * @param comment GerritComment
  84. */
  85. static String post(workflowScript, GerritComment comment) {
  86. def gr = new GerritReview(workflowScript, comment)
  87. def url = gr.getRequestURL()
  88. if (! url) {
  89. gr.workflowScript.error "Could not determine Gerrit ChangeID from Environment. Aborting."
  90. }
  91. def response = gr.workflowScript.httpRequest(
  92. url: url,
  93. httpMode: 'POST',
  94. customHeaders: [[name: "content-type", value: 'application/json']],
  95. requestBody: gr.getBody(),
  96. consoleLogResponseBody: true,
  97. validResponseCodes: "200",
  98. authentication: gr.gerritAuthCreds
  99. )
  100. response.content
  101. }
  102. }