Skip to content

Commit

Permalink
Apply black and flake8 to python scripts
Browse files Browse the repository at this point in the history
close #398

Signed-off-by: mgoerens <[email protected]>
  • Loading branch information
mgoerens authored and komish committed Aug 4, 2023
1 parent c32e876 commit a360fb2
Show file tree
Hide file tree
Showing 8 changed files with 557 additions and 189 deletions.
7 changes: 7 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[flake8]
# Recommend matching the black line length (default 88),
# rather than using the flake8 default of 79:
max-line-length = 88
extend-ignore =
# See https://github.com/PyCQA/pycodestyle/issues/373
E203,
69 changes: 44 additions & 25 deletions scripts/src/owners/checkuser.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""
Used by a github action to determine if the owner of a PR is permitted to change the files
associated with publishing a release of the chart verifier.
Used by a github action to determine if the owner of a PR is permitted to change the
files associated with publishing a release of the chart verifier.
parameters:
--api-url : API URL for the pull request
--user : user to be checked for authority to modify release files in a PR
results:
exit code 1 if pull request contains restricted files and user is not authorized to modify them.
exit code 1 if pull request contains restricted files and user is not authorized
to modify them.
"""

import re
Expand All @@ -16,10 +17,11 @@
import os
import sys
import yaml

try:
from yaml import CLoader as Loader, CDumper as Dumper
from yaml import CLoader as Loader
except ImportError:
from yaml import Loader, Dumper
from yaml import Loader


OWNERS_FILE = "OWNERS"
Expand All @@ -41,45 +43,62 @@ def verify_user(username):
print(f"[ERROR] {username} not auhtorized")
return False


def check_for_restricted_file(api_url):
files_api_url = f'{api_url}/files'
headers = {'Accept': 'application/vnd.github.v3+json'}
files_api_url = f"{api_url}/files"
headers = {"Accept": "application/vnd.github.v3+json"}
pattern_owners = re.compile(OWNERS_FILE)
pattern_versionfile = re.compile(VERSION_FILE)
pattern_thisfile = re.compile(THIS_FILE)
page_number = 1
max_page_size,page_size = 100,100
max_page_size, page_size = 100, 100

while (page_size == max_page_size):

files_api_query = f'{files_api_url}?per_page={page_size}&page={page_number}'
r = requests.get(files_api_query,headers=headers)
while page_size == max_page_size:
files_api_query = f"{files_api_url}?per_page={page_size}&page={page_number}"
r = requests.get(files_api_query, headers=headers)
files = r.json()
page_size = len(files)
page_number += 1

for f in files:
filename = f["filename"]
if pattern_versionfile.match(filename) or pattern_owners.match(filename) or pattern_thisfile.match(filename):
print(f"[INFO] restricted file found: {filename}")
return True

filename = f["filename"]
if (
pattern_versionfile.match(filename)
or pattern_owners.match(filename)
or pattern_thisfile.match(filename)
):
print(f"[INFO] restricted file found: {filename}")
return True

return False


def main():
parser = argparse.ArgumentParser()
parser.add_argument("-a", "--api-url", dest="api_url", type=str, required=True,
help="API URL for the pull request")
parser.add_argument("-u", "--user", dest="username", type=str, required=True,
help="user to be checked for authority to modify release files in a PR")
parser.add_argument(
"-a",
"--api-url",
dest="api_url",
type=str,
required=True,
help="API URL for the pull request",
)
parser.add_argument(
"-u",
"--user",
dest="username",
type=str,
required=True,
help="user to be checked for authority to modify release files in a PR",
)
args = parser.parse_args()

if check_for_restricted_file(args.api_url):
if verify_user(args.username):
print(f"[INFO] {args.username} is authorized to modify all files in the PR")
username = args.username
if verify_user(username):
print(f"[INFO] {username} is authorized to modify all files in the PR")
else:
print(f"[INFO] {args.username} is not authorized to modify all files in the PR")
print(f"[INFO] {username} is not authorized to modify all files in the PR")
sys.exit(1)
else:
print(f"[INFO] no restricted files found in the PR")
print("[INFO] no restricted files found in the PR")
16 changes: 11 additions & 5 deletions scripts/src/release/releasebody.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import sys

sys.path.append('./scripts/src/')
from release import releasechecker
from utils import utils
sys.path.append("./scripts/src/")
from release import releasechecker # noqa E402


def get_release_body(version, image_name, release_info):
"""Generate the body of the GitHub release"""
body = f"Chart verifier version {version} <br><br>Docker Image:<br>- {image_name}:{version}<br><br>"
body = f"Chart verifier version {version} <br><br>"
body += f"Docker Image:<br>- {image_name}:{version}<br><br>"
body += "This version includes:<br>"
for info in release_info:
if info.startswith("<"):
Expand All @@ -15,7 +16,12 @@ def get_release_body(version, image_name, release_info):
body += f"- {info}<br>"
return body


def main():
version_info = releasechecker.get_version_info()
release_body = get_release_body(version_info["version"],version_info["quay-image"],version_info["release-info"])
release_body = get_release_body(
version_info["version"],
version_info["quay-image"],
version_info["release-info"],
)
print(release_body)
108 changes: 69 additions & 39 deletions scripts/src/release/releasechecker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Used by a github action
1. To determine if the contents of pull request contain only the file which contains the chart verifier release.
1. To determine if the contents of pull request contain only the file which contains
the chart verifier release.
2. To determine if the release has been updated.
parameters:
Expand All @@ -15,10 +16,11 @@
PR_includes_release : Set to true if the PR contains the version file.
PR_release_body : Body of text to be used to describe the release.
if --version only is specified, output variables are set:
updated : set to true if the version specified is later than the version in the version file
from the main branch.
updated : set to true if the version specified is later than the version in the
version file from the main branch.
if neither parameters are specified, output variables are set:
PR_version : The chart verifier version read from the version file from main branch.
PR_version : The chart verifier version read from the version file from main
branch.
PR_release_image : The name of the image from the version file from main branch.
"""

