From 79fb0c8eedd8f4b41f352c40b774508002a8de58 Mon Sep 17 00:00:00 2001 From: Armando Belardo <11140328+armandobelardo@users.noreply.github.com> Date: Wed, 20 Mar 2024 20:19:39 -0400 Subject: [PATCH] fix: additional fixes to zod schemas to pass through signing config and publish via a script (#3234) --- fern/apis/public-api/generators.yml | 21 ++++++++++++++++ .../com/fern/java/AbstractGeneratorCli.java | 17 +++++++++---- .../generators/GithubWorkflowGenerator.java | 8 +++++- .../java/output/GeneratedBuildGradle.java | 6 ++--- .../output/GeneratedGradleProperties.java | 18 +++++++++++++ .../java/output/GeneratedPublishScript.java | 25 +++++++++++++++++++ generators/java/sdk/VERSION | 2 +- .../convertGeneratorsConfiguration.ts | 18 ++++++++++++- .../src/run-generator/getGeneratorConfig.ts | 9 ++++--- .../signed_publish/.github/workflows/ci.yml | 7 +++--- .../signed_publish/.publish/prepare.sh | 8 ++++++ .../exhaustive/signed_publish/build.gradle | 6 +---- .../signed_publish/gradle.properties | 0 13 files changed, 122 insertions(+), 23 deletions(-) create mode 100644 generators/java/generator-utils/src/main/java/com/fern/java/output/GeneratedGradleProperties.java create mode 100644 generators/java/generator-utils/src/main/java/com/fern/java/output/GeneratedPublishScript.java create mode 100644 seed/java-sdk/exhaustive/signed_publish/.publish/prepare.sh create mode 100644 seed/java-sdk/exhaustive/signed_publish/gradle.properties diff --git a/fern/apis/public-api/generators.yml b/fern/apis/public-api/generators.yml index f23973d9673..11f198f7280 100644 --- a/fern/apis/public-api/generators.yml +++ b/fern/apis/public-api/generators.yml @@ -29,3 +29,24 @@ groups: license: MIT config: client_class_name: Fern + java-sdk: + audiences: + - external + generators: + - name: fernapi/fern-java-sdk + version: 0.8.6 + output: + location: maven + coordinate: com.fern.api:fern-java-sdk + username: fernapi + password: ${MAVEN_PASSWORD} + # TODO: uncomment after this config is released + # signature: + # keyId: ${MAVEN_CENTRAL_SECRET_KEY_KEY_ID} + # password: ${MAVEN_CENTRAL_SECRET_KEY_PASSWORD} + # secretKey: ${MAVEN_CENTRAL_SECRET_KEY} + github: + repository: fern-api/java-sdk + license: MIT + config: + client_class_name: Fern diff --git a/generators/java/generator-utils/src/main/java/com/fern/java/AbstractGeneratorCli.java b/generators/java/generator-utils/src/main/java/com/fern/java/AbstractGeneratorCli.java index ebb0b5e8801..3e1ba4f4de9 100644 --- a/generators/java/generator-utils/src/main/java/com/fern/java/AbstractGeneratorCli.java +++ b/generators/java/generator-utils/src/main/java/com/fern/java/AbstractGeneratorCli.java @@ -12,6 +12,8 @@ import java.util.Optional; import java.util.stream.Collectors; +import com.fern.java.output.GeneratedGradleProperties; +import com.fern.java.output.GeneratedPublishScript; import org.immutables.value.Value; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -259,6 +261,11 @@ public final void runInPublishMode( publishEnvVars.put(GeneratedBuildGradle.MAVEN_SIGNING_KEY_ID, signature.getKeyId()); publishEnvVars.put(GeneratedBuildGradle.MAVEN_SIGNING_PASSWORD, signature.getPassword()); publishEnvVars.put(GeneratedBuildGradle.MAVEN_SIGNING_KEY, signature.getSecretKey()); + runCommandBlocking( + new String[] { "./.publish/prepare.sh" }, + Paths.get(generatorConfig.getOutput().getPath()), + publishEnvVars); + } runCommandBlocking( new String[] { "gradle", "publish" }, @@ -318,12 +325,12 @@ private void addRootProjectFiles(Optional maybeMavenCoordinate, .pluginId("signing") .build()); buildGradle.addCustomBlocks("signing {\n" - + " sign publishing.publications\n" - + " def signingKeyId = findProperty(\"signingKeyId\")\n" - + " def signingKey = findProperty(\"signingKey\")\n" - + " def signingPassword = findProperty(\"signingPassword\")\n" - + " useInMemoryPgpKeys(signingKeyId, signingKey, signingPassword)\n" + + " sign(publishing.publications)\n" + "}"); + // Generate an empty gradle.properties file + addGeneratedFile(GeneratedGradleProperties.getGeneratedFile()); + // Generate script to populate that file + addGeneratedFile(GeneratedPublishScript.getGeneratedFile()); } if (addTestBlock) { buildGradle.addCustomBlocks("test {\n" diff --git a/generators/java/generator-utils/src/main/java/com/fern/java/generators/GithubWorkflowGenerator.java b/generators/java/generator-utils/src/main/java/com/fern/java/generators/GithubWorkflowGenerator.java index 0960928c37f..e07338f08de 100644 --- a/generators/java/generator-utils/src/main/java/com/fern/java/generators/GithubWorkflowGenerator.java +++ b/generators/java/generator-utils/src/main/java/com/fern/java/generators/GithubWorkflowGenerator.java @@ -82,7 +82,13 @@ public static String getPublishWorkflow(String registryUrl, + " architecture: x64\n" + "\n" + " - name: Publish to maven\n" - + " run: |\n" + + " run: |\n"; + + if (maybeSignatureGithubInfo.isPresent()) { + content = content + + " ./.publish/prepare.sh\n"; + } + content = content + " ./gradlew publish\n" + " env:\n" + " MAVEN_USERNAME: ${{ secrets.MAVEN_USERNAME }}\n" diff --git a/generators/java/generator-utils/src/main/java/com/fern/java/output/GeneratedBuildGradle.java b/generators/java/generator-utils/src/main/java/com/fern/java/output/GeneratedBuildGradle.java index 729582f1939..b7454ff6b8d 100644 --- a/generators/java/generator-utils/src/main/java/com/fern/java/output/GeneratedBuildGradle.java +++ b/generators/java/generator-utils/src/main/java/com/fern/java/output/GeneratedBuildGradle.java @@ -20,11 +20,11 @@ public abstract class GeneratedBuildGradle extends GeneratedFile { public static final String JAVA_LIBRARY_PLUGIN_ID = "java-library"; public static final String MAVEN_PUBLISH_PLUGIN_ID = "maven-publish"; - public static final String MAVEN_SIGNING_KEY_ID = "ORG_GRADLE_PROJECT_signingKeyId"; + public static final String MAVEN_SIGNING_KEY_ID = "MAVEN_SIGNATURE_KID"; - public static final String MAVEN_SIGNING_KEY = "ORG_GRADLE_PROJECT_signingKey"; + public static final String MAVEN_SIGNING_KEY = "MAVEN_SIGNATURE_SECRET_KEY"; - public static final String MAVEN_SIGNING_PASSWORD = "ORG_GRADLE_PROJECT_signingPassword"; + public static final String MAVEN_SIGNING_PASSWORD = "MAVEN_SIGNATURE_PASSWORD"; public abstract List plugins(); diff --git a/generators/java/generator-utils/src/main/java/com/fern/java/output/GeneratedGradleProperties.java b/generators/java/generator-utils/src/main/java/com/fern/java/output/GeneratedGradleProperties.java new file mode 100644 index 00000000000..5b10e0bbbb6 --- /dev/null +++ b/generators/java/generator-utils/src/main/java/com/fern/java/output/GeneratedGradleProperties.java @@ -0,0 +1,18 @@ +package com.fern.java.output; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +public class GeneratedGradleProperties { + private static final RawGeneratedFile GENERATED_FILE = RawGeneratedFile.builder() + .filename("gradle.properties") + .contents("") + .build(); + + private GeneratedGradleProperties() {} + + public static RawGeneratedFile getGeneratedFile() { + return GENERATED_FILE; + } +} diff --git a/generators/java/generator-utils/src/main/java/com/fern/java/output/GeneratedPublishScript.java b/generators/java/generator-utils/src/main/java/com/fern/java/output/GeneratedPublishScript.java new file mode 100644 index 00000000000..597dc7cdb38 --- /dev/null +++ b/generators/java/generator-utils/src/main/java/com/fern/java/output/GeneratedPublishScript.java @@ -0,0 +1,25 @@ +package com.fern.java.output; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +public class GeneratedPublishScript { + private static final RawGeneratedFile GENERATED_FILE = RawGeneratedFile.builder() + .filename("prepare.sh") + .contents("# Write key ring file\n" + + "echo $MAVEN_SIGNATURE_SECRET_KEY > armored_key.asc\n" + + "gpg -o publish_key.gpg --dearmor armored_key.asc\n\n" + + "# Generate gradle.properties file\n" + + "echo \"signing.keyId=$MAVEN_SIGNATURE_KID\" > gradle.properties\n" + + "echo \"signing.secretKeyRingFile=publish_key.gpg\" >> gradle.properties\n" + + "echo \"signing.password=$MAVEN_SIGNATURE_PASSWORD\" >> gradle.properties\n") + .directoryPrefix(".publish") + .build(); + + private GeneratedPublishScript() {} + + public static RawGeneratedFile getGeneratedFile() { + return GENERATED_FILE; + } +} diff --git a/generators/java/sdk/VERSION b/generators/java/sdk/VERSION index 120f5321551..c7fcc8bb91d 100644 --- a/generators/java/sdk/VERSION +++ b/generators/java/sdk/VERSION @@ -1 +1 @@ -0.8.6 \ No newline at end of file +0.8.7-rc0 \ No newline at end of file diff --git a/packages/cli/configuration/src/generators-yml/convertGeneratorsConfiguration.ts b/packages/cli/configuration/src/generators-yml/convertGeneratorsConfiguration.ts index 8f1c5491e4d..288bf09172f 100644 --- a/packages/cli/configuration/src/generators-yml/convertGeneratorsConfiguration.ts +++ b/packages/cli/configuration/src/generators-yml/convertGeneratorsConfiguration.ts @@ -232,7 +232,15 @@ async function convertOutputMode({ registryUrl: generator.output.url ?? "https://s01.oss.sonatype.org/content/repositories/releases/", username: generator.output.username ?? "", password: generator.output.password ?? "", - coordinate: generator.output.coordinate + coordinate: generator.output.coordinate, + signature: + generator.output.signature != null + ? { + keyId: generator.output.signature.keyId, + secretKey: generator.output.signature.secretKey, + password: generator.output.signature.password + } + : undefined }) ); case "postman": @@ -316,6 +324,14 @@ function getGithubPublishInfo(output: GeneratorOutputSchema): FernFiddle.GithubP username: output.username, password: output.password } + : undefined, + signature: + output.signature != null + ? { + keyId: output.signature.keyId, + password: output.signature.password, + secretKey: output.signature.secretKey + } : undefined }); case "postman": diff --git a/packages/cli/generation/local-generation/local-workspace-runner/src/run-generator/getGeneratorConfig.ts b/packages/cli/generation/local-generation/local-workspace-runner/src/run-generator/getGeneratorConfig.ts index 570f9573c1b..78df531af66 100644 --- a/packages/cli/generation/local-generation/local-workspace-runner/src/run-generator/getGeneratorConfig.ts +++ b/packages/cli/generation/local-generation/local-workspace-runner/src/run-generator/getGeneratorConfig.ts @@ -53,9 +53,9 @@ function getGithubPublishConfig( signature: value.signature != null ? { - keyIdEnvironmentVariable: EnvironmentVariable(value.signature?.keyId ?? ""), - passwordEnvironmentVariable: EnvironmentVariable(value.signature?.password ?? ""), - secretKeyEnvironmentVariable: EnvironmentVariable(value.signature?.secretKey ?? "") + keyIdEnvironmentVariable: EnvironmentVariable(value.signature.keyId ?? ""), + passwordEnvironmentVariable: EnvironmentVariable(value.signature.password ?? ""), + secretKeyEnvironmentVariable: EnvironmentVariable(value.signature.secretKey ?? "") } : undefined }), @@ -205,7 +205,8 @@ function newDummyPublishOutputConfig( password: (outputMode as MavenOutput)?.password ?? "", registryUrl: (outputMode as MavenOutput)?.registryUrl ?? "", username: (outputMode as MavenOutput)?.username ?? "", - coordinate: (outputMode as MavenOutput)?.coordinate ?? "" + coordinate: (outputMode as MavenOutput)?.coordinate ?? "", + signature: (outputMode as MavenOutput)?.signature }, npm: { registryUrl: (outputMode as NpmOutput)?.registryUrl ?? "", diff --git a/seed/java-sdk/exhaustive/signed_publish/.github/workflows/ci.yml b/seed/java-sdk/exhaustive/signed_publish/.github/workflows/ci.yml index e366f7d86b5..57cb70599ff 100644 --- a/seed/java-sdk/exhaustive/signed_publish/.github/workflows/ci.yml +++ b/seed/java-sdk/exhaustive/signed_publish/.github/workflows/ci.yml @@ -54,11 +54,12 @@ jobs: - name: Publish to maven run: | + ./.publish/prepare.sh ./gradlew publish env: MAVEN_USERNAME: ${{ secrets.MAVEN_USERNAME }} MAVEN_PASSWORD: ${{ secrets.MAVEN_PASSWORD }} MAVEN_PUBLISH_REGISTRY_URL: "definitelyMavenCentral" - ORG_GRADLE_PROJECT_signingKeyId: ${{ secrets.MAVEN_SIGNATURE_KID }} - ORG_GRADLE_PROJECT_signingKey: ${{ secrets.MAVEN_SIGNATURE_SECRET_KEY }} - ORG_GRADLE_PROJECT_signingPassword: ${{ secrets.MAVEN_SIGNATURE_PASSWORD }} + MAVEN_SIGNATURE_KID: ${{ secrets.MAVEN_SIGNATURE_KID }} + MAVEN_SIGNATURE_SECRET_KEY: ${{ secrets.MAVEN_SIGNATURE_SECRET_KEY }} + MAVEN_SIGNATURE_PASSWORD: ${{ secrets.MAVEN_SIGNATURE_PASSWORD }} diff --git a/seed/java-sdk/exhaustive/signed_publish/.publish/prepare.sh b/seed/java-sdk/exhaustive/signed_publish/.publish/prepare.sh new file mode 100644 index 00000000000..50646d8e845 --- /dev/null +++ b/seed/java-sdk/exhaustive/signed_publish/.publish/prepare.sh @@ -0,0 +1,8 @@ +# Write key ring file +echo $MAVEN_SIGNATURE_SECRET_KEY > armored_key.asc +gpg -o publish_key.gpg --dearmor armored_key.asc + +# Generate gradle.properties file +echo "signing.keyId=$MAVEN_SIGNATURE_KID" > gradle.properties +echo "signing.secretKeyRingFile=publish_key.gpg" >> gradle.properties +echo "signing.password=$MAVEN_SIGNATURE_PASSWORD" >> gradle.properties diff --git a/seed/java-sdk/exhaustive/signed_publish/build.gradle b/seed/java-sdk/exhaustive/signed_publish/build.gradle index 9a33b7d601c..9c48dcbd520 100644 --- a/seed/java-sdk/exhaustive/signed_publish/build.gradle +++ b/seed/java-sdk/exhaustive/signed_publish/build.gradle @@ -37,11 +37,7 @@ java { } signing { - sign publishing.publications - def signingKeyId = findProperty("signingKeyId") - def signingKey = findProperty("signingKey") - def signingPassword = findProperty("signingPassword") - useInMemoryPgpKeys(signingKeyId, signingKey, signingPassword) + sign(publishing.publications) } test { useJUnitPlatform() diff --git a/seed/java-sdk/exhaustive/signed_publish/gradle.properties b/seed/java-sdk/exhaustive/signed_publish/gradle.properties new file mode 100644 index 00000000000..e69de29bb2d