Skip to content

Commit

Permalink
Merge pull request #140 from MikroElektronika/improvement/check-asset…
Browse files Browse the repository at this point in the history
…s-after-release

Added script for comparing assets for 2 releases
  • Loading branch information
StrahinjaJacimovic authored Dec 25, 2024
2 parents 48b6ccd + 32a1b47 commit 95164ac
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/compareAssets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Compare Assets between Releases

on:
workflow_dispatch:
inputs:
first_tag:
type: string
description: First Release Tag
default: "latest"
second_tag:
type: string
description: Second Release Tag (specify the tag)
default: "mikroSDK-2.13.0"

jobs:
compare-release-assets:
if: ${{ github.event.inputs.first_tag != github.event.inputs.second_tag }}
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'

- name: Cache Python packages
uses: actions/cache@v4
with:
path: ~/.cache/pip
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install requests
pip install chardet
pip install py7zr
- name: Run Asset Comparation Script
run: python -u scripts/compare_assets.py ${{ secrets.GITHUB_TOKEN }} ${{ github.repository }} "--current_tag" "${{ github.event.inputs.first_tag }}" "--previous_tag" "${{ github.event.inputs.second_tag }}"

- name: Upload result_compare.txt as artifact
uses: actions/upload-artifact@v3
with:
name: result-compare
path: result_compare.txt
9 changes: 9 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,12 @@ jobs:

- name: Run Package Script
run: python -u scripts/package.py ${{ steps.app-token.outputs.token }} ${{ github.repository }} ${{ github.event.release.tag_name }} "False"

- name: Compare Assets with Previous Release
run: python -u scripts/compare_assets.py ${{ steps.app-token.outputs.token }} ${{ github.repository }} "--current_tag" "${{ github.event.release.tag_name }}" "--previous_tag" "latest"

- name: Upload result_compare.txt as artifact
uses: actions/upload-artifact@v3
with:
name: result-compare
path: result_compare.txt
80 changes: 80 additions & 0 deletions scripts/compare_assets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import argparse, requests

import support as support

def get_headers(api, token):
if api:
return {
'Authorization': f'token {token}'
}
else:
return {
'Authorization': f'Bearer {token}',
'Accept': 'application/octet-stream'
}

def fetch_specified_release_version(repo, token, tag):
api_headers = get_headers(True, token)
url = f'https://api.github.com/repos/{repo}/releases'
response = requests.get(url, headers=api_headers)
response.raise_for_status() # Raise an exception for HTTP errors
return support.get_specified_release(response.json(), tag)

def fetch_latest_release_version(repo, token):
api_headers = get_headers(True, token)
url = f'https://api.github.com/repos/{repo}/releases'
response = requests.get(url, headers=api_headers)
response.raise_for_status() # Raise an exception for HTTP errors
return support.get_latest_release(response.json())

def fetch_existing_asset_names(release):
return [asset['name'] for asset in release['assets']]

def main(token, repo, current_tag, previous_tag):
found_dif = False

if current_tag != "latest":
current_release = fetch_specified_release_version(repo, token, current_tag)
else:
current_release = fetch_latest_release_version(repo, token)

if previous_tag != "latest":
previous_release = fetch_specified_release_version(repo, token, previous_tag)
else:
previous_release = fetch_latest_release_version(repo, token)

current_assets = [package.replace('.7z', '') for package in fetch_existing_asset_names(current_release)]
previous_assets = [package.replace('.7z', '') for package in fetch_existing_asset_names(previous_release)]

for current_asset in current_assets:
if current_asset not in previous_assets:
print(f"\033[93m{current_release['tag_name']} release has {current_asset} which is not present in {previous_release['tag_name']}!")
# Write the line to a file
with open("result_compare.txt", "a") as file:
file.write(f'{current_release['tag_name']} release has {current_asset} which is not present in {previous_release['tag_name']}!\n')
found_dif = True

for previous_asset in previous_assets:
if previous_asset not in current_assets:
print(f"\033[93m{previous_release['tag_name']} release has {previous_asset} which is not present in {current_release['tag_name']}!")
# Write the line to a file
with open("result_compare.txt", "a") as file:
file.write(f'{previous_release['tag_name']} release has {previous_asset} which is not present in {current_release['tag_name']}!\n')
found_dif = True

if found_dif == False:
print(f"\033[92mThere is no difference in asset names between {current_release['tag_name']} and {previous_release['tag_name']} releases!")

return


if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Compare release assets for 2 releases.")
parser.add_argument("token", help="GitHub Token")
parser.add_argument("repo", help="Repository name, e.g., 'username/repo'")
parser.add_argument("--current_tag", help="Tag name for the current release", default="latest")
parser.add_argument("--previous_tag", help="Tag name for the previous release", default="mikroSDK-2.13.0")
args = parser.parse_args()
print("Starting the comparation process...")
main(args.token, args.repo, args.current_tag, args.previous_tag)
print("Comparation process completed.")

0 comments on commit 95164ac

Please sign in to comment.