Apply label when an issue is updated, based on logic involving history data and comments data
Asked by Amit Gandhi:

My requirement is as below. Can you please let me know if that is achievable using JIRA automation?

When a jira issue is updated, I want to apply a label, say XYZ, based on below logic:

Capture the datetime of the second last EVENT, say, DATETIME-SECOND-LAST

AND

Capture the datetime of the last EVENT, say, DATETIME-LAST

EVENT here can be.....

comment added by a user belonging to ABC user group

OR

status changed to TO DO or IN PROGRESS

If the difference of DATETIME-SECOND-LAST and DATETIME-LAST is greater than X hours, then apply the label, XYZ.


Answers:
author: Bill Sheboy
body: Hi [@Amit Gandhi](/t5/user/viewprofilepage/user-id/2173609)

I suspect that theoretically you could do that with an automation rule (with the REST API, or with multiple rules and custom fields), however...

That sounds like a brittle solution: depending on the date/time difference between two unrelated events. Particularly because you have no control over what happens during the time between them (e.g. other issue updates).

Would you please describe the problem you are trying to solve? Knowing that may provide context for the community to offer suggestions. Thanks!

Kind regards,  
Bill

comments: author: Amit Gandhi
body: Thanks for your response.

We have a requirement as below:

ABC team is responsible to update JIRA ticket every X hours. If they miss this update, then we should apply a label, say LBL1, to the JIRA ticket.

The update they do can be a status change (transition to one of the selected statuses) or a comment.


author: Bill Sheboy
body: Thanks for clarifying!

I would recommend a single rule, on a scheduled trigger running every X-hours. Then use JQL to find any status changes (or lack thereof) and rule conditions to check for the last comment.

Here is an example below, which **you should adjust and test the JQL separately to meet your needs**.

