Skip to content

Commit

Permalink
Created release validation for yml file
Browse files Browse the repository at this point in the history
Added a release validation in github action to know if the version is one of the previous ones at least
  • Loading branch information
dmtzs committed Dec 19, 2023
1 parent 1e761c8 commit c0dcde9
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/scripts/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
requests==2.31.0
python-dotenv==1.0.0
75 changes: 75 additions & 0 deletions .github/scripts/validate_release.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import os
import sys
import traceback
import configparser
from http import HTTPStatus
import requests
from dotenv import load_dotenv


def main() -> None:
"""
Verify if the version is the same of one of the previous versions.
Returns:
- None
"""
token = os.getenv("GH_API_TOKEN")
user_repo = os.getenv("GITHUB_REPOSITORY")
headers = {
"Authorization": f"Bearer {token}",
"Accept": "application/json",
"X-GitHub-Api-Version": "2022-11-28"
}

releases = f"https://api.github.com/repos/{user_repo}/releases"
response = requests.get(releases, headers=headers, timeout=20)
response_body: list[dict[str, any]] = response.json()
if response.status_code == HTTPStatus.OK.value:
previous_versions = [release.get("tag_name")[1:] for release in response_body] # example: ['1.0.0']
root = os.getenv("GITHUB_WORKSPACE")
config = configparser.ConfigParser()
config.read(f"{root}/setup.cfg")
actual_version = config.get("metadata", "version")
if actual_version in previous_versions:
print("\033[33m The version is the same of one of the previous versions, please update the version \033[0m")
sys.exit(1)
else:
print(f"\033[33m Something went wrong getting the releases: {response_body} \033[0m")
sys.exit(1)

def load_env_vars() -> None:
"""
Load the environment variables from .env file.
Returns:
- None
"""
try:
load_dotenv("vars.env")
print("\033[92m The vars.env file loaded \033[0m")
except FileNotFoundError:
print("\033[33m The vars.env file was not found, using env vars of github action \033[0m")

if __name__ == "__main__":
try:
load_env_vars()
ENVIRONMENT = ""
destiny_branch = os.getenv("GITHUB_BASE_REF")
if destiny_branch == "master":
ENVIRONMENT = "PRD"
elif destiny_branch == "development":
ENVIRONMENT = destiny_branch.upper()
else:
print("\033[92m The destiny branch is not master or dev, script doesnt need to run \033[0m")
if ENVIRONMENT == "PRD":
main()
else:
print("\033[92m PR destiny is not to dev, skipping the execution of this code \033[0m")
except Exception:
print(f"\033[33m Complete exception traceback: {traceback.format_exc()} \033[0m")
sys.exit(1)
else:
print("\033[92m The release version is valid \033[0m")
finally:
print("\033[92m End of the script \033[0m")
37 changes: 37 additions & 0 deletions .github/workflows/validate_release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Validate Pull Requests

on:
pull_request:
branches:
- '*'
- '!master'

jobs:
validate-pull-requests:
runs-on: ubuntu-latest
env:
PR_NUMBER: ${{ github.event.number }}
GH_API_TOKEN: ${{ secrets.GH_API_TOKEN }}

steps:
- name: Show the PR number
run: |
echo "The PR number is: $PR_NUMBER"
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Python 3.11
uses: actions/setup-python@v5
with:
python-version: 3.11

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r .github/scripts/requirements.txt
- name: Validate PR release
run: |
echo "Validating PR release"
python .github/scripts/validate_release.py
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ MANIFEST

# Flask stuff:
instance/
.webassets-cache
.webassets-cache

# Environment variables
*.env

0 comments on commit c0dcde9

Please sign in to comment.