Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Containerize and refactor #1

Merged
merged 14 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename variables and files
  • Loading branch information
ottolote committed Apr 22, 2024
commit 1a666edb8b1a75e333c68f7a24e6e8201b9574db
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import yaml

def getConfig():

def get_config():
with open("./publisher/config/config.yaml", "r") as yamlFileConfig:
return yaml.safe_load(yamlFileConfig)

48 changes: 28 additions & 20 deletions publisher/main.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
import argparse
import logging

from config.getconfig import getConfig
from pagesController import deletePages, searchPages
from pagesPublisher import publishFolder
from config.get_config import get_config
from pages_controller import delete_pages, search_pages
from pages_publisher import publish_folder

logging.basicConfig(level=logging.INFO)


# Parse arguments with LOGIN and PASSWORD for Confluence
parser = argparse.ArgumentParser()
parser.add_argument(
'--login', help='Login with "" is mandatory', required=True)
parser.add_argument(
'--password', help='Password with "" is mandatory', required=True)
args = parser.parse_args()
inputArguments = vars(args)
def main():
# Parse arguments with LOGIN and PASSWORD for Confluence
parser = argparse.ArgumentParser(
description='Publish markdown files to Confluence.')
parser.add_argument('--login', required=True,
help='Confluence login username.')
parser.add_argument('--password', required=True,
help='Confluence login password.')
args = parser.parse_args()

# Extract the arguments for easier access
login = args.login
password = args.password

CONFIG = getConfig()
# Load configuration
config = get_config()
logging.debug(config)

logging.debug(CONFIG)
# Search and delete pages
pages = search_pages(login=login, password=password)
delete_pages(pages_id_list=pages, login=login, password=password)

pages = searchPages(
login=inputArguments['login'], password=inputArguments['password'])
deletePages(pagesIDList=pages,
login=inputArguments['login'], password=inputArguments['password'])
# Publish the markdown files from the specified folder
publish_folder(
folder=config["github_folder_with_md_files"], login=login, password=password)


if __name__ == '__main__':
main()

publishFolder(folder=str(CONFIG["github_folder_with_md_files"]),
login=inputArguments['login'],
password=inputArguments['password'])
92 changes: 65 additions & 27 deletions publisher/pagesController.py → publisher/pages_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import requests
from urllib3.exceptions import InsecureRequestWarning
from requests.auth import HTTPBasicAuth
from config.getconfig import getConfig
from config.get_config import get_config

CONFIG = getConfig()
CONFIG = get_config()


# Suppress only the single warning from urllib3 needed.
Expand All @@ -16,7 +16,7 @@
#


def createPage(title, content, parentPageID, login, password):
def create_page(title, content, parentPageID, login, password):

# descripe json query
newPageJSONQueryString = """
Expand Down Expand Up @@ -91,7 +91,7 @@ def createPage(title, content, parentPageID, login, password):
#
# Function for searching pages with SEARCH TEST in the title
#
def searchPages(login, password):
def search_pages(login, password):
# make call using Confluence query language
# GET /rest/api/search?cql=text~%7B%22SEARCH%20PATTERN%22%7D+and+type=page+and+space=%2212345%22&limit=1000 HTTP/1.1" 200
# "cqlQuery": "parent=301176119 and text~{\"SEARCH PATTERN\"} and type=page and space=\"12345\""
Expand Down Expand Up @@ -133,11 +133,11 @@ def searchPages(login, password):
#
# Function for deleting pages
#
def deletePages(pagesIDList, login, password):
def delete_pages(pages_id_list, login, password):

deletedPages = []

for page in pagesIDList:
for page in pages_id_list:
logging.info("Delete page: " + str(page))
logging.debug("Calling URL: " +
str(CONFIG["confluence_url"]) + "content/" + str(page))
Expand All @@ -156,35 +156,73 @@ def deletePages(pagesIDList, login, password):
#


def attachFile(pageIdForFileAttaching, attachedFile, login, password):
def attach_file(page_id, attached_file, login, password):
"""
Attach a file to a Confluence page.

Args:
page_id (str): ID of the Confluence page to attach the file to.
attached_file (file): The file to be attached.
login (str): The login username for authentication.
password (str): The login password for authentication.

