Skip to content

Commit 1665020

Browse files
Update Confluence page for new releases (AST-000)
1 parent 7802088 commit 1665020

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
name: Update Confluence Table
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
product:
7+
description: 'Product name'
8+
required: true
9+
version:
10+
description: 'Product version'
11+
required: true
12+
status:
13+
description: 'Pipeline status'
14+
required: true
15+
additional_info:
16+
description: 'Additional information'
17+
required: false
18+
19+
jobs:
20+
update-confluence:
21+
runs-on: ubuntu-latest
22+
steps:
23+
- name: Checkout code
24+
uses: actions/checkout@v2
25+
26+
- name: Set up Python
27+
uses: actions/setup-python@v2
28+
with:
29+
python-version: '3.x'
30+
31+
- name: Install dependencies
32+
run: |
33+
python -m pip install --upgrade pip
34+
pip install requests beautifulsoup4
35+
36+
- name: Update Confluence Page
37+
env:
38+
CONFLUENCE_API_USERNAME: ${{ secrets.CONFLUENCE_API_USERNAME }}
39+
CONFLUENCE_API_TOKEN: ${{ secrets.CONFLUENCE_API_TOKEN }}
40+
run: |
41+
python update_confluence.py \
42+
"${{ github.event.inputs.product }}" \
43+
"${{ github.event.inputs.version }}" \
44+
"${{ github.event.inputs.status }}" \
45+
"${{ github.event.inputs.additional_info }}"

0 commit comments

Comments
 (0)