From 60e128a42d6a1da90ee5defc9a2b71d1024b4189 Mon Sep 17 00:00:00 2001 From: Andrew Date: Sun, 21 May 2023 12:46:47 -0500 Subject: [PATCH] feat: ghe support (#23) * docs: cleanup badges * feat: support for github enterprise This adds github_server_url to the inputs and adds logic to try to autodsicover it via the Environment * docs: automated doc update * fix: api_url keyword * docs: fix action descriptions to remove newlines * docs: automated doc update --- README.md | 11 ++--------- action.yml | 9 ++++----- pyproject.toml | 1 - repo_manager/github/__init__.py | 4 ++-- repo_manager/utils/__init__.py | 15 ++++++++++++++- repo_manager/utils/_inputs.py | 6 +++++- 6 files changed, 27 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index de43c0d9..c9dedb2e 100644 --- a/README.md +++ b/README.md @@ -3,10 +3,6 @@ [![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors-) -[![Action Template](https://img.shields.io/badge/Action%20Template-Python%20Container%20Action-blue.svg?colorA=24292e&colorB=0366d6&style=flat&longCache=true&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAOCAYAAAAfSC3RAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAM6wAADOsB5dZE0gAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAERSURBVCiRhZG/SsMxFEZPfsVJ61jbxaF0cRQRcRJ9hlYn30IHN/+9iquDCOIsblIrOjqKgy5aKoJQj4O3EEtbPwhJbr6Te28CmdSKeqzeqr0YbfVIrTBKakvtOl5dtTkK+v4HfA9PEyBFCY9AGVgCBLaBp1jPAyfAJ/AAdIEG0dNAiyP7+K1qIfMdonZic6+WJoBJvQlvuwDqcXadUuqPA1NKAlexbRTAIMvMOCjTbMwl1LtI/6KWJ5Q6rT6Ht1MA58AX8Apcqqt5r2qhrgAXQC3CZ6i1+KMd9TRu3MvA3aH/fFPnBodb6oe6HM8+lYHrGdRXW8M9bMZtPXUji69lmf5Cmamq7quNLFZXD9Rq7v0Bpc1o/tp0fisAAAAASUVORK5CYII=)](https://github.com/andrewthetechie/gha-repo-manager) -[![Actions Status](https://github.com/andrewthetechie/gha-repo-manager/workflows/Lint/badge.svg)](https://github.com/andrewthetechie/gha-repo-manager/actions) -[![Actions Status](https://github.com/andrewthetechie/gha-repo-manager/workflows/Integration%20Test/badge.svg)](https://github.com/andrewthetechie/gha-repo-manager/actions) - ## Description @@ -64,13 +60,10 @@ jobs: | parameter | description | required | default | | - | - | - | - | -| action | What action to take with this action. One of validate, check, or apply. -Validate will validate your settings file, but not touch your repo. -Check will check your repo with your settings file and output a report of any drift. -Apply will apply the settings in your settings file to your repo - | `false` | check | +| action | What action to take with this action. One of validate, check, or apply. Validate will validate your settings file, but not touch your repo. Check will check your repo with your settings file and output a report of any drift. Apply will apply the settings in your settings file to your repo | `false` | check | | settings_file | What yaml file to use as your settings. This is local to runner running this action. | `false` | .github/settings.yml | | repo | What repo to perform this action on. Default is self, as in the repo this action is running in | `false` | self | +| github_server_url | Set a custom github server url for github api operations. Useful if you're running on GHE. Will try to autodiscover from env.GITHUB_SERVER_URL if left at default | `false` | none | | token | What github token to use with this action. | `true` | | diff --git a/action.yml b/action.yml index c5c7c986..f1f613ab 100644 --- a/action.yml +++ b/action.yml @@ -3,11 +3,7 @@ description: "Manage your Github repo(s) settings and secrets using Github Actio author: "Andrew Herrington" inputs: action: - description: | - What action to take with this action. One of validate, check, or apply. - Validate will validate your settings file, but not touch your repo. - Check will check your repo with your settings file and output a report of any drift. - Apply will apply the settings in your settings file to your repo + description: What action to take with this action. One of validate, check, or apply. Validate will validate your settings file, but not touch your repo. Check will check your repo with your settings file and output a report of any drift. Apply will apply the settings in your settings file to your repo default: "check" settings_file: description: What yaml file to use as your settings. This is local to runner running this action. @@ -15,6 +11,9 @@ inputs: repo: description: What repo to perform this action on. Default is self, as in the repo this action is running in default: "self" + github_server_url: + description: Set a custom github server url for github api operations. Useful if you're running on GHE. Will try to autodiscover from env.GITHUB_SERVER_URL if left at default + default: "none" token: description: What github token to use with this action. required: true diff --git a/pyproject.toml b/pyproject.toml index 839b66da..e7070274 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -58,6 +58,5 @@ show_error_codes = true show_error_context = true [tool.pytest.ini_options] -python_paths = "./" norecursedirs = ".github ci .git .idea" addopts = "--cov=repo_manager --cov-report xml:.coverage.xml --cov-report=term-missing" diff --git a/repo_manager/github/__init__.py b/repo_manager/github/__init__.py index 2577a40e..1e893f66 100644 --- a/repo_manager/github/__init__.py +++ b/repo_manager/github/__init__.py @@ -4,6 +4,6 @@ @lru_cache -def get_github_client(token: str) -> Github: +def get_github_client(token: str, api_url: str) -> Github: """ """ - return Github(token) + return Github(token, base_url=api_url) diff --git a/repo_manager/utils/__init__.py b/repo_manager/utils/__init__.py index db20fd7a..0683df9d 100644 --- a/repo_manager/utils/__init__.py +++ b/repo_manager/utils/__init__.py @@ -71,8 +71,21 @@ def validate_inputs(parsed_inputs: dict[str, Any]) -> dict[str, Any]: + "GITHUB_REPOSITORY env var is not set. Please set INPUT_REPO or GITHUB_REPOSITORY in the env" ) + if parsed_inputs["github_server_url"].lower() == "none": + parsed_inputs["github_server_url"] = os.environ.get("GITHUB_SERVER_URL", None) + if parsed_inputs["github_server_url"] is None: + actions_toolkit.set_failed( + "Error getting inputs. github_server_url is 'none' and " + + "GITHUB_SERVER_URL env var is not set. Please set " + + "INPUT_GITHUB_SERVER_URL or GITHUB_SERVER_URL in the env" + ) + if parsed_inputs["github_server_url"] == "https://github.com": + api_url = "https://api.github.com" + else: + api_url = parsed_inputs["github_server_url"] + "/api/v3" + try: - repo = get_github_client(parsed_inputs["token"]).get_repo(parsed_inputs["repo"]) + repo = get_github_client(parsed_inputs["token"], api_url=api_url).get_repo(parsed_inputs["repo"]) except Exception as exc: # this should be tighter actions_toolkit.set_failed(f"Error while retriving {parsed_inputs['repo']} from Github. {exc}") diff --git a/repo_manager/utils/_inputs.py b/repo_manager/utils/_inputs.py index b9a2a929..8c17389a 100644 --- a/repo_manager/utils/_inputs.py +++ b/repo_manager/utils/_inputs.py @@ -3,7 +3,7 @@ ###START_INPUT_AUTOMATION### INPUTS = { "action": { - "description": "What action to take with this action. One of validate, check, or apply.\nValidate will validate your settings file, but not touch your repo.\nCheck will check your repo with your settings file and output a report of any drift.\nApply will apply the settings in your settings file to your repo\n", + "description": "What action to take with this action. One of validate, check, or apply. Validate will validate your settings file, but not touch your repo. Check will check your repo with your settings file and output a report of any drift. Apply will apply the settings in your settings file to your repo", "default": "check", }, "settings_file": { @@ -14,6 +14,10 @@ "description": "What repo to perform this action on. Default is self, as in the repo this action is running in", "default": "self", }, + "github_server_url": { + "description": "Set a custom github server url for github api operations. Useful if you're running on GHE. Will try to autodiscover from env.GITHUB_SERVER_URL if left at default", + "default": "none", + }, "token": {"description": "What github token to use with this action.", "required": True}, } ###END_INPUT_AUTOMATION###