It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How to restrict logging work log for previous month

Hi,

Can we restrict the user from logging work for previous month. As per the scenario, I am trying to disable the time spent field in the Log Work Screen, if the Date Started is having a value of previous month. I tried behavior but in vain. Here is my script that I am trying to write using a listener:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.issue.worklog.*
import com.atlassian.jira.issue.MutableIssue

CustomField timespentField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Time Spent")
CustomField startDateField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Date Started")
def today=new Date().format("yyyy-MM-dd")
//def issue = event.issue as Issue
MutableIssue issue = event.issue as MutableIssue
//def startDateVal = issue.getCustomFieldValue(startDateField) as Date
log.error(issue.getCustomFieldValue(startDateField))
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def change = event?.getChangeLog()?.getRelated("ChildChangeItem").find {it.field == "Date Started"}
log.error("change is "+ change)
if(change){
    def startDateVal = issue.getCustomFieldValue(startDateField) as Date
    log.error("start date is "+ startDateVal)
    def month=startDateVal.format("MM")
    def year=startDateVal.format("yyyy")
    Calendar cacheCalendar = Calendar.instance
    cacheCalendar.set(Calendar.MONTH, (Integer.parseInt(month)+1));
    cacheCalendar.set(Calendar.YEAR, Integer.parseInt(year));
    def cutoffDate=cacheCalendar.time.format("yyyy-MM-05")
    def monthStart=startDateVal.format("yyyy-MM-01")
    log.error("Month Start:"+monthStart+"....cutOffDate:"+cutoffDate)
    if(monthStart<=today && today<cutoffDate){
              timespentField.hide();
        issue.timespentField.setVisible(false)
    }
    else {
       getFieldById("log-work-time-logged").setHidden(false)
       timespentField.setRequired(false)
    }
}

Any help will be highly appreciated.

2 answers

1 accepted

0 votes
Answer accepted

Hi,

I was able to resolve the problem with script listener.

Logic: Whenever work is logged with a date older than 30 days, we update the worklog date to current date.

Script Listener Events: Issue Worklog Updated, Work Logged on Issue

Script:


import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import java.lang.Object
import com.atlassian.jira.issue.worklog.WorklogImpl2
import java.text.SimpleDateFormat
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.worklog.DefaultWorklogManager
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.issue.worklog.Worklog
import com.atlassian.jira.datetime.LocalDate
import com.atlassian.jira.issue.worklog.*

def issue = event.issue as Issue
WorklogManager worklogManager = ComponentAccessor.getWorklogManager()
List worklogs = worklogManager.getByIssue(issue)
def last_worklog=worklogs.last()
def author = last_worklog.getAuthorKey()
def dateformat= new SimpleDateFormat("yyyy/MM/dd");
Date date = new Date();
def componentManager = ComponentManager.getInstance()
ProjectRoleManager projectRoleManager = ComponentManager.getComponentInstanceOfType(ProjectRoleManager.class) as ProjectRoleManager
def UsersRole = projectRoleManager.getProjectRole("Users")
long timespent = 1
def last_logged=last_worklog.getTimeSpent()
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -30);
Date todate1 = cal.getTime();    
def date_limit = dateformat.format(todate1);
def last_worklog_date=dateformat.format(last_worklog.getStartDate())
log.error(dateformat.parse(last_worklog_date).before(dateformat.parse(date_limit)))
if(last_worklog.getTimeSpent()>1 && dateformat.parse(last_worklog_date).before(dateformat.parse(date_limit))){
String comment=last_logged+'sec logged by '+author+' on date '+last_worklog_date
def worklog = new WorklogImpl2(issue, last_worklog.getId(), author, comment, new Date(), null, null,last_logged, UsersRole)
    worklogManager.update(issue.reporter, worklog, 0L, true)  
}

 

Thanks,

Sadaf Jabin

Hi How can restrict by Future Date to team log the work?

HI Prasanth,

Could you please elaborate the problem statement.

Thanks,
Sadaf Jabin

My team members are logging work hours for Future date how can we restrict this.

hi Prasanth,

Kindly remove the below line of code from the script:

Calendar cal = Calendar.getInstance();
//cal.add(Calendar.DATE, -30); ---> this makes the limit as 30 days prior to the current date. Commenting it out would make the restriction limit as the current date.
Date todate1 = cal.getTime();  

Like Ruslan PM likes this

Hi @Sadaf Jabin ,

 

I am able to add future dates even I added the script below:

