|
| 1 | +import os |
| 2 | +import requests |
| 3 | +from requests.auth import HTTPBasicAuth |
| 4 | +import json |
| 5 | +from bs4 import BeautifulSoup |
| 6 | +import argparse |
| 7 | + |
| 8 | +# Fetching Confluence API credentials from environment variables |
| 9 | +CONFLUENCE_BASE_URL = 'https://checkmarx.atlassian.net/wiki' |
| 10 | +CONFLUENCE_API_USERNAME = os.getenv('CONFLUENCE_API_USERNAME') |
| 11 | +CONFLUENCE_API_TOKEN = os.getenv('CONFLUENCE_API_TOKEN') |
| 12 | +PAGE_ID = '8251998711' # Replace with your actual page ID |
| 13 | + |
| 14 | +# Function to get the current page content |
| 15 | +def get_page_content(): |
| 16 | + url = f'{CONFLUENCE_BASE_URL}/rest/api/content/{PAGE_ID}?expand=body.storage,version' |
| 17 | + response = requests.get(url, auth=HTTPBasicAuth(CONFLUENCE_API_USERNAME, CONFLUENCE_API_TOKEN)) |
| 18 | + response.raise_for_status() |
| 19 | + return response.json() |
| 20 | + |
| 21 | +# Function to update the page content |
| 22 | +def update_page_content(new_content, version_number, title): |
| 23 | + url = f'{CONFLUENCE_BASE_URL}/rest/api/content/{PAGE_ID}' |
| 24 | + headers = { |
| 25 | + 'Content-Type': 'application/json' |
| 26 | + } |
| 27 | + data = { |
| 28 | + 'id': PAGE_ID, |
| 29 | + 'type': 'page', |
| 30 | + 'title': title, |
| 31 | + 'body': { |
| 32 | + 'storage': { |
| 33 | + 'value': new_content, |
| 34 | + 'representation': 'storage' |
| 35 | + } |
| 36 | + }, |
| 37 | + 'version': { |
| 38 | + 'number': version_number + 1 |
| 39 | + } |
| 40 | + } |
| 41 | + response = requests.put(url, headers=headers, data=json.dumps(data), auth=HTTPBasicAuth(CONFLUENCE_API_USERNAME, CONFLUENCE_API_TOKEN)) |
| 42 | + response.raise_for_status() |
| 43 | + return response.json() |
| 44 | + |
| 45 | +# Main function to perform the update |
| 46 | +def main(product, version, status, additional_info): |
| 47 | + # Step 1: Get the current page content |
| 48 | + page_data = get_page_content() |
| 49 | + content = page_data['body']['storage']['value'] |
| 50 | + version_number = page_data['version']['number'] |
| 51 | + title = page_data['title'] |
| 52 | + |
| 53 | + # Step 2: Parse the content to find and update the specific table row |
| 54 | + soup = BeautifulSoup(content, 'html.parser') |
| 55 | + # Locate the specific table by identifying it with a unique attribute or content |
| 56 | + table = soup.find('table', {'data-layout': 'default'}) |
| 57 | + if table: |
| 58 | + # Iterate over the rows to find the matching product |
| 59 | + for row in table.find_all('tr'): |
| 60 | + cells = row.find_all('td') |
| 61 | + if cells and cells[0].get_text(strip=True) == product: |
| 62 | + # Update the cells with new data |
| 63 | + cells[1].string = version |
| 64 | + cells[2].string = status |
| 65 | + cells[3].string = additional_info |
| 66 | + break |
| 67 | + else: |
| 68 | + print('Specified product not found in the table.') |
| 69 | + return |
| 70 | + else: |
| 71 | + print('Table not found.') |
| 72 | + return |
| 73 | + |
| 74 | + # Convert the modified content back to a string |
| 75 | + new_content = str(soup) |
| 76 | + |
| 77 | + # Step 3: Update the page with the new content |
| 78 | + update_page_content(new_content, version_number, title) |
| 79 | + print('Page updated successfully.') |
| 80 | + |
| 81 | +if __name__ == '__main__': |
| 82 | + parser = argparse.ArgumentParser(description='Update Confluence table row.') |
| 83 | + parser.add_argument('product', help='Product name') |
| 84 | + parser.add_argument('version', help='Product version') |
| 85 | + parser.add_argument('status', help='Pipeline status') |
| 86 | + parser.add_argument('additional_info', help='Additional information') |
| 87 | + args = parser.parse_args() |
| 88 | + main(args.product, args.version, args.status, args.additional_info) |
0 commit comments