From 5b55ef151c0add856cb2e7b90183e0314a79f9d5 Mon Sep 17 00:00:00 2001 From: Zack Siri Date: Tue, 13 Aug 2024 11:48:01 +0700 Subject: [PATCH 1/2] Update test case --- action.yml | 4 ++-- lib/pakman/push.ex | 17 ++++++++++++++--- test/pakman/push_test.exs | 40 +++++++++++++++++++++++++++++++++++++-- 3 files changed, 54 insertions(+), 7 deletions(-) diff --git a/action.yml b/action.yml index c780885..73fb3f6 100644 --- a/action.yml +++ b/action.yml @@ -47,14 +47,14 @@ runs: uses: actions/cache@v4 with: path: ~/.mix - key: ${{ runner.arch }}-alpine-${{ inputs.alpine }}-pakman-8.2.12 + key: ${{ runner.arch }}-alpine-${{ inputs.alpine }}-pakman-develop - name: Install Pakman if: steps.cache-pakman.outputs.cache-hit != 'true' run: | mix local.rebar --force mix local.hex --force - mix escript.install hex pakman 8.2.12 --force + mix escript.install github upmaru/pakman branch develop --force shell: alpine.sh {0} env: MIX_ENV: prod diff --git a/lib/pakman/push.ex b/lib/pakman/push.ex index c85691b..d14ee37 100644 --- a/lib/pakman/push.ex +++ b/lib/pakman/push.ex @@ -46,16 +46,27 @@ defmodule Pakman.Push do {:error, error} -> raise Error, message: "[Pakman.Push] #{inspect(error)}" - {:ok, %{"attributes" => _}} -> - Logger.info("[Pakman.Push] Deployment already exists...") + {:ok, %{"attributes" => attributes}} -> + Logger.info("[Pakman.Push] Deployment already exists copying existing...") - {:ok, :already_exists} + copy(attributes, config) _ -> raise Error, message: "[Pakman.Push] Deployment creation failed..." end end + defp copy(%{"archive_path" => archive_path} = existing_deployment, config) do + with {:ok, token} <- Instellar.authenticate(), + {:ok, deployment_message, response} <- Instellar.create_deployment(token, archive_path, config), + {:ok, configuration_message, _response} <- Instellar.create_configuration(token, response["attributes"]["id"], config) do + print_deployment_message(deployment_message) + print_configuration_message(configuration_message) + + {:ok, :copied} + end + end + def push_files(storage, archive, options) do home = System.get_env("HOME") sha = System.get_env("WORKFLOW_SHA") || System.get_env("GITHUB_SHA") diff --git a/test/pakman/push_test.exs b/test/pakman/push_test.exs index 785bc16..509a59f 100644 --- a/test/pakman/push_test.exs +++ b/test/pakman/push_test.exs @@ -114,10 +114,46 @@ defmodule Pakman.PushTest do Bypass.expect(bypass, "GET", "/publish/deployments/somesha", fn conn -> conn |> Plug.Conn.put_resp_header("content-type", "application/json") - |> Plug.Conn.resp(200, Jason.encode!(%{data: %{attributes: %{id: 1}}})) + |> Plug.Conn.resp( + 200, + Jason.encode!(%{ + data: %{ + attributes: %{ + "id" => 1, + "archive_path" => "archives/some-uuid/packages.zip" + } + } + })) + end) + + Bypass.expect(bypass, "POST", "/publish/deployments", fn conn -> + conn + |> Plug.Conn.put_resp_header("content-type", "application/json") + |> Plug.Conn.resp( + 201, + Jason.encode!(%{ + data: %{ + attributes: %{ + "id" => 1, + "archive_path" => "archives/some-uuid/packages.zip" + } + } + }) + ) end) - assert {:ok, :already_exists} = + Bypass.expect_once( + bypass, + "POST", + "/publish/deployments/1/configurations", + fn conn -> + conn + |> Plug.Conn.put_resp_header("content-type", "application/json") + |> Plug.Conn.resp(201, Jason.encode!(%{data: %{id: 1}})) + end + ) + + assert {:ok, :copied} = Push.perform(config: "test/fixtures/rails.yml") end end From b98499de80f9a14d620f0fbda1f86239c1dfb249 Mon Sep 17 00:00:00 2001 From: Zack Siri Date: Tue, 13 Aug 2024 11:51:10 +0700 Subject: [PATCH 2/2] clean up formatting --- lib/pakman/push.ex | 14 +++++++++++--- test/pakman/push_test.exs | 6 +++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/pakman/push.ex b/lib/pakman/push.ex index d14ee37..170a3bb 100644 --- a/lib/pakman/push.ex +++ b/lib/pakman/push.ex @@ -47,7 +47,9 @@ defmodule Pakman.Push do raise Error, message: "[Pakman.Push] #{inspect(error)}" {:ok, %{"attributes" => attributes}} -> - Logger.info("[Pakman.Push] Deployment already exists copying existing...") + Logger.info( + "[Pakman.Push] Deployment already exists copying existing..." + ) copy(attributes, config) @@ -58,8 +60,14 @@ defmodule Pakman.Push do defp copy(%{"archive_path" => archive_path} = existing_deployment, config) do with {:ok, token} <- Instellar.authenticate(), - {:ok, deployment_message, response} <- Instellar.create_deployment(token, archive_path, config), - {:ok, configuration_message, _response} <- Instellar.create_configuration(token, response["attributes"]["id"], config) do + {:ok, deployment_message, response} <- + Instellar.create_deployment(token, archive_path, config), + {:ok, configuration_message, _response} <- + Instellar.create_configuration( + token, + response["attributes"]["id"], + config + ) do print_deployment_message(deployment_message) print_configuration_message(configuration_message) diff --git a/test/pakman/push_test.exs b/test/pakman/push_test.exs index 509a59f..8276eb3 100644 --- a/test/pakman/push_test.exs +++ b/test/pakman/push_test.exs @@ -123,7 +123,8 @@ defmodule Pakman.PushTest do "archive_path" => "archives/some-uuid/packages.zip" } } - })) + }) + ) end) Bypass.expect(bypass, "POST", "/publish/deployments", fn conn -> @@ -153,8 +154,7 @@ defmodule Pakman.PushTest do end ) - assert {:ok, :copied} = - Push.perform(config: "test/fixtures/rails.yml") + assert {:ok, :copied} = Push.perform(config: "test/fixtures/rails.yml") end end end