Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing before in PR events #277

Merged
merged 4 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
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
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
Expand Down
Loading