Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,363,377
Community Members
 
Community Events
168
Community Groups

When executing python file which has connection in server is throwing error

Error: Max retries exceed with url: /rest/api/content/64425351/child/attachment

Caused by NewConnectionError ('<urllib3.connection.VerfiedHTTPSConnection object>') Failed to establish new connection)

1 answer

Hello, 

Show us your code, it looks like it's from your token, maybe also your user trying to fetch the info doesn't have the accreditations to have access. 

 

Also, it can be a maximum number of requests by API. 

Thanks for Reply Hani!

I have attached code Please look into and let me know anything required

#!/usr/bin/env python3
from jira import JIRA
import os
import pandas as pd
import xlrd
import time
import json
import requests
import re
from requests.auth import HTTPBasicAuth
from dotenv import load_dotenv

# Global variables are defined here

working_dir = "/home/jborrmann/tdbc/pip/"
working_dir = "/opt/ATLASSIAN/JIRA/CRON_JOB/tdbc/pip/"

os.chdir(working_dir)
load_dotenv(dotenv_path = working_dir + ".env", verbose=True)
log_path = working_dir + "tdbc.log"

tdbc_user = os.environ['TDBC_USER']
tdbc_password = os.environ['TDBC_PASSWORD']
jira_base_url = os.environ['JIRA_BASE_URL']
project_key = os.environ['JIRA_PROJECT_KEY']
project_id = os.environ['JIRA_PROJECT_ID']
tdbc_xlsx_file = os.environ['TDBC_XLSX_FILE']
wiki_base_url = os.environ['WIKI_BASE_URL']
wiki_get_tdbc_attachments_url = os.environ['WIKI_BASE_URL'] \
+ "/rest/api/content/" + os.environ['WIKI_PAGE_ID'] + "/child/attachment"

# Functions start here

def logger(log_text):
print(log_text)
with open(log_path, "a") as log_file:
log_file.write(time.strftime("%Y-%m-%d %H:%M:%S") + " | " + log_text + "\n")

# updates a TDBC jira issue and 3 fields in it
def update_jira_issue(issue_to_be_updated,
cf_ae_money,
cf_close_date,
cf_tk_status,
ae_money,
close_date,
tk_status):

fields_to_update = {}

if ae_money != "nan":
fields_to_update[cf_ae_money] = float(ae_money)
if close_date != "nan":
fields_to_update[cf_close_date] = close_date[0:10]
if tk_status != "nan":
fields_to_update[cf_tk_status] = float(tk_status) * 100

try:
issue_to_be_updated.update(fields=fields_to_update)
except Exception as e:
logger("exception while updating issue " + str(issue_to_be_updated.key) + ": " + str(e))

# loads a TDBC jira issue and checks which partner reference matches the one from the excel read earlier
def load_jira_issue_and_find_partner_ref_field(jira_issue_key,
partner_reference,
ae_money,
close_date,
tk_status,
jira_con):
# load issue
jira_issue = jira_con.issue(jira_issue_key)

# return if that fails (issue deleted? ...)
if type(jira_issue).__name__ == "int" and jira_issue == 0:
logger("could not load " + jira_issue_key)
return

# see which partner reference field 1-6 the reference in the file belongs to
if jira_issue.fields.customfield_10400 == partner_reference:
update_jira_issue(jira_issue, "customfield_14300", "customfield_14403", "customfield_14301", # PR 1
ae_money, close_date, tk_status)

if jira_issue.fields.customfield_14615 == partner_reference:
update_jira_issue(jira_issue, "customfield_14620", "customfield_14630", "customfield_14625", # PR 2
ae_money, close_date, tk_status)

if jira_issue.fields.customfield_14616 == partner_reference:
update_jira_issue(jira_issue, "customfield_14621", "customfield_14631", "customfield_14626", # PR 3
ae_money, close_date, tk_status)

if jira_issue.fields.customfield_14617 == partner_reference:
update_jira_issue(jira_issue, "customfield_14622", "customfield_14632", "customfield_14627", # PR 4
ae_money, close_date, tk_status)
if jira_issue.fields.customfield_14618 == partner_reference:
update_jira_issue(jira_issue, "customfield_14623", "customfield_14633", "customfield_14628", # PR 5
ae_money, close_date, tk_status)

if jira_issue.fields.customfield_14619 == partner_reference:
update_jira_issue(jira_issue, "customfield_14624", "customfield_14634", "customfield_14629", # PR 6
ae_money, close_date, tk_status)


# Script Logic starts here

logger("Starting daily TDBC Update Job")

# check for version of xlsx in the wiki newer than last run
session = requests.Session()
resp = session.get(wiki_get_tdbc_attachments_url, auth=HTTPBasicAuth(tdbc_user, tdbc_password))
if resp.status_code != 200:
logger("Could not get a good response from the TDBC wiki page " + wiki_get_tdbc_attachments_url)
exit()

session_cookies = session.cookies

has_tdbc_xlsx = False
tdbc_xlsx_download_path = ""
attachment_details = json.loads(resp.text)
for attachment in attachment_details['results']:
if attachment['title'] == "tdbc_template.xlsx" and attachment['type'] == "attachment":
has_tdbc_xlsx = True
tdbc_xlsx_download_path = attachment['_links']['download']

uploaded_timestamp = 0
if has_tdbc_xlsx:
regex = r".*modificationDate=(\d{13}).*"
match = re.search(regex, tdbc_xlsx_download_path)
if match is not None:
uploaded_timestamp = int(match.group(1))

if uploaded_timestamp > 0 and (int(time.time()) * 1000) - uploaded_timestamp < (86400000 * 1):
logger("xlsx modificationDate is from today, this file needs to be processed.")
logger("download path is " + tdbc_xlsx_download_path)
else:
logger("No xlsx file with a modification date newer since last run yesterday present. Exiting.")
exit()

# download xlsx locally if there is a new one or end here
dl_resp = session.get(wiki_base_url + tdbc_xlsx_download_path, cookies=session_cookies)

if dl_resp.status_code != 200:
logger("Could not download xlsx attachment from " + tdbc_xlsx_download_path)

open(tdbc_xlsx_file, 'wb').write(dl_resp.content)

# read xlsx file with pandas
excel_lines = pd.read_excel(tdbc_xlsx_file, skiprows=1, header=None, dtype='unicode', usecols=[0,1,2,3,4], sheet_name=0)

# iterate over each line, if any
if len(excel_lines) > 0:
l = 0
# open connection to jira already
jira_connection = JIRA(jira_base_url + "", basic_auth=(tdbc_user, tdbc_password))
#jira_connection = JIRA(options={'server':'https://jira.westconcomstor.com', 'verify': False}, basic_auth=(user_jira, password_jira))
for index, row in excel_lines.iterrows():
# load each issue in it and check if one of the partner refs is matching
load_jira_issue_and_find_partner_ref_field(str(row[0]).strip(),
str(row[1]).strip(),
str(row[2]).strip(),
str(row[3]).strip(),
str(row[4]).strip(),
jira_connection)
l += 1
logger("processed line " + str(l) + "/" + str(len(excel_lines)) + " with issue key " + str(row[0]).strip())
else:
logger("excel file seems to contain no lines in sheet one")

TDBC_USER: tdbc-automation

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events