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

can we use rest api in Atlassian webitem Edited

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
DPK J Community Leader Jan 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/

Thank you DPK J

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

DPK J Community Leader Jan 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

Thank you so much DPK J, for your wonderful suggestions

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

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

DPK J Community Leader Jan 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
TAGS

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