Confluence api python example

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Atlassian Python REST API wrapper

License

atlassian-api/atlassian-python-api

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Читайте также:  Php one based array

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

* Python2: Remove trailing commas after ** unpacking operators In Python2 it's not valid to have a comma after a final argument using the ** unpacking operator. Add fmt: skip to preserve Black formatting. * Python2: Use six.moves.urllib

Git stats

Files

Failed to load latest commit information.

README.rst

Atlassian Python API wrapper

The atlassian-python-api library provides a simple and convenient way to interact with Atlassian products (such as Jira Service management, Jira Software, Confluence, Bitbucket and apps Insight, X-Ray) using Python. It is based on the official REST APIs of these products, as well as additional private methods and protocols (such as xml+rpc and raw HTTP requests). This library can be used to automate tasks, integrate with other tools and systems, and build custom applications that interact with Atlassian products. It supports a wide range of Atlassian products, including Jira, Confluence, Bitbucket, StatusPage and others, and is compatible with both Atlassian Server and Cloud instances.

Overall, the atlassian-python-api is a useful tool for Python developers who want to work with Atlassian products. It is well-documented and actively maintained, and provides a convenient way to access the full range of functionality offered by the Atlassian REST APIs.

$ pip install atlassian-python-api
  • Git clone repository
  • Use pip install -r requirements.txt to install the required packages
  • or pipenv install && pipenv install —dev

More examples in examples/ directory.

Here’s a short example of how to create a Confluence page:

from atlassian import Confluence confluence = Confluence( url='http://localhost:8090', username='admin', password='admin') status = confluence.create_page( space='DEMO', title='This is the title', body='This is the body. You can use HTML tags!') print(status)

Please, note Confluence Cloud need to be used via token parameter. And here’s another example of how to get issues from Jira using JQL Query:

from atlassian import Jira jira = Jira( url='http://localhost:8080', username='admin', password='admin') JQL = 'project = DEMO AND status IN ("To Do", "In Progress") ORDER BY issuekey' data = jira.jql(JQL) print(data)

Also, you can use the Bitbucket module e.g. for getting project list

from atlassian import Bitbucket bitbucket = Bitbucket( url='http://localhost:7990', username='admin', password='admin') data = bitbucket.project_list() print(data)

Now you can use the Jira Service Desk module. See docs. Example to get your requests:

from atlassian import ServiceDesk sd = ServiceDesk( url='http://localhost:7990', username='admin', password='admin') data = sd.get_my_customer_requests() print(data)

Using Insight (CMDB Tool for Jira):

from atlassian import Insight insight = Insight( url='http://localhost:7990', username='admin', password='admin') data = insight.get_object(88) print(data)

Using Xray (Test Management tool for Jira):

from atlassian import Xray xr = Xray( url='http://localhost:7990', username='admin', password='admin') data = xr.get_tests('TEST-001') print(data)
from atlassian import Bamboo bamboo = Bamboo( url='http://localhost:6990/bamboo/', token="") data = bamboo.get_elastic_configurations() print(data)

If you want to see the response in pretty print format JSON. Feel free for use construction like:

from pprint import pprint # you code here # and then print using pprint(result) instead of print(result) pprint(response)

First of all, I am happy for any PR requests. Let’s fork and provide your changes 🙂 See the Contribution Guidelines for this project for details on how to make changes to this library.

In addition to all the contributors we would like to thank these vendors:

  • Atlassian for developing such a powerful ecosystem.
  • JetBrains for providing us with free licenses of PyCharm
  • Microsoft for providing us with free licenses of VSCode
  • GitHub for hosting our repository and continuous integration

About

Atlassian Python REST API wrapper

Источник

Confluence REST API for reading and updating wiki pages

Alexander V. Leonov

Alexander V. Leonov

In previous posts I wrote how to automate the work with Atlassian Jira, including automated ticket labeling. Now let’s try to use REST API of another popular Atlassian product – Confluence wiki engine.

Confluence REST API

What you may want to automate in Confluence? Obviously, it may be useful to read the pages that your colleagues regularly update and then use this data in some scripts as an input. You may also want to update your own Confluence pages, for example to post Vulnerability Scanning results. 😉

Reading pages

You can read the page by Id using this function:

import json import requests def get_page_json(page_id, expand = False): if expand: suffix = "?expand=" + expand #body.storage else: suffix = "" url="https://confluence.corporation.com/rest/api/content/" + page_id + suffix response = requests.get(url, auth=(user, password)) response.encoding = "utf8" return json.loads(response.text) print(get_page_json("287346883", "body.storage"))

Without “?expand=body.storage” Confluence will return detailed information about the version, but will not return the full content of the page to body -> storage -> value . 😉

