def publish(soup, args): client = ConfluenceAPI(username=args.username, password=args.password, uri_base=args.endpoint) EXPAND_FIELDS = 'body.storage,space,ancestors,version' content = client.get_content_by_id(content_id=args.page_id, expand=EXPAND_FIELDS) content_data = { 'id': args.page_id, 'type': 'page', #'space': {'key': space_key}, 'title': 'New Page', 'body': { 'storage': { 'value': str(soup), 'representation': 'storage' } }, #'ancestors': [] if not ancestor_id else [{'id': ancestor_id}] "version": { "number": content['version']['number'] + 1 }, } client.update_content_by_id(content_data=content_data, content_id=args.page_id)
def getFileContent(accountInfo, fileID): # Create API object connect = ConfluenceAPI( accountInfo['username'], accountInfo['password'], 'https://' + accountInfo['siteDomain'] + '.atlassian.net/wiki/') # Get latest visible content from confluence instance page = connect.get_content_by_id( fileID, expand= 'body.view,childTypes.all,operations,history,metadata.currentuser') # Get content content = page['body']['view']['value'] return content
def getFile(accountInfo, fileID): """Returns complete exported file data""" # Create API object connect = ConfluenceAPI( accountInfo['username'], accountInfo['password'], 'https://' + accountInfo['siteDomain'] + '.atlassian.net/wiki/') # Get latest visible content from confluence instance page = connect.get_content_by_id( fileID, expand= 'body.view,childTypes.all,operations,history,history.lastUpdated,metadata.currentuser' ) # Convert to file info f = confluenceToFile(page, accountInfo) return f
class Chatfluence(BotPlugin): """An Err plugin chatfluence""" min_err_version = '2.0.0' # Optional, but recommended max_err_version = '3.3.0' # Optional, but recommended def get_configuration_template(self): return CONFIG_TEMPLATE def activate(self): """Triggers on plugin activation""" self.log.debug( self.config['CONFLUENCE_USERNAME'], self.config['CONFLUENCE_PASSWORD'], self.config['CONFLUENCE_ENDPOINT'] ) self.api = ConfluenceAPI( self.config['CONFLUENCE_USERNAME'], self.config['CONFLUENCE_PASSWORD'], self.config['CONFLUENCE_ENDPOINT'] ) super(Chatfluence, self).activate() @botcmd() def recent(self, mess, args): """A command that shows recent blog posts. Optionally pass in a result limit.""" if args: limit = args else: limit = self.config['LIMIT'] results = self._recent(limit=limit) endpoint = self.config['CONFLUENCE_ENDPOINT'] for result in results: posted = self._get_posted_date(result['id'])[:10] line = '{0} {1} {2}{3}'.format( posted, result['title'], endpoint.rstrip('/'), result['_links']['tinyui'] ) yield line def _recent(self, limit=None, space=None): """Method to fetch recent blog posts wihtin a space.""" if not limit: limit = self.config['LIMIT'] if not space: space = self.config['CONFLUENCE_KEYSPACE'] recent = self.api.get_content( content_type='blogpost', space_key=space, limit=limit, ) return recent['results'] @botcmd() def search(self, mess, args): """A command that searches confluence. This uses the CQL language. https://developer.atlassian.com/confdev/confluence-rest-api/advanced-searching-using-cql """ if '=' not in str(args) and '~' not in str(args): cql = 'text ~ "{0}"'.format(args) else: cql = str(args) # remove slack's smart quotes cql = cql.replace('“', '"').replace('”', '"') self.log.debug('search: {0}'.format(cql)) results = self._search(cql) for result in results: posted = self._get_posted_date(result['id'])[:10] line = '{0} {1}'.format( posted, result['title'], ) yield line self.log.debug(result) def _search(self, cql, limit=None): """Searches confluence.""" if not limit: limit = self.config['LIMIT'] results = self.api.search_content( cql_str=cql, limit=limit ) return results['results'] def _get_posted_date(self, pageid): """Gets date content was created""" page = self.api.get_content_by_id(content_id=pageid) posted = page['history']['createdDate'] return posted # TODO: can these patterns come from the configuration? @re_botcmd(pattern=r"(^| )@news?( |$)", prefixed=False, flags=re.IGNORECASE) def listen_for_news(self, msg, match): """Mention @news and I will blog it.""" content = 'Posted by {0}: \n {1}'.format(msg.frm, msg.body) title = ' '.join(msg.body.split()[0:5]) keyspace = self.config['CONFLUENCE_KEYSPACE'] result = self._post(title, content, keyspace) return result def _post(self, title, content, space): """Method to make a blog post in a space.""" try: r = self.api.create_new_content({ 'type': 'blogpost', 'title': title, 'space': {'key': space}, 'body': { 'storage': {'value': content, 'representation': 'storage' } } }) self.log.debug(r) line = 'POSTED: {0} {1} {2}/{3}'.format( r['history']['createdDate'][:10], r['title'], r['_links']['base'], r['_links']['tinyui'], ) except Exception as e: self.log.critical(e) return line
def confluence(html, metadata): # Create API object. api = ConfluenceAPI(g_user, g_pass, g_confluence_url) update_content = False url = '' current_history = 0 update_content_id = '' # Check if confluence_id is not null and refer to a valid post if metadata['confluence_id'] is not None: content = api.get_content_by_id(metadata['confluence_id']) if content is not None: update_content = True current_history = content['version']['number'] update_content_id = metadata['confluence_id'] logging.info('updating post with given id') # Check if space is available if (metadata['confluence_space'] is not None) and (not update_content): space = api.get_space_information(metadata['confluence_space']) if space is None: raise Exception('Space not found') # Check if title is unique if (metadata['title'] is not None) and (not update_content): content = api.get_content(title=metadata['title']) if content is not None: if content['size'] > 0: if g_update_title: update_content = True found_id = content['results'][0]['id'] found_post = api.get_content_by_id(found_id) if found_post is not None: current_history = found_post['version']['number'] update_content_id = found_id logging.info('Updating duplicate Title') else: raise Exception('Title is found but not updating') if not update_content: post_content = { "type": "page", "title": metadata['title'], "space": { "key": metadata['confluence_space'] }, "body": { "storage": { "value": html, "representation": "storage" } }, "ancestors": [{ "id": metadata['confluence_parent_id'] }] } res = api.create_new_content(post_content) links = res['_links'] url = links['base'] + links['webui'] else: post_content = { "id": update_content_id, "type": "page", "title": metadata['title'], "space": { "key": metadata['confluence_space'] }, "version": { "number": current_history + 1, "minorEdit": False }, "body": { "storage": { "value": html, "representation": "storage" } } } res = api.update_content_by_id(post_content, update_content_id) links = res['_links'] url = links['base'] + links['webui'] return url