Skip to content

Commit

Permalink
fix(codemagic_app_preview): fix triggering a build with `CM_PULL_REQU…
Browse files Browse the repository at this point in the history
…EST` (#100)
  • Loading branch information
nilsreichardt authored Sep 30, 2023
1 parent d95c516 commit bbc1b80
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class PostCommand extends Command {
Future<void> 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;
}
Expand Down Expand Up @@ -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<GitHostRepository?> _getGitHostRepository(GitRepo gitRepo) async {
Expand Down
13 changes: 1 addition & 12 deletions packages/codemagic_app_preview/test/post_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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

Expand All @@ -94,20 +88,15 @@ 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';
environmentVariableAccessor.environmentVariables['FCI_BUILD_ID'] =
'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

Expand Down

0 comments on commit bbc1b80

Please sign in to comment.