From 71365e6a158ab3c9dd50e3a24f0ff3558de5852f Mon Sep 17 00:00:00 2001 From: Nihal Mirpuri Date: Fri, 12 Apr 2024 23:15:08 +0100 Subject: [PATCH 1/4] Cache (#130) --- build.sbt | 46 +++++++++++++++------------- src/main/scala/http/HttpClient.scala | 24 ++++++++++++++- 2 files changed, 47 insertions(+), 23 deletions(-) diff --git a/build.sbt b/build.sbt index 6c27e9d..f7d005a 100644 --- a/build.sbt +++ b/build.sbt @@ -15,6 +15,7 @@ val circeVersion = "0.14.6" val fs2Version = "3.7.0" val http4sVersion = "0.23.23" val logbackVersion = "1.4.11" +val scaffeineVersion = "5.2.1" val scalamockVersion = "5.2.0" val scalatestVersion = "3.2.17" val shapelessVersion = "2.3.10" @@ -23,28 +24,29 @@ val snakeYamlVersion = "2.2" val vaultVersion = "3.5.0" libraryDependencies ++= Seq( - "org.scala-lang" % "scala-library" % scalaVersion.value % "provided", - "ch.qos.logback" % "logback-classic" % logbackVersion % Runtime, - "org.slf4j" % "slf4j-api" % slf4jVersion, - "org.http4s" %% "http4s-ember-client" % http4sVersion, - "org.http4s" %% "http4s-circe" % http4sVersion, - "org.http4s" %% "http4s-client" % http4sVersion, - "org.http4s" %% "http4s-core" % http4sVersion, - "co.fs2" %% "fs2-core" % fs2Version, - "co.fs2" %% "fs2-io" % fs2Version, - "com.chuusai" %% "shapeless" % shapelessVersion, - "io.circe" %% "circe-core" % circeVersion, - "org.typelevel" %% "case-insensitive" % caseInsensitiveVersion, - "org.typelevel" %% "cats-core" % catsCoreVersion, - "org.typelevel" %% "cats-effect" % catsEffectVersion, - "org.typelevel" %% "cats-effect-kernel" % catsEffectKernelVersion, - "org.typelevel" %% "vault" % vaultVersion, - "io.circe" %% "circe-generic" % circeVersion, - "io.circe" %% "circe-generic-extras" % circeGenericExtrasVersion, - "org.yaml" % "snakeyaml" % snakeYamlVersion, - "io.circe" %% "circe-parser" % circeVersion % Test, - "org.scalamock" %% "scalamock" % scalamockVersion % Test, - "org.scalatest" %% "scalatest" % scalatestVersion % Test + "org.scala-lang" % "scala-library" % scalaVersion.value % "provided", + "ch.qos.logback" % "logback-classic" % logbackVersion % Runtime, + "org.slf4j" % "slf4j-api" % slf4jVersion, + "org.http4s" %% "http4s-ember-client" % http4sVersion, + "org.http4s" %% "http4s-circe" % http4sVersion, + "org.http4s" %% "http4s-client" % http4sVersion, + "org.http4s" %% "http4s-core" % http4sVersion, + "co.fs2" %% "fs2-core" % fs2Version, + "co.fs2" %% "fs2-io" % fs2Version, + "com.chuusai" %% "shapeless" % shapelessVersion, + "io.circe" %% "circe-core" % circeVersion, + "org.typelevel" %% "case-insensitive" % caseInsensitiveVersion, + "org.typelevel" %% "cats-core" % catsCoreVersion, + "org.typelevel" %% "cats-effect" % catsEffectVersion, + "org.typelevel" %% "cats-effect-kernel" % catsEffectKernelVersion, + "org.typelevel" %% "vault" % vaultVersion, + "io.circe" %% "circe-generic" % circeVersion, + "io.circe" %% "circe-generic-extras" % circeGenericExtrasVersion, + "org.yaml" % "snakeyaml" % snakeYamlVersion, + "com.github.blemale" %% "scaffeine" % scaffeineVersion % "compile", + "io.circe" %% "circe-parser" % circeVersion % Test, + "org.scalamock" %% "scalamock" % scalamockVersion % Test, + "org.scalatest" %% "scalatest" % scalatestVersion % Test ) enablePlugins(JavaAppPackaging) diff --git a/src/main/scala/http/HttpClient.scala b/src/main/scala/http/HttpClient.scala index 9e425d9..21c0e7a 100644 --- a/src/main/scala/http/HttpClient.scala +++ b/src/main/scala/http/HttpClient.scala @@ -1,25 +1,47 @@ package http import cats.effect.IO +import cats.effect.unsafe.implicits.global import io.circe.Json import org.http4s.circe._ import org.http4s.client.middleware.FollowRedirect import org.http4s.ember.client.EmberClientBuilder import org.http4s.{Header, Method, Request, Uri} import org.typelevel.ci.CIString +import com.github.blemale.scaffeine.{AsyncLoadingCache, Scaffeine} + +import scala.concurrent.duration._ class HttpClient { - val client = EmberClientBuilder + private val client = EmberClientBuilder .default[IO] .build .map(FollowRedirect(5)) + private val cacheTtl = 5.seconds + + private val cache: AsyncLoadingCache[(Method, Uri, Option[String], Option[Json]), Either[Throwable, Json]] = + Scaffeine() + .recordStats() + .expireAfterWrite(cacheTtl) + .maximumSize(1000) + .buildAsyncFuture { case (method, url, apiKey, payload) => + makeHttpRequest(method, url, apiKey, payload).unsafeToFuture() + } + def httpRequest( method: Method, url: Uri, apiKey: Option[String] = None, payload: Option[Json] = None + ): IO[Either[Throwable, Json]] = IO.fromFuture(IO(cache.get(method, url, apiKey, payload))) + + private def makeHttpRequest( + method: Method, + url: Uri, + apiKey: Option[String] = None, + payload: Option[Json] = None ): IO[Either[Throwable, Json]] = { val host = s"${url.host.getOrElse(Uri.Host.unsafeFromString("127.0.0.1")).value}" From a7e1dcc42b57bf9f7506a35afe28ad866a6df746 Mon Sep 17 00:00:00 2001 From: Ankit Choudhary Date: Tue, 23 Apr 2024 16:51:37 +0530 Subject: [PATCH 2/4] feat: Add merge preview bot (#139) --- .github/workflows/merge-preview.yml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 .github/workflows/merge-preview.yml diff --git a/.github/workflows/merge-preview.yml b/.github/workflows/merge-preview.yml new file mode 100644 index 0000000..10b4322 --- /dev/null +++ b/.github/workflows/merge-preview.yml @@ -0,0 +1,15 @@ +# .github/workflows/merge-preview.yml +name: "Merge preview" + +on: + issue_comment: + types: [created] + +jobs: + preview: + runs-on: ubuntu-18.04 + steps: + - uses: actions/checkout@v2 + - uses: nwtgck/actions-merge-preview@develop + with: + github-token: ${{ secrets.GITHUB_TOKEN }} From e315d0401a75fe40e4b99c758f0b757f9e7afa05 Mon Sep 17 00:00:00 2001 From: Ankit Choudhary Date: Wed, 24 Apr 2024 23:47:11 +0530 Subject: [PATCH 3/4] Fix: Use PAT so that next workflows can be triggered. (#144) feat: use Personal Access Token so that subesequent workflows can be triggered --- .github/workflows/merge-preview.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/merge-preview.yml b/.github/workflows/merge-preview.yml index 10b4322..2cf31cb 100644 --- a/.github/workflows/merge-preview.yml +++ b/.github/workflows/merge-preview.yml @@ -7,9 +7,12 @@ on: jobs: preview: - runs-on: ubuntu-18.04 + runs-on: ubuntu-latest steps: + # https://github.com/orgs/community/discussions/25702 - uses: actions/checkout@v2 + with: + token: ${{ secrets.PAT_TOKEN }} - uses: nwtgck/actions-merge-preview@develop with: - github-token: ${{ secrets.GITHUB_TOKEN }} + github-token: ${{ secrets.PAT_TOKEN }} From 0bfd5d129b29459dc35c1984d62d93230d70b40e Mon Sep 17 00:00:00 2001 From: Ankit Choudhary Date: Tue, 7 May 2024 22:16:17 +0530 Subject: [PATCH 4/4] Add support for ARM64 (#145) --- .github/workflows/docker-alpha.yml | 52 ++++++++++++++++++++-------- .github/workflows/docker-beta.yml | 47 ++++++++++++++++++------- .github/workflows/release-docker.yml | 51 ++++++++++++++++++++------- 3 files changed, 109 insertions(+), 41 deletions(-) diff --git a/.github/workflows/docker-alpha.yml b/.github/workflows/docker-alpha.yml index d7ac732..0f28bf2 100644 --- a/.github/workflows/docker-alpha.yml +++ b/.github/workflows/docker-alpha.yml @@ -1,29 +1,51 @@ name: Update Docker alpha tag on: - pull_request: - branches: [ "main" ] + push: + branches: ["actions-merge-preview/**"] -jobs: +env: + APP_NAME: watchlistarr + DOCKER_FILE: docker/Dockerfile + DOCKER_PLATFORMS: "linux/arm64/v8,linux/amd64" - build: +jobs: + build: runs-on: ubuntu-latest - - steps: - uses: actions/checkout@v3 - - name: Set up environment - run: echo "DOCKER_TAG=$(date +%s)" >> $GITHUB_ENV + - name: Login to DockerHub uses: docker/login-action@v1 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - - name: Build the Docker image - run: docker build . --file docker/Dockerfile --tag nylonee/watchlistarr:${DOCKER_TAG} --label=VERSION=${DOCKER_TAG} - - name: Push Docker image - run: | - docker tag nylonee/watchlistarr:${DOCKER_TAG} nylonee/watchlistarr:alpha - docker push nylonee/watchlistarr:${DOCKER_TAG} - docker push nylonee/watchlistarr:alpha + + - name: Docker Metadata action + id: meta + uses: docker/metadata-action@v4.3.0 + with: + images: | + ${{ secrets.DOCKERHUB_USERNAME }}/${{ env.APP_NAME }} + tags: | + type=sha + alpha + + - name: Docker Setup QEMU + uses: docker/setup-qemu-action@v2.1.0 + + - name: Docker Setup Buildx + uses: docker/setup-buildx-action@v2.5.0 + with: + buildkitd-flags: --debug + + - name: Build and push Docker images + uses: docker/build-push-action@v4.0.0 + with: + context: . + file: ${{ env.DOCKER_FILE }} + platforms: ${{ env.DOCKER_PLATFORMS }} + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/.github/workflows/docker-beta.yml b/.github/workflows/docker-beta.yml index b63f19a..5836d61 100644 --- a/.github/workflows/docker-beta.yml +++ b/.github/workflows/docker-beta.yml @@ -4,26 +4,47 @@ on: push: branches: [ "main" ] -jobs: +env: + APP_NAME: watchlistarr + DOCKER_FILE: docker/Dockerfile + DOCKER_PLATFORMS: "linux/arm64/v8,linux/amd64" +jobs: build: - runs-on: ubuntu-latest - - steps: - uses: actions/checkout@v3 - - name: Set up environment - run: echo "DOCKER_TAG=$(date +%s)" >> $GITHUB_ENV + - name: Login to DockerHub uses: docker/login-action@v1 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - - name: Build the Docker image - run: docker build . --file docker/Dockerfile --tag nylonee/watchlistarr:${DOCKER_TAG} --label=VERSION=${DOCKER_TAG} - - name: Push Docker image - run: | - docker tag nylonee/watchlistarr:${DOCKER_TAG} nylonee/watchlistarr:beta - docker push nylonee/watchlistarr:${DOCKER_TAG} - docker push nylonee/watchlistarr:beta + + - name: Docker Metadata action + id: meta + uses: docker/metadata-action@v4.3.0 + with: + images: | + ${{ secrets.DOCKERHUB_USERNAME }}/${{ env.APP_NAME }} + tags: | + type=sha + beta + + - name: Docker Setup QEMU + uses: docker/setup-qemu-action@v2.1.0 + + - name: Docker Setup Buildx + uses: docker/setup-buildx-action@v2.5.0 + with: + buildkitd-flags: --debug + + - name: Build and push Docker images + uses: docker/build-push-action@v4.0.0 + with: + context: . + file: ${{ env.DOCKER_FILE }} + platforms: ${{ env.DOCKER_PLATFORMS }} + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/.github/workflows/release-docker.yml b/.github/workflows/release-docker.yml index 8d30086..ca26bc0 100644 --- a/.github/workflows/release-docker.yml +++ b/.github/workflows/release-docker.yml @@ -4,25 +4,50 @@ on: release: types: [published] -jobs: +env: + APP_NAME: watchlistarr + DOCKER_FILE: docker/Dockerfile + DOCKER_PLATFORMS: "linux/arm64/v8,linux/amd64" - build: +jobs: + build: runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: Set up environment - run: echo "DOCKER_TAG=${{ github.event.release.tag_name }}" >> $GITHUB_ENV + - uses: actions/checkout@v3 + + - name: Login to DockerHub uses: docker/login-action@v1 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - - name: Build the Docker image - run: docker build . --file docker/Dockerfile --tag nylonee/watchlistarr:${DOCKER_TAG} --label=VERSION=${DOCKER_TAG} - - name: Push Docker image - run: | - docker tag nylonee/watchlistarr:${DOCKER_TAG} nylonee/watchlistarr:latest - docker push nylonee/watchlistarr:${DOCKER_TAG} - docker push nylonee/watchlistarr:latest + + - name: Docker Metadata action + id: meta + uses: docker/metadata-action@v4.3.0 + with: + images: | + ${{ secrets.DOCKERHUB_USERNAME }}/${{ env.APP_NAME }} + tags: | + type=sha + type=semver,pattern={{version}} + latest + + - name: Docker Setup QEMU + uses: docker/setup-qemu-action@v2.1.0 + + - name: Docker Setup Buildx + uses: docker/setup-buildx-action@v2.5.0 + with: + buildkitd-flags: --debug + + - name: Build and push Docker images + uses: docker/build-push-action@v4.0.0 + with: + context: . + file: ${{ env.DOCKER_FILE }} + platforms: ${{ env.DOCKER_PLATFORMS }} + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }}