Copy date from one field to another - groovy

I am trying to copy the value of one date field to another. But, the field is not getting set. When I hardcode the date value it works.
Original field is Scheduled Start Time and the field I am trying to copy the value to is: Planned Scheduled Start Time. These are both date type fields.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.config.properties.APKeys
import com.atlassian.jira.datetime.DateTimeFormatter as JiraDateTimeFormatter
import com.atlassian.jira.issue.customfields.impl.DateTimeCFType
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZoneId
import java.time.temporal.TemporalAccessor
import java.time.format.DateTimeFormatter
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.user.util.UserUtil
import java.util.Date.*
#BaseScript FieldBehaviours fieldBehaviours
// The date & date time field names to set a value
final plannedScheduledStart = "Planned Scheduled Start Time"
final plannedScheduledCompletion = "Planned Scheduled Completion Time"
final scheduledStart = "Scheduled Start Time"
final scheduledCompletion = "Scheduled Completion Time"
// Get the components
def customFieldManager = ComponentAccessor.customFieldManager
def authenticationContext = ComponentAccessor.jiraAuthenticationContext
// Get the date & date time field objects
//def dateFieldStart = customFieldManager.getCustomFieldObjectsByName(plannedScheduledStart).first()
def dateFieldStart = getFieldByName("Planned Scheduled Start Time")
def dateFieldCompletion = customFieldManager.getCustomFieldObjectsByName(plannedScheduledCompletion).first()
def dateScheduledStart = customFieldManager.getCustomFieldObjectsByName(scheduledStart).first()
def dateScheduledCompletion = customFieldManager.getCustomFieldObjectsByName(scheduledCompletion).first()
def dataValue = dateFieldStart.getValue().toString()
getFieldByName("Scheduled Start Time").setFormValue(dataValue)
I also tried def dataValue = dateFieldStart.getValue() and it failed

Related

How to retrieve the value of the summary system field using the postfunction of a workflow transition in scriptrunner

I am trying to retrieve the value of the Summary system field in Jira using ScriptRunner. I am using the following code in scriptrunner but the problem is that the variable cf returned by the line def cf = customFieldManager.getCustomFieldObject("Summary") is null. How can I fix this and retrieve the value of the summary field in ScriptRunner?
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import org.apache.log4j.Logger
def log = Logger.getLogger("atlassian-jira.log")
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def cf = customFieldManager.getCustomFieldObject("Summary")
log.warn("MOUNA 1: "+cf)
issue.setCustomFieldValue(cf, "mouna")
log.warn("MOUNA 2: "+issue)
"Summary" field in Jira is not a custom field.
You can access the Summary field (and other system fields) directly from issue:
log.warn(issue.summary)
But for updating it in Post Function, you need to use MutableIssue class:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.event.type.EventDispatchOption
def mIssue = (MutableIssue) issue
mIssue.setSummary("New Summary")
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() // Or you can get any user by using UserManager
ComponentAccessor.getIssueManager().updateIssue(user, mIssue, EventDispatchOption.ISSUE_UPDATED, false)
Of course, don't forget to import required classes at beginning of your code:

Block Jira opening ticket if custom field value is less than 1 using Groovy

