Skip to content

Commit

Permalink
format code by black and isort (#6167)
Browse files Browse the repository at this point in the history
Signed-off-by: Ye Sijun <[email protected]>
  • Loading branch information
junnplus authored Jul 11, 2023
1 parent 7f40837 commit 9b2f635
Show file tree
Hide file tree
Showing 219 changed files with 2,538 additions and 4,207 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ jobs:
- uses: actions/setup-python@v4
with:
python-version: '3.8'
- run: sudo pip install flake8
- run: ./bin/flake8_tests.sh
- run: sudo pip install flake8 black isort
- run: flake8 .
- run: black --check .
- run: isort --check-only --diff .

backend-unit-tests:
runs-on: ubuntu-22.04
Expand Down
2 changes: 1 addition & 1 deletion bin/flake8_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ set -o errexit # fail the build if any task fails
flake8 --version ; pip --version
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
25 changes: 17 additions & 8 deletions bin/get_changes.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,44 @@
#!/bin/env python3

import sys
import re
import subprocess
import sys


def get_change_log(previous_sha):
args = ['git', '--no-pager', 'log', '--merges', '--grep', 'Merge pull request', '--pretty=format:"%h|%s|%b|%p"', 'master...{}'.format(previous_sha)]
args = [
"git",
"--no-pager",
"log",
"--merges",
"--grep",
"Merge pull request",
'--pretty=format:"%h|%s|%b|%p"',
"master...{}".format(previous_sha),
]
log = subprocess.check_output(args)
changes = []

for line in log.split('\n'):
for line in log.split("\n"):
try:
sha, subject, body, parents = line[1:-1].split('|')
sha, subject, body, parents = line[1:-1].split("|")
except ValueError:
continue

try:
pull_request = re.match("Merge pull request #(\d+)", subject).groups()[0]
pull_request = re.match(r"Merge pull request #(\d+)", subject).groups()[0]
pull_request = " #{}".format(pull_request)
except Exception as ex:
except Exception:
pull_request = ""

author = subprocess.check_output(['git', 'log', '-1', '--pretty=format:"%an"', parents.split(' ')[-1]])[1:-1]
author = subprocess.check_output(["git", "log", "-1", '--pretty=format:"%an"', parents.split(" ")[-1]])[1:-1]

changes.append("{}{}: {} ({})".format(sha, pull_request, body.strip(), author))

return changes


if __name__ == '__main__':
if __name__ == "__main__":
previous_sha = sys.argv[1]
changes = get_change_log(previous_sha)

Expand Down
93 changes: 58 additions & 35 deletions bin/release_manager.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#!/usr/bin/env python3
import os
import sys
import re
import subprocess
import sys

import requests
import simplejson

github_token = os.environ['GITHUB_TOKEN']
auth = (github_token, 'x-oauth-basic')
repo = 'getredash/redash'
github_token = os.environ["GITHUB_TOKEN"]
auth = (github_token, "x-oauth-basic")
repo = "getredash/redash"


def _github_request(method, path, params=None, headers={}):
if not path.startswith('https://api.github.com'):
if not path.startswith("https://api.github.com"):
url = "https://api.github.com/{}".format(path)
else:
url = path
Expand All @@ -22,15 +24,18 @@ def _github_request(method, path, params=None, headers={}):
response = requests.request(method, url, data=params, auth=auth)
return response


def exception_from_error(message, response):
return Exception("({}) {}: {}".format(response.status_code, message, response.json().get('message', '?')))
return Exception("({}) {}: {}".format(response.status_code, message, response.json().get("message", "?")))


def rc_tag_name(version):
return "v{}-rc".format(version)


def get_rc_release(version):
tag = rc_tag_name(version)
response = _github_request('get', 'repos/{}/releases/tags/{}'.format(repo, tag))
response = _github_request("get", "repos/{}/releases/tags/{}".format(repo, tag))

if response.status_code == 404:
return None
Expand All @@ -39,84 +44,101 @@ def get_rc_release(version):

raise exception_from_error("Unknown error while looking RC release: ", response)


def create_release(version, commit_sha):
tag = rc_tag_name(version)

params = {
'tag_name': tag,
'name': "{} - RC".format(version),
'target_commitish': commit_sha,
'prerelease': True
"tag_name": tag,
"name": "{} - RC".format(version),
"target_commitish": commit_sha,
"prerelease": True,
}

response = _github_request('post', 'repos/{}/releases'.format(repo), params)
response = _github_request("post", "repos/{}/releases".format(repo), params)

if response.status_code != 201:
raise exception_from_error("Failed creating new release", response)

return response.json()


def upload_asset(release, filepath):
upload_url = release['upload_url'].replace('{?name,label}', '')
filename = filepath.split('/')[-1]
upload_url = release["upload_url"].replace("{?name,label}", "")
filename = filepath.split("/")[-1]

with open(filepath) as file_content:
headers = {'Content-Type': 'application/gzip'}
response = requests.post(upload_url, file_content, params={'name': filename}, headers=headers, auth=auth, verify=False)
headers = {"Content-Type": "application/gzip"}
response = requests.post(
upload_url, file_content, params={"name": filename}, headers=headers, auth=auth, verify=False
)

if response.status_code != 201: # not 200/201/...
raise exception_from_error('Failed uploading asset', response)
raise exception_from_error("Failed uploading asset", response)

return response


def remove_previous_builds(release):
for asset in release['assets']:
response = _github_request('delete', asset['url'])
for asset in release["assets"]:
response = _github_request("delete", asset["url"])
if response.status_code != 204:
raise exception_from_error("Failed deleting asset", response)


def get_changelog(commit_sha):
latest_release = _github_request('get', 'repos/{}/releases/latest'.format(repo))
latest_release = _github_request("get", "repos/{}/releases/latest".format(repo))
if latest_release.status_code != 200:
raise exception_from_error('Failed getting latest release', latest_release)
raise exception_from_error("Failed getting latest release", latest_release)

latest_release = latest_release.json()
previous_sha = latest_release['target_commitish']

args = ['git', '--no-pager', 'log', '--merges', '--grep', 'Merge pull request', '--pretty=format:"%h|%s|%b|%p"', '{}...{}'.format(previous_sha, commit_sha)]
previous_sha = latest_release["target_commitish"]

args = [
"git",
"--no-pager",
"log",
"--merges",
"--grep",
"Merge pull request",
'--pretty=format:"%h|%s|%b|%p"',
"{}...{}".format(previous_sha, commit_sha),
]
log = subprocess.check_output(args)
changes = ["Changes since {}:".format(latest_release['name'])]
changes = ["Changes since {}:".format(latest_release["name"])]

for line in log.split('\n'):
for line in log.split("\n"):
try:
sha, subject, body, parents = line[1:-1].split('|')
sha, subject, body, parents = line[1:-1].split("|")
except ValueError:
continue

try:
pull_request = re.match("Merge pull request #(\d+)", subject).groups()[0]
pull_request = re.match(r"Merge pull request #(\d+)", subject).groups()[0]
pull_request = " #{}".format(pull_request)
except Exception as ex:
except Exception:
pull_request = ""

author = subprocess.check_output(['git', 'log', '-1', '--pretty=format:"%an"', parents.split(' ')[-1]])[1:-1]
author = subprocess.check_output(["git", "log", "-1", '--pretty=format:"%an"', parents.split(" ")[-1]])[1:-1]

changes.append("{}{}: {} ({})".format(sha, pull_request, body.strip(), author))

return "\n".join(changes)


def update_release_commit_sha(release, commit_sha):
params = {
'target_commitish': commit_sha,
"target_commitish": commit_sha,
}

response = _github_request('patch', 'repos/{}/releases/{}'.format(repo, release['id']), params)
response = _github_request("patch", "repos/{}/releases/{}".format(repo, release["id"]), params)

if response.status_code != 200:
raise exception_from_error("Failed updating commit sha for existing release", response)

return response.json()


def update_release(version, build_filepath, commit_sha):
try:
release = get_rc_release(version)
Expand All @@ -125,21 +147,22 @@ def update_release(version, build_filepath, commit_sha):
else:
release = create_release(version, commit_sha)

print("Using release id: {}".format(release['id']))
print("Using release id: {}".format(release["id"]))

remove_previous_builds(release)
response = upload_asset(release, build_filepath)

changelog = get_changelog(commit_sha)

response = _github_request('patch', release['url'], {'body': changelog})
response = _github_request("patch", release["url"], {"body": changelog})
if response.status_code != 200:
raise exception_from_error("Failed updating release description", response)

except Exception as ex:
print(ex)

if __name__ == '__main__':

if __name__ == "__main__":
commit_sha = sys.argv[1]
version = sys.argv[2]
filepath = sys.argv[3]
Expand Down
Loading

0 comments on commit 9b2f635

Please sign in to comment.