diff --git a/packages/codemagic_app_preview/lib/src/commands/post_command.dart b/packages/codemagic_app_preview/lib/src/commands/post_command.dart index e0aafaa..a194d16 100644 --- a/packages/codemagic_app_preview/lib/src/commands/post_command.dart +++ b/packages/codemagic_app_preview/lib/src/commands/post_command.dart @@ -70,7 +70,7 @@ class PostCommand extends Command { Future run({DateTime? now}) async { if (!_isPullRequest()) { stderr.writeln( - '"CM_PULL_REQUEST" is not set to "true". Seems like the current build is not building a pull request. Aborting.'); + '"CM_PULL_REQUEST_NUMBER" is not set. Seems like the current build is not building a pull request. Aborting.'); exitCode = 1; return; } @@ -133,12 +133,12 @@ class PostCommand extends Command { /// Returns `true` if the current build is building a pull request, `false` /// otherwise. bool _isPullRequest() { - // Set to "true" if the current build is building a pull request, "false" - // otherwise. - // - // https://docs.codemagic.io/flutter-configuration/built-in-variables/ - final isPullRequest = environmentVariableAccessor.get('CM_PULL_REQUEST'); - return isPullRequest == 'true'; + // We don't use CM_PULL_REQUEST here because this would require to set + // an additional environment variable in the workflow to trigger the + // build when using a label. + final pullRequestId = + environmentVariableAccessor.get('CM_PULL_REQUEST_NUMBER') as String?; + return pullRequestId != null && pullRequestId.isNotEmpty; } Future _getGitHostRepository(GitRepo gitRepo) async { diff --git a/packages/codemagic_app_preview/test/post_command_test.dart b/packages/codemagic_app_preview/test/post_command_test.dart index c6eaac2..6256064 100644 --- a/packages/codemagic_app_preview/test/post_command_test.dart +++ b/packages/codemagic_app_preview/test/post_command_test.dart @@ -46,8 +46,6 @@ void main() { test('sets exit code to 1 when not setting the token', () async { const pullRequestId = '24'; - environmentVariableAccessor.environmentVariables['CM_PULL_REQUEST'] = - 'true'; environmentVariableAccessor .environmentVariables['CM_PULL_REQUEST_NUMBER'] = pullRequestId; when(() => gitRepo.getHost()).thenAnswer((_) async => GitHost.github); @@ -71,10 +69,6 @@ void main() { '62877273178d247b70405cb0'; environmentVariableAccessor.environmentVariables['FCI_COMMIT'] = '50b04d910c6b73472f7dfc1fee38a67e7132bf32'; - environmentVariableAccessor.environmentVariables['CM_PULL_REQUEST'] = - 'true'; - environmentVariableAccessor - .environmentVariables['CM_PULL_REQUEST_NUMBER'] = pullRequestId; environmentVariableAccessor.environmentVariables['CM_ARTIFACT_LINKS'] = '[]'; // no artifacts @@ -94,9 +88,6 @@ void main() { test('sets exit code to 1 when build is not executed in pull request', () async { - const pullRequestId = '24'; - environmentVariableAccessor - .environmentVariables['CM_PULL_REQUEST_NUMBER'] = pullRequestId; when(() => gitRepo.getHost()).thenAnswer((_) async => GitHost.github); environmentVariableAccessor.environmentVariables['FCI_PROJECT_ID'] = '6274fcfc87c748ce531c7376'; @@ -104,10 +95,8 @@ void main() { '62877273178d247b70405cb0'; environmentVariableAccessor.environmentVariables['FCI_COMMIT'] = '50b04d910c6b73472f7dfc1fee38a67e7132bf32'; - environmentVariableAccessor.environmentVariables['CM_PULL_REQUEST'] = - null; environmentVariableAccessor - .environmentVariables['CM_PULL_REQUEST_NUMBER'] = pullRequestId; + .environmentVariables['CM_PULL_REQUEST_NUMBER'] = null; environmentVariableAccessor.environmentVariables['CM_ARTIFACT_LINKS'] = '[]'; // no artifacts