r/pythonhelp • • Jan 04 '19

Python Regex GitLab Repo URL Matching

Hey all. I'm having an issue with my regular expression in Python and was hoping somebody could help. This is what I have so far:

git_url = re.compile(r'([\w+@\:/\-~]+\.git)')

This returns both:

https://gitlab.aa.bb.cc.dd/username/repo-name.git    # repo names could included - or _
[email protected]/username/repo-name.git

I basically want the possibility of either, so I can select using a script. To give some context, this Python script uses the GitLab API and clones all repositories based on the username specified. The second part of the script reads:

[..]
for url in re.findall(git_url, group_attr):    # group_attr is a list of group attributes
    subprocess.call(['git', 'clone', url])

Any help is much appreciated :-)

0 Upvotes

4 comments sorted by

1

u/acecile Jan 05 '19

I dont really understand your issue but what I'm pretty sure if is that you don't want regex parsing the find the cloning URI. Check GitLab API again there must be a field returning this.

1

u/thecrumpetman Jan 05 '19

Yeah I think you’re right. The GitLab Projects API works great for this task (for d/l personal repos) as each Project has it’s own attributes so I can loop through. As it’s a single group with multiple repos, there’s only one item with attributes so I’ve simply converted it to a string (probably where I’m going wrong).

1

u/acecile Jan 05 '19

Yeah it reminds me something, I did work with GitLab API sometimes ago.

1

u/thecrumpetman Jan 05 '19

This is what I working with atm (omitted the variables, etc.):

items = gl.groups.list(sudo=username, owned=True)

for item in items:

print(item.attributes)

group_attr = gl.groups.get(item.id)

print(group_attr)

Outputs:

print(item.attributes) returns {'id': 20, ... }

print(group_attr) returns <class 'gitlab.v4.objects.Group'> => {'id': 20, ... }

I simply need the value from the ssh_url_to_repo key from group_attr but I'm unsure how to get it.