- Pipeline: GitHub
- License
- Prerequisites
- Credentials
- Triggers
- Global Variables
- Auxiliary Classes
- Examples
The entry points for this plugin’s functionality are additional global variables, available to pipeline scripts when the plugin is enabled and the prerequisites are met.
MIT
-
Jenkins running Java 8 or higher.
-
Projects/jobs must be automatically created by the GitHub Organization folder/project type.
See: GitHub Branch Source Plugin
Currently all operations against GitHub will be performed using the builds Checkout Credentials
, if no Checkout Credentials
are configured then the Scan Credentials
will be used instead.
However you can override this in a pipeline script by calling setCredentials(String userName, String password)
before any properties or methods are accessed/invoked on the pullRequest
global variable.
pullRequest.setCredentials('John.Smith', 'qwerty4321')
If you plan to use this plugin to add/modify/remove comments, labels, commit statuses etc. Please ensure that the required permissions are assigned to the token supplied in the credentials (Checkout
/Scan
/Manually
).
This plugin adds the following pipeline triggers
- This trigger only works on Pull Requests, created by the GitHub Branch Source Plugin.
- Currently this trigger will only allow collaborators of the repository in question to trigger builds.
The Pull Request's job/build must have run at least once for the trigger to be registered. If an initial run never takes place then the trigger won't be registered and cannot pickup on any comments made.
This should not be an issue in practice, because a requirement of using this plugin is that your jobs are setup automatically by the GitHub Branch Source Plugin, which will trigger an initial build when it is notified of a new Pull Request.
This trigger would be of limited usefulness for people wishing to build public GitHub/Jenkins bots, using pipeline scripts. As there is no way to ensure that a Pull Request's Jenkinsfile
contains any triggers. Not to mention you would not want to trust just any Jenkinsfile
from a random Pull Request/non-collaborator.
This trigger is intended to be used inside enterprise organizations:
- Where all branches and forks just contain a token
Jenkinsfile
that delegates to the real pipeline script, using shared libraries. - Trust all their Pull Request authors.
commentPattern
(Required) - A Java style regular expression
properties([
pipelineTriggers([
issueCommentTrigger('.*test this please.*')
])
])
Coming soon!
Name | Type | Writable | Description |
---|---|---|---|
id | Integer |
false | |
state | String |
true | Valid values open or closed |
number | Integer |
false | |
url | String |
false | |
patch_url | String |
false | |
diff_url | String |
false | |
issue_url | String |
false | |
title | String |
true | |
body | String |
true | |
locked | Boolean |
true | Accepts true , false or 'true' , 'false' |
milestone | Integer |
true | |
head | String |
false | |
base | String |
true | Name of the base branch in the current repository this pull request targets |
files | Iterable<CommitFile> |
false | |
assignees | List<String> |
true | Accepts a List<String> |
commits | Iterable<Commit> |
false | |
comments | Iterable<IssueComment> |
false | |
review_comments | Iterable<ReviewComment> |
false | |
labels | Iterable<String> |
true | Accepts a List<String> |
statuses | List<CommitStatus> |
false | |
requested_reviewers | Iterable<String> |
false | |
updated_at | Date |
false | |
created_at | Date |
false | |
created_by | String |
false | |
closed_at | Date |
false | |
closed_by | String |
false | |
merged_at | Date |
false | |
merged_by | String |
false | |
commit_count | Integer |
false | |
comment_count | Integer |
false | |
additions | Integer |
false | |
deletions | Integer |
false | |
changed_files | Integer |
false | |
merged | Boolean |
false | |
mergeable | Boolean |
false | |
merge_commit_sha | String |
false | |
maintainer_can_modify | Boolean |
true | Accepts true , false or 'true' , 'false' |
String merge([String commitTitle, String commitMessage, String sha, String mergeMethod])
Returns the merge's SHA/commit id.
CommitStatus createStatus(String status [, String context, String description, String targetUrl])
void addLabels(String...labels)
void removeLabel(String label)
void addAssignees(String...assignees)
void removeAssignees(String...assignees)
ReviewComment reviewComment(String commitId, String path, int position, String body)
ReviewComment editReviewComment(long commentId, String body)
ReviewComment replyToReviewComment(long commentId, String body)
void deleteReviewComment(long commentId)
IssueComment comment(String body)
IssueComment editComment(long commentId, String body)
void deleteComment(long commentId)
void createReviewRequests(String...reviewers)
void deleteReviewRequests(String...reviewers)
void setCredentials(String userName, String password)
Name | Type | Writable | Description |
---|---|---|---|
id | String |
false | |
url | String |
false | |
status | String |
false | One of pending , success , failure or error |
context | String |
false | |
description | String |
false | |
target_url | String |
false | |
created_at | Date |
false | |
updated_at | Date |
false | |
creator | String |
false |
None.
Name | Type | Writable | Description |
---|---|---|---|
sha | String |
false | |
url | String |
false | |
author | String |
false | |
committer | String |
false | |
parents | List<String> |
false | List of parent commit SHA's |
message | String |
false | |
comment_count | Integer |
false | |
comments | Iterable<ReviewComment> |
false | |
additions | Integer |
false | |
deletions | Integer |
false | |
total_changes | Integer |
false | |
files | List<CommitFile> |
false | List of files added, removed and or modified in this commit |
statuses | List<CommitStatus> |
false | List of statuses associated with this commit |
CommitStatus createStatus(String status [, String context, String description, String targetUrl])
ReviewComment comment(String body [, String path, Integer position])
Name | Type | Writable | Description |
---|---|---|---|
sha | String |
false | |
filename | String |
false | |
status | String |
false | One of |
patch | String |
false | |
additions | Integer |
false | |
deletions | Integer |
false | |
changes | Integer |
false | |
raw_url | String |
false | |
blob_url | String |
false |
None.
Name | Type | Writable | Description |
---|---|---|---|
id | Integer |
false | |
url | String |
false | |
user | String |
false | |
body | String |
true | |
created_at | Date |
false | |
updated_at | Date |
false |
void delete()
Name | Type | Writable | Description |
---|---|---|---|
id | Integer |
false | |
url | String |
false | |
user | String |
false | |
created_at | Date |
false | |
updated_at | Date |
false | |
commit_id | String |
false | |
original_commit_id | Integer |
false | |
body | String |
true | |
path | String |
false | |
line | Integer |
false | |
position | Integer |
false | |
original_position | Integer |
false | |
diff_hunk | String |
false |
void delete()
pullRequest['title'] = 'Updated title'
pullRequest['body'] = pullRequest['body'] + '\nEdited by Pipeline'
pullRequest['status'] = 'closed'
pullRequest.createStatus(status: 'success',
context: 'continuous-integration/jenkins/pr-merge/tests',
description: 'All tests are passing',
targetUrl: "${JOB_URL}/testResults")
if (pullRequest['locked']) {
pullRequest['locked'] = false
}
if (pullRequest['mergeable']) {
pullRequest.merge('merge commit message here')
}
// or
if (pullRequest['mergeable']) {
pullRequest.merge(commitTile: 'Make it so..', commitMessage: 'TO BOLDLY GO WHERE NO MAN HAS GONE BEFORE...', mergeMethod: 'squash')
}
pullRequest.addLabel('Build Passing')
pullRequest.removeLabel('Build Passing')
pullRequest['labels'] = ['Bug', 'Feature']
pullRequest.addAssginee('Spock')
pullRequest.removeAssginee('McCoy')
pullRequest['assignees'] = ['Data', 'Scotty']
for (commitFile in pullRequest['files']) {
echo "SHA: ${commitFile['sha']} File Name: ${commitFile['filename']} Status: ${commitFile['status']}"
}
def comment = pullRequest.comment('This PR is highly illogical..')
pullRequest.editComment(comment['id'], 'Live long and prosper.')
// or
comment['body'] = 'Live long and prosper.'
pullRequest.deleteComment(commentId)
// or
comment.delete()
def commitId = 'SHA of the commit containing the change/file you wish to review';
def path = 'src/main/java/Main.java'
def lineNumber = 5
def body = 'The review comment'
def comment = pullRequest.reviewComment(commitId, path, lineNumber, body)
pullRequest.editReviewComment(comment['id'], 'Live long and prosper.')
// or
comment['body'] = 'Live long and prosper.'
pullRequest.deleteReviewComment(comment['id'])
// or
comment.delete()
pullRequest.replyToReviewComment(comment['id'], 'Khaaannnn!')
// or
comment.createReply('Khaaannnn!')
for (commit in pullRequest['commits']) {
echo "SHA: ${commit['sha']}, Committer: ${commit['commiter']}, Commit Message: ${commit['message']}"
}
for (comment in pullRequest['comments']) {
echo "Author: ${comment['user']}, Comment: ${comment['body']}"
}
for (reviewComment in pullRequest['review_comments']) {
echo "File: ${reviewComment['path']}, Line: ${reviewComment['line']}, Author: ${reviewComment['user']}, Comment: ${reviewComment['body']}"
}
for (commit in pullRequest['commits']) {
for (status in commit['statuses']) {
echo "Commit: ${commit['sha']}, Status: ${status['status']}, Context: ${status['context']}, URL: ${status['target_url']}"
}
}
for (commit in pullRequest['commits']) {
createStatus(status: 'pending')
}
for (status in pullRequest['statuses']) {
echo "Commit: ${pullRequest['head']}, Status: ${status['status']}, Context: ${status['context']}, URL: ${status['target_url']}"
}
for (requestedReviewer in pullRequest['requested_reviewers']) {
echo "${requestedReviewer} was requested to review this Pull Request"
}
pullRequest.createReviewRequests('Spock', 'McCoy')
pullRequest.deleteReviewRequests('McCoy')