Returns:
str: The ID of the attached file or None if the attachment failed.
"""

# make call to attache fale to a page
logging.debug("Calling URL: " + str(CONFIG["confluence_url"]) + "content/" + str(
pageIdForFileAttaching) + "/child/attachment")
# Construct the API endpoint URL
api_url = f"{CONFIG['confluence_url']}content/{page_id}/child/attachment"

attachedFileStructure = {'file': attachedFile}
attachedValues = {'comment': 'file was attached by the script'}
attachedHeader = {"Accept": "application/json",
"X-Atlassian-Token": "nocheck"} # disable token check. Otherwise it will be 443 status code
# Log the API call
logging.debug(f"Calling URL: {api_url}")

# Set up file and comment data, headers, and disable SSL verification
attached_file_structure = {'file': attached_file}
attached_values = {'comment': 'File was attached by the script'}
attached_header = {
"Accept": "application/json",
"X-Atlassian-Token": "nocheck" # Disable token check to avoid 403 status code
}

# Make the POST request to attach the file
response = requests.post(
url=CONFIG["confluence_url"] + "content/" +
str(pageIdForFileAttaching) + "/child/attachment",
files=attachedFileStructure,
data=attachedValues,
url=api_url,
files=attached_file_structure,
data=attached_values,
auth=HTTPBasicAuth(login, password),
headers=attachedHeader,
verify=False)
headers=attached_header,
verify=False # Not recommended in production
)

# Log the response status code
logging.debug(response.status_code)

if response.status_code == 200:
# Log success and parse JSON response
logging.info("File was attached successfully")
response_data = json.loads(response.text)
logging.debug(json.dumps(response_data, indent=4, sort_keys=True))

# Extract and return the ID of the attached file
attached_file_id = response_data['results'][0]['id']
logging.debug(f"Returning attached file id: {attached_file_id}")
return attached_file_id
else:
# Log failure and return None
logging.error("File has not been attached")
return None

if response.status_code == 200:
# Log success and parse JSON response
logging.info("File was attached successfully")
logging.debug(json.dumps(json.loads(
response.text), indent=4, sort_keys=True))
response_data = json.loads(response.text)
logging.debug(json.dumps(response_data, indent=4, sort_keys=True))

# return id of the attached file
logging.debug("Returning attached file id: " +
json.loads(response.text)['results'][0]['id'])
return json.loads(response.text)['results'][0]['id']
# Extract and return the ID of the attached file
attached_file_id = response_data['results'][0]['id']
logging.debug(f"Returning attached file id: {attached_file_id}")
return attached_file_id
else:
logging.error("File has not attached")
# Log failure and return None
logging.error("File has not been attached")
return None
46 changes: 23 additions & 23 deletions publisher/pagesPublisher.py → publisher/pages_publisher.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@
import os
import markdown
import re
from config.getconfig import getConfig
from pagesController import createPage
from pagesController import attachFile
from config.get_config import get_config
from pages_controller import create_page
from pages_controller import attach_file


CONFIG = getConfig()
CONFIG = get_config()


# parentPageID has the default input parameter "None" (it means ROOT)
def publishFolder(folder, login, password, parentPageID=None):
def publish_folder(folder, login, password, parentPageID=None):
logging.info("Publishing folder: " + folder)
for entry in os.scandir(folder):
if entry.is_dir():
# create page with the DISPLAY CHILDREN macro for the directories in the folder with MD files
logging.info("Found directory: " + str(entry.path))
currentPageID = createPage(title=str(entry.name),
# name of the DISPLAY CHILDREN macro
content="<ac:structured-macro ac:name=\"children\" ac:schema-version=\"2\" ac:macro-id=\"80b8c33e-cc87-4987-8f88-dd36ee991b15\"/>",
parentPageID=parentPageID,
login=login,
password=password)
currentPageID = create_page(title=str(entry.name),
# name of the DISPLAY CHILDREN macro
content="<ac:structured-macro ac:name=\"children\" ac:schema-version=\"2\" ac:macro-id=\"80b8c33e-cc87-4987-8f88-dd36ee991b15\"/>",
parentPageID=parentPageID,
login=login,
password=password)

# publish files in the current folder
publishFolder(folder=entry.path, login=login,
password=password, parentPageID=currentPageID)
publish_folder(folder=entry.path, login=login,
password=password, parentPageID=currentPageID)

elif entry.is_file():
logging.info("Found file: " + str(entry.path))
Expand Down Expand Up @@ -60,12 +60,12 @@ def publishFolder(folder, login, password, parentPageID=None):
newFileContent += line

# create new page
pageIDforFileAttaching = createPage(title=str(entry.name),
content=markdown.markdown(newFileContent, extensions=[
'markdown.extensions.tables', 'fenced_code']),
parentPageID=parentPageID,
login=login,
password=password)
pageIDforFileAttaching = create_page(title=str(entry.name),
content=markdown.markdown(newFileContent, extensions=[
'markdown.extensions.tables', 'fenced_code']),
parentPageID=parentPageID,
login=login,
password=password)

# if do exist files to Upload as attachments
if bool(filesToUpload):
Expand All @@ -77,10 +77,10 @@ def publishFolder(folder, login, password, parentPageID=None):
logging.info(
"Attaching file: " + imagePath + " to the page: " + str(pageIDforFileAttaching))
with open(imagePath, 'rb') as attachedFile:
attachFile(pageIdForFileAttaching=pageIDforFileAttaching,
attachedFile=attachedFile,
login=login,
password=password)
attach_file(page_id=pageIDforFileAttaching,
attached_file=attachedFile,
login=login,
password=password)
else:
logging.error(
"File: " + str(imagePath) + " not found. Nothing to attach")
Expand Down