Unable to update newly linked issue - Jira/Scriptrunner - Epic/Issues In Epic - groovy

I have a piece of code. I am able to view all the values, but when it comes to updating the newly linked issue's Tempo "Account" field, nothing happens. I am using Scriptrunner For Jira's custom listener
I would expect the newly linked issue (issueInEpic) to have it's Tempo "Account" field (linkedField) value (oldCfValue) updated with the value (newCfValue) of the Epic's "Account" field value.
log.info() shows all of the values properly. The only issue is updating the linked issue.
import com.atlassian.jira.event.issue.link.IssueLinkDeletedEvent
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.util.IssueChangeHolder
def log = Logger.getLogger(com.atlassian.jira.event.issue.link.IssueLinkCreatedEvent)
log.setLevel(Level.INFO)
if ([IssueLinkCreatedEvent].contains(event.getClass())) {
if (event.issueLink.issueLinkType.name == "Epic-Story Link") {
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
def issueManager = ComponentAccessor.getIssueManager()
def cfManager = ComponentAccessor.getCustomFieldManager()
def accountField = cfManager.getCustomFieldObjects(event.issueLink.sourceObject)?.find{it.name == "Account"}
def linkedField = cfManager.getCustomFieldObjects(event.issueLink.destinationObject)?.find{it.name == "Account"}
MutableIssue issueInEpic = event.issueLink.destinationObject as MutableIssue
MutableIssue epic = event.issueLink.sourceObject as MutableIssue
def newCfValue = accountField.getValue(epic)
def oldCfValue = linkedField.getValue(issueInEpic)
issueInEpic.setCustomFieldValue(linkedField, newCfValue)
issueManager.updateIssue(currentUser, issueInEpic, EventDispatchOption.DO_NOT_DISPATCH, false)
def issueIndexingService = ComponentAccessor.getComponent(IssueIndexingService.class)
def wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
issueIndexingService.reIndex(event.issueLink.destinationObject)
ImportUtils.setIndexIssues(wasIndexing)
log.info(accountField)
log.info(newCfValue)
log.info(oldCfValue)
log.info(issueInEpic)
log.info(epic)
log.info(linkedField)
}
}

Related

How to pass variable from views to model in Django

Models.py
from django.db import models
class Voiceapi(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=200)
voice_text = models.CharField(max_length=200,default="voice_data")
Views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
from rest_framework import viewsets
import requests
import gdown
from pydub import AudioSegment
import speech_recognition as sr
from .serializers import *
from .models import *
import time
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
from urllib.request import urlopen
# create a viewset
class VoiceViewSet(viewsets.ModelViewSet):
# define queryset
queryset = Voiceapi.objects.all()
# specify serializer to be used
serializer_class = VoiceSerializer
#
print(Voiceapi.objects.values())
datas = Voiceapi.objects.values()
print(datas)
for i in datas:
try:
print("Audio File-->",i['name'])
audio_url = i['name']
audio_id = i['id']
def VoiceRecognizer(audio,audio_id):
r = sr.Recognizer()
with sr.AudioFile(audio) as source:
audio_text = r.listen(source)
try:
text = r.recognize_google(audio_text)
print(text)
Voiceapi.objects.filter(pk=audio_id).update(voice_text=text)
except:
print('Audio Not Clear')
audio = "/home/venpep/voicetotext/messages/media/test.wav"
VoiceRecognizer(audio,audio_id)
except:
print("Not audio file")
I need to pass the variable "text" from my view.py to models.py to set the default in voice_text.Is there any solution to get the text from the view page. The text variable is a basic string that needs to pass to the models.py
as I understood from your comment, you can override the save method in django model and put your logic, in this way you can edit field value before save it and send it in response
class Voiceapi(models.Model):
...
def save(self, *args, **kwargs):
...

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:

How to set default epic link in Jira using Groovy

I need to set epic link using groovy if a condition is true. I'm using the following script:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue;
def customFieldManager = ComponentAccessor.getCustomFieldManager()
IssueManager issueManager = ComponentAccessor.getOSGiComponentInstanceOfType(IssueManager.class)
def epiclink = CustomFieldManager.getCustomFieldObject("customfield_1000")
issue.setCustomFieldValue(epiclink,"MAR-1518")
But im getting this error:
I tried this code from this question but it didn't worked for me.
You just have a typo. In line
def epiclink = CustomFieldManager.getCustomFieldObject("customfield_1000")
you are calling the CustomFieldManager class (inital is uppercase)
However, you need to call your variable customFieldManager which is from ComponentAccessor.customFieldManager
So, the above line should be:
def epiclink = customFieldManager.getCustomFieldObject("customfield_1000")

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

How to set epic link in Jira using Groovy

I need to set epic link using groovy if a condition is true.
I'm using the following script:
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue;
def customFieldManager = ComponentAccessor.getCustomFieldManager()
IssueManager issueManager = ComponentAccessor.getOSGiComponentInstanceOfType(IssueManager.class)
def epiclink = CustomFieldManager.getCustomFieldObject("customfield_10101")
issue.setCustomFieldValue(epiclink,"TS-14")
If I set the epic link to null it works,
issue.setCustomFieldValue(epiclink, null)
but setting it to an issue key doesn't work.
Any help is appreciated.
I'm not sure why you aren't using ComponentAccessor.getIssueManager(), but that is a different question.
Epic Link is an Issue Object, not the Key
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue;
def customFieldManager = ComponentAccessor.getCustomFieldManager()
IssueManager issueManager = ComponentAccessor.getOSGiComponentInstanceOfType(IssueManager.class)
def epiclink = CustomFieldManager.getCustomFieldObject("customfield_10101")
issue.setCustomFieldValue(epiclink,IssueManager.getIssueObject("TS-14"))

Resources