class ConfluenceHelper: def __init__(self, url, username, password, space): self.confluence = Confluence(url=url, username=username, password=password) self.space = space def create_page(self, title, body, parent_title): # Do some crazy page creation result = self.confluence.get_page_by_title(space=self.space, title=parent_title) parent_id = result["id"] result = self.confluence.create_page(space=self.space, title=title, body=body, parent_id=parent_id) page = result return True, page
def create_nbpm_collab_page(cir_title, creds): collab = Confluence(url='https://collab.test.net/', username=creds['user'], password=creds['password']) cir_date = datetime.now().strftime("%Y-%m-%d") cir_title = cir_title body_text = ''' ''' page = collab.create_page(space="NBPM", parent_id=143085345, title=cir_title, body=body_text)
def update_confluence(self): confluence = Confluence(url=self.url, username=self.username, password=self.password) # Confluence login if self.uploadOriginalFile: # if original xlsx_file is to be attach on the page file = self.attach_file(confluence) html = file + "<br /><br />" + self.html else: html = self.html new_page = confluence.create_page( self.space, self.pageTitle, html, parent_id=self.rootPage, type='page', representation='storage') # creating main page if self.CreateSubPagesForEachXXEntries: # if we want subpages for output try: # getting page if of main page. With help of this id, sub pages are created parent_page = new_page["id"] except KeyError: # if key is not present then it is most probably inside a list with key "results" parent_page = new_page["results"]["id"] self.create_child_pages(confluence, parent_page) # creating child pages
return data url = 'https://rockym.atlassian.net' username = '******' apikey = 'gpZgK6O6WC8Cnitgif6q0D08' space = 'Ops Fulfillment' switch = False if switch: confluence = Confluence(url=url, username=username, password=apikey) status = confluence.create_page( space=space, title='This is the title', body='This is the body. You can use <strong>HTML tags</strong>!') print(status) else: jira = Jira(url=url, username=username, password=apikey) JQL = 'project = "OF" AND text ~ Event ORDER BY created DESC'.format(space) data = jira.jql(JQL) #print(json.dumps(data, indent=4, sort_keys=True)) issues = data.get('issues', []) for item in issues: process_jira(item)
<td style="text-align: center;" colspan="1">2019/06/10</td>\ <td style="text-align: center;" colspan="1">지연</td>\ <td style="text-align: center;" colspan="1">0</td>\ <td style="text-align: center;" colspan="1">3</td>\ <td colspan="1"><br /></td></tr>\ </tbody></table><p class="auto-cursor-target"><br /></p><p><br />\ </p><ac:structured-macro ac:name="panel" ac:schema-version="1" ac:macro-id="85959282-270a-481c-a73b-82a3b37d96c7">\ <ac:parameter ac:name="title">TCCXXXX Linux 1st Release 마일스톤에서 지연중인 이슈</ac:parameter>\ <ac:rich-text-body><p><ac:structured-macro ac:name="jira" ac:schema-version="1" ac:macro-id="394c50ba-1b13-4a13-9fff-7ff8f112ebe3">\ <ac:parameter ac:name="server">TCS (Telechips Collaboration System)</ac:parameter>\ <ac:parameter ac:name="columns">key,type,priority,summary,status,creator,assignee,created,updated,start date,due</ac:parameter>\ <ac:parameter ac:name="maximumIssues">20</ac:parameter>\ <ac:parameter ac:name="jqlQuery">fixVersion = 10491 AND status not in(Resolved,closed) and due < now() ORDER BY due ASC </ac:parameter>\ <ac:parameter ac:name="serverId">1aff8ec6-5d59-3004-8410-8dc6eceba71e</ac:parameter></ac:structured-macro>\ </p></ac:rich-text-body></ac:structured-macro><p><br /></p>\ <ac:structured-macro ac:name="panel" ac:schema-version="1" ac:macro-id="9553c4bf-26ff-4b5a-b5db-c198339b4c87">\ <ac:parameter ac:name="title">TCCXXXX Linux 2st Release 마일스톤에서 지연중인 이슈</ac:parameter>\ <ac:rich-text-body><p><ac:structured-macro ac:name="jira" ac:schema-version="1" ac:macro-id="c7ab5435-ea03-4841-aa1f-5649f9f723ad">\ <ac:parameter ac:name="server">TCS (Telechips Collaboration System)</ac:parameter>\ <ac:parameter ac:name="columns">key,type,priority,summary,status,creator,assignee,created,updated,start date,due</ac:parameter>\ <ac:parameter ac:name="maximumIssues">20</ac:parameter>\ <ac:parameter ac:name="jqlQuery">fixVersion = 10492 AND status not in(Resolved,closed) and due < now() ORDER BY due ASC </ac:parameter>\ <ac:parameter ac:name="serverId">1aff8ec6-5d59-3004-8410-8dc6eceba71e</ac:parameter></ac:structured-macro>\ </p></ac:rich-text-body></ac:structured-macro><p class="auto-cursor-target"><br /></p>' # Wiki 특정 페이지 하위에 data를 본문에 넣어서 신규 생성 confluence.create_page(space='~B180093', title='주간보고 (' + s + ')', body='{0}'.format(data), parent_id=77172264, type='page')
class ConfluenceAdapter(object): """ Adapter for Atlassian Confluence class. Encapsulates content retrieve and update functionality. """ NAMESPACES = { "atlassian-content": "http://atlassian.com/content", "ac": "http://atlassian.com/content", "ri": "http://atlassian.com/content", "atlassian-template": "http://atlassian.com/template", "at": "http://atlassian.com/template", } def __init__(self, url, username, password, space_key): self.confluence = Confluence(url=url, username=username, password=password) self.space_key = space_key self.get_space_or_create() def get_space_or_create(self): """ Check whether space exists or not. If it doesn't, then create the space. :rtype: dict :return: Space data. """ space = self.confluence.get_space(self.space_key) if type(space) is not dict: raise WikiUpdateException("Can't retrieve valid information about Confluence space." " Please check configurations. Data: {}".format(space)) if space.get('statusCode', None) == 404: space = self.confluence.create_space(self.space_key, self.space_key) return space def get_page_or_create(self, page_title): """ Get page content, if no such page then create it. :type page_title: str :param page_title: Title of the page which should be retrieved. :raises: WikiUpdateException :rtype: tuple(int, lxml.etree._Element) :returns: Tuple where first element is the id of the page. The second is the parsed content data. """ data = self.confluence.get_page_by_title(title=page_title, space=self.space_key, expand="body.storage,version") if not data: # No such page exist. Then create such page. data = self.create_page(page_title) try: content_xml = data['body']['storage']['value'] except KeyError: raise WikiUpdateException("Can't get partial-devices page content.") body_xml = render_to_string('wrapper.xml', { 'content': content_xml }) return data['id'], etree.fromstring(body_xml) def create_page(self, page_title): """ Create new page.+ :type page_title: str :param page_title: Title of the page which should be created. :raises: WikiUpdateException :rtype: dict :return: Data of newly created page. """ data = self.confluence.create_page(self.space_key, page_title, body="") if not data or 'id' not in data: raise WikiUpdateException("Page `{}` could not be created. Response data: {}".format(page_title, data)) return data def update_page_content(self, page_id, page_title, body): """ Update existing page with new body. :type page_id: int :param page_id: Page id which should be modified. :type page_title: str :param page_title: Title of the page which should be modified. :type body: lxml.etree._Element :param body: Page content data. :raises: WikiUpdateException :rtype: dict :returns: Data of newly updated page. """ # Get raw xml. body_xml = etree.tostring(body).decode('utf-8') # <xml xmlns:******> <p></p>***<ac:structured-macro>***</ac:structured-macro> </xml> # ^ ^ # Take the content starting right after `>` of the opening xml tag and till `<` of xml closing tag. content_xml = body_xml[body_xml.find('>') + 1:body_xml.rfind('<')] data = self.confluence.update_existing_page(page_id, page_title, content_xml) if not data or 'id' not in data: raise WikiUpdateException("Page `{}` could not be updated. Response data: {}".format(page_title, data)) return data @classmethod def get_field_element(cls, element, field): """ Get element from tree by field name. :rtype: lxml.etree._Element :returns: Element data. """ return element.xpath('//ac:structured-macro/ac:parameter' '[@ac:name="MultiExcerptName" and translate(normalize-space(text()), " ", "")="{}"]' '/following-sibling::ac:rich-text-body/*[local-name()="p"]'.format(field.name), namespaces=cls.NAMESPACES) @classmethod def update_content_for_field(cls, page_content, field): """ Update content for field. :type page_content: lxml.etree._Element :param page_content: Wiki page content. :type field: AbstractLinkedField :param field: Field for which the page_content should be updated. :rtype: lxml.etree._Element :returns: Page content data. """ field_elements = cls.get_field_element(page_content, field) if not field_elements: # If element does not exist then create it. macro_id = uuid.uuid4() data = render_to_string('multiexcerpt.xml', { "macro_id": macro_id, "field_name": field.name, "field_value": field.provide_value() }) element = etree.fromstring(data) page_content.insert(-1, element) # Can leave without this return, but `Explicit is better than implicit.` (C) Python Zen. return page_content for field_element in field_elements: field_element.text = field.provide_value() return page_content
key[i]][j][0] + '</td><td>' + weekly_data[ key[i]][j][1] + '</td><td style="text-align: center;">' + str( weekly_data[key[i]][j][2]) + '</td><td><br /></td></tr>' wiki_data = '<h3><span style="color: rgb(0,0,255);">● 진행한 업무</span></h3>\ <table><colgroup><col /><col /><col /><col /><col /></colgroup>\ <tbody><tr><th style="text-align: center;">프로젝트</th><th style="text-align: center;">Issue Key</th><th style="text-align: center;">Issue Summary</th><th style="text-align: center;">WorkLog(H)</th><th style="text-align: center;">특이사항</th></tr>' + \ wiki_body + \ '</tbody></table><p><span style="color: rgb(0,0,255);"><br /></span></p>\ <h3><span style="color: rgb(0,0,255);">● 이번 주 할 일</span></h3>\ <ac:structured-macro ac:name="panel" ac:schema-version="1" ac:macro-id="74dc65d1-aa95-46a2-bca6-5a9bf1be2bdf">\ <ac:rich-text-body><p><ac:structured-macro ac:name="jira" ac:schema-version="1" ac:macro-id="192659b0-28b5-44a5-9c43-bd182b31a1e8">\ <ac:parameter ac:name="server">TCS (Telechips Collaboration System)</ac:parameter>\ <ac:parameter ac:name="columns">priority,type,key,summary,status,start date,due</ac:parameter>\ <ac:parameter ac:name="maximumIssues">20</ac:parameter>\ <ac:parameter ac:name="jqlQuery">("Start date" <= endofWeek(0) AND duedate >= startOfweek(0) OR "Start date" >= startOfweek(0) AND "Start date" <= endofWeek(0) OR duedate >= startOfweek(0) AND duedate <= endofWeek(0)) AND status not in (Resolved, closed) AND (assignee = b180093 OR Sub_Assignee = ' + \ user + ') </ac:parameter><ac:parameter ac:name="serverId">1aff8ec6-5d59-3004-8410-8dc6eceba71e</ac:parameter></ac:structured-macro></p>\ </ac:rich-text-body></ac:structured-macro><p class="auto-cursor-target"><br /></p><p><br /></p><p><span style="color: rgb(0,0,255);"><br /></span></p>' # Wiki auth confluence = Confluence(url='https://wiki.telechips.com:8443', username='******', password='******') # Wiki 페이지 생성 confluence.create_page(space='TA3', title='신호찬_주간보고', body='{0}'.format(wiki_data), parent_id=88902085, type='page')
def export_to_confluence(request: HttpRequest, incident_id: str): try: incident = Incident.objects.get(pk=incident_id) except Incident.DoesNotExist: raise Http404("Incident does not exist") module_dir = os.path.dirname(__file__) file_path = os.path.join(module_dir, "postmortem.html") # full path to text. f = open(file_path, "r") content_file = f.read() content_file = content_file.replace("%SEVERITY%", incident.severity_text()) if incident.impact is not None: content_file = content_file.replace("%IMPACT%", incident.impact) else: content_file = content_file.replace("%IMPACT%", '') content_file = content_file.replace( "%START_TIME%", incident.start_time.strftime("%Y-%m-%d %H:%M:%S") + " UTC") content_file = content_file.replace( "%END_TIME%", incident.end_time.strftime("%Y-%m-%d %H:%M:%S") + " UTC") content_file = content_file.replace( "%LINK%", "{0}://{1}{2}".format(request.scheme, request.get_host(), request.path).replace("export", ""), ) if incident.summary is None: content_file = content_file.replace("%SUMMARY%", '') else: content_file = content_file.replace("%SUMMARY%", incident.summary) events = PinnedMessage.objects.filter( incident=incident).order_by("timestamp") updates = StatusUpdate.objects.filter( incident=incident).order_by("timestamp") timeline_list = sorted(chain(events, updates), key=attrgetter("timestamp"), reverse=False) timeline_content = "" for item in timeline_list: update = "" print(item.__class__.__name__) if "StatusUpdate" in item.__class__.__name__: update = "UPDATE: " timeline_content += "<tr>" timeline_content += ("<td>" + item.timestamp.strftime("%Y-%m-%d %H:%M:%S") + " UTC" + "</td>") timeline_content += "<td>" + update + html.escape(item.text) + "</td>" timeline_content += "</tr>" content_file = content_file.replace("%TIMELINE%", timeline_content) s = requests.Session() s.headers["Authorization"] = "Bearer " + settings.CONFLUENCE_TOKEN b = Confluence( url=settings.CONFLUENCE_URL, username=settings.CONFLUENCE_USER, password=settings.CONFLUENCE_TOKEN, ) logger.info(content_file) page_created = b.create_page( space=settings.CONFLUENCE_SPACE, title="[" + incident.start_time.strftime("%Y-%m-%d") + "] PostMortem " + incident.report, body=content_file, parent_id=settings.CONFLUENCE_PARENT, ) links_data = page_created["_links"] incident.post_mortem = links_data["base"] + links_data["webui"] incident.save() return redirect("incident_doc", incident_id=incident_id)
class Readme2Confluence: """ Main class that will create the Confluence page from Markdown """ def __init__(self, url, username, password, space): self.confluence = Confluence(url, username=username, password=password) spaces = self.confluence.get_all_spaces(start=0, limit=500) if any(s for s in spaces if s["name"] == space): self.space = space else: raise ValueError("{} is not valid Confluence Space".format(space)) def get_page_id(self, title): """ Retrieve the ID of a page using it's title This is basically a small wrapper around confluence.get_page_id(). The main reason for the wrapper is to better handle pages that aren't found by raising an exception """ page_id = self.confluence.get_page_id(self.space, title) if page_id is None: raise ValueError("{} is not a valid page title in {}".format( title, self.space)) return page_id def get_page_html(self, title): """ Retrieve a confluence page and return it as a HTML (string) """ page_id = self.get_page_id(title) return self.confluence.get_page_by_id(page_id) def send2confluence(self, title, parent_title=None, md_file=None, md_text=""): """ Method that creates the confluence page. Page will either be created or updated depending on what is required. """ if md_file is None: body = md2html(md_text) else: body = md2html(get_md_text(md_file)) parent_id = None if parent_title is None else self.get_page_id( parent_title) page_id = self.confluence.get_page_id(self.space, title) if page_id is None: ret = self.confluence.create_page(self.space, title, body, parent_id=parent_id, type='page', representation='storage') else: ret = self.confluence.update_page(page_id, title, body, parent_id=parent_id, type='page', representation='storage') return ret
<tbody><tr><th>Project</th><th>Start Date</th><th>End Date</th></tr>\ <tr><td>마일스톤</td><td>' + project_due_min + '</td><td>' + project_due_max + \ '</td></tr></tbody></table><p class="auto-cursor-target"><br /></p><table><colgroup><col /><col /><col /><col /></colgroup>\ <tbody><tr><th>Project</th><th>Milestone</th><th>Description</th><th>Due Date</th></tr>' + \ tableRow1 + \ '</tbody></table><p class="auto-cursor-target"><br /></p>\ <table><colgroup><col /><col /><col /></colgroup>\ <tbody><tr><th>Event</th><th>Description of Event</th><th>Date</th></tr><tr><td>Today</td><td><br /></td><td>today</td></tr></tbody></table>\ <p class="auto-cursor-target"><br /></p></ac:rich-text-body></ac:structured-macro><p class="auto-cursor-target"><br /></p>\ <p style="text-align: center;">★ : 완료, <span style="color: rgb(51,102,255);">★</span> : 진행중, <span style="color: rgb(255,0,0);">★</span> : 지연</p>\ <table><colgroup><col /><col /><col /><col /><col /><col /></colgroup><tbody><tr><th style="text-align: center;">Milestone</th><th style="text-align: center;">종료일</th>\ <th style="text-align: center;" colspan="1">상태</th><th style="text-align: center;">미해결 이슈</th><th style="text-align: center;">전체 이슈</th><th style="text-align: center;">특이사항</th></tr>' + \ tableRow2 + \ '</tbody></table><p class="auto-cursor-target"><br /></p><p><br /></p>' + tableRow3 # 현재 날짜 생성 now = datetime.now() nowdate = ('%s-%s-%s' % (now.year, now.month, now.day)) #print(wiki_data) ''' # Wiki 페이지 생성 confluence.create_page( space='~B180093', title= data_version[data_version_key[0]]['projectName'] + ' (' + nowdate + ')', body='{0}'.format(wiki_data), parent_id=77172264, type='page' ) '''
ok = input("\nDoes this look good? y/n [n]:").strip() or 'n' if not ok.lower().startswith('y'): print("\n\nPlease start again") else: all_cards = [c for v in data.values() for c in v] if all_cards: today = datetime.date.today() title = f"{today.strftime('%d %B %Y')} - {today.strftime('%A')} Retrospective" #TODO: Make this more generic title = input( f"\n\nSelect a page title [{title}]: ").strip() or title confluence.create_page(space=parent_page['space']['key'], title=title, parent_id=parent, body=body, representation='wiki') else: print("\n\nNo cards to add to page") for card in all_cards: card.set_closed(True) except: print_exc() sleep(2) input("\n\nPress enter to close")
class ConfluenceRenderer(MarkdownRenderer): """Render report to comfluence via the atlassian API """ def __init__(self, space="", url="", username="", token="", parent=None): self.parent = parent self.space = space # if not all([space, url, username, token]): # raise ConfluenceAPIError("Must assigne a space, url, unername and token") # Define atlassian api connection self.conn = Confluence(url=url, username=username, password=token, cloud=True) def set_parent(self, parent): """Set id of parent to write conluence page to """ self.parent = parent def set_space(self, space): """Set id of space to write conluence page to """ self.space = space def save(self, report): """Save report to Confluence """ report, plots_to_upload = self._process_images(report) content = self._convert_to_jira_wiki(report) response = self.conn.create_page(self.space, report.title, content, type="page", representation="wiki", parent_id=self.parent) page_id = response['id'] # upload images for filepath, name in plots_to_upload: self._upload_image(filepath, name, page_id) def _process_images(self, report): """Prepare report and images to be uploaded """ out_report = deepcopy(report) out_report.components = [] images_to_upload = [] for component in report.get_components(): if isinstance(component, Plot): # prepare upload filepath = component.get_path() name = os.path.basename(filepath) images_to_upload.append((filepath, name)) # Change Plot to match new_plot = deepcopy(component) new_plot.set_path(name) out_report.add_component(new_plot) else: out_report.add_component(component) return out_report, images_to_upload def _upload_image(self, filepath, name, page_id): """Upload Iamges to Confluence """ self.conn.attach_file(filepath, space=self.space, page_id=page_id, name=name) def _test_connection(self): """Test initialized confluence connection """ def _test_space_page_exists(self): """Test initialized confluence connection """ def _convert_to_jira_wiki(self, report): """Convert Markdown content to Atlassian wiki markup """ content = self._render_markdown(report.get_components()) content = pypandoc.convert_text(content, "jira", "markdown") return content
# coding=utf-8 from atlassian import Confluence confluence = Confluence(url='http://localhost:8090', username='******', password='******') status = confluence.create_page(space='DEMO', title='This is the title', body='This is the body') print(status)
def main(): # create logging file handler logger = logging.getLogger("Perf_reporter") logger.setLevel(logging.INFO) fh = logging.FileHandler("perf_report.log") sh = logging.StreamHandler(sys.stdout) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setFormatter(formatter) sh.setFormatter(formatter) logger.addHandler(fh) logger.addHandler(sh) logger.info("Reporter started") settings_path = 'settings.ini' bank_list = [] # creating settings.ini if not exists, reading all settings if os.path.exists(settings_path): db_host = config.get_setting(settings_path, 'Settings', 'db_host') db_port = config.get_setting(settings_path, 'Settings', 'db_port') db_name = config.get_setting(settings_path, 'Settings', 'db_name') db_start = config.get_setting(settings_path, 'Settings', 'db_start') db_end = config.get_setting(settings_path, 'Settings', 'db_end') db_info = config.get_setting(settings_path, 'Settings', 'db_info') confluence_auth = (config.get_setting(settings_path, 'Settings', 'confluence_user'), config.get_setting(settings_path, 'Settings', 'confluence_password')) confluence_url = config.get_setting(settings_path, 'Settings', 'confluence_url') confluence_space = config.get_setting(settings_path, 'Settings', 'confluence_space') confluence_page_id = config.get_setting(settings_path, 'Settings', 'confluence_page_id') confluence_html_to_post = config.get_setting(settings_path, 'Settings', 'confluence_html_to_post') confluence_path_to_post = config.get_setting(settings_path, 'Settings', 'confluence_path_to_post') grafana_url = config.get_setting(settings_path, 'Settings', 'grafana_url') garafana_dashboard_uri = config.get_setting(settings_path, 'Settings', 'garafana_dashboard_uri') grafana_org_id = config.get_setting(settings_path, 'Settings', 'grafana_org_id') grafana_panel_id_list = config.get_setting(settings_path, 'Settings', 'grafana_panel_id_list') grafana_panel_width = config.get_setting(settings_path, 'Settings', 'grafana_panel_width') grafana_panel_height = config.get_setting(settings_path, 'Settings', 'grafana_panel_height') logger.info(db_info) else: config.create_config('settings.ini') db_host = config.get_setting(settings_path, 'Settings', 'db_host') db_port = config.get_setting(settings_path, 'Settings', 'db_port') db_name = config.get_setting(settings_path, 'Settings', 'db_name') db_start = config.get_setting(settings_path, 'Settings', 'db_start') db_end = config.get_setting(settings_path, 'Settings', 'db_end') db_info = config.get_setting(settings_path, 'Settings', 'db_info') confluence_auth = (config.get_setting(settings_path, 'Settings', 'confluence_user'), config.get_setting(settings_path, 'Settings', 'confluence_password')) confluence_url = config.get_setting(settings_path, 'Settings', 'confluence_url') confluence_space = config.get_setting(settings_path, 'Settings', 'confluence_space') confluence_page_id = config.get_setting(settings_path, 'Settings', 'confluence_page_id') confluence_html_to_post = config.get_setting(settings_path, 'Settings', 'confluence_html_to_post') confluence_path_to_post = config.get_setting(settings_path, 'Settings', 'confluence_path_to_post') grafana_url = config.get_setting(settings_path, 'Settings', 'grafana_url') garafana_dashboard_uri = config.get_setting(settings_path, 'Settings', 'garafana_dashboard_uri') grafana_org_id = config.get_setting(settings_path, 'Settings', 'grafana_org_id') grafana_panel_id_list = config.get_setting(settings_path, 'Settings', 'grafana_panel_id_list') grafana_panel_width = config.get_setting(settings_path, 'Settings', 'grafana_panel_width') grafana_panel_height = config.get_setting(settings_path, 'Settings', 'grafana_panel_height') logger.info(db_info) # command line arguments parser settings parser = argparse.ArgumentParser(description='Report builder requires relative template path for ' 'report generation.') parser.add_argument( "template_path", metavar='1', type=str, help='file name or template file path') args = parser.parse_args() logger.debug(parser.parse_args()) template_path = parser.parse_args().template_path db_connection_string = db_query.set_db_connection_string(host=db_host, port=db_port, name=db_name) logger.debug("db_connection_string = " + db_connection_string) test_time = db_query.set_time_string(start=db_start, end=db_end) logger.debug("test_time = " + test_time) all_bank_list = db_query.get_bank_list(db_conn=db_connection_string, test_time=test_time) for b in all_bank_list: if db_query.check_bank_query(db_conn=db_connection_string, test_time=test_time, bank=b): bank_list.append(b) else: continue logger.debug(bank_list) last_line_list = csv_processing.csv_read_last_line(read_path=template_path) csv_report_file_name = csv_processing.csv_file_name(template_path=template_path) html_report_file_name = html_processing.html_file_name(template_path=template_path) csv_headers = csv_processing.csv_read(read_path=template_path) csv_processing.csv_add(data=csv_headers, start_index=0, end_index=1, write_path=csv_report_file_name) logger.info("Start info retrieving") for bank in sorted(bank_list): service_list = [] target_service_list = [] found_t_service = [] all_service_list = db_query.get_service_list(db_conn=db_connection_string, test_time=test_time, bank=bank) for s in all_service_list: if db_query.check_service_query(db_conn=db_connection_string, test_time=test_time, service=s, bank=bank): service_list.append(s) else: continue all_target_service_list = db_query.get_target_service_list(db_conn=db_connection_string, test_time=test_time, bank=bank) for ts in all_target_service_list: if db_query.check_target_service_query(db_conn=db_connection_string, test_time=test_time, t_service=ts, bank=bank): target_service_list.append(ts) else: continue logger.debug(bank_list, service_list, target_service_list) for service in sorted(service_list): calculated_list = [] logger.info("BANK: " + bank) logger.info("SERVICE: " + service) db_query_list = csv_processing.csv_read(read_path=template_path) service_result_list = db_query.query_db(db_conn=db_connection_string, q_list=db_query_list, service=service, test_time=test_time) for target_service in sorted(target_service_list): if service in target_service: found_t_service.append(target_service) for t in found_t_service: target_service_result_list = db_query.query_db_with_target_services( db_conn=db_connection_string, q_ts_list=last_line_list, bank=bank, service=service, target_service=t, test_time=test_time) logger.debug(target_service_result_list) service_result_list.append(target_service_result_list) logger.debug("OVERALL RESULTS" + str(service_result_list)) calculated_list = process_table_data(list_to_p=service_result_list, bank=bank, service=service, target_service=target_service) logger.debug(calculated_list) # service_result_list = [] found_t_service = [] csv_processing.csv_add(data=calculated_list, start_index=1, end_index=len(calculated_list), write_path=csv_report_file_name) html_processing.html_add(calculated_list, html_report_file_name) list_for_html = csv_processing.csv_read(read_path=csv_report_file_name) html_processing.html_write(html_data=list_for_html, report_file_name=html_report_file_name) # image retrieving logger.info("Image retrieving started") time_for_url_grafana = images_processing.set_time_string(start=db_start, end=db_end) grafana_panel_width = ast.literal_eval(grafana_panel_width) grafana_panel_height = ast.literal_eval(grafana_panel_height) grafana_panel_id_list = grafana_panel_id_list.split(',') i_count = 99 for i in range(len(grafana_panel_id_list)): images_processing.retrieve_pics(url=grafana_url, uri=garafana_dashboard_uri, org_id=grafana_org_id, test_time=time_for_url_grafana, id=grafana_panel_id_list[i], width=grafana_panel_width[grafana_panel_id_list[i]], height=grafana_panel_height[grafana_panel_id_list[i]], template_path=template_path, i_count=i_count) i_count -= 1 logger.info("Image retrieving done!") # confluence posting confluence = Confluence(url=confluence_url, username=confluence_auth[0], password=confluence_auth[1]) descr = os.path.splitext(template_path)[0].split('/')[1] test_start = db_start.strip('\'') test_end_time = db_end.strip('\'').split(" ")[1] page_title = "{} {} - {}".format(descr, test_start, test_end_time) files = [] confluence.create_page(space=confluence_space, title=page_title, body=confluence_html_to_post, parent_id=confluence_page_id) new_page_id = confluence.get_page_id(space=confluence_space, title=page_title) for (dirpath, dirnames, filenames) in walk(confluence_path_to_post): files.extend(filenames) break description = "<p> TEST SUMMARY" + confluence_html_to_post + "</p>" page_body = description test_duration = "<p> Test duration: " + str(datetime.strptime(db_end, "'%Y-%m-%d %H:%M:%S'") - datetime.strptime(db_start, "'%Y-%m-%d %H:%M:%S'")) + "</p>" page_body += test_duration logger.info("Start confluence report attachment") for file in sorted(files, reverse=True): if str(os.path.splitext(file)[1]) == '.png': response = confluence_processing.upload_attachment(auth=confluence_auth, page_id=new_page_id, file_path=confluence_path_to_post+file) base_url, filename, container_id, modification_date, attachment_id = confluence_processing.\ get_attachments_info(response) image_tag = confluence_processing.prepare_html_for_image(base_url=base_url, filename=filename, container_id=container_id, modification_date=modification_date, attachment_id=attachment_id, height="300", width="825") image_tag = "<p>" + image_tag + "</p>" page_body += image_tag elif str(os.path.splitext(file)[1]) == '.html': with open(confluence_path_to_post + file, 'r') as fr: html_table = fr.read() page_body += html_table else: continue logger.info("Start confluence update") # confluence.update_page(new_page_id, title=page_title, body=page_body, minor_edit=True) confluence_processing.write_data_storage(auth=confluence_auth, html=page_body, page_id=new_page_id) logger.info("Report completed")
password='******') html = ["""<table> <tr> <th>Project Key</th> <th>Project Name</th> <th>Leader</th> <th>Email</th> </tr>"""] for data in jira.project_leaders(): log.info('{project_key} leader is {lead_name} <{lead_email}>'.format(**data)) row = """<tr> <td>{project_key}</td> <td>{project_name}</td> <td>{lead_name}</td> <td><a href="mailto:{lead_email}">{lead_email}</a></td> </tr>""" html.append(row.format(**data)) html.append('</table><p></p><p></p>') status = confluence.create_page( space='DEMO', parent_id=confluence.get_page_id('DEMO', 'demo'), title='Jira Administrators', body='\r\n'.join(html)) print(status)
# coding=utf-8 from atlassian import Confluence confluence = Confluence(url="http://localhost:8090", username="******", password="******") status = confluence.create_page(space="DEMO", title="This is the title", body="This is the body") print(status)
<ac:parameter ac:name="pieKeys">' + pieKeys_Top + pieKeys_Middle + pieKeys_Bottom + \ '<ac:parameter ac:name="id">1562303225565_213485116</ac:parameter>\ <ac:parameter ac:name="worklog">5|8|w d h m|w d h m</ac:parameter>\ <ac:parameter ac:name="formatVersion">3</ac:parameter>\ <ac:rich-text-body><p class="auto-cursor-target"><br /></p>\ <table><colgroup><col /><col /><col /></colgroup><tbody><tr><th>Project</th><th>Start Date</th><th>End Date</th></tr>' + \ '<tr><td>마일스톤</td><td>' + table_project.loc[data_index[0]]['startDate'] + '</td><td>' + \ table_project.loc[data_index[0]]['endDate'] + \ '</td></tr></tbody></table><p class="auto-cursor-target"><br /></p>\ <table><colgroup><col /><col /><col /></colgroup><tbody><tr><th>Project</th><th>Milestone</th><th>Due Date</th></tr>' + \ tableRow1 + '</tbody></table><p class="auto-cursor-target"><br /></p>\ <table><colgroup><col /><col /></colgroup><tbody><tr><th>Event</th><th>Date</th></tr><tr><td>Today</td>\ <td>today</td></tr></tbody></table>\ <p class="auto-cursor-target"><br /></p></ac:rich-text-body></ac:structured-macro><p class="auto-cursor-target"><br /></p>\ <p style="text-align: center;">★ : 완료, <span style="color: rgb(51,102,255);">★</span> : 진행중, \ <span style="color: rgb(255,0,0);">★</span> : 지연</p><h3><span style="color: rgb(0,0,255);"><strong>● </strong>\ 프로젝트 주요이슈/특이사항</span></h3><table><colgroup><col /><col /><col /><col /><col /><col /><col />\ </colgroup><tbody><tr><th style="text-align: center;">Milestone</th><th style="text-align: center;">종료일\ </th><th style="text-align: center;" colspan="1">상태</th><th style="text-align: center;">미해결 이슈</th>\ <th style="text-align: center;">전체 이슈</th><th style="text-align: center;" colspan="1">지연 이슈</th>\ <th style="text-align: center;">주간 보고 특이사항</th></tr>' + tableRow2 + '</tbody></table>' # Wiki 페이지 생성 confluence.create_page( space='TA3', title= table_version.loc[data_index[0]]['projectName'] + ' (' + nowdate + ')', body='{0}'.format(wiki_data), parent_id=82184747, type='page' )
from pprint import pprint from atlassian import Confluence confluence = Confluence( url='http://localhost:8090', username='******', password='******') status = confluence.create_page( space='DEMO', title='This is the title', body='This is the body') pprint(status)
"""<table> <tr> <th>Project Key</th> <th>Project Name</th> <th>Leader</th> <th>Email</th> </tr>""" ] for data in jira.project_leaders(): log.info( "{project_key} leader is {lead_name} <{lead_email}>".format(**data)) row = """<tr> <td>{project_key}</td> <td>{project_name}</td> <td>{lead_name}</td> <td><a href="mailto:{lead_email}">{lead_email}</a></td> </tr>""" html.append(row.format(**data)) html.append("</table><p></p><p></p>") status = confluence.create_page( space="DEMO", parent_id=confluence.get_page_id("DEMO", "demo"), title="Jira Administrators", body="\r\n".join(html), ) print(status)
class ConfluenceService: releasenote_template = "" product_changelog_template = "" component_changelog_template = "" def __init__(self, confluence_url, jira_url, bibucket_url, username, password, verifyssl): self.verifyssl = verifyssl self.jira_service = JiraService(url=jira_url, username=username, password=password, verifyssl=self.verifyssl) self.bitbucket_service = BitbucketService(url=bibucket_url, username=username, password=password, verifyssl=self.verifyssl) self.confluence = Confluence(url=confluence_url, username=username, password=password, verify_ssl=self.verifyssl) self.load_product_changelog_template() self.load_component_changelog_template() def generate_releasenote(self, project_key, version, template_file): self.load_releasenote_template(template_file) if self.releasenote_template is None: sys.exit("ERROR: Release note template is missing") versionData = self.jira_service.get_project_version_infos( project_key, version) if versionData is not None: tasks = self.jira_service.get_issues_confluence_markup( project_key, versionData["id"]) releasenote = self.releasenote_template.replace( "%fixversion%", versionData["id"]) releasenote = releasenote.replace("%project-key%", project_key) releasenote = releasenote.replace("%validate_task%", tasks) return (releasenote, versionData["startDate"], versionData["releaseDate"]) else: return None def push_releasenote(self, spacekey, version, start_date, release_date, parent_page_id, releasenote): if release_date is None: release_date = start_date title = "{0} - {1}".format(version, release_date) self.push_to_confluence(spacekey, parent_page_id, title, releasenote) semantic_version = "" def push_changelog(self, name, space_key, version, parent_page_id, isComponent=False): changelog_content = None current_date = datetime.today().strftime("%Y-%m-%d") semantic_version = "" m = re.search( "(([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?)", version) if m: semantic_version = m.group(1) page_title = "{0} - {1} - {2}".format(name, semantic_version, current_date) if isComponent: changelog_content = self.generate_component_changelog() print("creates component changelog") else: changelog_content = self.generate_product_changelog() print("creates product changelog") if changelog_content: print("") else: sys.exit( "ERROR: An Issue occured while trying to generate the changelog" ) self.push_to_confluence(space_key, parent_page_id, page_title, changelog_content) def generate_component_changelog(self, component_name, product_name, version): release = self.bitbucket_service.get_release(product_name, component_name, version) changelog_content = self.component_changelog_template.replace( "%component_version%", version) changelog_content = changelog_content.replace("%jira_tasks%", version) return changelog_content def generate_product_changelog(self): changelog_content = "" return changelog_content def push_to_confluence(self, space_key, parent_page_id, title, content): converted_content = self.confluence.convert_wiki_to_storage( content)["value"] try: self.confluence.create_page(space_key, title, converted_content, parent_page_id, 'page', 'storage', 'v2') print("Page \"{0}\" is pushed to confluence".format(title)) except HTTPError: print() sys.exit( "ERROR: You may not have the permission to access this page or missing argument" ) def load_releasenote_template(self, file_path): try: file = open(file_path, encoding='utf-8', mode="r") self.releasenote_template = file.read() except IOError: print("Warning: Releasenote template file is missing.") def load_product_changelog_template(self): try: file = open("templates/product-changelog-template.gdlf", encoding='utf-8', mode="r") self.product_changelog_template = file.read() except IOError: print("Warning: Product changelog template file is missing.") def load_component_changelog_template(self): try: file = open("templates/component-changelog-template.gdlf", encoding='utf-8', mode="r") self.component_changelog_template = file.read() except IOError: print("Warning: Component changelog template file is missing.")
# pip install atlassian-python-api from atlassian import Confluence confluence = Confluence(url="https://asc.ubsgroup.net", username="******", password="******") print(confluence) create_result = confluence.create_page(space="AD", title="remove me", body="checking confluence api") print(create_result)
print( "########################################################################") pageTitle = "My Test Page" print("Looking for page \"" + pageTitle + "\" in space " + confluenceSpace) pageID = confluence.get_page_id(confluenceSpace, pageTitle) if pageID is None: print("Page not found: " + pageTitle + " in space " + confluenceSpace) else: print("Found page : " + pageTitle + " pageID: " + str(pageID)) print( "########################################################################") pageTitle = "My New Page " + str(random.randint(0, 100)) print("Looking for page \"" + pageTitle + "\" in space " + confluenceSpace) pageID = confluence.get_page_id(confluenceSpace, pageTitle) if pageID is None: print("Page not found: " + pageTitle + " in space " + confluenceSpace) status = confluence.create_page(space=confluenceSpace,title=pageTitle, \ body='Hello<strong>World</strong>!') if "id" in status: #print(status) print("Created page : " + str(status["id"]) + " title : \"" + status["title"] + "\" in space : " + status["space"]["name"]) else: print("Error creating page : " + pageTitle + " in space " + confluenceSpace) print(status) exit() else: print("Found already exists : " + pageTitle + " pageID: " + str(pageID)) exit()