Expand All @@ -30,27 +32,28 @@
import requests
import semver
import sys
sys.path.append('./scripts/src/')
from release import tarfile_asset, releasebody
from utils import utils

VERSION_FILE = 'pkg/chartverifier/version/version_info.json'
sys.path.append("./scripts/src/")
from release import tarfile_asset, releasebody # noqa 402
from utils import utils # noqa 402

VERSION_FILE = "pkg/chartverifier/version/version_info.json"


def check_if_only_version_file_is_modified(api_url):
# api_url https://api.github.com/repos/<organization-name>/<repository-name>/pulls/<pr_number>
# Example format for api_url:
# https://api.github.com/repos/<organization-name>/<repository-name>/pulls/<pr_number>

files_api_url = f'{api_url}/files'
headers = {'Accept': 'application/vnd.github.v3+json'}
files_api_url = f"{api_url}/files"
headers = {"Accept": "application/vnd.github.v3+json"}
pattern_versionfile = re.compile(r"pkg/chartverifier/version/version_info.json")
page_number = 1
max_page_size,page_size = 100,100

max_page_size, page_size = 100, 100

version_file_found = False
while (page_size == max_page_size):

files_api_query = f'{files_api_url}?per_page={page_size}&page={page_number}'
r = requests.get(files_api_query,headers=headers)
while page_size == max_page_size:
files_api_query = f"{files_api_url}?per_page={page_size}&page={page_number}"
r = requests.get(files_api_query, headers=headers)
files = r.json()
page_size = len(files)
page_number += 1
Expand All @@ -64,12 +67,14 @@ def check_if_only_version_file_is_modified(api_url):

return version_file_found


def get_version_info():
data = {}
with open(VERSION_FILE) as json_file:
data = json.load(json_file)
return data


def release_exists(version):
g = Github(os.environ.get("GITHUB_TOKEN"))
releases = g.get_repo(os.environ.get("GITHUB_REPOSITORY")).get_releases()
Expand All @@ -81,40 +86,65 @@ def release_exists(version):

def main():
parser = argparse.ArgumentParser()
parser.add_argument("-a", "--api-url", dest="api_url", type=str, required=False,
help="API URL for the pull request")
parser.add_argument("-v", "--version", dest="version", type=str, required=False,
help="Version to compare")
parser.add_argument(
"-a",
"--api-url",
dest="api_url",
type=str,
required=False,
help="API URL for the pull request",
)
parser.add_argument(
"-v",
"--version",
dest="version",
type=str,
required=False,
help="Version to compare",
)

