From acebbc9b0049e5f70e12178524ea42c3bd3d3000 Mon Sep 17 00:00:00 2001 From: Arran Ubels Date: Wed, 28 Aug 2024 11:38:05 +1000 Subject: [PATCH] Use github action provided environment variables as a fallback if custom specified ones don't exist. (#217) --- .../github/publish_github_config.dart | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/flutter_app_publisher/lib/src/publishers/github/publish_github_config.dart b/packages/flutter_app_publisher/lib/src/publishers/github/publish_github_config.dart index 92a29be9..189ac0e6 100644 --- a/packages/flutter_app_publisher/lib/src/publishers/github/publish_github_config.dart +++ b/packages/flutter_app_publisher/lib/src/publishers/github/publish_github_config.dart @@ -3,6 +3,8 @@ import 'dart:io'; import 'package:flutter_app_publisher/src/api/app_package_publisher.dart'; const kEnvGithubToken = 'GITHUB_TOKEN'; +const kEnvGithubRepositoryOwner = 'GITHUB_REPOSITORY_OWNER'; +const kEnvGithubRepository = 'GITHUB_REPOSITORY'; class PublishGithubConfig extends PublishConfig { PublishGithubConfig({ @@ -17,15 +19,31 @@ class PublishGithubConfig extends PublishConfig { Map? publishArguments, ) { String? token = (environment ?? Platform.environment)[kEnvGithubToken]; + String? githubRepository = (environment ?? Platform.environment)[kEnvGithubRepository]; + String? githubRepositoryOwner = (environment ?? Platform.environment)[kEnvGithubRepositoryOwner]; if ((token ?? '').isEmpty) { throw PublishError('Missing `$kEnvGithubToken` environment variable.'); } String? owner = publishArguments?['repo-owner']; - if ((owner ?? '').isEmpty) { - throw PublishError(' is null'); + if ((owner ?? '').isEmpty && githubRepositoryOwner?.isNotEmpty == true) { + print("Using provided ${githubRepositoryOwner} from ENV ${kEnvGithubRepositoryOwner} as repo-owner"); + owner = githubRepositoryOwner; } String? name = publishArguments?['repo-name']; + if ((owner ?? '').isEmpty && githubRepository?.isNotEmpty == true && githubRepository!.contains("/")) { + print("Extracting repo name from provided ${githubRepository} from ENV ${kEnvGithubRepository} as repo-name"); + var parts = githubRepository.split("/"); + if ((owner ?? '').isEmpty) { + owner = parts[0]; + } else if (owner != parts[0]) { + throw PublishError(' is mismatch error between ${githubRepository} from ENV ${kEnvGithubRepository} and ${githubRepositoryOwner} from ENV ${kEnvGithubRepositoryOwner}'); + } + name = parts[1]; + } + if ((owner ?? '').isEmpty) { + throw PublishError(' is null'); + } if ((name ?? '').isEmpty) { throw PublishError(' is null'); }