This is a step-by-step guide for setting up a GitHub Actions workflow to synchronize modified .feature files from AssertThat to a GitHub repository.
...
| Code Block |
|---|
import requests
import os
import zipfile
from github import Github
# AssertThat credentials
at_access_key = 'YOUR_ASSERTTHAT_ACCESS_KEY'
at_secret_key = 'YOUR_ASSERTTHAT_SECRET_KEY'
at_project_id = 'YOUR_ASSERTTHAT_PROJECT_ID'
at_api_endpoint = 'https://YOUR_JIRA_HOST/rest/assertthat/latest'
# GitHub credentials
github_token = 'YOUR_GITHUB_TOKEN'
github_repo = 'YOUR_GITHUB_USERNAME/YOUR_GITHUB_REPO'
# Directory to store feature files
feature_directory = 'feature_files'
# Create a GitHub repository object
g = Github(github_token)
repo = g.get_repo(github_repo)
# Create a directory to store feature files if it doesn't exist
if not os.path.exists(feature_directory):
os.makedirs(feature_directory)
# Fetch feature files from AssertThat
response = requests.get(
f'{at_api_endpoint}/project/{at_project_id}/client/features',
headers={'Authorization': f'Basic {at_access_key}:{at_secret_key}'}
)
# NOTE: The example above is for Jira Data Center. In case you use cloud version the following URL should be used for downloading features: https://bdd.assertthat.app/rest/api/1/project/YOUR_ASSERT_THAT_PROJECT_ID/features
if response.status_code == 200:
# Save the feature files ZIP to the local directory
zip_file_path = os.path.join(feature_directory, 'features.zip')
with open(zip_file_path, 'wb') as f:
f.write(response.content)
# Extract the ZIP file
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:
zip_ref.extractall(feature_directory)
# Commit and push each extracted .feature file
for root, _, files in os.walk(feature_directory):
for file in files:
if file.endswith(".feature"):
file_path = os.path.join(root, file)
with open(file_path, 'rb') as f:
content = f.read()
# Specify the file path within the repository
file_path_within_repo = os.path.relpath(file_path, feature_directory)
repo.create_file(
path=file_path_within_repo,
message=f'Update feature file: {file}',
content=content,
branch='main'
)
# Clean up (delete the local files)
os.remove(zip_file_path)
for root, _, files in os.walk(feature_directory):
for file in files:
os.remove(os.path.join(root, file))
else:
print(f'Failed to fetch feature files from AssertThat. Status code: {response.status_code}')
|
...
YOUR_ASSERTTHAT_ACCESS_KEY,YOUR_ASSERTTHAT_SECRET_KEY: Your AssertThat access key and secret key.YOUR_ASSERTTHAT_PROJECT_ID: The ID of your project in AssertThat.YOUR_JIRA_HOST: The AssertThat API endpoint.YOUR_GITHUB_TOKEN: Your GitHub personal access token.YOUR_GITHUB_USERNAME: Your GitHub username.YOUR_GITHUB_REPO: The name of your GitHub repository.NOTE: The example above is for Jira Data Center. In case you use cloud version the following URL should be used for downloading features:https://bdd.assertthat.app/rest/api/1/project/YOUR_ASSERT_THAT_PROJECT_ID/features
Step 3: Set Up GitHub Secrets
...