args = parser.parse_args()
if args.api_url:
version_info = get_version_info()
asset_file = tarfile_asset.create(version_info["version"])
print(f'[INFO] Verifier tarball created : {asset_file}.')
utils.add_output("PR_tarball_name",asset_file)
print(f"[INFO] Verifier tarball created : {asset_file}.")
utils.add_output("PR_tarball_name", asset_file)
if check_if_only_version_file_is_modified(args.api_url):
## should be on PR branch
# should be on PR branch
print(f'[INFO] Release found in PR files : {version_info["version"]}.')
utils.add_output("PR_version",version_info["version"])
utils.add_output("PR_release_image",version_info["quay-image"])
utils.add_output("PR_release_info",version_info["release-info"])
utils.add_output("PR_includes_release","true")
release_body = releasebody.get_release_body(version_info["version"],version_info["quay-image"],version_info["release-info"])
utils.add_output("PR_release_body",release_body)
utils.add_output("PR_version", version_info["version"])
utils.add_output("PR_release_image", version_info["quay-image"])
utils.add_output("PR_release_info", version_info["release-info"])
utils.add_output("PR_includes_release", "true")
release_body = releasebody.get_release_body(
version_info["version"],
version_info["quay-image"],
version_info["release-info"],
)
utils.add_output("PR_release_body", release_body)
else:
version_info = get_version_info()
if args.version:
# should be on main branch
version_compare = semver.compare(args.version,version_info["version"])
if version_compare > 0 :
print(f'[INFO] Release {args.version} found in PR files is newer than: {version_info["version"]}.')
utils.add_output("updated","true")
version_compare = semver.compare(args.version, version_info["version"])
if version_compare > 0:
print(
f"[INFO] Release {args.version} found in PR files is newer than: "
f'{version_info["version"]}.'
)
utils.add_output("updated", "true")
elif version_compare == 0 and not release_exists(args.version):
print(f'[INFO] Release {args.version} found in PR files is not new but no release exists yet.')
utils.add_output("updated","true")
print(
f"[INFO] Release {args.version} found in PR files is not new but "
"no release exists yet."
)
utils.add_output("updated", "true")
else:
print(f'[INFO] Release found in PR files is not new : {version_info["version"]} already exists.')
print(
"[INFO] Release found in PR files is not new: "
f'{version_info["version"]} already exists.'
)
else:
utils.add_output("PR_version",version_info["version"])
utils.add_output("PR_release_image",version_info["quay-image"])
utils.add_output("PR_version", version_info["version"])
utils.add_output("PR_release_image", version_info["quay-image"])
print("[INFO] PR contains non-release files.")
27 changes: 17 additions & 10 deletions scripts/src/release/tarfile_asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,38 @@
import tarfile
from utils import utils

tar_content_files = [ {"name": "out/chart-verifier", "arc_name": "chart-verifier"} ]
tar_content_files = [{"name": "out/chart-verifier", "arc_name": "chart-verifier"}]


def create(release):

tgz_name = f"chart-verifier-{release}.tgz"
utils.add_output("tarball_base_name",tgz_name)
utils.add_output("tarball_base_name", tgz_name)

if os.path.exists(tgz_name):
os.remove(tgz_name)

with tarfile.open(tgz_name, "x:gz") as tar:
for tar_content_file in tar_content_files:
tar.add(os.path.join(os.getcwd(),tar_content_file["name"]),arcname=tar_content_file["arc_name"])
tar.add(
os.path.join(os.getcwd(), tar_content_file["name"]),
arcname=tar_content_file["arc_name"],
)

return os.path.join(os.getcwd(), tgz_name)

return os.path.join(os.getcwd(),tgz_name)

def main():
parser = argparse.ArgumentParser()
parser.add_argument("-r", "--release", dest="release", type=str, required=True,
help="Release name for the tar file")
parser.add_argument(
"-r",
"--release",
dest="release",
type=str,
required=True,
help="Release name for the tar file",
)

args = parser.parse_args()
tarfile = create(args.release)
print(f'[INFO] Verifier tarball created : {tarfile}.')
utils.add_output("tarball_full_name",tarfile)

print(f"[INFO] Verifier tarball created : {tarfile}.")
utils.add_output("tarball_full_name", tarfile)
Loading

0 comments on commit a360fb2

Please sign in to comment.