In the Jira issue I'm looking at, there are fields with a drop down list for valid values. I would like to access that drop down list using python. When looking at the returned fields for the issue, the object has a value customfield_14651 which is an object with value and id. Jira documentation shows there is a custom_field_option() method which should return the fields? I call the method like below:
self.jira = JIRA('https://jira.companyname.com',basic_auth (login['username'], login['password']) )
print self.jira.custom_field_option('14651')
and receive back the following error:
response text = {"errorMessages":["A custom field option with id '14651' does not exist"],"errors":{}}
Jira has the .fields() function which returns a list of all fields that are visible to the account you are using.
from jira import JIRA
jira = JIRA(basic_auth=('username', 'password'), options = {'server': 'url'})
# Fetch all fields
allfields = jira.fields()
# Make a map from field name -> field id
name_map = {field['name']:field['id'] for field in allfields}
name_map is now a dict in the format of {"field name":"customfield_xxxx", ... }
It looks like the way to do this in the API is:
from jira import JIRA
jira = JIRA(basic_auth=('username', 'password'), options = {'server': 'url'})
# get an example issue that has the field you're interested in
issue = jira("PRJ-1")
meta = jira.editmeta(issue)
# inspect the meta to get the field you want to look at
allowed_values = [v['value'] for v in meta['fields']['customfield_99999']['allowedValues']]
Related
Basicly, I've done with Python-requests and Django search feature through Google Books API with single q parameter (as shown in link below)
https://developers.google.com/books/docs/v1/using#WorkingVolumes
and after submiting form I'm getting list of dicts in json as I want with this single parameter, and I'm getting in json data where appers keyword "Hobbit" and URL looks like this
http://127.0.0.1:8000/api?books=hobbit
but when I'm trying to add special keywords provided by Google Books API like,
intitle, inauthor, inpublisher, subject, etc.
and trying to search for it I'm getting URL
http://127.0.0.1:8000/api?books=hobbit&intitle=&inauthor=&inpublisher=&isbn=&lccn=&oclc=
which only returns the data of single q parameter, because the correct URL for special keywords in Google Books API looks like this
https://www.googleapis.com/books/v1/volumes?q=flowers+inauthor:keyes+subject:somesubject
So as you see then correct URL got signs
+ against & and : against =
so the Correct URL that I want to get would look like this
http://127.0.0.1:8000/api?books=hobbit+intitle:something+inauthor:something+inpublisher:something+isbn:something+lccn:something+oclc:something
My question is how to change this structure to correct as Google books API require?
Tried to find this in python-requests docs but there are nothing about this
views.py
def api(request):
books = {
'intitle': 'intitle',
'inauthor': 'inauthor',
'inpublisher': 'inpublisher',
'subject': 'subject',
'isbn': 'isbn',
'lccn': 'lccn',
'oclc': 'oclc'
}
if 'books' in request.GET:
books = request.GET['books']
url = 'https://www.googleapis.com/books/v1/volumes?q=%s' % books
response = requests.get(url)
books = response.json()
print(type(books))
with open("data_file.json", "w") as write_file:
json.dump(books, write_file)
return render(request, 'books/api.html', {'books': books})
You will have to construct the query string manually. Assuming that your request will look like http://127.0.0.1:8000/api?books=hobbit&intitle=a&inauthor=b&inpublisher=c, you can construct the query string like this:
def api(request):
# ...
if 'books' in request.GET:
books = request.GET['books']
query_dict = request.GET.copy()
del query_dict['books']
query = '+'.join([books, *['{}:{}'.format(k, v) for k, v in query_dict.items()]])
url = 'https://www.googleapis.com/books/v1/volumes?q=' + query
# ...
The final google query requires books as the first parameter. So, we need to extract the books value from request.GET. Now, to get all other values, we need to delete the books key. But, request.GET is a QueryDict object, which is immutable. To convert it into a mutable object, request.GET.copy() can be used (which creates a mutable copy).
I am trying to get all comments of issues created in JIRA of a certain search query. My query is fairly simple:
import jira
from jira.client import JIRA
def fetch_tickets_open_yesterday(jira_object):
# JIRA query to fetch the issues
open_issues = jira_object.search_issues('project = Support AND issuetype = Incident AND \
(status = "Open" OR status = "Resolved" OR status = "Waiting For Customer")', maxResults = 100,expand='changelog')
# returns all open issues
return open_issues
However, if I try to access the comments of tickets created using the following notation, I get a key error.
for issue in issues:
print issue.raw['fields']['comment']
If I try to get comments of a single issue like below, I can access the comments:
single_issue = jira_object.issue('SUP-136834')
single_issue.raw['fields']['comment']
How do I access these comments through search_issues() function?
The comment field is not returned by the search_issues method you have to manually state the fields that must be included by setting the corresponding parameter.
just include the 'fields' and 'json_result' parameter in the search_issue method and set it like this
open_issues = jira_object.search_issues('project = Support AND issuetype = Incident AND \
(status = "Open" OR status = "Resolved" OR status = "Waiting For Customer")', maxResults = 100,expand='changelog',fields = 'comment',json_result ='True')
Now you can access the comments without getting keytype error
comm=([issue.raw['fields']['comment']['comments'] for issue in open_issues])
I struggled with the same issue. Assuming "issue" is an object of type Issue, and "jira" is an object of type JIRA, according to http://jira.readthedocs.org/en/latest/#issues
issue.fields.comment.comments
should work, but the fields object does not have any key "comment".
The other option mentioned there works for me:
jira.comments(issue)
So, for it to work you use the issues from your search result and call jira.comments. E.g.
issues = jira.search_issues(query)
comments = jira.comments(issues[index])
(My version of the library is 1.0.3, python 2.7.10)
from jira import JIRA
Jira = JIRA('https://jira.atlassian.com')
issue_num = "ISSUE-123"
issue = Jira.issue(issue_num)
comments = issue.fields.comment.comments
for comment in comments:
print("Comment text : ",comment.body)
print("Comment author : ",comment.author.displayName)
print("Comment time : ",comment.created)
My requirement is: I want to update the labels for the issues present in the filter.
import jira.client
from jira.client import jira
options = {'server': 'https://URL.com"}
jira = JIRA(options, basic_auth=('username], 'password'))
issue = jira.search_issues('jqlquery')
issue.update(labels=['Test']
I'm getting attribute error which states that 'Resultlist' object has no attibute 'update'.
Update only works on a single issue. Search_issues returns a ResultList.
The JIRA API does not support bulk change. However, you can loop over the issues yourself and do the update for each one. Something like:
import jira.client
from jira.client import jira
options = {'server': 'https://URL.com'}
jira = JIRA(options, basic_auth=('username', 'password'))
issues = jira.search_issues('jqlquery')
for issue in issues:
issue.update(labels=['Test'])
It's documented in the jira-python docs at http://jira-python.readthedocs.org/en/latest/
You might have to also do
issue = jira.issue(issue.key)
to get a modifiable objects
# You can update the entire labels field like this
issue.update(labels=['AAA', 'BBB'])
# Or modify the List of existing labels. The new label is unicode with no spaces
issue.fields.labels.append(u'new_text')
issue.update(fields={"labels": issue.fields.labels})
I am trying to use endpoints to update some JSON values in my datastore. I have the following Datastore in GAE...
class UsersList(ndb.Model):
UserID = ndb.StringProperty(required=True)
ArticlesRead = ndb.JsonProperty()
ArticlesPush = ndb.JsonProperty()
In general what I am trying to do with the API is have the method take in a UserID and a list of articles read (with an article being represented by a dictionary holding an ID and a boolean field saying whether or not the user liked the article). My messages (centered on this logic) are the following...
class UserID(messages.Message):
id = messages.StringField(1, required=True)
class Articles(messages.Message):
id = messages.StringField(1, required=True)
userLiked = messages.BooleanField(2, required=True)
class UserIDAndArticles(messages.Message):
id = messages.StringField(1, required=True)
items = messages.MessageField(Articles, 2, repeated=True)
class ArticleList(messages.Message):
items = messages.MessageField(Articles, 1, repeated=True)
And my API/Endpoint method that is trying to do this update is the following...
#endpoints.method(UserIDAndArticles, ArticleList,
name='user.update',
path='update',
http_method='GET')
def get_update(self, request):
userID = request.id
articleList = request.items
queryResult = UsersList.query(UsersList.UserID == userID)
currentList = []
#This query always returns only one result back, and this for loop is the only way
# I could figure out how to access the query results.
for thing in queryResult:
currentList = json.loads(thing.ArticlesRead)
for item in articleList:
currentList.append(item)
for blah in queryResult:
blah.ArticlesRead = json.dumps(currentList)
blah.put()
for thisThing in queryResult:
pushList = json.loads(thisThing.ArticlesPush)
return ArticleList(items = pushList)
I am having two problems with this code. The first is that I can't seem to figure out (using the localhost Google APIs Explorer) how to send a list of articles to the endpoints method using my UserIDAndArticles class. Is it possible to have a messages.MessageField() as an input to an endpoint method?
The other problem is that I am getting an error on the 'blah.ArticlesRead = json.dumps(currentList)' line. When I try to run this method with some random inputs, I get the following error...
TypeError: <Articles
id: u'hi'
userLiked: False> is not JSON serializable
I know that I have to make my own JSON encoder to get around this, but I'm not sure what the format of the incoming request.items is like and how I should encode it.
I am new to GAE and endpoints (as well as this kind of server side programming in general), so please bear with me. And thanks so much in advance for the help.
A couple things:
http_method should definitely be POST, or better yet PATCH because you're not overwriting all existing values but only modifying a list, i.e. patching.
you don't need json.loads and json.dumps, NDB does it automatically for you.
you're mixing Endpoints messages and NDB model properties.
Here's the method body I came up with:
# get UsersList entity and raise an exception if none found.
uid = request.id
userlist = UsersList.query(UsersList.UserID == uid).get()
if userlist is None:
raise endpoints.NotFoundException('List for user ID %s not found' % uid)
# update user's read articles list, which is actually a dict.
for item in request.items:
userslist.ArticlesRead[item.id] = item.userLiked
userslist.put()
# assuming userlist.ArticlesPush is actually a list of article IDs.
pushItems = [Article(id=id) for id in userlist.ArticlesPush]
return ArticleList(items=pushItems)
Also, you should probably wrap this method in a transaction.
How do I get a specific album from the picasa api?
Currently I am doing something like this:
def album(gd_client, album_id, user):
all_albums = albums(gd_client,user)
for album in all_albums:
if album.service_id == album_id:
return album
Which means I get all the albums and filter out the one I need.
But there must be an easier and more efficient way.
Thanks!
In Google Data Protocol version 2.0 there is experimental support for PartialResponse, which allows you to filter responses at the server by passing in a fields parameter.
Unfortunately only the Java API supports 2.0, but you can easily add it to the Python API yourself.
It looks like you want "entry" API. Here is how I can get AlbumEntry for a public album (do not want to deal with auth):
base = "https://picasaweb.google.com";
user = "101405741057659770470" # myself
albumid = "6333524051829767505"
# must be inside of a function to use locals()
url = "%(base)s/data/entry/api/user/%(user)s/albumid/%(albumid)s" % locals()
gd_client = gdata.photos.service.PhotosService()
entry = gd_client.GetEntry(url)
assert isinstance(entry, gdata.photos.AlbumEntry)
Similar in Java:
String baseURL = "https://picasaweb.google.com";
String userId = "101405741057659770470" // myself
String albumid = "6333524051829767505"
String albumUrl = String.format(
"%s/data/entry/api/user/%s/albumid/%s", baseURL, userId, albumId);
// myService is defined here: https://developers.google.com/picasa-web/docs/2.0/developers_guide_java
AlbumEntry entry = myService.getEntry(new URL(albumUrl), AlbumEntry.class, (DateTime) null /* modified since*/);