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 #4850: Bump version codes for alpha 0.10 release, and fix version code ordering #4851

Merged
merged 8 commits into from
Mar 10, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private class TransformAndroidManifest(
}
val versionNameAttribute = manifestDocument.createAttribute("android:versionName").apply {
value = computeVersionName(
buildFlavor, majorVersion, minorVersion, commitHash = gitClient.branchMergeBase
buildFlavor, majorVersion, minorVersion, commitHash = gitClient.currentCommit
)
}
val applicationNameAttribute = manifestDocument.createAttribute("android:name").apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class GitClient(
) {
private val commandExecutor by lazy { CommandExecutorImpl() }

/** The commit hash of the HEAD of the local Git repository. */
val currentCommit: String by lazy { retrieveCurrentCommit() }

/** The name of the current branch of the local Git repository. */
val currentBranch: String by lazy { retrieveCurrentBranch() }

Expand All @@ -25,6 +28,10 @@ class GitClient(
*/
val changedFiles: Set<String> by lazy { retrieveChangedFilesWithPotentialDuplicates().toSet() }

private fun retrieveCurrentCommit(): String {
return executeGitCommandWithOneLineOutput("rev-parse HEAD")
}

private fun retrieveCurrentBranch(): String {
return executeGitCommandWithOneLineOutput("rev-parse --abbrev-ref HEAD")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class TransformAndroidManifestTest {
@JvmField
var tempFolder = TemporaryFolder()

private val commandExecutor by lazy { CommandExecutorImpl() }
private lateinit var testGitRepository: TestGitRepository

@Before
Expand Down Expand Up @@ -349,6 +350,11 @@ class TransformAndroidManifestTest {
val manifestFile = tempFolder.newFile(TEST_MANIFEST_FILE_NAME).apply {
writeText(TEST_MANIFEST_CONTENT_WITHOUT_VERSIONS)
}
// Use a separate branch to ensure the version uses the latest commit rather than the common
// merge base with the develop branch.
testGitRepository.checkoutNewBranch("release-branch")
testGitRepository.commit(message = "Release-only commit, e.g. a cherry-pick", allowEmpty = true)
val latestCommit = getMostRecentCommitOnCurrentBranch()

runScript(
tempFolder.root.absolutePath,
Expand All @@ -367,7 +373,7 @@ class TransformAndroidManifestTest {
assertThat(transformedManifest)
.containsMatch(
"android:versionName=\"$MAJOR_VERSION\\.$MINOR_VERSION" +
"-$BUILD_FLAVOR-[a-f0-9]{10}\""
"-$BUILD_FLAVOR-${latestCommit.take(10)}\""
)
assertThat(transformedManifest)
.containsMatch("<application android:name=\"$APPLICATION_RELATIVE_QUALIFIED_CLASS\"")
Expand All @@ -386,4 +392,11 @@ class TransformAndroidManifestTest {
testGitRepository.checkoutNewBranch("develop")
testGitRepository.commit(message = "Initial commit.", allowEmpty = true)
}

private fun getMostRecentCommitOnCurrentBranch(): String {
// See https://stackoverflow.com/a/949391 for a reference to validate that this is correct.
return commandExecutor.executeCommand(
tempFolder.root, "git", "rev-parse", "HEAD"
).output.single()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,42 @@ class GitClientTest {
println(testGitRepository.status())
}

@Test
fun testCurrentCommit_forNonRepository_throwsException() {
val gitClient = GitClient(tempFolder.root, "develop")

val exception = assertThrows(IllegalStateException::class) { gitClient.currentCommit }

assertThat(exception).hasMessageThat().contains("Expected non-zero exit code")
assertThat(exception).hasMessageThat().ignoringCase().contains("not a git repository")
}

@Test
fun testCurrentCommit_forValidRepository_returnsCorrectBranch() {
initializeRepoWithDevelopBranch()
val developHash = getMostRecentCommitOnCurrentBranch()

val gitClient = GitClient(tempFolder.root, "develop")
val currentCommit = gitClient.currentCommit

assertThat(currentCommit).isEqualTo(developHash)
}

@Test
fun testCurrentCommit_switchBranch_returnsCorrectBranch() {
initializeRepoWithDevelopBranch()
val developHash = getMostRecentCommitOnCurrentBranch()
testGitRepository.checkoutNewBranch("introduce-feature")
testGitRepository.commit(message = "Test empty commit", allowEmpty = true)
val featureBranchHash = getMostRecentCommitOnCurrentBranch()

val gitClient = GitClient(tempFolder.root, "develop")
val currentCommit = gitClient.currentCommit

assertThat(currentCommit).isNotEqualTo(developHash)
assertThat(currentCommit).isEqualTo(featureBranchHash)
}

@Test
fun testCurrentBranch_forNonRepository_throwsException() {
val gitClient = GitClient(tempFolder.root, "develop")
Expand Down
14 changes: 7 additions & 7 deletions version.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ MAJOR_VERSION = 0
MINOR_VERSION = 10

# TODO(#4419): Remove the Kenya-specific alpha version code.
OPPIA_DEV_KITKAT_VERSION_CODE = 48
OPPIA_DEV_VERSION_CODE = 49
OPPIA_ALPHA_KITKAT_VERSION_CODE = 50
OPPIA_ALPHA_VERSION_CODE = 51
OPPIA_ALPHA_KENYA_VERSION_CODE = 52
OPPIA_BETA_VERSION_CODE = 53
OPPIA_GA_VERSION_CODE = 54
OPPIA_DEV_KITKAT_VERSION_CODE = 68
OPPIA_DEV_VERSION_CODE = 67
OPPIA_ALPHA_KITKAT_VERSION_CODE = 66
OPPIA_ALPHA_VERSION_CODE = 65
OPPIA_ALPHA_KENYA_VERSION_CODE = 64
OPPIA_BETA_VERSION_CODE = 63
OPPIA_GA_VERSION_CODE = 62