i need to block the creation of an issue on Jira if a custom field value is less than 1.
I tried with this code with no success, the custom field ID is: 13200.
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.opensymphony.workflow.InvalidInputException;
import org.apache.log4j.Category;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import groovy.xml.MarkupBuilder;
import com.atlassian.jira.config.properties.APKeys;
def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject("customfield_13200")
def cFieldValue = issue.getCustomFieldValue(cField) as Integer
if (cFieldValue > 0){
log.debug("ERRORE, QUANTITA' NON DISPONIBILI");
throw "ERRORE";
throw new InvalidInputException("QUANTITA' NON DISPONIBILI");
return cFieldValue;
return false;
}
That custom field is taken from another DB (linked via Elements) and it's the quantity of a particular item, if the quantity is less than 0 the ticket should throw an exception but it doesnt work as i would, giving me this error in the LOG:
For input string: "{"keys":["47"]}"
java.lang.NumberFormatException: For input string: "{"keys":["47"]}"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.valueOf(Integer.java:766)
The taken value is right (47) but maybe it's in a wrong type?
Can anybody help me?
Thank you,
Luca
Your variable content is "{"keys":["47"]}" , not "47". You can see this here: java.lang.NumberFormatException: For input string: "{"keys":["47"]}"
that's why you get this exception, as "{"keys":["47"]}" cannot be converted to a number, so please double-check the value of the variable you are converting to a number
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.InvalidInputException
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def Scorte = customFieldManager.getCustomFieldObject("customfield_13200")
def ValoreScorte = issueObject.getCustomFieldValue(Scorte)
def QuantitaRichiesta = customFieldManager.getCustomFieldObject("customfield_13201")
def ValoreQuantitaRichiesta = issueObject.getCustomFieldValue(QuantitaRichiesta)
def casting_ValoreScorte = ValoreScorte.toString().replaceAll("[^\\d.]", "")
def Scorte_Intero = casting_ValoreScorte as int
def QuantitaRichiesta_Intero = ValoreQuantitaRichiesta as int
if (Scorte_Intero <= 0 || QuantitaRichiesta_Intero > Scorte_Intero){
log.debug("ERRORE, QUANTITA' NON DISPONIBILI")
throw new InvalidInputException("","QUANTITA' NON DISPONIBILI")
}
This is the final code :) Thank you for your help!

extract multiple URLs using the datetime function

In this program i am not using request or beautiful soup function. I'm instead only using the datetime to extract the URLs. Now in the current program, I have written to extract the values for a long period. I want to make it in such a way that, if I automate this program and it runs today, it will extract yesterday's data. Similarly if it runs tomorrow, it will extract todays data and so on.
here is the code,
import datetime
from datetime import date, datetime,timedelta
import warnings
import datetime
import pandas as pd
import wget
import glob
import os
warnings.filterwarnings("ignore")
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
from urllib.error import HTTPError
def date_range(start_date,end_date):
for n in range(int((end_date-start_date).days)):
yield start_date + timedelta(n)
def get_urls(base_url):
part_two = "/dailyCoal1-"
end_part = ".xlsx"
start_date = date(2020,11,1)
end_date = datetime.datetime.now().date()
start_urls = list()
for single_date in date_range(start_date, end_date):
start_urls.append(single_date.strftime(base_url+'%d-%m-%Y'+part_two+'%Y-%m-%d'+end_part))
return start_urls
def excel_download(link,out):
#downloads a given link provided to a output directory in out
wget.download(link,out)
if __name__ =="__main__":
base_url = "https://npp.gov.in/public-reports/cea/daily/fuel/"
mypath = "/Users/vp/Desktop/temp"
temp_folder = '/Users/vp/Desktop/temp'
out_folder = "/Users/vp/Desktop/NPP"
log_file = os.path.join(out_folder,'debug_log_npp.log')
out_file = os.path.join(out_folder,'Energy_inputs_npp.csv')
file_links = get_urls(base_url)
for link in file_links:
try:
excel_download(link,temp_folder)
except HTTPError:
content = "HTTP issue while capturing data for this link - " + link
log_writer(log_file,content)
continue
file = glob.glob(os.path.join(temp_folder,'*.xlsx'),recursive=True)[0]
df = pd.read_excel(file)
To capture yesterday's data, i created this in the main function where i check for yesterday = and then cancel if it isnt yesterday. But then its throwing error as it constantly picks the start date as its day one.
if(date_time_obj != Yesterday):
os.remove(file)
content = "Date mis-matched - " + str(date_time_obj) + " " + str(Yesterday)
In this program, date_time_obj - is the date it is currently trying to extract data for.
Everyday if this program runs at 8pm, it needs to only capture one day before data on a daily basis.
if this cannot be done in datetime, but only on request or bs4, then how do i approach this problem?
I don't know if you wanted a valid link as your code doesn't seem to produce those for me but you only need to tweak to work off start_date only and return a single item to return yesterday's link matching with your current output for same date.
import datetime
from datetime import date, datetime,timedelta
import warnings
import datetime
import pandas as pd
import glob
import os
warnings.filterwarnings("ignore")
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
from urllib.error import HTTPError
def get_url(base_url):
part_two = "/dailyCoal1-"
end_part = ".xlsx"
start_date = datetime.datetime.now().date() + timedelta(-1)
start_url = start_date.strftime(base_url+'%d-%m-%Y'+part_two+'%Y-%m-%d'+end_part)
return start_url
def excel_download(link,out):
#downloads a given link provided to a output directory in out
wget.download(link,out)
if __name__ =="__main__":
base_url = "https://npp.gov.in/public-reports/cea/daily/fuel/"
mypath = "/Users/vp/Desktop/temp"
temp_folder = '/Users/vp/Desktop/temp'
out_folder = "/Users/vp/Desktop/NPP"
log_file = os.path.join(out_folder,'debug_log_npp.log')
out_file = os.path.join(out_folder,'Energy_inputs_npp.csv')
file_link = get_url(base_url)
print(file_link)

