resolve ticket as fixed with python jira api - python

import jira
def resolve_issue(jira,issue):
jira.transition_issue(issue, '5', assignee={'name': 'pm_user'}, resolution={'id': '3'},comment={'name':"Resolved the ticket."}))
[(u'5', u'Resolve Issue'), (u'821', u'Request Info'), (u'1011', u'Rejected'), (u'1031', u' Duplicate ')]
are the available transitions. does not work to resolve an issue as fixed with python jira rest api. I have tried to list out the transitions, but I don't see 'fixed' resolution id. Any suggestions?
added error below
text: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: N/A; line: -1, column: -1] (through reference chain: com.atlassian.jira.issue.fields.rest.json.beans.CommentJsonBean["body"])

I'm not sure if this is actually causing your problem but you have to wrap your 'assignee' and 'resolution' changes in a "fields" dict. So it would have to be something like:
fields = {
"resolution:: {
"id": "3"
},
"assignee: {
"name": "pm_user"
}
}
jira.transition_issue(issue, fields=fields, comment="Resolved the ticket.")
The JIRA REST API doesn't have very good error handling for transitions and I've seen a number of different errors (usually a 500) when the request is malformed.

Related

How to transfers files with Google Workspace Admin SDK (python)

I am trying to write a program that transfers users drive and docs files from one user to another. It looks like I can do it using this documentation Documentation.
I created the data transfer object, which looks like this:
datatransfer = {
'kind': 'admin#datatransfer#DataTransfer',
'oldOwnerUserId': 'somenumberhere',
'newOwnderUserId': 'adifferentnumberhere',
'applicationDataTransfers':[
{
'applicationId': '55656082996', #the app id for drive and docs
'applicationTransferParams': [
{
'key': 'PRIVACY_LEVEL',
'value': [
{
'PRIVATE',
'SHARED'
}
]
}
]
}
]
}
I have some code here for handling Oauth and then I bind the service with:
service = build('admin', 'datatransfer_v1', credentials=creds)
Then I attempt to call insert() with
results = service.transfers().insert(body=datatransfer).execute()
and I get back an error saying that it 'missing required field: resource'.
I tried nesting all of this inside a field called resource and I get the same message.
I tried passing in JUST a json structure that looked like this {'resource': 'test'} and I get the same message.
So I tried using the "Try this method" live tool on the documentation website,
If I pass in no arguments at all, or just pass in the old and new user, I get the same message 'missing required nested field: resource'.
If I put in 'id':'55656082996' with ANY other arguments it just says error code 500 backend error.
I tried manually adding a field named "resource" to the live tool and it says 'property 'resource' does not exist in object specification"
I finally got this to work. If anyone else is struggling with this and stumbles on this, "applicationId" is a number, not a string. Also, the error message is misleading - there is no nested field called "resource." This is what worked for me:
datatransfer = {
"newOwnerUserId": "SomeNumber",
"oldOwnerUserId": "SomeOtherNumber",
"kind": "admin#datatransfer#DataTransfer",
"applicationDataTransfers": [
{
"applicationId": 55656082996,
"applicationTransferParams": [
{
"key": "PRIVACY_LEVEL"
},
{
"value": [
"{PRIVATE, SHARED}"
]
}
]
}
]
}
service = build('admin', 'datatransfer_v1', credentials=creds)
results = service.transfers().insert(body=datatransfer).execute()
print(results)
To get the user's Id's I'm first using the Directory API to query all users who are suspended, and getting their ID from that. Then passing their ID into this to transfer their files to another user before deleting them.

Jira API : when using PUT call getting "no single-String constructor/factory method"

