How to pull data from JIRA using JS? - javascript

I want to pull the Data from my JIRA account using JS, as I want to automate a thing i.e. Time Tracking.
It will track the time whether the existing resources have sufficient time/bandwidth to deliver the upcoming JIRA Tickets or not.
Kindly Help!!

You should use the REST-API of JIRA in combination with angular http client. This is one of the first tutorials you already should have done with angular.
Edit Because of Comments:
JIRA REST API and JQL should work. it should look like this:
$http({
method: "GET",
url: 'http://my-url/rest/api/2/search?jql=' +
'reporter=reporterXY' +
'&project=projectXY'
}).then(function successCallback(response) {
return response.data;
}, function errorCallback() {
console.log("Error calling API")
});

Related

Making a GET request that uses POST method - Javascript to C# API

I have a situation where I'm required to encrypt data in flight from a JavaScript based application (Chrome Extension popup.html) to an API hosted in a .NET MVC website(C#).
I can run the request as a GET but project requirements make it necessary to encrypt the parameters I'm sending. For example if I was searching for all people named 'John' I need to encrypt 'John' in flight.
My JavaScript application is using jsencrypt and a RSA (public) key provided by the website in other APIs that work quite well to return counts.
If my GET version of the link looks like this:
<span class="person" id="personList">Person: '+ query +'</span>
On the .NET website end, this request is processed into a view that generates a table using a database call. As you can see in my link the results are populated onto a new tab in the browser.
How would you recommend changing it to calling a POST method and getting the same result as a GET?
EDIT
I tried to employ an AJAX function to make the POST but it doesn't like the data element:
event.preventDefault();
$.ajax({
url: repurl + 'Persons/Search',
timeout:30000,
type: "POST",
data: {body: encryptedmsg},
success : function(msg) {
if (msg.ws_resultat.result_ok == true)
{
alert('ok');
}
else
{
alert('some thing went wrong, plz try again');
}
}
});
});```

Confluence REST API request while not being admin ends in 401 error

I am developing confluence blueprint where a user can choose between jira projects and use them for specific jira issues report.
Both instances are connected correctly with each other and I get results but only if I am logged as an admin. With normal user I am getting this:
<status>
<status-code>401</status-code>
<message>This resource requires WebSudo.</message>
</status>
Unfortunately I have to get the information from the jira server as AJAX post request with JavaScript and here is my code:
function pickDate(e, state) {
AJS.$('#spLebenStart').datePicker({
overrideBrowserDefault: true
});
getJiraUrl();
}
function getJiraUrl(){
var appUrl = AJS.contextPath() + "/rest/applinks/1.0/applicationlink/type/jira";
$.ajax({
type: 'GET',
url: appUrl,
data: {
key: "value"
},
dataType: "xml",
success: function (xml){
jiraID = $(xml).find("id").text();
},
complete: function(){
getJiraProjects(jiraID);
},
error: function() {
alert("ERROR # getJiraUrl");
}
});
}
function getJiraProjects(applicationId){
var restUrl = AJS.contextPath() + "/rest/applinks/1.0/entities/"+applicationId+"?os_authType=any";
$.ajax({
type: 'GET',
url: restUrl,
data: {
key: "value"
},
dataType: "xml",
success: function (xml){
jiraProjectKeys = [];
$(xml).find("entity").each(function(){
jiraProjectKeys.push({id: $(this).attr("key"), text: $(this).attr("name")});
});
},
crossDomain: true,
xhrFields: {
withCredentials: true
},
error: function() {
alert("ERROR # getJiraProjects");
},
complete: function(){
AJS.$('#spSelect').auiSelect2({
placeholder: 'Projekt auswählen...',
data:jiraProjectKeys,
multiple: false
});
}
});
}
I have tried to use login information with basic authentication in AJAX but it didn't help. Of course I can hardcode the id in the code but what if it get changed? Its not the best solution imo. How can I manage the websudo problem?
I'm new here (as a contributor) so pardon my newbie bloopers.
Looks like accessing /rest/applinks/1.0/applicationlink/type/jira indeed requires admin permissions. But there's an undocumented (AFAIK) workaround and this is how I do it.
There's an Atlassian plugin called Confluence JIRA Plugin. It's bundled with Confluence (hence it should be available in your installation). It provides you with a few cool features allowing JIRA integration (e.g. JIRA and JIRA Chart macros). To provide the integration it also adds a few useful endpoints to your Confluence REST API (which don't require admin access):
/rest/jiraanywhere/1.0/servers or /rest/jira-integration/1.0/servers to list the linked JIRA servers (inlcuding applink id)
/jira-integration/1.0/servers/{INSERT APPLINK ID HERE}/projects to list JIRA projects available to the logged-in user
Now, per your requirements, I'd hit 1. to get the applink id and then 2. to get the list of the projects. Hope it works with your product versions.
BONUS - JIRA Proxy
Another nice endpoint is /plugins/servlet/applinks/proxy. It allows forwarding simple REST requests to the linked JIRA instances. For example /plugins/servlet/applinks/proxy?appId={INSERT APPLINK ID HERE}&path=%2Frest%2Fapi%2F2%2Fsearch will call JIRA's issue search REST endpoint and list issues available to the user (as in JIRA search). By "simple request" I mean that only GET and POST HTTP methods are supported in the current version (with POST limited to application/xml and multipart/form-data content types). The servlet supports both query-string and HTTP-header parameters. Check out the source of the servlet in plugin's source to get more info as I haven't found any online documentation for it.
Using this servlet you can get the projects list as well by requesting /plugins/servlet/applinks/proxy?appId={INSERT APPLINK ID HERE}&path=%2Frest%2Fapi%2F2%2Fproject
Servlets's path in the repo is confluence-jira-plugin/src/main/java/com/atlassian/confluence/plugins/jira/AppLinksProxyRequestServlet.java, but most of the important stuff is in its base class confluence-jira-plugin/src/main/java/com/atlassian/confluence/plugins/jira/AbstractProxyServlet.java

How can load the html, get the value and do some calculation by the javascript

I want to make a HTML using javascript to do the following task:
loading a HTML such as here
put the stock price value in a variable
display using that variable for calculation
You can't. XSS protection. Cross site contents can not be read by javascript. No major browser will allow you to do that. Your only option should be making a server-side call then do whatever you want
Contact that provider and request/buy access to their information. They might provide you with an endpoint of a Web API (or other service). You can then access it through (for instance) AngularJS:
// Simple GET request example:
$http({
method: 'GET',
url: 'http://somehosturl/api/stockprices/' // or whatever
}).then(function successCallback(response) {
$scope.stockPrices = response;
}, function errorCallback(response) {
HandleError(response);
});
(based on their general usage sample)

How can I manage Google account's contacts through JavaScript?

I'm trying to manage Google account contacts through JavaScript programs. When I'm trying to delete contacts through JavaScript, this error occurs: "Network Error: 405 Not Allowed Method."
Here is My Code:
function deleteContacts() {
$.ajax({
type: 'DELETE',
url: 'https://www.google.com/m8/feeds/contacts/default/full/{client_Id}?access_token=' + tokenId,
headers: {
'If-Match': '*',
'Gdata-version': '3.0'
},
dataType: 'jsonp',
data: {},
success: function (data) {
console.log("response: " + data)
}
});
}
Please help me in this, is this possible to manage Google account's contacts through JavaScript? If it is really possible then please tell me all possibilities for managing Google account's contacts....
Is there any other JavaScript API available?
Is there any alternate solution?

I Know Google has listed solutions in java, PHP, nodes as well, and I am writing server code in Nodejs, but seems like Google client code for NodeJs in alpha version, not sure how much strong it is to manage contacts...
Make use of the google client api for javascript, Authenticate, getToken and then use Request and then execute it. Pass on the method and url as per your need. For delete purpose, pass these are its input:
method : 'DELETE',
url : '/m8/feeds/contacts/default/full/<friend id to delete>'

How can I add an event to a Google calendar using v3 API and JQuery?

I am attempting to teach myself some JQuery/REST/Google API by putting together a simple page that does the following:
Authenticates with Google
Validates a token
Gets a list of Google Calendars for the user
List events for a selected calendar
Add an event for a selected calendar
I have #1 through #4 working (although no doubt in an ugly manner), and am getting tripped up on #5. Here's the JQuery ajax call:
var url = 'https://www.googleapis.com/calendar/v3/calendars/[MY_CALENDAR_ID]/events?sendNotifications=false&access_token=[GOOGLE_API_TOKEN]';
var data = { end: { dateTime: "2012-07-22T11:30:00-07:00" }
, start: { dateTime: "2012-07-22T11:00:00-07:00" }
, summary: "New Calendar Event from API"
};
var ajax = $.ajax({ url: url
, data: data
, type: 'POST'
}).done(addEventDone)
.fail(function (jqHXR, textStatus) {
console.log("addEvent(): ajax failed = " + jqHXR.responseText);
console.log(jqHXR);
});
The results are a global parseError: "This API does not support parsing form-encoded input.". The first four steps are all using GET ajax calls, so I'm not sure if that is what is tripping me up.
Here's the API in question: https://developers.google.com/google-apps/calendar/v3/reference/events/insert
I think I may be doing things the long and hard way, and my new approach is to tackle this using the javascript API instead of going straight at it with manual JQuery and REST. This is the approach I am attempting going forward http://code.google.com/p/google-api-javascript-client/wiki/Samples#Calendar_API, although I would still love to use this as a learning opportunity if there is something simple I am screwing up in the code above.
Thanks for any help, insights, pointers, etc. I will post updates if I make any progress using the javascript API.
Interestingly, I just answered a similar question here. Your intuition to use Google's JS client library is a good one. It's going to handle OAuth 2 for you, which is a requirement if you're going to do any manipulation of the Calendar data.
My other answer has both a link to a blog post that I authored (which demonstrates configuration of the client and user authorization), as well as an example of inserting a Calendar event.
you need to set contentType: "application/json", to do JSON.stringify for your data and method : 'POST'
var ajax = $.ajax({
url: url,
contentType: "application/json",
data: JSON.stringify(data),
method : 'POST',
});

Categories