Using Jira REST API v2 to Retrieve Wiki Table Content

Using Jira REST API v2 to Retrieve Wiki Table Content

Using Jira REST API v2 to Retrieve Wiki Table Content

Popular project management software Jira exposes strong REST API for querying and manipulating a large set of data points across Jira projects. It is customary for developers and administrators to have a task to get data of wiki-formatted table from Jira issues. This article will help you to understand, how to use Jira REST API v2 in order to get and analyze such data.

Prerequisites

Before getting started, ensure you have the following: Jira Account: A user with a valid Jira account, that has the necessary permission level to access the content of the target issue.

API Token: Get an API token for authentication if you are in the Jira Cloud version.

REST Client: Postman, curl and any HTTP library of your programming language to do REST API calls.

Base URL: The base URL of your Jira instance, for example, you can use the address like this – https://your-domain.atlassian.net.

Step 1: This understanding of the API Endpoint is important because it forms the core of the API, as it will contain all the elements of the API. To retrieve the content of a Jira issue, use the following endpoint:

POST /rest/api/2/issue/{issueIdOrKey}

In its stead, use {issueIdOrKey} and refer to the particular issue identification number or key, as in, PROJ-123. The response will be a JSON object that consists of the details of the issue, especially the field containing the wiki formatted contents is stored in the description field.

Step 2: Authenticating the Request

It is important to note that, Jira REST API primarily requires authentication before one can make any API request from it. Use Basic Authentication with your email and API token: Encode your credentials (email:api_token) in Base64. Add an Authorization header to your request: Authorization: Basic base64_encoded_credentials

Step 3: Fetching the Issue Data

Here’s an example of using curl to fetch issue data:

curl -X GET \
-H “Authorization: Basic <base64_encoded_credentials>” \
-H “Content-Type: application/json” \
“https://your-domain.atlassian.net/rest/api/2/issue/PROJ-123”

Step 4: The content of the table of Wiki

||Header 1||Header 2||
|Row 1, Col 1|Row 1, Col 2|
|Row 2, Col 1|Row 2, Col 2|

The issue description, often stored in the fields.description field, may include wiki-formatted tables, which look like this: This format uses || to portray headers and | for normal cell. Step

5: Parsing the Wiki Table

As with many wiki tables, you can parse the content using a JavaScript or Python script transformed into JSON or even HTML. Here’s an example of parsing it using Python:

import re

def parse_wiki_table(wiki_text):
lines = wiki_text.strip().split(‘\n’)
headers = re.findall(r’\|\|(.+?)\|\|’, lines[0])
rows = [
re.findall(r’\|(.+?)\|’, line) for line in lines[1:] if line.startswith(‘|’)
]
return {“headers”: headers, “rows”: rows}

wiki_table = “””
||Header 1||Header 2||
|Row 1, Col 1|Row 1, Col 2|
|Row 2, Col 1|Row 2, Col 2|
“””

parsed_table = parse_wiki_table(wiki_table)
print(parsed_table)

 

                                                                                                          Output:

{
“headers”: [“Header 1”, “Header 2”],
“rows”: [
[“Row 1, Col 1”, “Row 1, Col 2”],
[“Row 2, Col 1”, “Row 2, Col 2”]
]
}

 

Step 6: Using the Parsed Data

You can now use the structured data for various purposes, such as generating reports, feeding into other systems, or transforming it into another format like HTML:

def convert_to_html_table(parsed_table):
html = “<table><thead><tr>”
html += “”.join(f”<th>{header}</th>” for header in parsed_table[“headers”])
html += “</tr></thead><tbody>”
for row in parsed_table[“rows”]:
html += “<tr>” + “”.join(f”<td>{cell}</td>” for cell in row) + “</tr>”
html += “</tbody></table>”
return html

html_table = convert_to_html_table(parsed_table)
print(html_table)

FAQs

Q: What permissions does one need to get in order to use the API to retrieve issue data?

A: For plain text on the web page, you simply need “Browse Projects” permission for the project including the issue.

Q: How do I consume paginated result in the context of the Jira API?

A: As it will be shown, paginated data can be controlled via two query parameters: startAt and maxResults.

Q: Is it possible to get answers to data from more than one issue in a single call?

A: Yes, use the JQL endpoint to get all the issues for a query you want.

Q: Can the wiki formatted tables be edited using API?

A: Yes, you do it by making a PUT call to rest/api/2/issue/{issueIdOrKey} to update the description field of the issue.

Q: What are the recommended libraries for Jira API calling?

A: There are some famous libraries today, for example, for the Python programming language, there is jira for, for Java programming language, there is atlassian-jira and for Node.js programming language, there is node-jira-client.

Conclusion

Specifically, the Jira REST API v2 enables fetching of wiki-formatted table content that can be further preprocessed in any way necessary. By applying good authentication and parsing logic, tranla;ating Jira issue data becomes pertinent to meaningful outcomes for your workflows.

 

ALSO READ THIS: jira show subtasks on list view team managed

Leave a Reply

Your email address will not be published. Required fields are marked *