Skip to content

Tag products, bom and release versions during release. #3038

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

Merged
merged 9 commits into from
Nov 8, 2021
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 @@ -21,10 +21,13 @@
import com.google.common.collect.Sets;
import com.google.firebase.gradle.bomgenerator.model.Dependency;
import com.google.firebase.gradle.bomgenerator.model.VersionBump;
import com.google.firebase.gradle.bomgenerator.tagging.GitClient;
import com.google.firebase.gradle.bomgenerator.tagging.ShellExecutor;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand All @@ -37,6 +40,7 @@
import javax.xml.parsers.ParserConfigurationException;
import org.eclipse.aether.resolution.VersionRangeResolutionException;
import org.gradle.api.DefaultTask;
import org.gradle.api.logging.Logger;
import org.gradle.api.tasks.TaskAction;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
Expand Down Expand Up @@ -177,10 +181,14 @@ public class BomGeneratorTask extends DefaultTask {
* everything in gMaven. This is meant to be a post-release task so that the BoM contains the most
* recent versions of all artifacts.
*
* <p>This task also tags the release candidate commit with the BoM version, the new version of
* releasing products, and the M version of the current release.
*
* <p>Version overrides may be given to this task in a map like so: versionOverrides =
* ["com.google.firebase:firebase-firestore": "17.0.1"]
*/
@TaskAction
// TODO(yifany): needs a more accurate name
public void generateBom() throws Exception {
// Repo Access Setup
RepositoryClient depPopulator = new RepositoryClient();
Expand Down Expand Up @@ -241,6 +249,8 @@ public void generateBom() throws Exception {
xmlWriter.writeXmlDocument(outputXmlDoc);
documentationWriter.writeDocumentation(outputDocumentation);
recipeWriter.writeVersionUpdate(outputRecipe);

tagVersions(version, bomDependencies);
}

// Finds the version for the BoM artifact.
Expand Down Expand Up @@ -312,4 +322,23 @@ private Map<String, String> getBomMap(String bomVersion) {
throw new RuntimeException("Failed to get contents of BoM version " + bomVersion, e);
}
}

private void tagVersions(String bomVersion, List<Dependency> firebaseDependencies) {
Logger logger = this.getProject().getLogger();
if (!System.getenv().containsKey("FIREBASE_CI")) {
logger.warn("Tagging versions is skipped for non-CI environments.");
return;
}

String mRelease = System.getenv("PULL_BASE_REF");
String rcCommit = System.getenv("PULL_BASE_SHA");
ShellExecutor executor = new ShellExecutor(Paths.get(".").toFile(), logger::lifecycle);
GitClient git = new GitClient(mRelease, rcCommit, executor, logger::lifecycle);
git.tagReleaseVersion();
git.tagBomVersion(bomVersion);
firebaseDependencies.stream()
.filter(d -> d.versionBump() != VersionBump.NONE)
.forEach(d -> git.tagProductVersion(d.artifactId(), d.version()));
git.pushCreatedTags();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.gradle.bomgenerator.tagging;

import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;
import java.util.function.Consumer;

public class GitClient {

private final String mRelease;
private final String rcCommit;
private final ShellExecutor executor;
private final Consumer<String> logger;

private final List<String> newTags;
private final Consumer<List<String>> handler; // handles shell outputs of git commands

public GitClient(
String mRelease, String rcCommit, ShellExecutor executor, Consumer<String> logger) {
this.mRelease = mRelease;
this.rcCommit = rcCommit;
this.executor = executor;
this.logger = logger;

this.newTags = new ArrayList<>();
this.handler = this.deriveGitCommandOutputHandlerFromLogger(logger);
this.configureSshCommand();
}

public void tagReleaseVersion() {
this.tag(mRelease);
}

public void tagBomVersion(String version) {
String tag = "bom@" + version;
this.tag(tag);
}

public void tagProductVersion(String product, String version) {
String tag = product + "@" + version;
this.tag(tag);
}

public void pushCreatedTags() {
if (!this.onProw() || newTags.isEmpty()) {
return;
}

StringJoiner joiner = new StringJoiner(" ");
newTags.forEach(joiner::add);
String tags = joiner.toString();

logger.accept("Tags to be pushed: " + tags);

logger.accept("Pushing tags to FirebasePrivate/firebase-android-sdk ...");
String command = String.format("git push origin %s", tags);
executor.execute(command, handler);
}

private boolean onProw() {
return System.getenv().containsKey("FIREBASE_CI");
}

private Consumer<List<String>> deriveGitCommandOutputHandlerFromLogger(Consumer<String> logger) {
return outputs -> outputs.stream().map(output -> "[git] " + output).forEach(logger);
}

private void tag(String tag) {
logger.accept("Creating tag: " + tag);
newTags.add(tag);
String command = String.format("git tag %s %s", tag, rcCommit);
executor.execute(command, handler);
}

private void configureSshCommand() {
if (!this.onProw()) {
return;
}
// TODO(yifany):
// - Change to use environment variables according to the Git doc:
// https://git-scm.com/docs/git-config#Documentation/git-config.txt-coresshCommand
// - Call of `git config core.sshCommand` in prow/config.yaml will become redundant as well
String ssh =
"ssh -i /etc/github-ssh-key/github-ssh"
+ " -o IdentitiesOnly=yes"
+ " -o UserKnownHostsFile=/dev/null"
+ " -o StrictHostKeyChecking=no";
String command = String.format("git config core.sshCommand %s", ssh);
executor.execute(command, handler);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.gradle.bomgenerator.tagging;

import com.google.common.io.CharStreams;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.function.Consumer;
import org.gradle.api.GradleException;

public class ShellExecutor {
private final Runtime runtime;
private final File cwd;
private final Consumer<String> logger;

public ShellExecutor(File cwd, Consumer<String> logger) {
this.runtime = Runtime.getRuntime();
this.cwd = cwd;
this.logger = logger;
}

public void execute(String command, Consumer<List<String>> consumer) {
try {
logger.accept("[shell] Executing: \"" + command + "\" at: " + cwd.getAbsolutePath());
Process p = runtime.exec(command, null, cwd);
int code = p.waitFor();
logger.accept("[shell] Command: \"" + command + "\" returned with code: " + code);
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
consumer.accept(CharStreams.readLines(reader));
} catch (IOException e) {
throw new GradleException("Failed when executing command: " + command, e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.gradle.bomgenerator.tagging

import java.util.concurrent.atomic.AtomicReference
import java.util.function.Consumer
import org.junit.Assert
import org.junit.Assume
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@RunWith(JUnit4::class)
class GitClientTest {
@Rule @JvmField val testGitDirectory = TemporaryFolder()
private val branch = AtomicReference<String>()
private val commit = AtomicReference<String>()

@Before
fun setup() {
testGitDirectory.newFile("hello.txt").writeText("hello git!")
val executor = ShellExecutor(testGitDirectory.root, System.out::println)
val handler: (List<String>) -> Unit = { it.forEach(System.out::println) }

executor.execute("git init", handler)
executor.execute("git config user.email '[email protected]'", handler)
executor.execute("git config user.name 'GitClientTest'", handler)
executor.execute("git add .", handler)
executor.execute("git commit -m 'init_commit'", handler)
executor.execute("git status", handler)

executor.execute("git rev-parse --abbrev-ref HEAD") { branch.set(it[0]) }
executor.execute("git rev-parse HEAD") { commit.set(it[0]) }
}

@Test
fun `tag M release version succeeds on local file system`() {
val executor = ShellExecutor(testGitDirectory.root, System.out::println)
val git = GitClient(branch.get(), commit.get(), executor, System.out::println)
git.tagReleaseVersion()
executor.execute("git tag --points-at HEAD") {
Assert.assertTrue(it.stream().anyMatch { x -> x.contains(branch.get()) })
}
}

@Test
fun `tag bom version succeeds on local file system`() {
val executor = ShellExecutor(testGitDirectory.root, System.out::println)
val git = GitClient(branch.get(), commit.get(), executor, System.out::println)
git.tagBomVersion("1.2.3")
executor.execute("git tag --points-at HEAD") {
Assert.assertTrue(it.stream().anyMatch { x -> x.contains("[email protected]") })
}
}

@Test
fun `tag product version succeeds on local file system`() {
val executor = ShellExecutor(testGitDirectory.root, System.out::println)
val git = GitClient(branch.get(), commit.get(), executor, System.out::println)
git.tagProductVersion("firebase-database", "1.2.3")
executor.execute("git tag --points-at HEAD") {
Assert.assertTrue(it.stream().anyMatch { x -> x.contains("[email protected]") })
}
}

@Test
fun `tags are pushed to the remote repository`() {
Assume.assumeTrue(System.getenv().containsKey("FIREBASE_CI"))

val mockExecutor = object : ShellExecutor(testGitDirectory.root, System.out::println) {
override fun execute(command: String, consumer: Consumer<List<String>>) {
consumer.accept(listOf("Received command: $command"))
}
}

val outputs = mutableListOf<String>()
val git = GitClient(branch.get(), commit.get(), mockExecutor) { outputs.add(it) }
git.tagBomVersion("1.2.3")
git.tagProductVersion("firebase-functions", "1.2.3")
git.pushCreatedTags()

Assert.assertTrue(outputs.stream().anyMatch { it.contains("git push origin") })
}
}