利用jira-python及selenium完成jira的统计报表及日报的填写

最近需要统计公司项目的jira项并需要将统计内容更新到confluence上,故利用jira-python及selenium编写相应的自动化脚本,避免重复性工作。

  1 from selenium import webdriver
  2 from selenium.webdriver.common.by import By
  3 from jira.client import JIRA
  4 import datetime
  5 
  6 driver = webdriver.Chrome()
  7 driver.implicitly_wait(10)
  8 
  9 def run():
 10     """
 11     login confluence
 12     :return: None
 13     """
 14     driver.get(confluence_url)
 15     login()
 16     click_key('#space-menu-link')
 17     driver.find_element_by_partial_link_text('data center').click()
 18     click_key('#plusminus3773763-0')
 19     click_key('#plusminus3774539-0')
 20     click_key('#plusminus3774542-0')
 21     click_key('#plusminus13533496-0')
 22     current_time = datetime.datetime.now().strftime("%Y%m%d")
 23     try:
 24         result = driver.find_element_by_partial_link_text('NSTS ({})'.format(current_time))
 25     except:
 26         result = None
 27     if result is None:
 28         create_new_page(0)
 29     else:
 30         result.click()
 31         click_key('#editPageLink')
 32     fill_data()
 33     click_key('#rte-button-publish')
 34 
 35 def login():
 36     """
 37     Keep the login status alive
 38     :return: None
 39     """
 40     result = driver.find_element(By.CSS_SELECTOR, '#loginButton')
 41     if result:
 42         input_key('#os_username', username)
 43         input_key('#os_password', password)
 44         click_key('#loginButton')
 45 
 46 def input_key(css_path, content):
 47     """
 48     Enter information into the input box.
 49     :param css_path: Locate the input box.
 50     :param content: Information for the input box.
 51     :return: None
 52     """
 53     driver.find_element(By.CSS_SELECTOR, css_path).clear()
 54     driver.find_element(By.CSS_SELECTOR, css_path).send_keys(content)
 55 
 56 def click_key(css_path):
 57     """
 58     Click the corresponding button
 59     :param css_path: Button path
 60     :return: None
 61     """
 62     driver.find_element(By.CSS_SELECTOR, css_path).click()
 63 
 64 def create_new_page(days):
 65     """
 66     Add a daily page if it is missing
 67     :param days: Daily's interval days
 68     :return: None
 69     """
 70     days += 1
 71     page_time = datetime.datetime.now() + datetime.timedelta(days=days)
 72     page_time = page_time.strftime("%Y%m%d")
 73     result = driver.find_element_by_partial_link_text('NSTS ({})'.format(page_time))
 74     if len(result) == 0:
 75         create_new_page(days)
 76     else:
 77         result.click()
 78         click_key('#action-menu-link')
 79         click_key('#action-copy-page-link')
 80         click_key('#copy-dialog-next')
 81         current_time = datetime.datetime.now().strftime("%Y%m%d")
 82         input_key('#content-title', 'NSTS ({})'.format(current_time))
 83         input_key('#tinymce>h2', '{}日报'.format(current_time))
 84 
 85 def jira_statistics(status=None, priority=None, severity=None):
 86     """
 87     Today's statistics of jira
 88     :return: The result of today's jira statistics
 89     """
 90     jira = JIRA(server=jira_url, basic_auth=(username, password))
 91     priority = 'and priority={}'.format(priority) if priority else ''
 92     severity = 'and 严重级别={}'.format(severity) if severity else ''
 93     if status:
 94         status = '({}, reopened)'.format(status) if status == 'open' else '({})'.format(status)
 95         issues_in_proj = jira.search_issues('project=NSTS  and status in {status} and issuetype="故障" and updated >= "-24H" {priority} {severity}'.format(status=status, priority=priority, severity=severity),
 96                                             maxResults=100)
 97     else:
 98         issues_in_proj = jira.search_issues('project=NSTS  and status in (open, reopened) and issuetype="故障" and updated < "-24H" {priority} {severity}'.format(priority=priority, severity=severity), maxResults=100)
 99     return len(issues_in_proj)
100 
101 def jira_result():
102     """
103     Summary the statistics of jira
104     :return: Summary result
105     """
106     result = []
107     # Different priority bug statistics
108     dif_pri_bugs = []
109     # Different severity bug statistics
110     dif_sev_bugs = []
111     # Covariance item
112     covariance_item = ['open', 'resolved', 'closed', None]
113     # Covariance item of priority
114     cov_item_of_pri = ['P0-Highest', 'P1-High', 'P2-Medium', 'P3-Low', 'P4-Lowest', None]
115     # Covariance item of severity
116     cov_item_of_sev = ['S0-Blocker', 'S1-Major', 'S2-Normal', 'S3-Minor', 'S4-Enhancement', None]
117     for k, v in enumerate(covariance_item):
118         dif_pri_bug = []
119         for x, y in enumerate(cov_item_of_pri):
120             dif_pri_bug.append(jira_statistics(status=v, priority=y))
121         dif_pri_bugs.append(dif_pri_bug)
122         dif_sev_bug = []
123         for i, j in enumerate(cov_item_of_sev):
124             dif_sev_bug.append(jira_statistics(status=v, severity=j))
125         dif_sev_bugs.append(dif_sev_bug)
126     result.append(dif_pri_bugs)
127     result.append(dif_sev_bugs)
128     return result
129 
130 def fill_data():
131     """
132     Fill the statistical results of jira into the table
133     :return: None
134     """
135     results = jira_result()
136     driver.switch_to.frame('wysiwygTextarea_ifr')
137     for index, result in enumerate(results):
138         for i, j in enumerate(result):
139             for x, y in enumerate(j):
140                 first_child = 15 if index == 0 else 18
141                 sencond_child = i + 1
142                 third_child = x + 2
143                 # fill data into bugfix table
144                 init_js = 'document.querySelector(\'#tinymce > table:nth-child({}) > tbody > tr:nth-child({}) > td:nth-child({})\').innerText={}; '.format(first_child, sencond_child, third_child, y)
145                 driver.execute_script(init_js)
146     driver.switch_to.default_content()
147 
148 if __name__ == '__main__':
149     run()
150     driver.quit()

统计结果样例如下:

 利用jira-python及selenium完成jira的统计报表及日报的填写_第1张图片

 

转载于:https://www.cnblogs.com/Goongo/p/9898365.html

你可能感兴趣的:(利用jira-python及selenium完成jira的统计报表及日报的填写)