import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import java.lang.Object
import com.atlassian.jira.issue.worklog.WorklogImpl2
import java.text.SimpleDateFormat
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.worklog.DefaultWorklogManager
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.issue.worklog.Worklog
import com.atlassian.jira.datetime.LocalDate
import com.atlassian.jira.issue.worklog.*
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.user.ApplicationUser

def issueManager = ComponentAccessor.getIssueManager()
def user= ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
MutableIssue issue = event.issue as MutableIssue

WorklogManager worklogManager = ComponentAccessor.getWorklogManager()
List worklogs = worklogManager.getByIssue(issue)
def last_worklog=worklogs.last()
def author = last_worklog.getAuthorKey()
def dateformat= new SimpleDateFormat("yyyy/MM/dd");
Date date = new Date();
//IssueManager issueManager = ComponentAccessor.getIssueManager();
//def projectRoleManager = ComponentAccessor.getGroupManager()
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
//ProjectRoleManager projectRoleManager = issueManager.getComponentInstanceOfType(ProjectRoleManager.class) as ProjectRoleManager
def UsersRole = projectRoleManager.getProjectRole("Users")
long timespent = 1
def last_logged=last_worklog.getTimeSpent()
Calendar cal = Calendar.getInstance();
//cal.add(Calendar.DATE, -30);
Date todate1 = cal.getTime();
def date_limit = dateformat.format(todate1);
def last_worklog_date=dateformat.format(last_worklog.getStartDate())
log.error(dateformat.parse(last_worklog_date).before(dateformat.parse(date_limit)))
if(last_worklog.getTimeSpent()>1 && dateformat.parse(last_worklog_date).before(dateformat.parse(date_limit))){
String comment=last_logged+'sec logged by '+author+' on date '+last_worklog_date
def worklog = new WorklogImpl2(issue, last_worklog.getId(), author, comment, new Date(), null, null,last_logged, UsersRole)
worklogManager.update(issue.reporter, worklog, 0L, true)
}

 

Thanks,

Swarna

@Swarna Radha  Please provide the logs from the listener after entering a future date. Also request you to share the screenshot of the worklog tab for the issue where the efforts are being logged.

Hi @Sadaf Jabin ,

 

Please find attached screenshots of worklog tab and log of listener.

Log Time 2.pngLog time.pngLogs in Listener.png

HI @Swarna Radha ,

The code works fine for me. I have added few logs in the code, please update and share the latest log.

def issueManager = ComponentAccessor.getIssueManager()
def user= ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
MutableIssue issue = event.issue as MutableIssue

WorklogManager worklogManager = ComponentAccessor.getWorklogManager()
List worklogs = worklogManager.getByIssue(issue)
def last_worklog=worklogs.last()
def author = last_worklog.getAuthorKey()
def dateformat= new SimpleDateFormat("yyyy/MM/dd");
Date date = new Date();
//IssueManager issueManager = ComponentAccessor.getIssueManager();
//def projectRoleManager = ComponentAccessor.getGroupManager()
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
//ProjectRoleManager projectRoleManager = issueManager.getComponentInstanceOfType(ProjectRoleManager.class) as ProjectRoleManager
def UsersRole = projectRoleManager.getProjectRole("Users")
long timespent = 1
def last_logged=last_worklog.getTimeSpent()
Calendar cal = Calendar.getInstance();
//cal.add(Calendar.DATE, -30);
Date todate1 = cal.getTime();
def date_limit = dateformat.format(todate1);
def last_worklog_date=dateformat.format(last_worklog.getStartDate())
log.error(dateformat.parse(date_limit).before(dateformat.parse(last_worklog_date)))
log.error(date_limit)
log.error(last_worklog_date)
if(last_worklog.getTimeSpent()>1 && dateformat.parse(date_limit).before(dateformat.parse(last_worklog_date))){
String comment=last_logged+'sec logged by '+author+' on date '+last_worklog_date
log.error(comment)
def worklog = new WorklogImpl2(issue, last_worklog.getId(), author, comment, new Date(), null, null,last_logged, UsersRole)
worklogManager.update(issue.reporter, worklog, 0L, true)
}

Hi Sadaf,

did you considered to use some plugin? I think the Tempo Timesheets plugin should be able to disable logging of work for previous month.

Best regards,

Ondřej

Hi Ondřej,

Thanks for your response.

I am actually looking for a solution without having to involve additional plugins. It would be great if you could suggest in that line.

Thanks,

Sadaf Jabin

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

303 views 1 3
Join discussion

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you