From aa7f4bafdfb80e1fcd27d240b243202e4a953245 Mon Sep 17 00:00:00 2001 From: "ivan.ruzavin" Date: Wed, 13 Nov 2024 09:02:58 +0100 Subject: [PATCH 1/7] Added assets comparation script/step/workflow --- .github/workflows/compareAssets.yaml | 44 +++++++++++++++ .github/workflows/release.yaml | 3 ++ scripts/compare_assets.py | 81 ++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 .github/workflows/compareAssets.yaml create mode 100644 scripts/compare_assets.py diff --git a/.github/workflows/compareAssets.yaml b/.github/workflows/compareAssets.yaml new file mode 100644 index 000000000..94a6280b3 --- /dev/null +++ b/.github/workflows/compareAssets.yaml @@ -0,0 +1,44 @@ +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 (latest stays for tag before latest) + default: "latest" + +jobs: + upload-release-asset: + 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 Keyword Replacement Script + ## Replace keyword 'code' with '_code' (Microchip MCUs) + 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 }}" diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index e06c735c5..dd4dc81dd 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -37,3 +37,6 @@ jobs: - name: Run Package Script run: python -u scripts/package.py ${{ secrets.GITHUB_TOKEN }} ${{ github.repository }} ${{ github.event.release.tag_name }} "False" + + - name: Compare Assets with Previous Release + run: python -u scripts/compare_assets.py ${{ secrets.GITHUB_TOKEN }} ${{ github.repository }} "--current_tag" "${{ github.event.release.tag_name }}" "--previous_tag" "latest" diff --git a/scripts/compare_assets.py b/scripts/compare_assets.py new file mode 100644 index 000000000..f881a84e5 --- /dev/null +++ b/scripts/compare_assets.py @@ -0,0 +1,81 @@ +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_previous_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_previous_release(response.json()) + +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 = 0 + + 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_previous_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']}!") + found_dif = 1 + + 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']}!") + found_dif = 1 + + if found_dif == 0: + 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="latest") + args = parser.parse_args() + print("Starting the comparation process...") + main(args.token, args.repo, args.current_tag, args.previous_tag) + print("Comparation process completed.") \ No newline at end of file From 7ded205dc1f4758cae348bed425022aa19bb89f4 Mon Sep 17 00:00:00 2001 From: "ivan.ruzavin" Date: Fri, 15 Nov 2024 08:43:20 +0100 Subject: [PATCH 2/7] AR fixing in progress -> need to output artifacts --- .github/workflows/compareAssets.yaml | 5 ++--- scripts/compare_assets.py | 6 ++++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/.github/workflows/compareAssets.yaml b/.github/workflows/compareAssets.yaml index 94a6280b3..209823e49 100644 --- a/.github/workflows/compareAssets.yaml +++ b/.github/workflows/compareAssets.yaml @@ -13,7 +13,7 @@ on: default: "latest" jobs: - upload-release-asset: + compare-release-assets: runs-on: ubuntu-latest steps: - name: Checkout code @@ -39,6 +39,5 @@ jobs: pip install chardet pip install py7zr - - name: Run Keyword Replacement Script - ## Replace keyword 'code' with '_code' (Microchip MCUs) + - 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 }}" diff --git a/scripts/compare_assets.py b/scripts/compare_assets.py index f881a84e5..1c4349b5a 100644 --- a/scripts/compare_assets.py +++ b/scripts/compare_assets.py @@ -56,11 +56,17 @@ def main(token, repo, current_tag, previous_tag): 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 = 1 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 = 1 if found_dif == 0: From f3f27acf8148b35545fd9e9aec9af0093e3390a1 Mon Sep 17 00:00:00 2001 From: "ivan.ruzavin" Date: Tue, 24 Dec 2024 14:34:35 +0100 Subject: [PATCH 3/7] upload result-compare.txt as artifact --- .github/workflows/release.yaml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 574301f87..30ae44c65 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -49,3 +49,9 @@ jobs: - name: Compare Assets with Previous Release run: python -u scripts/compare_assets.py ${{ secrets.GITHUB_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 From a053402113e5262274e82d9f5368409453fbbaff Mon Sep 17 00:00:00 2001 From: "ivan.ruzavin" Date: Tue, 24 Dec 2024 14:42:00 +0100 Subject: [PATCH 4/7] Changed latest to previous latest --- .github/workflows/compareAssets.yaml | 10 ++++++++-- .github/workflows/release.yaml | 2 +- scripts/compare_assets.py | 4 ++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/.github/workflows/compareAssets.yaml b/.github/workflows/compareAssets.yaml index 209823e49..4e316a144 100644 --- a/.github/workflows/compareAssets.yaml +++ b/.github/workflows/compareAssets.yaml @@ -9,8 +9,8 @@ on: default: "latest" second_tag: type: string - description: Second Release Tag (latest stays for tag before latest) - default: "latest" + description: Second Release Tag (previous latest stays for tag before latest) + default: "previous latest" jobs: compare-release-assets: @@ -41,3 +41,9 @@ jobs: - 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 diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 30ae44c65..3f5022212 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -48,7 +48,7 @@ jobs: 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 ${{ secrets.GITHUB_TOKEN }} ${{ github.repository }} "--current_tag" "${{ github.event.release.tag_name }}" "--previous_tag" "latest" + run: python -u scripts/compare_assets.py ${{ steps.app-token.outputs.token }} ${{ github.repository }} "--current_tag" "${{ github.event.release.tag_name }}" "--previous_tag" "previous latest" - name: Upload result_compare.txt as artifact uses: actions/upload-artifact@v3 diff --git a/scripts/compare_assets.py b/scripts/compare_assets.py index 1c4349b5a..2fb48a791 100644 --- a/scripts/compare_assets.py +++ b/scripts/compare_assets.py @@ -45,7 +45,7 @@ def main(token, repo, current_tag, previous_tag): else: current_release = fetch_latest_release_version(repo, token) - if previous_tag != "latest": + if previous_tag != "previous latest": previous_release = fetch_specified_release_version(repo, token, previous_tag) else: previous_release = fetch_previous_release_version(repo, token) @@ -80,7 +80,7 @@ def main(token, repo, current_tag, previous_tag): 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="latest") + parser.add_argument("--previous_tag", help="Tag name for the previous release", default="previous latest") args = parser.parse_args() print("Starting the comparation process...") main(args.token, args.repo, args.current_tag, args.previous_tag) From 92d6f5192ecc968f1440175953a7f98b060cf2bd Mon Sep 17 00:00:00 2001 From: "ivan.ruzavin" Date: Tue, 24 Dec 2024 14:45:52 +0100 Subject: [PATCH 5/7] Added specific tag for previous tag --- .github/workflows/compareAssets.yaml | 4 ++-- .github/workflows/release.yaml | 2 +- scripts/compare_assets.py | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/compareAssets.yaml b/.github/workflows/compareAssets.yaml index 4e316a144..08f7d439b 100644 --- a/.github/workflows/compareAssets.yaml +++ b/.github/workflows/compareAssets.yaml @@ -9,8 +9,8 @@ on: default: "latest" second_tag: type: string - description: Second Release Tag (previous latest stays for tag before latest) - default: "previous latest" + description: Second Release Tag (specify the tag) + default: "mikroSDK-2.13.0" jobs: compare-release-assets: diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 3f5022212..f91d113d3 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -48,7 +48,7 @@ jobs: 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" "previous latest" + 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 diff --git a/scripts/compare_assets.py b/scripts/compare_assets.py index 2fb48a791..b4fea4bcb 100644 --- a/scripts/compare_assets.py +++ b/scripts/compare_assets.py @@ -45,10 +45,10 @@ def main(token, repo, current_tag, previous_tag): else: current_release = fetch_latest_release_version(repo, token) - if previous_tag != "previous latest": + if previous_tag != "latest": previous_release = fetch_specified_release_version(repo, token, previous_tag) else: - previous_release = fetch_previous_release_version(repo, token) + 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)] @@ -80,7 +80,7 @@ def main(token, repo, current_tag, previous_tag): 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="previous 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) From 76f5fcc9bd7478fd0777477b72c9382419fb9937 Mon Sep 17 00:00:00 2001 From: "ivan.ruzavin" Date: Tue, 24 Dec 2024 14:46:31 +0100 Subject: [PATCH 6/7] Removed unnecessary functions --- scripts/compare_assets.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/scripts/compare_assets.py b/scripts/compare_assets.py index b4fea4bcb..bc0b307b5 100644 --- a/scripts/compare_assets.py +++ b/scripts/compare_assets.py @@ -20,13 +20,6 @@ def fetch_specified_release_version(repo, token, tag): response.raise_for_status() # Raise an exception for HTTP errors return support.get_specified_release(response.json(), tag) -def fetch_previous_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_previous_release(response.json()) - def fetch_latest_release_version(repo, token): api_headers = get_headers(True, token) url = f'https://api.github.com/repos/{repo}/releases' From 32a1b47c2d58e7f3cbe499403ab2c4dc79266113 Mon Sep 17 00:00:00 2001 From: "ivan.ruzavin" Date: Wed, 25 Dec 2024 10:23:17 +0100 Subject: [PATCH 7/7] Updated code according to ARs --- .github/workflows/compareAssets.yaml | 1 + scripts/compare_assets.py | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/compareAssets.yaml b/.github/workflows/compareAssets.yaml index 08f7d439b..0e22bd239 100644 --- a/.github/workflows/compareAssets.yaml +++ b/.github/workflows/compareAssets.yaml @@ -14,6 +14,7 @@ on: jobs: compare-release-assets: + if: ${{ github.event.inputs.first_tag != github.event.inputs.second_tag }} runs-on: ubuntu-latest steps: - name: Checkout code diff --git a/scripts/compare_assets.py b/scripts/compare_assets.py index bc0b307b5..f56f6b912 100644 --- a/scripts/compare_assets.py +++ b/scripts/compare_assets.py @@ -31,7 +31,7 @@ def fetch_existing_asset_names(release): return [asset['name'] for asset in release['assets']] def main(token, repo, current_tag, previous_tag): - found_dif = 0 + found_dif = False if current_tag != "latest": current_release = fetch_specified_release_version(repo, token, current_tag) @@ -52,7 +52,7 @@ def main(token, repo, current_tag, previous_tag): # 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 = 1 + found_dif = True for previous_asset in previous_assets: if previous_asset not in current_assets: @@ -60,9 +60,9 @@ def main(token, repo, current_tag, previous_tag): # 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 = 1 + found_dif = True - if found_dif == 0: + 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