From b3591a1e1844ec915d58052e86c26bf3eb00631f Mon Sep 17 00:00:00 2001 From: Guy Sartorelli <36352093+GuySartorelli@users.noreply.github.com> Date: Mon, 13 Mar 2023 17:09:04 +1300 Subject: [PATCH] ENH Allow using .env file for github token (#240) --- README.md | 2 +- src/Steps/Release/PublishRelease.php | 3 +++ src/Utility/GitHubApi.php | 5 +++-- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 21346d2..1e6fd4d 100644 --- a/README.md +++ b/README.md @@ -177,7 +177,7 @@ Cow includes commands to help synchronise standardised data to all * `cow github:ratelimit` Check your current GitHub API rate limiting status (sync commands can use this up quickly) **Note:** All GitHub API commands require a `GITHUB_ACCESS_TOKEN` environment variable to be set before they can be -used. +used. It can be in the .env file (see [dev mode](#dev_mode)). ### Labels diff --git a/src/Steps/Release/PublishRelease.php b/src/Steps/Release/PublishRelease.php index 090f9b0..c1d60e1 100644 --- a/src/Steps/Release/PublishRelease.php +++ b/src/Steps/Release/PublishRelease.php @@ -366,6 +366,9 @@ protected function getGithubClient(OutputInterface $output) protected function getOAUTHToken(OutputInterface $output) { $token = getenv('GITHUB_API_TOKEN'); + if (empty($token)) { + $token = $_ENV['GITHUB_API_TOKEN']; + } if (empty($token)) { $token = Composer::getOAUTHToken($this->getCommandRunner($output)); } diff --git a/src/Utility/GitHubApi.php b/src/Utility/GitHubApi.php index 1071cd6..68cfd8a 100644 --- a/src/Utility/GitHubApi.php +++ b/src/Utility/GitHubApi.php @@ -28,13 +28,14 @@ class GitHubApi public function getClient() { if (!$this->client) { + $token = getenv(self::TOKEN_ENV_VAR) ?: $_ENV[self::TOKEN_ENV_VAR]; // Handled here rather than constructor so that exceptions will be formatted by SymfonyStyle - if (!getenv(self::TOKEN_ENV_VAR)) { + if (!$token) { throw new RuntimeException(self::TOKEN_ENV_VAR . ' environment variable is not defined!'); } $this->client = new Client(); - $this->client->authenticate(getenv(self::TOKEN_ENV_VAR), null, Client::AUTH_HTTP_TOKEN); + $this->client->authenticate($token, null, Client::AUTH_HTTP_TOKEN); } return $this->client; }