Skip to content

Commit

Permalink
chore: uses predefined GitLab CI variable to find API url
Browse files Browse the repository at this point in the history
Signed-off-by: Jennifer Power <[email protected]>
  • Loading branch information
jpower432 committed Jul 27, 2023
1 parent b39e90e commit 3e65a90
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 22 deletions.
2 changes: 1 addition & 1 deletion tests/trestlebot/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def test_with_target_branch(monkeypatch, valid_args_dict, capsys):
args_dict["target-branch"] = "main"
monkeypatch.setattr(sys, "argv", ["trestlebot", *args_dict_to_list(args_dict)])

with patch("trestlebot.cli.is_github_actions") as mock_check:
with patch("trestlebot.github.is_github_actions") as mock_check:
mock_check.return_value = False

with pytest.raises(SystemExit):
Expand Down
27 changes: 6 additions & 21 deletions trestlebot/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@

import argparse
import logging
import os
import sys
from typing import List, Optional

from trestlebot import bot, const, log
from trestlebot.github import GitHub
from trestlebot.gitlab import GitLab
from trestlebot.github import GitHub, is_github_actions
from trestlebot.gitlab import GitLab, get_gitlab_root_url, is_gitlab_ci
from trestlebot.provider import GitProvider
from trestlebot.tasks.assemble_task import AssembleTask
from trestlebot.tasks.authored import types
Expand Down Expand Up @@ -237,7 +236,10 @@ def run() -> None:
if is_github_actions():
git_provider = GitHub(access_token=args.with_token.read().strip())
elif is_gitlab_ci():
git_provider = GitLab(api_token=args.with_token.read().strip())
server_api_url = get_gitlab_root_url()
git_provider = GitLab(
api_token=args.with_token.read().strip(), server_url=server_api_url
)
else:
logger.error(
(
Expand Down Expand Up @@ -282,20 +284,3 @@ def comma_sep_to_list(string: str) -> List[str]:
"""Convert comma-sep string to list of strings and strip."""
string = string.strip() if string else ""
return list(map(str.strip, string.split(","))) if string else []


# GitHub ref:
# https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
def is_github_actions() -> bool:
var_value = os.getenv("GITHUB_ACTIONS")
if var_value and var_value.lower() in ["true", "1"]:
return True
return False


# GitLab ref: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
def is_gitlab_ci() -> bool:
var_value = os.getenv("GITLAB_CI")
if var_value and var_value.lower() in ["true", "1"]:
return True
return False
11 changes: 11 additions & 0 deletions trestlebot/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

"""GitHub related functions for the Trestle Bot."""

import os
import re
from typing import Optional, Tuple

Expand Down Expand Up @@ -102,3 +103,13 @@ def create_pull_request(
raise GitProviderException(
"Failed to create pull request in {ns}/{repo_name} for {head_branch} to {base_branch}"
)


# GitHub ref:
# https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
def is_github_actions() -> bool:
"""Determine in the environment is GitHub Actions"""
var_value = os.getenv("GITHUB_ACTIONS")
if var_value and var_value.lower() in ["true", "1"]:
return True
return False
24 changes: 24 additions & 0 deletions trestlebot/gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

"""GitLab related functions for the Trestle Bot."""

import os
import re
from typing import Tuple

Expand Down Expand Up @@ -99,3 +100,26 @@ def create_pull_request(
raise GitProviderException(
f"Authentication error during merge request creation in {ns}/{repo_name}: {e}"
)


# GitLab ref: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html


def is_gitlab_ci() -> bool:
"""Determine if the environment in GitLab CI"""
var_value = os.getenv("GITLAB_CI")
if var_value and var_value.lower() in ["true", "1"]:
return True
return False


def get_gitlab_root_url() -> str:
"""Get the GitLab URL"""
protocol = os.getenv("CI_SERVER_PROTOCOL")
host = os.getenv("CI_SERVER_HOST")
if protocol and host:
return f"{protocol}://{host}"
else:
raise GitProviderException(
"Set CI_SERVER_PROTOCOL and CI SERVER HOST environment variables"
)

0 comments on commit 3e65a90

Please sign in to comment.