need groovy script to restrict users from logging the hours on past days, please help

need groovy script to restrict users from logging the hours on past days, please help

2 answers

This widget could not be displayed.

Hi Chirannjevi,

If you have ScriptRunner for JIRA installed you could use a Script Listener to remove work logs added which log time on previous days. It is not possible to prevent them from being logged in the first place though.

A sample script to do this, for use with the Worklog Created event, is:

import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.time.ZoneOffset
import java.time.ZoneId

def loggedDateTime = worklog.started
def formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
ZonedDateTime loggedZoned = ZonedDateTime.parse(loggedDateTime, formatter);
ZonedDateTime nowZoned = ZonedDateTime.now(loggedZoned.getZone())
def startOfTodayZoned = nowZoned.withHour(0).withMinute(0).withSecond(0).withNano(0)


logger.info("Work logged at ${nowZoned.toString()} for ${loggedZoned.toString()}")


if (loggedZoned.isBefore(startOfTodayZoned)) {
    logger.info("Time was logged before start of today, deleting worklog")
    def deleteResp = delete("/rest/api/2/issue/${worklog.issueId}/worklog/${worklog.id}")
        .asObject(Map)
    assert deleteResp.status == 204
    
    def notifyResp = post("/rest/api/2/issue/${worklog.issueId}/notify")
        .header("Content-Type", "application/json")
        .body([
            subject: "Worklogs cannot be made on past days",
            textBody: "Worklogs cannot be made on past days, your worklog has been deleted",
            htmlBody: "<p>Worklogs cannot be made on past days, your worklog has been deleted</p>",
            to: [
                reporter: false,
                assignee: false,
                voters: false,
                watchers: false,
                users: [
                    [name: worklog.author.name]
                ]
            ]
        ])
        .asString()
} else {
    logger.info("Time was logged after start of today")
}

Thanks,

Jon

This widget could not be displayed.

Thank you jon,

 

should we have script runner add-on installed in JIRA to use this script? please suggest me

Yes, you'll need ScriptRunner for JIRA in order to use this script. 

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Aug 06, 2018 in Jira Service Desk

A is for Activate: Share your top Jira Service Desk onboarding tips for new users!

Hi, everyone! Molly here from the Jira Service Desk Product Marketing Team :).  In the spirit of this month's  august-challenge, we're sourcing stories of Jira Service Desk activation fro...

575 views 25 15
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you