Skip to content

Commit

Permalink
feat: Add utils.py and gh_utils.py
Browse files Browse the repository at this point in the history
Has function to get name of latest gh release for a repo
  • Loading branch information
iamsergio committed Nov 27, 2024
1 parent 58ba47d commit 7c1aa65
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 1 deletion.
22 changes: 22 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
# SPDX-License-Identifier: MIT

name: Test scripts
on:
push:
branches:
- main
pull_request:
branches:
- main
workflow_dispatch:

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run gh_utils test
run: python gh_utils.py KDAB/KDDockWidgets
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
2 changes: 2 additions & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[MESSAGES CONTROL]
disable=C0114,C0116
9 changes: 8 additions & 1 deletion REUSE.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ SPDX-PackageDownloadLocation = "https://www.github.com/KDABLabs/ci-release-tools

#misc FILES
[[annotations]]
path = ["README.md", "REUSE.toml", "LICENSE.txt", ".pre-commit-config.yaml"]
path = [
".gitignore",
"README.md",
"REUSE.toml",
"LICENSE.txt",
".pre-commit-config.yaml",
".pylintrc",
]
precedence = "aggregate"
SPDX-FileCopyrightText = "Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>"
SPDX-License-Identifier = "MIT"
35 changes: 35 additions & 0 deletions gh_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env python3

# SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
# SPDX-License-Identifier: MIT

# Scripts related to GitHub

# Examples:
# $ gh_utils.py --get-latest-release=KDAB/KDDockWidgets
# v2.1.0

import argparse
from utils import run_command_with_output

# Returns the tag of latest release
# repo is for example 'KDAB/KDReports'


def get_latest_release_tag_in_github(repo):
lines = run_command_with_output(
f"gh release list --repo {repo} --limit 1").split('\n')
# example:
# TITLE TYPE TAG NAME PUBLISHED
# KDReports 2.3.0 Latest kdreports-2.3.0 about 2 months ago
version = lines[0].split('\t')[2]
return version


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--get-latest-release',
help="returns latest release for a repo")
args = parser.parse_args()
if args.get_latest_release:
print(get_latest_release_tag_in_github(args.get_latest_release))
29 changes: 29 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env python3

# SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
# SPDX-License-Identifier: MIT

# Generic utils used by the other scripts

import os
import sys


def exit_because(reason):
print(reason)
sys.exit(1)


def run_command(command, fatal=True):
if os.system(command) == 0:
return True

if fatal:
exit_because(f"Failed to run command: {command}")

return False


def run_command_with_output(command):
output = os.popen(command).read()
return output

0 comments on commit 7c1aa65

Please sign in to comment.