Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Jayarr03 authored May 3, 2024
1 parent 467b3d3 commit 97a1b00
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 14 deletions.
40 changes: 26 additions & 14 deletions Integrations/Templates_Manager/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,39 @@

The purpose of this script is to collect templates from a GitHub repo, download those locally to a local repo and then post those to the IriusRisk API.


# Installation

Run the setup.py script to install the script dependencies
Setup the virtual file and clone the GitHub environment

```python
python .\setup.py install
```bash
python3 -m venv env_name

source env_name/bin/activate

git clone https://github.com/IriusRisk/IriusRisk-Central.git
```

Update the config.py file with the required variables:
```python
# API URL and Token
url = "https://insert_your_domain.iriusrisk.com/api/v2/templates/import"
api_token = 'insert_your_api_token'
Navigate to the Templates_Manager Folder

# GitHub
repo_url = "insert_your_repo_root_url"
repo_sub_folder = "insert_your_sub_folder_if_needed"
```bash
cd Integrations/Templates_Manager
```

# Execute the download and import of templates
Install the dependencies for this script.

```python
python .\templates_manager.py
```
pip3 install -r requirements.txt
```

# Execute the script and provide the neccessary arguments to run the script

```python
python3 templates_manager_v2.py --subdomain release --api_token <api_key>
```
This will perform the following actions -
1. Check if the repo has all of the templates and clone the IriusRisk repo containing the Templates if it does not
2. If that repo already exists, it will attempt to update the local repo with any new templates
3. It will post any template found to the templates endpoint through the API
4. It will post any libraries found to the libraries endpoint through the API
5. Status of the overall migration will be displayed
Binary file added Integrations/Templates_Manager/requirements.txt
Binary file not shown.
104 changes: 104 additions & 0 deletions Integrations/Templates_Manager/templates_manager_v2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import os
import shutil
import requests
import xml.etree.ElementTree as ET
from git import Repo
from tqdm import tqdm
import argparse

# Set up command line arguments
parser = argparse.ArgumentParser(description="Upload XML files to IriusRisk as templates or libraries.")
parser.add_argument("--subdomain", type=str, required=True, help="The subdomain for the IriusRisk API.")
parser.add_argument("--api_token", type=str, required=True, help="API token for authentication with the IriusRisk API.")
args = parser.parse_args()

# API and GitHub Configuration
base_url = f"https://{args.subdomain}.iriusrisk.com/api/v2"
api_url_templates = f"{base_url}/templates/import"
api_url_libraries = f"{base_url}/libraries/import"

repo_url = "https://github.com/iriusrisk/IriusRisk-Central.git"
repo_sub_folder = "Templates/IR_Published_Templates"
repo_path = os.path.join(os.getcwd(), "cloned_repo")

try:
# Clone or update repository
if not os.path.exists(repo_path):
print("Cloning repository...")
Repo.clone_from(repo_url, repo_path)
print("Repository cloned successfully.")
else:
print("Repository already exists. Updating...")
repo = Repo(repo_path)
origin = repo.remote(name='origin')
origin.pull()
print("Repository updated.")

templates_dir = os.path.join(repo_path, repo_sub_folder)
if not os.path.exists(templates_dir):
print("IR_Published_Templates directory not found.")
exit()
print("IR_Published_Templates directory found.")

templates_contents = os.listdir(templates_dir)
if not templates_contents:
print("No files found in the IR_Published_Templates directory.")
exit()

files_to_process = [file for file in templates_contents if file.endswith(".xml")]
if not files_to_process:
print("No XML files found to process.")
exit()

results = []
with tqdm(total=len(files_to_process), desc="Processing files", leave=False) as pbar:
for file_name in files_to_process:
file_path = os.path.join(templates_dir, file_name)
tree = ET.parse(file_path)
root = tree.getroot()

is_template = root.tag.lower() == 'template' # Adjust this condition based on your XML schema

if not is_template:
# Process as library
with open(file_path, 'rb') as f:
files = {
'file': (file_name, f, 'application/xml'),
'name': (None, os.path.splitext(file_name)[0]),
'referenceId': (None, os.path.splitext(file_name)[0])
}
headers = {
'Accept': 'application/hal+json',
'api-token': args.api_token
}
response = requests.post(api_url_libraries, headers=headers, files=files)
result_message = f"{file_name} imported as a library into {args.subdomain}. Status: {response.status_code}"
if response.status_code != 200:
result_message += f", Error: {response.text}"
results.append(result_message)
else:
# Process as template
with open(file_path, 'rb') as f:
files = {
'file': (file_name, f, 'application/xml'),
'name': (None, os.path.splitext(file_name)[0].replace("-", " ").capitalize()),
'referenceId': (None, os.path.splitext(file_name)[0].lower())
}
headers = {
'Accept': 'application/hal+json',
'api-token': args.api_token
}
response = requests.post(api_url_templates, headers=headers, files=files)
result_message = f"{file_name} imported as a template into {args.subdomain}. Status: {response.status_code}"
if response.status_code != 200:
result_message += f", Error: {response.text}"
results.append(result_message)
pbar.update(1)

# Print all results clearly after processing
print("Processing complete. Results:")
for result in results:
print(result)

except Exception as e:
print("An error occurred:", e)

0 comments on commit 97a1b00

Please sign in to comment.