* trigger: scheduled every X hours (let's assume 6 hours), with JQL
  * project = *myProjectName* AND statusCategory != Done AND NOT status CHANGED AFTER -6h
* advanced compare condition: check if the last comment was added after the last 6h
  * first value: {{issue.comments.last.created}}
  * condition: less than
  * second value: {{now.minusHours(6)}}
* action: add label


author: Amit Gandhi
body: Thanks.

The only confusion I have here is will it take care of OR conditions below?

*The update they do can be*

*a status change (transition to one of the selected statuses)*

*OR*

*a comment.*

The label should be added if they have not performed EITHER of above actions after X hours passed from last datetime of EITHER of above actions.

Example,

A team is responsible to update jira card every 2 hours.

User1 belongs to this team.

User1 has transitioned to one of the statuses or added a comment at 3 pm

If he/she has not transitioned to one of the statuses AND not added a comment till 5 pm, then the label should be applied.


author: Bill Sheboy
body: There is no OR clause or condition structure in automation rules at this time.

A work-around would be to use a less restrictive JQL for the trigger, and then test both conditions using if/else structures, such as:

* trigger: scheduled every 2 hours, with JQL
  * project = *myProjectName* AND statusCategory != Done
* if/else condition
  * test if the status has not changed in 2 hours with JQL
    * action: add label
* elseif condition
  * test with advanced compare condition: check if the last comment was added after the last 2h
    * action: add label


author: Amit Gandhi
body: Thanks, but seems my requirements do not get addressed with this logic. See below.

**User updated status at 12 noon**

Rule ran at 12 noon - no label added

**User added a comment at 1 pm**

**User added a comment at 2 pm**

Rule ran at 2 pm - label not added

Rule ran at 4 pm - label added as last status change was at 12 noon, but it should not have been as last comment is added at 2 pm

Rule ran at 6 pm - label added

++The above is due to status condition executing first. Suppose we remove status condition for time being. Then -++

**User added a comment at 6:30 pm**

Rule ran at 8 pm - label not added

**User added a comment at 9 pm**

Rule ran at 10 pm - label not added as it sees the last comment added at 9 pm but it missed the earlier part where comment added at 6:30 pm and then 9 pm comment was added after 2.5 hours

**User added a comment at 11:45 pm**

Rule ran at 12 night - label not added as it sees the last comment added at 11:45 pm but it missed the earlier part where comment added at 9 pm and then 11:45 pm comment was added after 2.75 hours


author: Bill Sheboy
body: What I provided was an example of the structure. You will need to look at all possible combinations to **decide what your want the outcome to be**, and adjust the rule accordingly, reducing the similar cases.

Off the top of my head, here are some scenarios to decide if a label should/should not be added. Assumption: the issue is not in a "done" status.

```
Status                       Comment                      Add label?
---------------------------  ---------------------------  ----------
Never changed after create   No comment ever added        ???
Changed > 2 hours ago        No comment ever added        ???
Changed = 2 hours ago        No comment ever added        ???
Changed < 2 hours ago        No comment ever added        ???

Never changed after create   Comment added > 2 hours ago  ???
Changed > 2 hours ago        Comment added > 2 hours ago  ???
Changed = 2 hours ago        Comment added > 2 hours ago  ???
Changed < 2 hours ago        Comment added > 2 hours ago  ???

Never changed after create   Comment added = 2 hours ago  ???
Changed > 2 hours ago        Comment added = 2 hours ago  ???
Changed = 2 hours ago        Comment added = 2 hours ago  ???
Changed < 2 hours ago        Comment added = 2 hours ago  ???

Never changed after create   Comment added < 2 hours ago  ???
Changed > 2 hours ago        Comment added < 2 hours ago  ???
Changed = 2 hours ago        Comment added < 2 hours ago  ???
Changed < 2 hours ago        Comment added < 2 hours ago  ???

```


author: Amit Gandhi
body: Thanks for helping on this again.

See below.

Wil check on the JIRA automation rule logic. Any further help would be appreciated to make it optimum and effective.

```
Status                       Comment                      Add label?
---------------------------  ---------------------------  ----------
Never changed after create   No comment ever added        YES
Changed > 2 hours ago        No comment ever added        YES
Changed = 2 hours ago        No comment ever added        NO
Changed < 2 hours ago        No comment ever added        NO

Never changed after create   Comment added > 2 hours ago  YES
Changed > 2 hours ago        Comment added > 2 hours ago  YES
Changed = 2 hours ago        Comment added > 2 hours ago  NO
Changed < 2 hours ago        Comment added > 2 hours ago  NO

Never changed after create   Comment added = 2 hours ago  NO
Changed > 2 hours ago        Comment added = 2 hours ago  NO
Changed = 2 hours ago        Comment added = 2 hours ago  NO
Changed < 2 hours ago        Comment added = 2 hours ago  NO

Never changed after create   Comment added < 2 hours ago  NO
Changed > 2 hours ago        Comment added < 2 hours ago  NO
Changed = 2 hours ago        Comment added < 2 hours ago  NO
Changed < 2 hours ago        Comment added < 2 hours ago  NO
```


author: Bill Sheboy
body: Thanks for the additional details, and **with the example rule I provided you should be able to create the rule**, with these adjustments:

* adjust the JQL for the schedule trigger to check for status value not changed in the last 2 hours
* and then use a couple of conditions to check for "no comments" or "last comment added \> 2 hours ago"

As a reminder, this is a community forum of Atlassian tool users voluntarily helping each other. This is not a free-staffing pool of labor to build solutions for a request. Please try implementing this rule yourself, test it, and if you run into problems the community can help when you show your rule and describe what does not work as you expect. Or you could contact your Jira Site Admin for support in creating a rule to solve the problem.


author: Amit Gandhi
body: Thanks.

Just wanted to check the requirements shared can be fulfilled with JIRA automation.

It seems it is not.


author: Bill Sheboy
body: Please re-read the responses above; this rule can be implemented as you have described.


author: Amit Gandhi
body: Let me reiterate/elaborate again.

Lets focus on requirements, not automation logic (I will take care of it).

Issue 1 has 10 comments in which user A belonging to user group B has added comments at position 2,3,5,7.

It should check for the difference in datetime for each of below and apply label if any of that is greater than 2 hours.

datetime of comment 3 creation - datetime of comment 2 creation

datetime of comment 5 creation - datetime of comment 3 creation

datetime of comment 7 creation - datetime of comment 5 creation


author: Bill Sheboy
body: Hi [@Amit Gandhi](/t5/user/viewprofilepage/user-id/2173609)

Thanks for the additional details on the use case. I do not believe that would be possible (or easily possible) to implement with out-of-the-box features, including automation rules. It would require more complex conditional and filtering logic than exists today.

More complex work-arounds would be to capture in custom fields when people in that user group add a comment, and then compare to that information with "now", such as with a JQL filter subscription. There may also be marketplace addons to perform this type of monitoring with such needs.

Kind regards,  
Bill