有没有办法以编程方式将jira问题导出到word文档

问题描述 投票:0回答:2

我正在尝试使用 Jira 上的“导出到 word”功能,顾名思义,您可以将当前选定的 Jira 问题导出到 Word 文档中。

我在他们的 API 文档中找不到任何相关信息,所以我想知道这里是否有人能够做到这一点?

jira
2个回答
0
投票

没有官方API,但您可以重复使用网站上的Word下载链接,因为它是可定制的。只需右键单击下拉菜单中的链接并复制下载链接即可。然后,您可以使用您最喜欢的编程语言并自定义该链接,并从那里下载 Word 文档。以下示例在 Python 中使用

requests

import requests

headers = {
    "Authorization": "Bearer " + JIRA_API_TOKEN,
    "Content-Type": "application/json",
}

# For a filter
url = f"{jira_url}/sr/jira.issueviews:searchrequest-word/2/SearchRequest-2.doc?tempMax=1000"

# For a jql search query
url = f"{jira_url}/sr/jira.issueviews:searchrequest-word/temp/SearchRequest.doc?jqlQuery=project+%3D+TEST&tempMax=1000"

# For a single issue
url = f"{jira_url}/si/jira.issueviews:issue-word/TEST-1/TEST-1.doc"

response = requests.get(url, headers=headers)

with open("test.doc", "wb") as file:
    file.write(response.content)

这种方法也应该适用于所有其他出口目标。很遗憾,它没有记录,但效果很好。


0
投票

这是对我有用的解决方案

import requests
from requests.auth import HTTPBasicAuth

username = 'username'
password = 'password'

# Jira API URL for getting the currently authenticated user's details
user_url = "https://jira_server/rest/api/2/myself"

# Set up the request headers
headers = {
    'Content-Type': 'application/json',
}

# Set up the authentication
auth = HTTPBasicAuth(username, password)

# Make the request to get user details
response = requests.get(user_url, headers=headers, auth=auth)

# Check if the request was successful. DisplayName is just to know if you are authenticated
if response.status_code == 200:
    user_data = response.json()
    user_name = user_data['displayName']
    print(f"Authenticated as: {user_name}")

    # For a single issue
    issue_key = "issue"
    attachment_url = f"https://jira_server/si/jira.issueviews:issue-word/{issue_key}/{issue_key}.doc"

    # Make the request to download the file
    attachment_response = requests.get(attachment_url, headers=headers, auth=auth)

    # Check if the request was successful
    if attachment_response.status_code == 200:
 
        # Save the file
        with open(f"{issue_key}.doc", "wb") as file:
            file.write(attachment_response.content)

        print(f"File '{issue_key}' downloaded successfully.")
    else:
        print(f"Failed to download file. Status code: {attachment_response.status_code}, Response: {attachment_response.text}")

else:
    print(f"Authentication failed. Status code: {response.status_code}, response: {response.text}")
© www.soinside.com 2019 - 2024. All rights reserved.