There are times when you need to perform repetitive tasks like restoring permissions or archiving spaces. Fortunately, Python and Selenium can come to the rescue. In this blog post, we’ll explore a Python script that automates these tasks for Confluence using the Selenium web automation framework.
Prerequisites
Before you begin, make sure you have the following:
- Python installed on your system.
- The Selenium library installed. You can install it using
pip:pip install selenium - The Chrome WebDriver executable, which you can download from the official website.
- A Confluence server URL, username, and password.
Script Overview
The provided script is a Python program that uses Selenium to interact with a Confluence server. Here’s what it can do:
- Restore Permissions: This option allows you to recover permissions for all spaces in Confluence.
- Archive Spaces: This option lets you archive spaces in Confluence (you will need a csv with the space keys with the name spaces.csv having a header called spacekey.
How to Use the Script
Follow these steps to use the script:
- Open the script in your favorite text editor or IDE.
- Replace the placeholders in the script with your Confluence server’s URL, your server email, and password.
- Save the script with a
.pyextension. - Run the script by executing it using Python:pythonCopy code
python script_name.py - The script will prompt you to choose an option: 1 for restoring permissions and 2 for archiving spaces.
- If you choose to restore permissions, the script will recover the permissions for all spaces.
- If you choose to archive spaces, you’ll need to provide a CSV file with space keys. The script will archive the specified spaces.
The Code
from bs4 import BeautifulSoup
import csv
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
server_base_URL = "https://confluence.domain.com"
server_email = "admin"
server_password = "password"
def initialize_driver():
driver = webdriver.Chrome()
driver.get(server_base_URL)
driver.maximize_window()
return driver
def login(driver, username, password):
inputElement = driver.find_element(By.ID, "os_username")
inputElement.send_keys(username)
inputElement = driver.find_element(By.ID, "os_password")
inputElement.send_keys(password)
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "loginButton")))
driver.find_element(By.ID, 'loginButton').click()
def restore(driver):
url = f'{server_base_URL}/admin/permissions/viewdefaultspacepermissions.action'
driver.get(url)
inputElement = driver.find_element(By.ID, "password")
inputElement.send_keys(server_password)
driver.find_element(By.ID, 'authenticateButton').click()
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "space-permissions-table")))
links = driver.find_elements(By.XPATH, "//a[text()='Recover Permissions']")
for link in links:
link.click()
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "button-panel-button")))
driver.find_element(By.CLASS_NAME, "button-panel-button").click()
restoreagain(driver)
def restoreagain(driver):
url = f'{server_base_URL}/admin/permissions/viewdefaultspacepermissions.action'
driver.get(url)
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "space-permissions-table")))
links = driver.find_elements(By.XPATH, "//a[text()='Recover Permissions']")
for link in links:
link.click()
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, "button-panel-button")))
driver.find_element(By.CLASS_NAME, "button-panel-button").click()
restoreagain(driver)
def process_space(driver, spacekey):
url = f'{server_base_URL}/spaces/editspace.action?key={spacekey}'
driver.get(url)
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "space-tools-tabs")))
driver.find_element(By.ID, 'spaceArchived').click()
driver.execute_script('AJS.$("#spaceArchived").val("true");')
driver.find_element(By.ID, 'confirm').click()
def main():
print("Choose an option:")
print("1. Restore Permissions")
print("2. Archive Spaces")
option = input("Enter the option (1 or 2): ")
driver = initialize_driver()
login(driver, server_email, server_password)
if option == "1":
restore(driver)
elif option == "2":
with open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'spaces.csv')) as csv_file:
csv_reader = csv.DictReader(csv_file)
for row in csv_reader:
spacekey = row['spacekey']
process_space(driver, spacekey)
else:
print("Invalid option. Please choose 1 or 2.")
driver.quit()
if __name__ == "__main__":
main()
Script in Action
Here’s a breakdown of how the script works:
- It uses Selenium to automate a web browser.
- The
initialize_driverfunction sets up the Chrome WebDriver and opens the Confluence server URL. - The
loginfunction logs into the Confluence server. - If you choose the option to restore permissions, the script navigates to the relevant Confluence page and recovers permissions for all spaces.
- If you choose to archive spaces, the script reads space keys from a CSV file and archives each space.
Conclusion
Automation can save you time and effort, especially when dealing with repetitive tasks in Confluence. This Python script, combined with Selenium, makes it easy to restore permissions and archive spaces. Customize the script to fit your specific needs and enjoy the time you save for more important tasks.
Remember to handle automation with care and ensure you have the necessary permissions and backups in place when performing actions that may affect your Confluence content.