Skip to content

Commit

Permalink
Swagger CodeGen for WaaS [BA-5746] (broadinstitute#5038)
Browse files Browse the repository at this point in the history
* Swagger CodeGen for WaaS

* addressed PR feedback

* more PR feedback

* instructions for generating and publishing swagger client libraries
  • Loading branch information
kcibul authored Jun 27, 2019
1 parent 01481cd commit 09ada09
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 0 deletions.
28 changes: 28 additions & 0 deletions codegen_java/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Publishing._
import Version._

lazy val root = (project in file(".")).
settings(
Seq(organization := "org.broadinstitute.cromwell",
name := "cromwell-client",
version := createVersion("0.1"),
scalaVersion := "2.12.8",
scalacOptions ++= Seq("-feature"),
javacOptions in compile ++= Seq("-Xlint:deprecation"),
publishArtifact in (Compile, packageDoc) := false,
resolvers += Resolver.mavenLocal,
updateOptions := updateOptions.value.withGigahorse(false),
libraryDependencies ++= Seq(
"io.swagger" % "swagger-annotations" % "1.5.21",
"com.squareup.okhttp3" % "okhttp" % "3.12.1",
"com.squareup.okhttp3" % "logging-interceptor" % "3.12.1",
"com.google.code.gson" % "gson" % "2.8.5",
"org.apache.commons" % "commons-lang3" % "3.8.1",
"org.apache.oltu.oauth2" % "org.apache.oltu.oauth2.client" % "1.0.1",
"org.threeten" % "threetenbp" % "1.3.5" % "compile",
"io.gsonfire" % "gson-fire" % "1.8.0" % "compile",
"junit" % "junit" % "4.12" % "test",
"com.novocode" % "junit-interface" % "0.10" % "test"
)) ++ publishSettings:_*
)

4 changes: 4 additions & 0 deletions codegen_java/project/Artifactory.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
object Artifactory {
val artifactoryHost = "broadinstitute.jfrog.io"
val artifactory = s"https://$artifactoryHost/broadinstitute/"
}
37 changes: 37 additions & 0 deletions codegen_java/project/Publishing.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import sbt.Keys._
import sbt._
import Artifactory._

object Publishing {
private val buildTimestamp = System.currentTimeMillis() / 1000

private def artifactoryResolver(isSnapshot: Boolean): Resolver = {
val repoType = if (isSnapshot) "snapshot" else "release"
val repoUrl =
s"${artifactory}libs-$repoType-local;build.timestamp=$buildTimestamp"
val repoName = "artifactory-publish"
repoName at repoUrl
}

private val artifactoryCredentials: Credentials = {
val username = sys.env.getOrElse("ARTIFACTORY_USERNAME", "")
val password = sys.env.getOrElse("ARTIFACTORY_PASSWORD", "")
Credentials("Artifactory Realm", artifactoryHost, username, password)
}

val publishSettings: Seq[Setting[_]] =
//we only publish to libs-release-local because of a bug in sbt that makes snapshots take
//priority over the local package cache. see here: https://github.com/sbt/sbt/issues/2687#issuecomment-236586241
Seq(
publishTo := Option(artifactoryResolver(false)),
publishArtifact in Compile := true,
publishArtifact in Test := true,
credentials += artifactoryCredentials
)

val noPublishSettings: Seq[Setting[_]] =
Seq(
publish := {},
publishLocal := {}
)
}
20 changes: 20 additions & 0 deletions codegen_java/project/Version.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import scala.sys.process._

object Version {

def createVersion(baseVersion: String) = {
def getLastCommitFromGit = { s"""git rev-parse --short HEAD""" !! }

// either specify git hash as an env var or derive it
// if building from the broadinstitute/scala-baseimage docker image use env var
// (scala-baseimage doesn't have git in it)
val lastCommit = sys.env.getOrElse("GIT_HASH", getLastCommitFromGit ).trim()
val version = baseVersion + "-" + lastCommit

// The project isSnapshot string passed in via command line settings, if desired.
val isSnapshot = sys.props.getOrElse("project.isSnapshot", "true").toBoolean

// For now, obfuscate SNAPSHOTs from sbt's developers: https://github.com/sbt/sbt/issues/2687#issuecomment-236586241
if (isSnapshot) s"$version-SNAP" else version
}
}
1 change: 1 addition & 0 deletions codegen_java/project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=1.2.6
23 changes: 23 additions & 0 deletions processes/release_processes/README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,26 @@ This performance testing process happens as part of the release into FireCloud.

![caas-prod](caas-prod.dot.png)

## How to Generate and Publish Swagger Client Library

The first step is to generate the client library. From the root of the repo run

```
./scripts/gen_java_client.sh
```

This generates the client library and runs the generated tests as well. A successful run should end with something similar to

```
[debug] Test run finished: 0 failed, 0 ignored, 7 total, 0.007s
[info] Passed: Total 103, Failed 0, Errors 0, Passed 100, Skipped 3
[success] Total time: 4 s, completed Jun 26, 2019 3:01:19 PM
```

To publish to artifactory, first obtain the artifactory username and credentials. Then run

```
export ARTIFACTORY_USERNAME=<the-username>
export ARTIFACTORY_PASSWORD=<the-password>
./scripts/publish-client.sh
```
46 changes: 46 additions & 0 deletions scripts/gen_java_client.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
set -e

BASE_PACKAGE="cromwell.client"

ORIGINAL_API_YAML=engine/src/main/resources/swagger/cromwell.yaml
API_YAML=codegen_java/cromwell.nofile.yaml

# Cromwell doesn't specify the OAuth configuration in it's swagger, and
# without it the client doesn't support authentication.
cat << EOF > $API_YAML
security:
- googleoauth:
- openid
- email
- profile
securityDefinitions:
googleoauth:
type: oauth2
authorizationUrl: 'https://accounts.google.com/o/oauth2/auth'
flow: implicit
scopes:
openid: open id authorization
email: email authorization
profile: profile authorization
EOF

# Swagger autogenerates clients that match the input types, and for File
# that is less than useful because clients need to supply their inputs as
# File, which means actually making a file. Replacing with 'string' has nearly
# the same HTTP semantics, except for suppling the name of the client-side file
# itself, but is much more usable to a client.
cat $ORIGINAL_API_YAML | sed s/type:\ file/type:\ string/g >> $API_YAML

docker run --rm -v ${PWD}:/local openapitools/openapi-generator-cli generate \
-i /local/$API_YAML \
-g java \
-o /local/codegen_java \
--skip-validate-spec \
--api-package ${BASE_PACKAGE}.api \
--model-package ${BASE_PACKAGE}.model

cd codegen_java

sbt test
13 changes: 13 additions & 0 deletions scripts/publish-client.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env bash

set -e

# sbt publish publishes libs to Artifactory for the scala version sbt is running as.
# sbt +publish publishes libs to Artifactory for all scala versions listed in crossScalaVersions.
# We only do sbt publish here because Travis runs against 2.11 and 2.12 in separate jobs, so each one publishes its version to Artifactory.
cd codegen_java
if [[ "$TRAVIS_PULL_REQUEST" == "false" && "$TRAVIS_BRANCH" == "develop" ]]; then
sbt -Dproject.isSnapshot=false "+ publish"
else
sbt -Dproject.isSnapshot=true "+ publish"
fi

0 comments on commit 09ada09

Please sign in to comment.