forked from complytime/trestle-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds support for GitLab as a Provider type
Refs: PSCE-192 Signed-off-by: Jennifer Power <barnabei.jennifer@gmail.com>
- Loading branch information
Showing
7 changed files
with
252 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#!/usr/bin/python | ||
|
||
# Copyright 2023 Red Hat, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
"""Test for GitLab provider logic""" | ||
|
||
from typing import Tuple | ||
|
||
import pytest | ||
from git.repo import Repo | ||
|
||
from tests.testutils import clean | ||
from trestlebot.gitlab import GitLab | ||
from trestlebot.provider import GitProviderException | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"repo_url", | ||
[ | ||
"https://gitlab.com/owner/repo", | ||
"https://gitlab.com/owner/repo.git", | ||
"gitlab.com/owner/repo.git", | ||
], | ||
) | ||
def test_parse_repository(repo_url: str) -> None: | ||
"""Tests parsing valid GitLab repo urls""" | ||
gl = GitLab("fake") | ||
|
||
owner, repo_name = gl.parse_repository(repo_url) | ||
|
||
assert owner == "owner" | ||
assert repo_name == "repo" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"repo_url", | ||
[ | ||
"https://mygitlab.com/owner/repo", | ||
"https://mygitlab.com/owner/repo.git", | ||
"mygitlab.com/owner/repo.git", | ||
], | ||
) | ||
def test_parse_repository_with_server_url(repo_url: str) -> None: | ||
"""Test an invalid url input""" | ||
gl = GitLab("fake", "https://mygitlab.com") | ||
|
||
owner, repo_name = gl.parse_repository(repo_url) | ||
|
||
assert owner == "owner" | ||
assert repo_name == "repo" | ||
|
||
|
||
def test_parse_repository_integration(tmp_repo: Tuple[str, Repo]) -> None: | ||
"""Tests integration with git remote get-url""" | ||
repo_path, repo = tmp_repo | ||
|
||
repo.create_remote("origin", url="gitlab.com/test/repo.git") | ||
|
||
remote = repo.remote() | ||
|
||
gl = GitLab("fake") | ||
|
||
owner, repo_name = gl.parse_repository(remote.url) | ||
|
||
assert owner == "test" | ||
assert repo_name == "repo" | ||
|
||
clean(repo_path, repo) | ||
|
||
|
||
def test_parse_repository_with_incorrect_name() -> None: | ||
"""Test an invalid url input""" | ||
gl = GitLab("fake") | ||
with pytest.raises( | ||
GitProviderException, | ||
match="https://notgitlab.com/owner/repo.git is an invalid repo URL", | ||
): | ||
gl.parse_repository("https://notgitlab.com/owner/repo.git") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#!/usr/bin/python | ||
|
||
# Copyright 2023 Red Hat, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
"""GitLab related functions for the Trestle Bot.""" | ||
|
||
import re | ||
from typing import Tuple | ||
|
||
import gitlab | ||
|
||
from trestlebot.provider import GitProvider, GitProviderException | ||
|
||
|
||
class GitLab(GitProvider): | ||
def __init__(self, api_token: str, server_url: str = "https://gitlab.com"): | ||
"""Create GitLab object to interact with the GitLab API""" | ||
|
||
self._gitlab_client = gitlab.Gitlab(server_url, private_token=api_token) | ||
|
||
stripped_url = re.sub(r"^(https?://)?", "", server_url) | ||
self.pattern = r"^(?:https?://)?{0}/([^/]+)/([^/.]+)".format( | ||
re.escape(stripped_url) | ||
) | ||
|
||
def parse_repository(self, repo_url: str) -> Tuple[str, str]: | ||
""" | ||
Parse repository url | ||
Args: | ||
repo_url: Valid url for a GitLab repo | ||
Returns: | ||
Owner and project name in a tuple, respectively | ||
""" | ||
match = re.match(self.pattern, repo_url) | ||
|
||
if not match: | ||
raise GitProviderException(f"{repo_url} is an invalid repo URL") | ||
|
||
owner = match.group(1) | ||
repo = match.group(2) | ||
return (owner, repo) | ||
|
||
def create_pull_request( | ||
self, | ||
ns: str, | ||
repo_name: str, | ||
base_branch: str, | ||
head_branch: str, | ||
title: str, | ||
body: str, | ||
) -> int: | ||
""" | ||
Create a pull (merge request in the GitLab) request in the repository | ||
Args: | ||
ns: Namespace or owner of the repository | ||
repo_name: Name of the repository | ||
base_branch: Branch that changes need to be merged into | ||
head_branch: Branch with changes | ||
title: Text for the title of the pull_request | ||
body: Text for the body of the pull request | ||
Returns: | ||
Pull/Merge request number | ||
""" | ||
|
||
try: | ||
project = self._gitlab_client.projects.get(f"{ns}/{repo_name}") | ||
merge_request = project.mergerequests.create( | ||
{ | ||
"source_branch": head_branch, | ||
"target_branch": base_branch, | ||
"title": title, | ||
"description": body, | ||
} | ||
) | ||
|
||
return merge_request.id | ||
|
||
except gitlab.exceptions.GitlabCreateError as e: | ||
raise GitProviderException( | ||
f"Failed to create merge request in {ns}/{repo_name}: {e}" | ||
) | ||
except gitlab.exceptions.GitlabAuthenticationError as e: | ||
raise GitProviderException( | ||
f"Authentication error during merge request creation in {ns}/{repo_name}: {e}" | ||
) |