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 28, 2023
1 parent b39e90e commit e3b86e8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 21 deletions.
2 changes: 2 additions & 0 deletions tests/trestlebot/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,5 @@ def test_with_target_branch(monkeypatch, valid_args_dict, capsys):
)

assert expected_string in captured.err

mock_check.assert_called_once()
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 e3b86e8

Please sign in to comment.