Trouble commiting DueDate after setting it with groovy/script runner [JIRA]

Currently, we have routine issues where the status resets each week. This escalation service is working as intended. However, we are trying to incorporate the following script to adjust the system field 'DueDate' to be set for 7 days from the date when the escalation fires. The following code updates the date and returns the correct value but it doesn't appear to be committing the date of the provided ISSUEKEY.
Here is our code:
import java.util.Date
import java.sql.Timestamp
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
IssueManager im=ComponentAccessor.getIssueManager()
MutableIssue issue = im.getIssueObject('ISSUEKEY')
issue.setDueDate(new Timestamp((new Date() + 7).time))
issue.getDueDate()
What you are doing is correct but you need to commit your changes by calling IssueManager.updateIssue
import com.atlassian.jira.event.type.EventDispatchOption //add-this-line
import java.util.Date
import java.sql.Timestamp
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
IssueManager im=ComponentAccessor.getIssueManager()
MutableIssue issue = im.getIssueObject('ISSUEKEY')
issue.setDueDate(new Timestamp((new Date() + 7).time))
issue.getDueDate()
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()//add-this-line
im.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH , false) //add-this-line
You are setting the issue value correctly but you are not saving your changes
IssueManager.updateIssue will save any changes

Calculate age in Groovy from today

Trying to figure out how old someone is based on their bday. The following code gives us the number of days
def now = new Date()
def dob = new Date('08/21/1982')
println now - dob
We could divide by 365 but that isn't entirely accurate. There has to be an easy way to do this but I just can't seem to find it. Any ideas?
Or using Java 8 and Groovy 2.3 RC (if you want to be on the bleeding edge) ;-)
import java.time.*
LocalDate today = LocalDate.now()
LocalDate birthday = LocalDate.of( 1982, Month.AUGUST, 8 )
Period period = Period.between( birthday, today )
println """$period.years years,
|$period.months months,
|$period.days days""".stripMargin()
Here is a sleek alternative using joda time :)
#Grab('joda-time:joda-time:2.3')
import org.joda.time.Period
import org.joda.time.DateTime
def now = new DateTime()
def dob = new DateTime(1982, 8, 21, 0, 0)
def period = new Period(dob, now)
println """$period.years years,
|$period.months months,
|$period.days days,
|$period.hours hours,
|$period.minutes minutes
""".stripMargin()
For me joda was not available so I searched another, more groovy solution and found:
def duration = groovy.time.TimeCategory.minus(
new Date(),//date1
new Date(session.creationTime)//date2
);
println "seconds: " + dur2.seconds;
println "min: " + dur2.minutes;
println "hours: " + dur2.hours;
import com.sap.it.api.mapping.*;
import java.util.HashMap;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
import java.util.TimeZone;
import groovy.time.TimeCategory;
import groovy.time.TimeDuration;
def String customFunc(String DOB){
if(DOB == '')
{
DOB = '';
return DOB;
}
else
{
Date birth_yr=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").parse(DOB);
Date date = new Date();
def duration = groovy.time.TimeCategory.minus(date,birth_yr);
def age = (duration.days).intdiv(365)
return age
}
}

Resources