{"id":138,"date":"2018-08-26T20:07:48","date_gmt":"2018-08-26T12:07:48","guid":{"rendered":"http:\/\/www.yinyubo.cn\/?p=138"},"modified":"2022-05-17T09:50:49","modified_gmt":"2022-05-17T01:50:49","slug":"jira-api-2","status":"publish","type":"post","link":"https:\/\/www.yinyubo.com\/?p=138","title":{"rendered":"Jira-API\u7684\u8be6\u7ec6\u4f7f\u7528\u4f8b\u5b50"},"content":{"rendered":"<p>\u4e0b\u9762\u662fJira-API\u7684\u8be6\u7ec6\u4f7f\u7528\u7684\u4f8b\u5b50\uff0c\u5305\u542b:<\/p>\n<ul>\n<li>Jira\u7684\u767b\u9646\uff0c\u901a\u8fc7jql\u6279\u91cf\u67e5\u8be2jira-issue\uff0c<\/li>\n<li>\u83b7\u5f97jira-project\u4e0b\u7684\u6240\u6709issue,assignee\u7684\u8be6\u7ec6\u4fe1\u606f,<\/li>\n<li>\u6dfb\u52a0\u548c\u66f4\u65b0defect<\/li>\n<li>\u4e0b\u8f7d\u548c\u4e0a\u4f20\u9644\u4ef6<\/li>\n<li>\u901a\u8fc7Jira\u767b\u5f55\u7684cookies\u642d\u914drequsts\u5e93\u53d1\u9001\u81ea\u5b9a\u4e49\u7684\u4e00\u4e9bhttp\u8bf7\u6c42<\/li>\n<\/ul>\n<pre class=\"lang:python decode:true\">\u00a0<\/pre>\n\n\n<pre class=\"wp-block-code\"><code># -*- coding: UTF-8 -*-\r\nimport traceback\r\nimport requests\r\nfrom atlassian import Jira\r\nfrom jira import JIRA\r\nimport globalParam\r\njira = object\r\njira2 = object\r\n#url\u662fjira\u7684\u5730\u5740\r\nurl = \"https:\/\/www.Jira.com\"\r\n# \u767b\u5f55jira\uff0c\u6210\u529f\u4e3a1\uff0c\u5931\u8d25\u4e3a0\r\ndef login(username, password):\r\n    global jira, jira2\r\n    result = 2\r\n    try:\r\n        jira = JIRA(server=url, basic_auth=(username, password), max_retries=1, timeout=400)\r\n        jira2 = Jira(url=url, username=username, password=password)\r\n        globalParam.reporter = get_current_user()\r\n    except Exception:\r\n        result = 0\r\n        return result\r\n    result = 1\r\n    return result\r\n# \u83b7\u5f97\u4e00\u4e2a\u9879\u76ee\u7684\u6240\u6709assignee\u7684\u4fe1\u606f\r\ndef get_all_assignable_users_by_project_key(project_key):\r\n    return jira2.get_all_assignable_users_for_project(project_key)\r\n#\u4e0b\u8f7d\u9644\u4ef6\u5230\u672c\u5730\r\ndef download_attachment(jira_attachment_id, filepath):\r\n    attachment = jira.attachment(jira_attachment_id)\r\n    image = attachment.get()\r\n    with open(filepath, 'wb') as f:\r\n        f.write(image)\r\n#\u5220\u9664Jira\u4e0a\u7684\u9644\u4ef6\r\ndef delete_attachment(jira_attachment_id):\r\n    jira.delete_attachment(jira_attachment_id)\r\n#\u83b7\u5f97\u5f53\u524d\u767b\u9646\u7684\u7528\u6237\u540d\r\ndef get_current_user():\r\n    return jira.myself()&#91;'displayName']\r\n#\u83b7\u5f97\u5f53\u524d\u767b\u9646\u7684\u7528\u6237\u7684Key\r\ndef get_current_eid():\r\n    return jira.myself()&#91;'key']\r\n#\u6839\u636ejira-key\u83b7\u5f97\u5f00\u53d1\u4eba\u5458\r\n# key \u53ef\u4ee5\u662fkey'JIRA-1451'\r\ndef get_assignee(self, key):\r\n    global jira\r\n    issue = jira.issue(key)\r\n    return issue.fields.assignee\r\n#\u83b7\u5f97\u6240\u6709\u7684Jira\u9879\u76ee\r\ndef get_all_project():\r\n    try:\r\n        projects = jira.projects()\r\n    except Exception:\r\n        print(traceback.format_exc())\r\n        result = 0\r\n        return result\r\n    return projects\r\n# \u6839\u636ejira-project-key\u53c2\u6570\u83b7\u5f97\u6240\u6709\u7684issue\u7684\u5404\u7c7b\u4fe1\u606f\r\ndef get_issues_by_project_key(project_key):\r\n    # \u56e0\u4e3acomponent\u4ecejira\u53d6\u51fa\u6765\u662flist\uff0c\u6240\u4ee5\u7528\u9017\u53f7\u5c06\u5b83\u5206\u5272\uff0c\u91cd\u65b0\u7ec4\u5408\u6210string\r\n    jqlstring = \"project = \" + project_key + \" AND issuetype = Defect ORDER BY updated DESC\"\r\n    try:\r\n        query = jira.search_issues(\r\n            jql_str=jqlstring,\r\n            json_result=True,\r\n            fields=\"key,summary,description,environment,comment,\"\r\n                   \"components,labels,assignee,reporter,status,attachment,updated\",\r\n            maxResults=4000)\r\n    except Exception:\r\n        print(traceback.format_exc())\r\n        result = 0\r\n        return result\r\n    issues = query&#91;\"issues\"]\r\n    return issues\r\n# \u6839\u636ejira-project-key\u53c2\u6570\u548c\u65f6\u95f4\u83b7\u5f97\u6240\u6709\u7684issue\u7684\u5404\u7c7b\u4fe1\u606f\r\ndef get_issues_by_project_key_and_time(project_key, last_synctime):\r\n    # \u56e0\u4e3acomponent\u4ecejira\u53d6\u51fa\u6765\u662flist\uff0c\u6240\u4ee5\u7528\u9017\u53f7\u5c06\u5b83\u5206\u5272\uff0c\u91cd\u65b0\u7ec4\u5408\u6210string\r\n    jqlstring = \"project = \" + project_key + \" AND updatedDate >='\" + last_synctime + \\\r\n                \"' AND issuetype = Defect ORDER BY updated DESC\"\r\n    try:\r\n        query = jira.search_issues(\r\n            jql_str=jqlstring,\r\n            json_result=True,\r\n            fields=\"key,summary,description,environment,comment,\"\r\n                   \"components,labels,assignee,reporter,status,attachment,updated\",\r\n            maxResults=4000)\r\n    except Exception:\r\n        print(traceback.format_exc())\r\n        result = 0\r\n        return result\r\n    issues = query&#91;\"issues\"]\r\n    return issues\r\n#\u6839\u636ejira-project-key\u53c2\u6570\u83b7\u5f97\u6240\u6709\u7684component\u4fe1\u606f\r\ndef get_components_by_project_key(project_key):\r\n    try:\r\n        components = jira.project_components(project_key)\r\n    except Exception:\r\n        print(traceback.format_exc())\r\n        result = 0\r\n        return result\r\n    return components\r\n#\u6839\u636ejira-project-key\u53c2\u6570\u83b7\u5f97\u6240\u6709\u7684version\u4fe1\u606f\r\ndef get_version_by_project_key(project_key):\r\n    jira_project = jira.project(project_key)\r\n    versions = jira_project.versions\r\n    return versions\r\n#\u6dfb\u52a0\u4e00\u4e2adefect\r\ndef add_defect(project_key, summary, description, steps_to_reproduce, labels, defect_severity, env, components,\r\n               frequency, product_id, version_name, assignee_eid):\r\n    jira_project = jira.project(project_key)\r\n    label_list = labels.split(\",\")\r\n    pid = jira_project.id\r\n    if isinstance(product_id, int):\r\n        product_id = str(product_id)\r\n    components_list = &#91;]\r\n    components_name = components.split(\",\")\r\n    for item in components_name:\r\n        components_list.append({\"name\": item})\r\n    issue_dict = {\r\n        'project': {'id': pid},\r\n        'summary': summary,\r\n        'description': description,\r\n        'issuetype': {'name': 'Defect'},\r\n        'labels': label_list,\r\n        'customfield_10040': {\"value\": defect_severity},  # BUG\u7684Defect Severity\u5b57\u6bb5\r\n        'customfield_10033': steps_to_reproduce,  # BUG\u7684\u63cf\u8ff0\u6b65\u9aa4\r\n        'environment': env,\r\n        'components': components_list,\r\n        'customfield_10336': {\"value\": frequency},\r\n        'customfield_13600': product_id,\r\n        'customfield_11170': {\"name\": version_name},\r\n        \"assignee\": {\"name\": assignee_eid}\r\n    }\r\n    if assignee_eid is None:\r\n        del issue_dict&#91;'assignee']\r\n    if env == \"\":\r\n        del issue_dict&#91;'environment']\r\n    if components == \"\":\r\n        del issue_dict&#91;'components']\r\n    if product_id == \"\" or product_id == \"n\":\r\n        del issue_dict&#91;'customfield_13600']\r\n    if version_name == \"\":\r\n        del issue_dict&#91;'customfield_11170']\r\n    new_issue = jira.create_issue(fields=issue_dict)\r\n    return new_issue.key\r\n#\u66f4\u65b0\u4e00\u4e2adefect\r\ndef update_defect(issue_key, summary, description, steps_to_reproduce,\r\n                  labels, defect_severity, env, components, frequency, product_id, version_name, assignee_eid):\r\n    issue = jira.issue(issue_key)\r\n    label_list = labels.split(\",\")\r\n    components_list = &#91;]\r\n    components_name = components.split(\",\")\r\n    if components == \"\":\r\n        pass\r\n    for item in components_name:\r\n        components_list.append({\"name\": item})\r\n    issue_dict = {\r\n        'summary': summary,\r\n        'description': description,\r\n        'issuetype': {'name': 'Defect'},\r\n        'labels': label_list,\r\n        'customfield_10040': {\"value\": defect_severity},  # BUG\u7684Defect Severity\u5b57\u6bb5\r\n        'customfield_10033': steps_to_reproduce,  # BUG\u7684\u63cf\u8ff0\u6b65\u9aa4\r\n        'environment': env,\r\n        'components': components_list,\r\n        'customfield_10336': {\"value\": frequency},\r\n        'customfield_13600': str(product_id),\r\n        'customfield_11170': {\"name\": version_name},\r\n        \"assignee\": {\"name\": assignee_eid}\r\n    }\r\n    if assignee_eid is None:\r\n        del issue_dict&#91;'assignee']\r\n    if env == \"\":\r\n        del issue_dict&#91;'environment']\r\n    if components == \"\":\r\n        del issue_dict&#91;'components']\r\n    if version_name == \"\":\r\n        del issue_dict&#91;'customfield_11170']\r\n    update_issue = issue.update(fields=issue_dict)\r\n    cccc = update_issue\r\n    return issue_key\r\n#\u5c06\u4e00\u4e2a\u672c\u5730\u7684\u9644\u4ef6\u4e0a\u4f20\u5230Jira\u4e0a\r\ndef add_attachment(issue, path, filename):\r\n    newpath = path.encode('utf-8')\r\n    cc = unicode(newpath, \"utf-8\")\r\n    result = jira.add_attachment(issue=issue, attachment=cc, filename=filename)\r\n    return result\r\n#\u6839\u636ejira-key\u83b7\u5f97\u8be5issue\u6240\u6709\u7684\u9644\u4ef6\r\ndef get_attachment(issue_key):\r\n    issue = jira.issue(issue_key)\r\n    return issue.fields.attachment\r\n#\u901a\u8fc7\u9879\u76eeproject_key\u503c\uff0c\u6293\u53d6\u9875\u9762\u4e0a\u7684\u4e00\u4e9b\u81ea\u5b9a\u4e49\u53c2\u6570\uff0c\u4f8b\u5982\u81ea\u5b9a\u4e49\u53c2\u657013600\uff0c\u5bf9\u5e94products\u53c2\u6570\r\ndef get_products_by_project_key(project_key):\r\n    product_map = {}\r\n    jiraproject = jira.project(project_key)\r\n    #jira._session.cookies\u53ef\u4ee5\u7528\u6765\u642d\u914drequests\u5e93\uff0c\u53d1\u9001\u8bf7\u6c42\r\n    cookies = jira._session.cookies\r\n    body = {\"pid\": jiraproject.id}\r\n    url = \"https:\/\/www.jira.com\/secure\/QuickCreateIssue!default.jspa?decorator=none\"\r\n    result = requests.post(url, data=body, cookies=cookies,\r\n                           headers={\"Content-type\": \"application\/x-www-form-urlencoded\"})\r\n    html = result.text\r\n    try:\r\n        select1 = html.split('\"id\":\"customfield_13600\",\"label\":\"Product\/s\",\"required\":false,')&#91;1]\r\n        select2 = select1.split('&lt;\/select>')&#91;0]\r\n        if not select2.__contains__(\"\\\\n\\\\tNone\\\\n\\\\n\"):\r\n            select3 = select2.split('&lt;option')\r\n            c = select3\r\n            for item in select3:\r\n                if item.__contains__('value='):\r\n                    patern_id = item.split('value=\\\\\"')&#91;1]\r\n                    id = patern_id.split('\\\\\"')&#91;0]\r\n                    patern_value = item.split(\"\\\\n    \")\r\n                    value = patern_value&#91;1].strip()\r\n                    product_map&#91;id] = value\r\n    except Exception:\r\n        # print(traceback.format_exc())\r\n        pass\r\n    return product_map\r\n#\u7ed9\u4e00\u4e2ajira-issue\u6dfb\u52a0comment\r\ndef add_comment(issue_key, comment_body):\r\n    comment = jira.add_comment(issue_key, comment_body)\r\n    return comment.id\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u662fJira-API\u7684\u8be6\u7ec6\u4f7f\u7528\u7684\u4f8b\u5b50\uff0c\u5305\u542b: Jira\u7684\u767b\u9646\uff0c\u901a\u8fc7jql\u6279\u91cf\u67e5\u8be2jira-issue\uff0c \u83b7\u5f97 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-138","post","type-post","status-publish","format-standard","hentry","category-jira"],"_links":{"self":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/138","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=138"}],"version-history":[{"count":1,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/138\/revisions"}],"predecessor-version":[{"id":1011,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/138\/revisions\/1011"}],"wp:attachment":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=138"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=138"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=138"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}