diff --git a/gradle-plugin/plugin/src/functionalTest/kotlin/com/emergetools/android/gradle/NoVcsEmergePluginTest.kt b/gradle-plugin/plugin/src/functionalTest/kotlin/com/emergetools/android/gradle/NoVcsEmergePluginTest.kt index 0361b68a..0f7fb65f 100644 --- a/gradle-plugin/plugin/src/functionalTest/kotlin/com/emergetools/android/gradle/NoVcsEmergePluginTest.kt +++ b/gradle-plugin/plugin/src/functionalTest/kotlin/com/emergetools/android/gradle/NoVcsEmergePluginTest.kt @@ -4,6 +4,7 @@ import com.emergetools.android.gradle.base.EmergeGradleRunner import com.emergetools.android.gradle.mocks.assertSuccessfulUploadRequests import com.emergetools.android.gradle.tasks.internal.SaveExtensionConfigTask.Companion.EmergePluginExtensionData import com.emergetools.android.gradle.utils.EnvUtils.withGitHubPREvent +import com.emergetools.android.gradle.utils.EnvUtils.withGitHubPREventNoBefore import com.emergetools.android.gradle.utils.EnvUtils.withGitHubPushEvent import kotlinx.serialization.json.Json import kotlinx.serialization.json.decodeFromStream @@ -130,4 +131,27 @@ class NoVcsEmergePluginTest : EmergePluginTest() { // BaseSha not set by default assertNull(configuration.vcsOptions!!.baseSha) } + + @Test + fun `Assert previousSha and baseSha are the same on first commit to PR`() { + val runner = EmergeGradleRunner.create("no-vcs-params") + val configurationJson = File(runner.tempProjectDir, "emerge_config.json") + + runner + .withArguments("saveExtensionConfig", "--outputPath", configurationJson.path) + .withDebugTasks() + .withGitHubPREventNoBefore() + .assert { result, _ -> + result.assertSuccessfulTask(":saveExtensionConfig") + } + .build() + + val configuration = Json.decodeFromStream( + configurationJson.inputStream() + ) + + assertEquals("github_head_sha", configuration.vcsOptions!!.sha) + assertEquals("github_base_sha", configuration.vcsOptions!!.baseSha) + assertEquals("github_base_sha", configuration.vcsOptions!!.previousSha) + } } diff --git a/gradle-plugin/plugin/src/functionalTest/kotlin/com/emergetools/android/gradle/SimpleEmergePluginTest.kt b/gradle-plugin/plugin/src/functionalTest/kotlin/com/emergetools/android/gradle/SimpleEmergePluginTest.kt index 3ee6546a..06512330 100644 --- a/gradle-plugin/plugin/src/functionalTest/kotlin/com/emergetools/android/gradle/SimpleEmergePluginTest.kt +++ b/gradle-plugin/plugin/src/functionalTest/kotlin/com/emergetools/android/gradle/SimpleEmergePluginTest.kt @@ -4,6 +4,7 @@ import com.emergetools.android.gradle.base.EmergeGradleRunner import com.emergetools.android.gradle.mocks.assertSuccessfulUploadRequests import com.emergetools.android.gradle.tasks.internal.SaveExtensionConfigTask.Companion.EmergePluginExtensionData import com.emergetools.android.gradle.utils.EnvUtils.withGitHubPREvent +import com.emergetools.android.gradle.utils.EnvUtils.withGitHubPREventNoBefore import junit.framework.TestCase.assertEquals import kotlinx.serialization.json.Json import kotlinx.serialization.json.decodeFromStream diff --git a/gradle-plugin/plugin/src/functionalTest/kotlin/com/emergetools/android/gradle/utils/EnvUtils.kt b/gradle-plugin/plugin/src/functionalTest/kotlin/com/emergetools/android/gradle/utils/EnvUtils.kt index b740fdb2..c7034428 100644 --- a/gradle-plugin/plugin/src/functionalTest/kotlin/com/emergetools/android/gradle/utils/EnvUtils.kt +++ b/gradle-plugin/plugin/src/functionalTest/kotlin/com/emergetools/android/gradle/utils/EnvUtils.kt @@ -16,6 +16,17 @@ object EnvUtils { ) } + fun EmergeGradleRunner.withGitHubPREventNoBefore(): EmergeGradleRunner { + + val resource = this.javaClass.getResource("/github-event-mocks/mock_pr_event_no_before.json") + val jsonFile = File(resource.toURI()) + + return withEnvironment( + "GITHUB_EVENT_NAME" to "pull_request", + "GITHUB_EVENT_PATH" to jsonFile.path, + ) + } + fun EmergeGradleRunner.withGitHubPushEvent(): EmergeGradleRunner { val resource = this.javaClass.getResource("/github-event-mocks/mock_push_event.json") diff --git a/gradle-plugin/plugin/src/functionalTest/resources/github-event-mocks/mock_pr_event_no_before.json b/gradle-plugin/plugin/src/functionalTest/resources/github-event-mocks/mock_pr_event_no_before.json new file mode 100644 index 00000000..ef3eefd1 --- /dev/null +++ b/gradle-plugin/plugin/src/functionalTest/resources/github-event-mocks/mock_pr_event_no_before.json @@ -0,0 +1,13 @@ +{ + "number": 123, + "pull_request": { + "head": { + "ref": "feature", + "sha": "github_head_sha" + }, + "base": { + "ref": "main", + "sha": "github_base_sha" + } + } +} diff --git a/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/util/GitHub.kt b/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/util/GitHub.kt index 190145ad..7493b3f4 100644 --- a/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/util/GitHub.kt +++ b/gradle-plugin/plugin/src/main/kotlin/com/emergetools/android/gradle/util/GitHub.kt @@ -70,7 +70,9 @@ internal class GitHub(private val execOperations: ExecOperations) { fun previousSha(): String? { return when { isPush() -> getPushEventData().before - isPullRequest() -> getPullRequestEventData().before + // In the case of the first commit on a PR, the before sha is not available. + // We want previousSha to be the last commit on main, so we'll fallback to baseSha + isPullRequest() -> getPullRequestEventData().before ?: baseSha() else -> null } } @@ -109,8 +111,8 @@ internal class GitHub(private val execOperations: ExecOperations) { data class GitHubPullRequestEvent( @SerialName("pull_request") val pr: GitHubPullRequest, - val before: String, val number: Int, + val before: String? = null, ) @Serializable