In this page

Adding a comment to an issue

This automation adds a comment to an issue by parsing the comment text from the VCS commit message:

Note that the issue comment text is not the complete commit message, but the part after the @comment command.

Configuration

  1. Login to Jira as admin, go to AdministrationSystemAutomation rules.
  2. Click Create rule.
  3. Select the trigger Genius Commit created (from the DevOps category).
  4. Choose the command Comment issue, and click Save.
  5. Click New action.
  6. Select the action Run Groovy script.
    1. Enter "Add comment as committer" to the Description field.
    2. Enter this Groovy script:
      import com.atlassian.jira.bc.issue.comment.CommentService
      import com.atlassian.jira.bc.issue.comment.CommentService.CommentParameters
      import com.atlassian.jira.component.ComponentAccessor
      
      def author = devops.committerByUsername ?: {throw new IllegalArgumentException("No user found with username \"${devops.commit.username}\"")}()
      // def author = devops.committerByEmailAddress ?: {throw new IllegalArgumentException("No user found with email address \"${devops.commit.emailAddress}\"")}() // (alternative, see the Smart Value Reference!)
      def commentService = ComponentAccessor.getOSGiComponentInstanceOfType(CommentService)
      
      issues.each { issue ->
      	def commentInput = CommentParameters.builder()
      		.issue(issue)
      		.body(devops.comment)
      		.created(new Date())
      		.author(author)
      		.build()
      	def commentResult = commentService.validateCommentCreate(author, commentInput)
      	if (commentResult.valid) {
      		commentService.create(author, commentResult, true)
      		auditLog.addAssociatedIssue(issue)
      		auditLog.addAssociatedUser(author)
      		auditLog.info("Comment added to ${issue.key}")
      	} else {
      		throw new IllegalArgumentException(commentResult.getErrorCollection().toString())
      	}
      }
      
  7. Click Save.
  8. Click New action.
  9. Select the action Comment on issue.
    1. Enter the comment content. Use the smart value {{devops.comment}} to refer to the text that is entered to the commit message after the @comment command.
  10. Click Save.
  11. Name your automation rule intuitively, and click Turn it on.

Usage

Single-line commit message
  1. Create a commit with this commit message:
    Protect against the null value that caused the FOO-1 bug. @comment Bug fixed!
  2. This comment will be added to the issue FOO-1:
    Bug fixed!
Multi-line commit message
  1. Create a commit with this commit message:
    Protect against the null value that caused the FOO-1 bug.
    @comment Bug fixed!
    (It was caused by an unexpected null value.)
    
  2. This comment will be added to the issue FOO-1:
    Bug fixed!
    (It was caused by an unexpected null value.)
    
Multi-line commit message with markup
  1. In case you want to use formatting, you can use the Jira text formatting notation (wiki markup) in the commit messages! Create a commit with this commit message:
    Protect against the null value that caused the FOO-1 bug.
    @comment Affected input fields in the [Acme sign-up form|https://www.acme.com/sign-up]:
    * First name
    * Last name
    * Age
    
  2. This comment will be added to the issue FOO-1:
    Affected input fields in the Acme sign-up form:
    • First name
    • Last name
    • Age
    
    ...where "Acme sign-up form" is a link with list items below it!

Troubleshooting

If you don't get the expected results:

  1. Check if the @comment command is defined in the Genius Commands screen? (It's a default command.)
  2. Check if the @comment command followed by the comment text was included in the commit message?
  3. See the general troubleshooting steps.

Questions?

Ask us any time.