From 1e04467a4ca2c2f3d0208ef044af98850d03f2d9 Mon Sep 17 00:00:00 2001 From: enexety Date: Tue, 22 Aug 2023 17:58:24 +0300 Subject: [PATCH] Edited: flake8.yml, pytest.yml. Created: create_release.yml, scripts for GH Actions. --- .github/workflows/create_release.yml | 59 ++++++++++++++++ .github/workflows/flake8.yml | 30 -------- .github/workflows/pytest.yml | 30 -------- .github/workflows/tests.yml | 51 ++++++++++++++ scripts/build_zip_file.py | 29 ++++++++ scripts/create_release.py | 101 +++++++++++++++++++++++++++ 6 files changed, 240 insertions(+), 60 deletions(-) create mode 100644 .github/workflows/create_release.yml delete mode 100644 .github/workflows/flake8.yml delete mode 100644 .github/workflows/pytest.yml create mode 100644 .github/workflows/tests.yml create mode 100644 scripts/build_zip_file.py create mode 100644 scripts/create_release.py diff --git a/.github/workflows/create_release.yml b/.github/workflows/create_release.yml new file mode 100644 index 0000000..8ee4be1 --- /dev/null +++ b/.github/workflows/create_release.yml @@ -0,0 +1,59 @@ +name: Upload Release Asset + +on: + push: + branches: + - master + workflow_run: + workflows: + - "tests.yml" + types: + - completed + +jobs: + build-project: + name: Upload Release Asset + runs-on: windows-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: 3.9 + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + pip install requests + + - name: Build zip file + run: scripts/build_zip_file.py + + - name: Print a message + run: echo "Starting the Python release process." + + - name: Create Release + id: create_release + run: | + scripts/create_release.py || exit 1 + env: + GITHUB_TOKEN: ${{ secrets.TOKEN }} + + - name: Print another message + run: echo "Python release process completed." + + - name: Create and Upload Release Asset + id: upload-release-asset + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.TOKEN }} + UPLOAD_URL: ${{ steps.create_release.outputs.upload_url }} + with: + upload_url: ${{ env.UPLOAD_URL }} + asset_path: ./Overwatch Rank Tracker.zip + asset_name: Overwatch Rank Tracker.zip + asset_content_type: application/zip diff --git a/.github/workflows/flake8.yml b/.github/workflows/flake8.yml deleted file mode 100644 index 1953dd2..0000000 --- a/.github/workflows/flake8.yml +++ /dev/null @@ -1,30 +0,0 @@ -name: Run Flake8 - -on: - push: - branches: - - master - -jobs: - lint: - name: Run Flake8 - runs-on: windows-latest - - steps: - - name: Clone repository - uses: actions/checkout@v2 - - - name: Set up Python - uses: actions/setup-python@v2 - with: - python-version: 3.9 - - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install -r requirements.txt - pip install flake8 - - - name: Run Flake8 - run: | - flake8 --config config.flake8 || exit 1 \ No newline at end of file diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml deleted file mode 100644 index 84f8e68..0000000 --- a/.github/workflows/pytest.yml +++ /dev/null @@ -1,30 +0,0 @@ -name: Run Pytest - -on: - push: - branches: - - master - -jobs: - test: - name: Run Pytest - runs-on: windows-latest - - steps: - - name: Clone repository - uses: actions/checkout@v2 - - - name: Set up Python - uses: actions/setup-python@v2 - with: - python-version: 3.9 - - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install -r requirements.txt - pip install pytest - - - name: Run Pytest - run: | - pytest || exit 1 diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..ec0a7bd --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,51 @@ +name: Tests + +on: + push: + branches: + - master + +jobs: + linters: + name: Run Flake8 + runs-on: windows-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: 3.9 + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + pip install flake8 + + - name: Run Flake8 + run: flake8 --config config.flake8 || exit 1 + + pytest: + name: Run Pytest + runs-on: windows-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: 3.9 + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + pip install pytest + + - name: Run Pytest + run: pytest || exit 1 diff --git a/scripts/build_zip_file.py b/scripts/build_zip_file.py new file mode 100644 index 0000000..a560601 --- /dev/null +++ b/scripts/build_zip_file.py @@ -0,0 +1,29 @@ +import os +import shutil +import subprocess + +temporarily_folder_name = "Overwatch Rank Tracker" + +os.chdir("..") + +# paths +project_path = os.getcwd() +dist_path = os.path.join(project_path, "dist") +zip_file_path = os.path.join(project_path, "Overwatch_Rank_Tracker.zip") +temporary_folder = os.path.join(project_path, temporarily_folder_name) + +# there is an old archive - delete it +if os.path.exists(zip_file_path): + os.remove(zip_file_path) + +# executing the pyinstaller command to create an exe file inside the dist folder +subprocess.run(["pyinstaller", "Overwatch Rank Tracker.spec"]) + +# make a duplicate of the dist folder to create an archive with a folder with the desired name Overwatch Rank Tracker inside it +shutil.copytree(dist_path, temporarily_folder_name) + +# creating archive +shutil.make_archive(temporary_folder, "zip") + +# deleting the "Overwatch Rank Tracker" temporary folder +shutil.rmtree(temporary_folder) diff --git a/scripts/create_release.py b/scripts/create_release.py new file mode 100644 index 0000000..73462d8 --- /dev/null +++ b/scripts/create_release.py @@ -0,0 +1,101 @@ +import subprocess +import requests + + +def increment_version(tag): + """Version Increase + Example: v0.0.1 > v0.0.2""" + + tag = tag[1:] + parts = tag.split('.') + major = int(parts[0]) + minor = int(parts[1]) + patch = int(parts[2]) + + if patch < 9: + patch += 1 + elif minor < 9: + minor += 1 + patch = 0 + else: + major += 1 + minor = 0 + patch = 0 + + new_version = f"v{major}.{minor}.{patch}" + return new_version + + +def generate_release_description(tag: str): + """Takes the message of the last commit and edits it to fit the Release description""" + + # getting the name of the last commit + command = ["git", "log", "-1", "--pretty=format:%s"] + last_commit_message = subprocess.check_output(command, stderr=subprocess.STDOUT).decode("utf-8").strip().split(". ") + + changes_formatted = "\n".join(f"> * {change.strip('.')}." for change in last_commit_message) + description = f"> **{tag}**:\n>\n{changes_formatted}\n>\n> Download below" + return description + + +# datas +owner = 'enexety' +repo = 'Overwatch_Rank_Tracker' +# github_token = os.environ.get('TOKEN') +github_token = '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' +headers = { + "Authorization": f"Bearer {github_token}" +} + +# urls +create_release_url = f"https://api.github.com/repos/{owner}/{repo}/releases" +get_tag_url = f"https://api.github.com/repos/{owner}/{repo}/tags" + + +# get new-next tag +response_get_last_tag = requests.get(url=get_tag_url, headers=headers) +if response_get_last_tag.status_code == 200: + tags = response_get_last_tag.json() + latest_tag = tags[0]['name'] + new_tag = increment_version(tag=latest_tag) + + # create release + data = { + "owner": owner, + "repo": repo, + "tag_name": new_tag + } + response = requests.post(url=create_release_url, json=data, headers=headers) + + # was created + if response.status_code == 201: + + # get the upload_url from the response + upload_url = response.json()["upload_url"] + + # get the id of the created release + release_id = response.json()["id"] + + # describe this release + update_data = {"body": generate_release_description(tag=new_tag)} + update_release_url = f"https://api.github.com/repos/{owner}/{repo}/releases/{release_id}" + requests.patch(update_release_url, json=update_data, headers=headers) + + # create release title + edit_release_url = f"https://api.github.com/repos/{owner}/{repo}/releases/{release_id}" + inf = { + "name": new_tag + } + headers_release_title = { + "Authorization": f"Bearer {github_token}", + "Accept": "application/vnd.github.v3+json" + } + response_release_title = requests.patch(edit_release_url, json=inf, headers=headers_release_title) + if response_release_title.status_code != 200: + raise Exception(f"Response when changing release title - {response_release_title.status_code}") + + else: + raise Exception(f"Response to get latest tag - {response.status_code}") + +else: + raise Exception(f"Response to get latest tag - {response_get_last_tag.status_code}")