GitHub (AssertThat as master)
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.
Prerequisites:
-
Access to GitHub and a GitHub repository where you want to store the feature files.
-
Access to your AssertThat instance with the necessary permissions.
-
Your AssertThat project’s
ASSERTTHAT_ACCESS_KEYandASSERTTHAT_SECRET_KEY, which can be obtained from the AssertThat project configuration. -
Your AssertThat project’s
PROJECT_ID, which you can obtain from AssertThat.
Step 1: Set Up a GitHub Repository
If you don’t have an existing GitHub repository, create one where you want to store your feature files.
Step 2: Create a Python Script
2.1. Create a Python script (e.g., sync_features.py) with the following content:
import requestsimport osimport zipfilefrom github import Github
# AssertThat credentialsat_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 credentialsgithub_token = 'YOUR_GITHUB_TOKEN'github_repo = 'YOUR_GITHUB_USERNAME/YOUR_GITHUB_REPO'
# Directory to store feature filesfeature_directory = 'feature_files'
# Create a GitHub repository objectg = Github(github_token)repo = g.get_repo(github_repo)
# Create a directory to store feature files if it doesn't existif not os.path.exists(feature_directory): os.makedirs(feature_directory)
# Fetch feature files from AssertThatresponse = 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}')2.2. In the script, replace the placeholders with your specific credentials and file paths:
-
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.
Step 3: Set Up GitHub Secrets
In your GitHub repository, navigate to “Settings” > “Secrets” and add the following secrets:
-
YOUR_ASSERTTHAT_ACCESS_KEY: Your AssertThat access key. -
YOUR_ASSERTTHAT_SECRET_KEY: Your AssertThat secret key. -
YOUR_GITHUB_TOKEN: Your GitHub personal access token.
Step 4: Create a GitHub Actions Workflow
4.1. In your GitHub repository, go to the “Actions” tab and click on “New workflow.”
4.2. Choose “Set up a workflow yourself.”
4.3. Replace the contents of the generated YAML file with the following:
name: Sync Features from AssertThat to GitHub
on: schedule: - cron: '0 0 * * *' # Run the workflow daily at midnight as an example
jobs: sync_features: runs-on: ubuntu-latest
steps: - name: Checkout code uses: actions/checkout@v2
- name: Set up Python uses: actions/setup-python@v2 with: python-version: 3.x
- name: Install Python dependencies run: pip install -r requirements.txt
- name: Run the script run: python sync_features.py
- name: Clean up local files run: | rm -rf feature_filesThis GitHub Actions workflow:
-
Runs daily at midnight as scheduled.
-
Checks out your code.
-
Sets up Python and installs any necessary dependencies.
-
Runs the Python script to fetch feature files from AssertThat and push to GitHub