-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[pontos-release]: Remove project parameter, f-string-ed strings, refactor #105
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
99d9578
Moved functions into helper class, fstring-ed strings, remove --proje…
y0urself dbbf43a
Adjust the tests
y0urself 4c9c6e0
Set versions nicely
y0urself 4e5a19f
Add CHANGELOG entry
y0urself 9cabf60
Adjust the release readme
y0urself c2876e8
Fully documented helper functions
y0urself 15c0020
Readded project parameter, but not required, reworked get_project_nam…
y0urself afa12e2
Use tmpdir
y0urself c7bd967
Update pontos/release/helper.py
y0urself File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,224 @@ | ||
# -*- coding: utf-8 -*- | ||
# pontos/release/release.py | ||
# Copyright (C) 2020 - 2021 Greenbone Networks GmbH | ||
# | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
|
||
from typing import Callable, Dict, List, Tuple, Union | ||
import json | ||
import tempfile | ||
from pathlib import Path | ||
import shutil | ||
|
||
import requests | ||
|
||
from pontos import version | ||
|
||
|
||
def build_release_dict( | ||
release_version: str, | ||
release_changelog: str, | ||
*, | ||
name: str = '', | ||
target_commitish: str = '', | ||
draft: bool = False, | ||
prerelease: bool = False, | ||
) -> Dict[str, Union[str, bool]]: | ||
""" | ||
builds the dict for release post on github, see: | ||
https://docs.github.com/en/rest/reference/repos#create-a-release | ||
for more details. | ||
|
||
Arguments: | ||
release_version: The version (str) that will be set | ||
release_changelog: content of the Changelog (str) for the release | ||
name: name (str) of the release, e.g. 'pontos 1.0.0' | ||
target_commitish: needed when tag is not there yet (str) | ||
draft: If the release is a draft (bool) | ||
prerelease: If the release is a pre release (bool) | ||
|
||
Returns: | ||
The dictionary containing the release information. | ||
""" | ||
tag_name = ( | ||
release_version | ||
if release_version.startswith('v') | ||
else "v" + release_version | ||
) | ||
return { | ||
'tag_name': tag_name, | ||
'target_commitish': target_commitish, | ||
'name': name, | ||
'body': release_changelog, | ||
'draft': draft, | ||
'prerelease': prerelease, | ||
} | ||
|
||
|
||
def commit_files( | ||
filename: str, | ||
commit_msg: str, | ||
shell_cmd_runner: Callable, | ||
*, | ||
git_signing_key: str = '', | ||
): | ||
"""Add files to staged and commit staged files. | ||
|
||
filename: The filename of file to add and commit | ||
commit_msg: The commit message for the commit | ||
shell_cmd_runner: | ||
git_signing_key: The signing key to sign this commit | ||
|
||
Arguments: | ||
to: The version (str) that will be set | ||
develop: Wether to set version to develop or not (bool) | ||
|
||
Returns: | ||
executed: True if successfully executed, False else | ||
filename: The filename of the project definition | ||
""" | ||
|
||
shell_cmd_runner(f"git add {filename}") | ||
shell_cmd_runner("git add *__version__.py || echo 'ignoring __version__'") | ||
shell_cmd_runner("git add CHANGELOG.md") | ||
shell_cmd_runner( | ||
f"git commit -S {git_signing_key} -m '{commit_msg}'", | ||
) | ||
|
||
|
||
def download( | ||
url: str, | ||
filename: str, | ||
requests_module: requests, | ||
path: Path, | ||
) -> Path: | ||
"""Download file in url to filename | ||
|
||
Arguments: | ||
url: The url of the file we want to download | ||
filename: The name of the file to store the download in | ||
requests_module: the python request module | ||
path: the python pathlib.Path module | ||
|
||
Returns: | ||
Path to the downloaded file | ||
""" | ||
|
||
file_path = path(tempfile.gettempdir()) / filename | ||
|
||
with requests_module.get(url, stream=True) as resp, file_path.open( | ||
mode='wb' | ||
) as download_file: | ||
shutil.copyfileobj(resp.raw, download_file) | ||
|
||
return file_path | ||
|
||
|
||
def get_project_name( | ||
shell_cmd_runner: Callable, | ||
*, | ||
remote: str = 'origin', | ||
) -> str: | ||
"""Get the git repository name""" | ||
# https://stackoverflow.com/a/42543006 | ||
ret = shell_cmd_runner(f'git remote get-url {remote}') | ||
return ret.stdout.split('/')[-1].replace('.git', '').replace('\n', '') | ||
|
||
|
||
def update_version( | ||
to: str, _version: version, *, develop: bool = False | ||
) -> Tuple[bool, str]: | ||
"""Use pontos-version to update the version. | ||
|
||
Arguments: | ||
to: The version (str) that will be set | ||
_version: Version module | ||
develop: Wether to set version to develop or not (bool) | ||
|
||
Returns: | ||
executed: True if successfully executed, False else | ||
filename: The filename of the project definition | ||
""" | ||
args = ['--quiet'] | ||
args.append('update') | ||
args.append(to) | ||
if develop: | ||
args.append('--develop') | ||
executed, filename = _version.main(False, args=args) | ||
y0urself marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if not executed: | ||
if filename == "": | ||
print("No project definition found.") | ||
else: | ||
print(f"Unable to update version {to} in {filename}") | ||
|
||
return executed, filename | ||
|
||
|
||
def upload_assets( | ||
username: str, | ||
token: str, | ||
pathnames: List[str], | ||
github_json: Dict, | ||
path: Path, | ||
requests_module: requests, | ||
) -> bool: | ||
"""Function to upload assets | ||
|
||
Arguments: | ||
username: The GitHub username to use for the upload | ||
token: That username's GitHub token | ||
pathnames: List of paths to asset files | ||
github_json: The github dictionary, containing relevant information | ||
for the uplaod | ||
path: the python pathlib.Path module | ||
requests_module: the python request module | ||
|
||
Returns: | ||
True on success, false else | ||
""" | ||
print(f"Uploading assets: {pathnames}") | ||
|
||
asset_url = github_json['upload_url'].replace('{?name,label}', '') | ||
paths = [path(f'{p}.asc') for p in pathnames] | ||
|
||
headers = { | ||
'Accept': 'application/vnd.github.v3+json', | ||
'content-type': 'application/octet-stream', | ||
} | ||
auth = (username, token) | ||
|
||
for path in paths: | ||
to_upload = path.read_bytes() | ||
resp = requests_module.post( | ||
f"{asset_url}?name={path.name}", | ||
headers=headers, | ||
auth=auth, | ||
data=to_upload, | ||
) | ||
|
||
if resp.status_code != 201: | ||
print( | ||
f"Wrong response status {resp.status_code}" | ||
f" while uploading {path.name}" | ||
) | ||
print(json.dumps(resp.text, indent=4, sort_keys=True)) | ||
return False | ||
else: | ||
print(f"uploaded: {path.name}") | ||
|
||
return True |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would recommend to only pass boolean args by keyword.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The called main looks like this:
def main(leave=True, args=None):
So i can not pass any parameter but
leave
andargs
...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the readability it is always better to pass pass booleans as keywords.
somefunc(True, False, True)
is not readable. In our caseversion.main(leave=False, args=args)
is easier to read.