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

Add check command #11

Merged
merged 1 commit into from
Oct 4, 2024
Merged
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
47 changes: 47 additions & 0 deletions guw/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import sys
import tempfile
from datetime import date
from functools import cache

import colorlog
import git
Expand All @@ -20,6 +21,24 @@
stream_handle.setFormatter(formatter)
logger.addHandler(stream_handle)

VALID_STATUS = ["integrated", "merging", "pending"]


@cache
def branch_refs(repo_url):
g = git.Git()

return g.ls_remote("--heads", repo_url)


def branch_exists_remote(repo_url, branch_name):
refs = branch_refs(repo_url)

for ref in refs.splitlines():
if ref.endswith(f"refs/heads/{branch_name}"):
return True
return False


class GUW:
def __init__(self, config):
Expand Down Expand Up @@ -227,6 +246,30 @@ def markdown(self):
li += f" [(Branch link)]({branch_url}{feature['name']})"
print(li)

def check(self):
remotes = {}
for remote in self.config["remotes"]:
remotes[remote["name"]] = remote["url"]

for feature in self.config["features"]:
remote = feature["remote"]

if feature["status"] not in VALID_STATUS:
logger.error(
f"Invalid status for '{feature['name']}': '{feature['status']}'"
)
exit(1)

if not branch_exists_remote(
remotes.get(feature["remote"]), feature["name"]
):
logger.error(
f"'{feature['name']}' in '{feature['remote']}' does not exist"
)
exit(1)

logger.info(f"The toml file is correct")

def add(
self, backup, keep, local, folder, new_feature, new_feature_remote, prev_feature
):
Expand Down Expand Up @@ -345,6 +388,8 @@ def run():
_common_command_arguments(sync_args)
# Markdown subcommand
markdown_args = subparser.add_parser("markdown", help="Create a markdown content")
# Check subcommand
check_args = subparser.add_parser("check", help="Check toml file is correct")
# Add subcommand
add_args = subparser.add_parser("add", help="Add a new feature branch")
_common_command_arguments(add_args)
Expand Down Expand Up @@ -386,6 +431,8 @@ def run():
guw.sync(args.backup, args.keep, args.local, args.directory)
elif args.command == "markdown":
guw.markdown()
elif args.command == "check":
guw.check()
elif args.command == "add":
guw.add(
args.backup,
Expand Down