How to add attachment to JIRA issue from web url using python? - python

I've successfully attached a file to a existing issue via JIRA rest api using python :
from jira.client import JIRA
jira = JIRA(basic_auth=([username], [password]), options={'server': [our_jira_server]})
issue = jira.issue([issue_id])
file = open('test.txt')
attachment_object = jira.add_attachment(issue,file)
But instead of local filepath , I need the file to be fetched from remote url. (Let's say google drive)
I 've read the this document , but no help. If python implementation is there, its awesome ..But curl implementation would do good too.
Thanks in advance.

Related

Generate a Kibana/Elasticsearch report PDF to be opened directly in Python

I'm trying to write a python script to automatically grab a pdf snapshot of a Kibana dashboard and upload it to a website called Jira.
I've worked out the part of uploading a pdf stored locally to Jira via python, however I need to remove the step of having to download the pdf to my computer and then open it in python (i.e., a function that opens a link from Kibana to the pdf). Is this possible?
from jira import JIRA
import requests
# Server Authentication
username = xxxxxxx
password = xxxxxxx
options = {'server': 'https://yourhostname.jira.net'}
jira = JIRA(options = options, auth = (username, password))
issue = jira.issue('PROJ-1000')
# Upload the file
with open('/some/path/attachment.pdf', 'rb') as f:
jira.add_attachment(issue=issue, attachment=f)
This code that I've been using works for attaching a pdf that I have stored locally, but I would like to be able to grab a pdf dashboard report directly from Kibana without having to store the pdf on my computer first.

Connecting to jira cloud using python

I am trying to connect jira cloud using REST API. This is my Python code for it:
pip install jira
from jira import JIRA
jiraOptions = {'server': "https:url"}
user = 'emailid'
apikey = 'api token'
server = 'https:url'
options = {
'server': server
}
After this when I execute this line I get a connection error
jira = JIRA(basic_auth=(user,apikey))
ConnectionRefusedError
I am not sure what has gone wrong and I tried finding the right syntax but I dont see what the problem is can anyone please help?
Hey #Bobby I ran into some similar problems. I created this repo to use straight Python and the API to get most things done - https://github.com/dren79/JiraScripting_public
A lot of the basic functionality is in the helpers.py file, let me know if it helps.

Access Office365 SharePoint with Single Sign-On via Python

I don't know much of anything about SharePoint, but was tasked to use Python to download a SharePoint list. SharePoint is Office365 and is accessed with Single Sign-on. I found the below sample on how to connect to SharePoint:
from office365.runtime.auth.user_credential import UserCredential
from office365.sharepoint.client_context import ClientContext
ctx = ClientContext('https://<site>.sharepoint.com').with_credentials(UserCredential('domain\\user', 'password'))
web = ctx.web
ctx.load(web)
ctx.execute_query()
print(web.properties["Url"])
When executing this code I receive the following error:
Cannot get binary security token for from https://login.microsoftonline.com/extSTS.srf
KeyError: 'FedAuth'
Can anyone point me in the right direction?
Recently when I was searching for similar kind of solution, I found something useful, you can check below code on github.
https://github.com/vgrem/Office365-REST-Python-Client

Using Python to Connect/ Download file from SharePoint 2013

Seek your guidance, I am new #Python and in learning phase.
Problem Statement: I want to connect & Download the xlsx file from my office SharePoint 2013 site.
Steps so far:
import sharepy
s = sharepy.connect("https://office.XXX.com/sites/pkdyns")
r = s.getfile("https://office.XXX.com/sites/pkdyns/Shared%20Documents/YYYY.xlsx")
r = s.getfile("https://office.XXX.com/sites/pkdyns/Shared%20Documents/YYYY.xlsx", filename="/PYTHON/YYYY.xlsx")
After this I dont see the downloaded file, not sure if it ran or not, using Jupyter Notebook but it didnt throw up any error. Please suggest.
sharepy library seems to only support sharepoint online.
SharePy - Simple SharePoint Online authentication for Python
Below is a sample about how to download file from sharepoint, you may take a reference( it uses another library):
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.file import File
ctx = ClientContext.connect_with_credentials(url,UserCredential(username, password))
web = ctx.web
ctx.load(web)
ctx.execute_query()
print "Web title: {0}".format(web.properties['Title'])
path = "../../tests/data/SharePoint User Guide.docx"
response = File.open_binary(context, "Shared Documents/SharePoint User Guide.docx")
response.raise_for_status()
with open(path, "wb") as local_file:
local_file.write(response.content)

Basic authentication with jira-python

I'm new to Python, new to the jira-python library, and new to network programming, though I do have quite a bit of experience with application and integration programming and database queries (though it's been a while).
Using Python 2.7 and requests 1.0.3
I'm trying to use this library - http://jira-python.readthedocs.org/en/latest/ to query Jira 5.1 using Python. I successfully connected using an unauthenticated query, though I had to make a change to a line in client.py, changing
I changed
self._session = requests.session(verify=verify, hooks={'args': self._add_content_type})
to
self._session = requests.session()
I didn't know what I was doing exactly but before the change I got an error and after the change I got a successful list of project names returned.
Then I tried basic authentication so I can take advantage of my Jira permissions and do reporting. That failed initially too. And I made the same change to
def _create_http_basic_session
in client.py , but now I just get another error. So problem not solved. Now I get a different error:
HTTP Status 415 - Unsupported Media Type
type Status report
message Unsupported Media Type
description The server refused this request because the request entity is in
a format not` `supported by the requested resource for the requested method
(Unsupported Media Type).
So then I decided to do a super simple test just using the requests module, which I believe is being used by the jira-python module and this code seemed to log me in. I got a good response:
import requests
r = requests.get(the_url, auth=(my username , password))
print r.text
Any suggestions?
Here's how I use the jira module with authentication in a Python script:
from jira.client import JIRA
import logging
# Defines a function for connecting to Jira
def connect_jira(log, jira_server, jira_user, jira_password):
'''
Connect to JIRA. Return None on error
'''
try:
log.info("Connecting to JIRA: %s" % jira_server)
jira_options = {'server': jira_server}
jira = JIRA(options=jira_options, basic_auth=(jira_user, jira_password))
# ^--- Note the tuple
return jira
except Exception,e:
log.error("Failed to connect to JIRA: %s" % e)
return None
# create logger
log = logging.getLogger(__name__)
# NOTE: You put your login details in the function call connect_jira(..) below!
# create a connection object, jc
jc = connect_jira(log, "https://myjira.mydom.com", "myusername", "mypassword")
# print names of all projects
projects = jc.projects()
for v in projects:
print v
Below Python script connects to Jira and does basic authentication and lists all projects.
from jira.client import JIRA
options = {'server': 'Jira-URL'}
jira = JIRA(options, basic_auth=('username', 'password'))
projects = jira.projects()
for v in projects:
print v
It prints a list of all the project's available within your instance of Jira.
Problem:
As of June 2019, Atlassian Cloud users who are using a REST endpoint in Jira or Confluence Cloud with basic or cookie-based authentication will need to update their app or integration processes to use an API token, OAuth, or Atlassian Connect.
After June 5th, 2019 attempts to authenticate via basic auth with an Atlassian account password will return an invalid credentials error.
Reference: Deprecation of basic authentication with passwords for Jira and Confluence APIs
Solution to the Above-mentioned Problem:
You can use an API token to authenticate a script or other process with an Atlassian cloud product. You generate the token from your Atlassian account, then copy and paste it to the script.
If you use two-step verification to authenticate, your script will need to use a REST API token to authenticate.
Steps to Create an API Token from your Atlassian Account:
Log in to https://id.atlassian.com/manage/api-tokens
Click Create API token.
From the dialog that appears, enter a memorable and concise Label for your token and click Create.
Click Copy to clipboard, then paste the token to your script.
Reference: API tokens
Python 3.8 Code Reference
from jira.client import JIRA
jira_client = JIRA(options={'server': JIRA_URL}, basic_auth=(JIRA_USERNAME, JIRA_TOKEN))
issue = jira_client.issue('PLAT-8742')
print(issue.fields.summary)
Don't change the library, instead put your credentials inside the ~/.netrc file.
If you put them there you will also be able to test your calls using curl or wget.
I am not sure anymore about compatibility with Jira 5.x, only 7.x and 6.4 are currently tested. If you setup an instance for testing I could modify the integration tests to run against it, too.
My lucky guess is that you broke it with that change.
As of 2019 Atlassian has deprecated authorizing with passwords.
You can easily replace the password with an API Token created here.
Here's a minimalistic example:
pip install jira
from jira import JIRA
jira = JIRA("YOUR-JIRA-URL", basic_auth=("YOUR-EMAIL", "YOUR-API-TOKEN"))
issue = jira.issue("YOUR-ISSUE-KEY (e.g. ABC-13)")
print(issue.fields.summary)
I recommend storing your API Token as an environment variable and accessing it with os.environ[key].

Categories