can we use rest api in Atlassian webitem

Sarika January 10, 2020

I have Created a addon button in the view issue  screen using Webitemn ,Now on clicking this Button the issue must create as a Jira project, Using the rest api,kindly answer in this case

1 answer

1 accepted

0 votes
Answer accepted
DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 10, 2020

@SarikaWelcome to the community!

You can use Javascript and call Jira's Rest API using 'web-resource-module' by adding it to view-issue page context, and listing to click event of your button.

You can learn about Web Resource Module here - https://developer.atlassian.com/server/jira/platform/web-resource/

Sarika January 10, 2020

Thank you DPK J

Sarika January 10, 2020

This is my web resource and webitem

<web-resource key="JiraPluginbutton-resources" name="JiraPluginbutton Web Resources">
<dependency>com.atlassian.auiplugin:ajs</dependency>

<resource type="download" name="JiraPluginbutton.css" location="/css/JiraPluginbutton.css"/>

<resource type="download" name="JiraPluginbutton.js" location="/js/JiraPluginbutton.js"/>

<resource type="download" name="images/" location="/images"/>

<context>atl.general</context>

<context>atl.admin</context>


<context>jira.view.issue</context>


<context>jira.userprofile</context>

 

</web-resource>

 


<web-item name="JiraProject" i18n-name-key="jira-project.name" key="jira-project" section="operations-top-level" weight="1">
<description key="jira-project.description">The JiraProject Plugin</description>
<label key="jira-project.label"></label>
<link linkId="jira-project-link"/>
</web-item>
</atlassian-plugin>

This is my Java Script file

JiraPluginbutton.js

AJS.$(document).ready(function() {
AJS.toInit(function() {
AJS.$('#jira-project-link').click(function(e) {
e.preventDefault();
alert("A new jira project gets created ")
});
});
});

 

As i am very new to Atlassian SDk, here where i need to call rest api 

inside the javaScript

and where to set the location  view-issue page context

DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 10, 2020

@SarikaYou are on right track,

Simply call API using various functions provided by '$' i.e. JQuery. For example,

AJS.toInit(function () {
var API_BASE_URL = contextPath + "rest/api/2";

AJS.$("#integration-button-link").click(function(eve) {
eve.preventDefault();

//Get projects list
AJS.$.getJSON(API_BASE_URL + "/project", function(data) {
console.log(data);
});

//Create new project
var data = JSON.stringify({
"key": "SAM",
"name": "Sample Project"
// Add more params as defined in API reference
});
AJS.$.ajax({
url: API_BASE_URL + "/project",
type: 'POST',
dataType: 'json',
contentType: 'application/json',
data: data,
success: function (response) {
console.log(response);
}
});

return false;
});
});

 

API reference for creating project - https://docs.atlassian.com/software/jira/docs/api/REST/8.5.3/#api/2/project-createProject

Like Sarika likes this
Sarika January 12, 2020

Thank you so much DPK J, for your wonderful suggestions

Sarika January 12, 2020

The context path need to be given is http://localhost:2990/jira isin't  it?

Sarika January 12, 2020

AJS.toInit(function () {
var API_BASE_URL = http://localhost:2990/jira + "rest/api/2";

AJS.$("#jira-project-link").click(function(eve) {
eve.preventDefault();

//Get projects list
AJS.$.getJSON(API_BASE_URL + "/project/13502", function(data) {
console.log(data);
});

//Create new project
var data = JSON.stringify({
"id":"1102",
"key": "SAM",
"name": "Sample Project"
// Add more params as defined in API reference
});
AJS.$.ajax({
url: API_BASE_URL + "/project",
type: 'POST',
dataType: 'json',
contentType: 'application/json',
data: data,
success: function (response) {
console.log(response);
}
});

return false;
});
});

 

 

I get no error but the Project doesnot created ,Can you please help in this case

DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 12, 2020

Please add,

'failure': function (response) {

  console.log(response);

}

to your ajax request. To see what is missing.

Like Sarika likes this

Suggest an answer

Log in or Sign up to answer