I was trying to update a custom field on a Jira ticket using Jira API, following this Jira Documentation. however, I am getting the below error.
{'errorMessages': ['Can not instantiate value of type [simple type, class com.atlassian.jira.rest.v2.issue.IssueUpdateBean] from JSON String; no single-String constructor/factory method']}
this is my code:
data = {'update': {'customfield_25305': [{'set': [{'value': '1c1a07d49af1b1cde8a1a7bd93cbbeef8efd50c9'}, {'value': 'c6f1e31ce0138cba658f769accaac729bebc42d6'}]}]}}
data = json.dumps(json.dumps(data)) #because the API accepts only strings enclosed in double quotes
upload = requests.put(url, headers=headers, data=data)
print(upload.json())
as per the documentation, I tried "/editmeta" the custom field I am trying to update is editable and has the following attributes.
{'required': False, 'schema': {'type': 'string', 'custom': 'com.atlassian.jira.plugin.system.customfieldtypes:textfield', 'customId': 25305}, 'name': 'Commit ID(s)', 'fieldId': 'customfield_25305', 'operations': ['set']}
Not sure what I am doing wrong here, any help would be appreciated!
Tried jira documentation
and searched through the Jira community none of the answers helped, everything points to malformed data but the data that I am passing is as per documentation.
the end result would be a 204 status code.
I think is a matter of single quotes.
As far as I remember, for valid JSON it should be double quotes.
{
"update": {
"customfield_25305": [{
"set": [{
"value": "1c1a07d49af1b1cde8a1a7bd93cbbeef8efd50c9"
}, {
"value": "c6f1e31ce0138cba658f769accaac729bebc42d6"
}]
}]
}
}
I use to do a online validator tool to my JSON strings when I got stuck.
E.g. https://jsonlint.com/ (first result on google)
I was able to resolve the issue, the proper formatting for data is as below:
{"fields": {"customfield_25305": "\"1c1a07d49af1b1cde8a1a7bd93cbbeef8efd50c9\", \"c6f1e31ce0138cba658f769accaac729bebc42d6\", \"1c1a07d49af1b1cde8a1a7bd93cbbeef8efd50c9\""}}

Trouble in comparison JSON with Python

I have to implement a web service using Flask and MongoDB. I have two collections. The JSON files are shown below:
File products.json:
{ "name":"red apples", "price":0.5, "description":"Apples from Greece", "category":"fruits", "stock":25 }
{ "name":"fish", "price":5, "description":"Fresh fish", "category":"fish", "stock":25 }
{ "name":"pasta", "price":1.5, "description":"Pasta from Italia", "category":"pasta", "stock":25 }
File users.json:
{"name":"George Vaios", "password":"321", "email":"george#gmail.gr", "category":"admin"}
{"name":"Admin", "password":"admin", "email":"admin#gmail.gr", "category":"admin"}
What I'm trying to do?
I'm trying to implement a function called add_product. This function allows users whose category is admin ("category": "admin"). As a route has a POST method because I'm parsing the email of a user and the details are needed to import a new product in products.json.
Code:
(lines of code for loading data)
user = users.find_one({"email":data['email']})
if (user['category']=="admin"):
product = {
"name": data['name'],
"price": data['price'],
"description": data['description'],
"category": data['category'],
"stock": data['stock']
}
products.insert_one(product)
return Response("Product was added succesfully", status=200, mimetype='application/json')
else:
return Response("Account has no permission. Must be an admin", status=400, mimetype='application/json')
With that code I'm trying to check if the user is an admin by finding him in the JSON file using the find_one method and then compare the user['category'] with the string 'admin'.
Data that I'm parsing to postman:
{
"email": "george#gmail.gr",
"name": "chocolate",
"price": 25,
"description": "mklkgj",
"category": "sweets",
"stock": 30
}
Error that I get:
File "/home/michael/main.py", line 241, in add_product_admin
if (user['category']=="admin"):
TypeError: 'NoneType' object is not subscriptable
I can't understand why the if statement doesn't make the comparison. Any thoughts?
Mongo is telling you that nothing came up when searching for that value. I would start by checking if data['email'] has the value you would expect and if your collection is initialized correctly.
Thanks to everyone who explained to me what's going on. The actual problem was on MongoDB and client parameters on Python script. That's why the entries and JSON files were unreadable. If anyone comes up again with this problem be sure that you have checked:
# Choose database
database = client['database you want to access']
Then don't forget to check with mongo shell if your expected collections exist:
db.(your database name).find({})
Thank you for your time!

