Skip to content

Instantly share code, notes, and snippets.

@1010sachin
Last active September 11, 2018 22:22
Show Gist options
  • Save 1010sachin/c282e6a1a3562e9b23f343cbc1ec11d3 to your computer and use it in GitHub Desktop.
Save 1010sachin/c282e6a1a3562e9b23f343cbc1ec11d3 to your computer and use it in GitHub Desktop.
Post the scrummary report to confluence

Post scrummary report to Confluence

It is assumed that the user of this script has:

  1. Manta client installed locally. (includes json) (https://apidocs.joyent.com/manta/index.html)
  2. Access to Joyent_Dev account (in order to get the scrummary report).

Instructions to get the scrummary report

From the terminal, get the scrummary report, called as all1999.html and store it locally, as:

mget ~~/stor/scrum/2018/09/11/all1999.html > scrum_out.html

Once you have the html report, copy the following code snippet into a new python script, say srummin.py. Before that, please make sure you have python installed alongwith the following packages for python:

  • requests
  • json
  • argparse
  • getpass
  • markdown
  • bs4

(All the above packages can be easily installed using pip)

In order to run the snippet provide it the arguments as:

python scrummin.py -u <user_name> -p <password> -url <confluence url> -k <confluence space key> -f <complete path to the all1999.html>

Code snippet for the scrummin.py

import requests
import json
import argparse
import getpass
import markdown
from requests.auth import HTTPBasicAuth
from bs4 import BeautifulSoup

class Password:

    DEFAULT = 'Prompt if not specified'

    def __init__(self, value):
        if value == self.DEFAULT:
            value = getpass.getpass('Confluence Password: ')
        self.value = value

    def __str__(self):
        return self.value

parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)

parser.add_argument(
    "-u",
    "--user",
    required=True,
    help="Specify the username to log into Confluence")

parser.add_argument(
    "-p",
    "--password",
    type=Password,
    default=Password.DEFAULT,
    help="Specify the password to log into Confluence")

parser.add_argument(
    "-url",
    "--url",
    required=True,
    help="Specify the Confluence URL alongwith the port. Eg: http://confluence.server.com:8090")

parser.add_argument(
    "-f",
    "--file",
    required=True,
    help="Specify the scrum html file to be used. If absolute path to the file is not specified then it will be assumed to in the current directory")

parser.add_argument(
    "-k",
    "--key",
    required=True,
    help="Specify the Confluence Space key")

options = parser.parse_args()

user = options.user
password = options.password
url = options.url
file = options.file
spaceKey = options.key

auth = HTTPBasicAuth(user, password)

with open(file) as fp:
    soup = BeautifulSoup(fp, "html.parser")

#Get rid of the style tag
soup.style.decompose()

title = soup.title.string

#Get rid of the title
soup.title.decompose()

#Extract the text from the html. This is still in markdown
htmlText = soup.getText()

#Convert the markdown in to xhtml format that confluence loves
html = markdown.markdown(htmlText, output_format='xhtml')

#Ask confluence to convert our version of html into its own
dataToConvertToStorageFormat = {
    'value' : html,
    'representation' : 'editor'
    }

r = requests.post(url+'/rest/api/contentbody/convert/storage', data=json.dumps(dataToConvertToStorageFormat), auth=auth, headers={"Content-Type": "application/json"})

viewFormattedString = r.json()

viewFormattedStringValue = viewFormattedString['value']

viewFormattedStringValue = str(viewFormattedStringValue)

data = {
    'type': 'page',
    'title': str(title),
    'space': {
        'key': str(spaceKey)
    },
    'body': {
        'storage' : {
            'value': viewFormattedStringValue,
            'representation': 'storage'
        }
    }
}

data = json.dumps(data)
response = requests.post(url+'/rest/api/content/', data=data, headers={"Content-Type": "application/json"}, auth=auth)

response.raise_for_status()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment