Skip to content

Commit

Permalink
Fix missing before in PR events (#277)
Browse files Browse the repository at this point in the history
* Fix missing before in PR events

* Adjust

* Add test

* Cleanup
rbro112 authored Oct 14, 2024
1 parent 45695e5 commit 428360f
Showing 5 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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<EmergePluginExtensionData>(
configurationJson.inputStream()
)

assertEquals("github_head_sha", configuration.vcsOptions!!.sha)
assertEquals("github_base_sha", configuration.vcsOptions!!.baseSha)
assertEquals("github_base_sha", configuration.vcsOptions!!.previousSha)
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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")
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"number": 123,
"pull_request": {
"head": {
"ref": "feature",
"sha": "github_head_sha"
},
"base": {
"ref": "main",
"sha": "github_base_sha"
}
}
}
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 428360f

Please sign in to comment.