400 Error while trying to POST to JIRA issue

I am trying to set the 'transition' property in a JIRA issue from whatever it is, to completed(which according to the doc is 10000). According to the documentation, this error is 'If there is no transition specified.'
Also I have used ?expand=transitions.fields to verify that 10000 is for complete.
using these docs
https://docs.atlassian.com/jira/REST/latest/#api/2/issue-doTransition
https://jira.atlassian.com/plugins/servlet/restbrowser#/resource/api-2-issue-issueidorkey-transitions/POST
Here is my request
url = 'http://MYURL/rest/api/2/issue/ISSUE-ID/transitions'
payload1 = open('data3.json', 'r').read()
payload = json.loads(payload1)
textFile = requests.post(url, auth=('username', 'password'), json=payload)
The contents on my data3.json file are
{
"transition": 10000
}
edit: I also changed my JSON to this and I get a 500 error
{
"transition": {
"id": "10000"
}
}
The error I get
{"errorMessages":["Can not instantiate value of type [simple type,classcom.atlassian.jira.rest.v2.issue.TransitionBean] from JSON integral number;no single-int-arg constructor/factory method (through reference chain:com.atlassian.jira.rest.v2.issue.IssueUpdateBean[\"transition\"])"]}400
I'm pretty confident that my issue is in my json file since I have used GET in the code above this snippit multiple times, but I could be wrong.
Possible cause - https://jira.atlassian.com/browse/JRA-32132
I believe the issue I was having was a process flow one. I cannot jump right from my issue being opened, to 'completed'. However, I can go from the issue being created to 'Done'.
{
"transition": {
"name": "Done",
"id": "151"
}
}
As this does what I need, I will use it. If I find how to make ticket complete I will post back.
Also, I think the fact we customize our JIRA lead to my getting 'Completed' as a valid transition even though it wasn't.
Yes, you're right that the JSON is wrong, it's not even a valid json since the value is not a number, string, object, or array. The doc says:
The fields that can be set on transtion, in either the fields
parameter or the update parameter can be determined using the
/rest/api/2/issue/{issueIdOrKey}/transitions?expand=transitions.fields
resource.
So you need to do a get request on /rest/api/2/issue/{issueIdOrKey}/transitions?expand=transitions.fields to get the list of possible values and then set that in the json
{
"transition": {
"id" : "an_id_from_response"
}
}

Error while getting degree connection between two users

I am facing a problem while fetching degree connection between two LinkedIn users. I am sending a request at
https://api.linkedin.com/v1/people::(~,id=<other person's linkedin id>):(relation-to-viewer:(distance))?format=json&oauth2_access_token=<user's access token>.
Sometimes I get a correct response:
{
"_total": 2,
"values": [
{
"_key": "~",
"relationToViewer": {"distance": 0}
},
{
"_key": "id=x1XPVjdXkb",
"relationToViewer": {"distance": 2}
}
]
}
while most of the time I get an erroneous response:
{
"_total": 1,
"values": [{
"_key": "~",
"relationToViewer": {"distance": 0}
}]
}
I have gone through LinkdIn api's profile fields and I believe that I am using the api correctly. I am not sure what's wrong here. Please help.
After posting it on LinkedIn forum, I got the response
The behavior you're seeing where you only get yourself back from your
call falls in line with what I'd expect to see if the member ID you're
asking for isn't legitimate. If the member ID exists, but isn't in ~'s
network, you should get a -1 distance back, not nothing at all, as you
are seeing. However if you put in a completely invalid member ID, only
information about ~ will be returned from the call.
This was indeed the problem. The client on Android and the client on iOS had different API keys and both were using the same backend to access the degree connection. By using the same API key for both the clients resolved the issue.

Categories

Resources