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

g.extension: get branch from version #1700

Merged
merged 21 commits into from
Jul 21, 2021
Merged
21 changes: 13 additions & 8 deletions scripts/g.extension/g.extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@

from six.moves.urllib import request as urlrequest
from six.moves.urllib.error import HTTPError, URLError
from six.moves.urllib.parse import urlparse

# Get the XML parsing exceptions to catch. The behavior changed with Python 2.7
# and ElementTree 1.3.
Expand Down Expand Up @@ -270,25 +271,29 @@ def get_github_branches(

def get_default_branch(full_url):
"""Get default branch for repo (currently only implemented for github API and gitlab API)"""
if "/github.com/" in full_url:
organization, repository = re.split(r"/github.com/", full_url)[1].split("/")[
0:2
# Parse URL
url_parts = urlparse(full_url)
# Get organization and repository component
try:
organization, repository = url_parts.path.split("/")[
1:3
]
except URLError:
gscript.fatal(_("Cannot retrieve organization and repository from URL: <{}>.".format(full_url)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having this part here (and not e.g. in a separate, possibly nested function and called only when needed), prohibits general URLs to be used here (just to get main). I'm not sure how relevant that is, though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is only meant for known hosting services (currently: github, gitlab, bitbucket), so, this should not be an issue...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking the main would be used as a the default for any other service, but in that case, use likely needs to (and can) specify branch explicitly anyway.


# Construct API call and retrieve default branch
if url_parts.netloc == "github.com":
req = urlrequest.urlopen(
f"https://api.github.com/repos/{organization}/{repository}"
)
content = json.loads(req.read())
default_branch = content["default_branch"]
elif "/gitlab.com/" in full_url:
organization, repository = re.split(r"/gitlab.com/", full_url)[1].split("/")[
0:2
]
elif url_parts.netloc == "gitlab.com":
req = urlrequest.urlopen(
f"https://gitlab.com/api/v4/projects/{organization}%2F{repository}"
)
content = json.loads(req.read())
default_branch = content["default_branch"]

else:
default_branch = "main"
return default_branch
Expand Down