Skip to content
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

Test v3 2 #83

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/autorelease.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Autorelease

on:
pull_request:
types: [ closed ]

jobs:
autorelease:
if: github.event.pull_request.merged == true

runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3

- name: Set up python
uses: actions/setup-python@v4
with:
python-version: 3.8
- name: Set up PDM
uses: pdm-project/setup-pdm@v3

- name: Install dependencies
run: pdm sync

- name: Run autorelease
run: |
. .venv/bin/activate
python3 auto_release.py --github_token ${{ secrets.GITHUB_TOKEN }} --current_branch ${{ github.base_ref }}

2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Build and publish package to PyPI

on:
release:
types: [ published ]
types: [ created ]

jobs:
build-and-publish:
Expand Down
36 changes: 0 additions & 36 deletions .github/workflows/python-linters.yml

This file was deleted.

29 changes: 0 additions & 29 deletions .github/workflows/python-tests.yml

This file was deleted.

95 changes: 95 additions & 0 deletions auto_release.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import argparse
import logging
import re
from typing import List, Optional

import httpx
import toml # type: ignore

logger = logging.getLogger(__name__)
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.INFO)


def get_project_version() -> str:
with open("pyproject.toml") as f:
data = toml.loads(f.read())
return data["project"]["version"]


def make_request(
method: str,
path: str,
token: str,
data: Optional[dict] = None,
params: Optional[dict] = None,
) -> httpx.Response:
base_url = "https://api.github.com/repos/huntflow/huntflow-api-client-python"
url = base_url + path
headers = {"Authorization": f"Bearer {token}"}
response = httpx.request(method=method, url=url, json=data, headers=headers, params=params)
response.raise_for_status()
return response


def get_release_tags(github_token: str) -> List[str]:
result: List[str] = []
params = {"page": 1}
while True:
response = make_request(method="GET", path="/releases", token=github_token, params=params)
data = response.json()
if not data:
return result
existing_tags = [item["tag_name"] for item in data]
result.extend(existing_tags)
params["page"] += 1


def main(github_token: str, current_branch: str) -> None:
branch_patter = r"^v(?P<major_release>\d+)$"
branch_matching = re.match(branch_patter, current_branch, re.I)
if not branch_matching:
logger.info("Branch %s is not a valid release branch", current_branch)
return

major_release = branch_matching.group("major_release")
project_version = get_project_version()
logger.info("Project version %s", project_version)

if not project_version.startswith(major_release):
logger.info(
"Release tag %s is invalid, major releases in the %s branch must start with %s",
project_version,
current_branch,
major_release,
)
return
existing_releases = get_release_tags(github_token=github_token)

if project_version in existing_releases:
logger.info("Release %s already exists", project_version)
return

is_latest = all(project_version > item for item in existing_releases)
logger.info("Latest release: %s", is_latest)
release_data = {
"tag_name": project_version,
"name": f"Release {project_version}",
"target_commitish": current_branch,
"make_latest": str(is_latest).lower(),
}
make_request(method="POST", path="/releases", token=github_token, data=release_data)

logger.info("Release %s successfully created", project_version)


def parse_args(): # type: ignore
parser = argparse.ArgumentParser()
parser.add_argument("--github_token", required=True, type=str)
parser.add_argument("--current_branch", required=True, type=str)
return parser.parse_args()


if __name__ == "__main__":
args = parse_args()
main(args.github_token, args.current_branch)
Loading