Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Update Release description via script\Rest Endpoint

Alexander July 16, 2021

We have a custom button that we added via Script Fragments to the Release page.

We want to update a Release description field via this button.

How we can do it? Using some script\Rest endpoint or smth else?

Could you, please, advise.

Thanks.

2 answers

0 votes
Alexander August 12, 2021

I've solved this. If anybody is interested

def panelOutput = $/
<script>
function getVersionID()
{
var versionId = AJS.$('#release-report-tabs-section').attr('data-version-id');
var user;
AJS.$.ajax({
url: "/rest/gadget/1.0/currentUser",
type: 'get',
dataType: 'json',
async: false,
success: function(data) {
user = data.fullName;
}
});
var datos = {"description":"Release ID" + " " + versionId + " " + "was sent to SD by" + " " + user};

var parameters = JSON.stringify(datos);
AJS.$.ajax({
url: "/rest/api/2/version/" + versionId,
type: "PUT",
contentType: 'application/json',
data: parameters,
dataType: 'json',
async: false,
processData: false,
success: function(data, status){
var myFlag = AJS.flag({
type: 'success',
body: "Release ID" + " " + versionId + " " + "was sent to SD by" + " " + user,
});
},
error: function(error){
alert(JSON.stringify(error));
}
});
}
</script>
<a class="aui-button aui-button-primary" href="javascript&colon;getVersionID()">CUSTOMBUTTON</a>&nbsp;
/$
writer.write(panelOutput)

0 votes
PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 16, 2021

Yes, normally, your web item button should have a "link" configuration.

This will be the URL that will trigger some action.

On an issue, you could use "run code and display a flag" ... but the framework to display that flag will not be available in the context of the release page.

But the question is ... how are you selecting which release to update the description for? Or did you add the web item to "atl.jira.version.admin.operations" ?

If so you will need to somehow wire some javascript yourself against that button to call a rest api so that you can identify which release item the user clicked and pass that to the rest call.. 

Alexander July 18, 2021

Hello.

I've added a Web panel into

12344.jpg

And It looks like this

1234.jpg

How I can add a script to this button? Also, maybe you have an example of such script which will update\change a Description field for Release?

PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 19, 2021

If you've added a panel, presumably, you wrote some HTML code to display the button.

Just write some javascript that responds to the button click event.

PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 19, 2021

Here is an example ... you'll probably want to grab the version id for the selected release. Then make an ajax call to some rest API that you'll have to create and possibly do something the API response:

def panelOutput = $/
<script>
function yourJsFunction(){
var versionId = AJS.$('#release-report-tabs-section').attr('data-version-id')
var payload = {"versionId": versionId}
AJS.$.ajax({
type: "POST",
url: "/rest/scriptrunner/latest/custom/yourRestAPI",
headers: { 'X-Atlassian-Token': 'no-check' },
data: JSON.stringify(payload),
success: function(data, status){
console.log(data);
require(['aui/flag'], function(flag) {
flag({
type: "success" ,
title: "Default description",
close: "auto",
body: "Default description added to current release"
});
});
},
error: function(data) {
console.log(data.responseText);
require(['aui/flag'], function(flag) {
flag({
type: "error" ,
title: "Error setting default description for release",
close: "manual",
body: data.responseText
});
});
}
})
}
</script>
<a class="aui-button aui-button-primary" href="javascript&colon;yourJsFunction()">CAB</a>&nbsp;
/$
writer.write(panelOutput)
Alexander July 20, 2021

Hello. Thank you for your response. Am I right that I need to create a REST Endpoint that will update a Description field?

url: "/rest/scriptrunner/latest/custom/yourRestAPI",
PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 20, 2021

Yes, unless there is a built-in REST Api you can use, any change you want to do permanently has to be triggered somehow on the server and that's generally done with a custom rest api.

In this case, maybe the built-in api will work for you: https://docs.atlassian.com/software/jira/docs/api/REST/8.13.9/#api/2/version-updateVersion

Alexander August 9, 2021

Hello. To be honest, I'm confused. Don't know how I can use the info from the provided link.

 Could you, please, help?

PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 9, 2021

You have some java script code already that can capture a user's action.

Javascript is a powerful development language. 

Write some java script code that calls the JIRA API described in the link to make the desired change to the version.

Alexander August 12, 2021

I've solved it by the script above. But maybe you know how I can disable or hide this button after clicking for everyone? So we need to do only one click on this button

Alexander August 19, 2021

Need to disable or hide this button after clicking for everyone?

PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 19, 2021

In the scriptrunner condition for your panel, you can include something like this:

!version.description.contains('was sent to SD by')

Then, you might want to add something in your javascript to hide the button after success.  

And while you are at it, you could either force a page refresh to show the updated description or inject it with your javascript.

Something like:

var desc = "Release ID" + " " + versionId + " " + "was sent to SD by" + " " + user;
...
AJS.$('#cab-button').hide()
AJS.$('.top-level-version-info').after('<div id="description-text-content" class="version-description">' + desc + '</div>')

But you'll need to give your button an ID so you can find it easily

Suggest an answer

Log in or Sign up to answer