As you can see, JSON data is very convenient. You can easily read the title and html content:

print(json_data['title']) print(json_data['body']['storage']['value'])

Editing

OK. How can we change the content of this page? We need to send some minimal JSON to the Confluence API, for example using this function:

def set_page_json(page_id,json_content): headers = < 'Content-Type': 'application/json', >response = requests.put("https://confluence.corporation.com/rest/api/content/" + page_id, headers=headers, data=json.dumps(json_content), auth=(user, password)) return(response.text)

What should be the structure of this JSON?

json_data = functions_confluence.get_page_json("277387103") new_json_data = dict() new_json_data['id'] = json_data['id'] new_json_data['type'] = json_data['type'] new_json_data['title'] = json_data['title'] new_json_data['type'] = json_data['type'] new_json_data['version'] = if not 'key' in json_data: new_json_data['key'] = json_data['space']['key'] else: new_json_data['key'] = json_data['key'] new_json_data['body'] = This is the updated text for the new page

','representation':'storage'>>

There should be something like this in new_json_data:

This is the updated text for the new page

‘>>, ‘title’: u’Test page’, ‘version’: , ‘key’: u’INFO’, ‘type’: u’page’, ‘id’: u’277387103’>

The response after sending JSON will be:

And the content of the page should change.

Tables

What if we try to add a table?

new_json_data[‘body’] =

head1 head2
value1 value2
value3 value4

‘,’representation’:’storage’>>

Confluence table example

Images and other attacments

Unfortunately, it’s not possible to send a picture in base64 this way =(

What if I attach a file to a page and use a link to it in code? It’s possible.

I’m uploading the file using this function:

def attach_file_to_page(page_id, filepath, filename): headers = < 'X-Atlassian-Token': 'no-check', >files = < 'file': (filename, open(filepath, 'rb')), >#delete attach with the same name response = requests.get("https://confluence.corporation.com/rest/api/content/" + page_id + "/child/attachment", auth=(user, password)) for attachment in json.loads(response.text)['results']: if attachment['title'] == filename: requests.delete("https://confluence.corporation.com/rest/api/content/" + attachment['id'], headers=headers, auth=(user, password)) #attack files response = requests.post("https://confluence.corporation.com/rest/api/content/" +page_id+ "/child/attachment", headers=headers, files=files, auth=(user, password)) return(response.text)

Note that before you upload a file, you need to verify that the file with such name is not attached already or you will get an error. So I check all the attached files and delete files with the specified name.

URL of the file will be https://confluence.corporation.com/download/attachments/277387103/test.jpg and we can use it in code:

functions_confluence.attach_file_to_page("277387103", "data/test.jpg", "test.jpg") img_url = "https://confluence.corporation.com/download/attachments/277387103/test.jpg" new_json_data['body'] = ','representation':'storage'>>

And test.jpg will appear on the page:

Confluence image attached

Hi! My name is Alexander and I am a Vulnerability Management specialist. You can read more about me here. Currently, the best way to follow me is my Telegram channel @avleonovcom. I update it more often than this site. If you haven’t used Telegram yet, give it a try. It’s great. You can discuss my posts or ask questions at @avleonovchat.

А всех русскоязычных я приглашаю в ещё один телеграмм канал @avleonovrus, первым делом теперь пишу туда.

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Examples of using atlassian-python-api

rajdor/pythonConfluenceExamples

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

  • Click the page ellipsis and select Page Information
  • The page id is found in the web browser url
i.e. http://192.168.1.224:8090/pages/viewinfo.action?pageId=10256402 

Hot to get macros and other confluence page source

i.e. http://192.168.1.224:8090/rest/api/content/10256402?expand=body.storage or (depending on your server configuration) http://192.168.1.224:8090/confluence/rest/api/content/10256402?expand=body.storage 
  • Option 2, copy and modify the included script updatePage.py to print the returned values from the following snippet
contents=confluence.get_page_by_id(pageID, expand="body.storage,version", status="current") 
< "confluence":"http://192.168.1.224:8090" ,"user":"username" ,"password":"password" ,"space":"HOME" ,"page":"My Test Page" >
  • Verify Space exists
  • Get Page ID by Space and Page Title
  • Create Page in Space with Page Title and return Page ID
  • Get Page ID by Space and Page Title
  • Get Page contents and properties by Page ID
  • Update page body and title by Page ID
  • Get Page ID by Space and Page Title
  • Generate random data for ‘Internet download speeds’
  • Use confluence chart macro and html table for page body
  • Update page body and title by Page ID

example chart output

About

Examples of using atlassian-python-api

Источник

Оцените статью