Skip to content
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
```

### Bug fixes
* None
* [`ISSUE-203`](https://github.com/thiagoesteves/deployex/issues/203) DeployEx restarted after Github returned 504

### Enhancements
* [`ISSUE-188`](https://github.com/thiagoesteves/deployex/issues/188) Add DeployEx Secrets via environment vars
Expand Down
2 changes: 1 addition & 1 deletion apps/deployer/lib/deployer/github.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ defmodule Deployer.Github do
### ==========================================================================
### Public functions
### ==========================================================================
@spec latest_release() :: {:ok, Release.t()}
@spec latest_release() :: Release.t()
def latest_release, do: Release.latest_release()

@spec download_artifact(url :: String.t(), token :: String.t()) ::
Expand Down
51 changes: 28 additions & 23 deletions apps/deployer/lib/deployer/github/release.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,25 @@ defmodule Deployer.Github.Release do
|> Finch.build(@deployex_latest_tag, [], [])
|> Finch.request(Deployer.Finch)

case response do
{:ok, %{body: body}} ->
info = Jason.decode!(body)

tag_name = info["tag_name"]
prerelease = info["prerelease"]
current_version = Application.spec(:foundation, :vsn) |> to_string

new_release? =
tag_name != nil and prerelease == false and new_release?(current_version, tag_name)

%__MODULE__{
tag_name: tag_name,
prerelease: prerelease,
new_release?: new_release?,
created_at: info["created_at"],
updated_at: info["updated_at"],
published_at: info["published_at"]
}

{:error, _reason} ->
with {:ok, %{body: body}} <- response,
{:ok, info} <- Jason.decode(body) do
tag_name = info["tag_name"]
prerelease = info["prerelease"]
current_version = Application.spec(:foundation, :vsn) |> to_string

new_release? =
tag_name != nil and prerelease == false and new_release?(current_version, tag_name)

%__MODULE__{
tag_name: tag_name,
prerelease: prerelease,
new_release?: new_release?,
created_at: info["created_at"],
updated_at: info["updated_at"],
published_at: info["published_at"]
}
else
_any ->
# NOTE: Keep the latest state, since the application may not have acceess to
# network or GitHub is not available
state
Expand All @@ -92,9 +90,16 @@ defmodule Deployer.Github.Release do
### ==========================================================================
### Public functions
### ==========================================================================
@spec latest_release(module :: module()) :: {:ok, __MODULE__.t()}
@spec latest_release(module :: module()) :: __MODULE__.t()
def latest_release(module \\ __MODULE__) do
Common.call_gen_server(module, :latest_release)
case Common.call_gen_server(module, :latest_release) do
{:ok, release} ->
release

{:error, reason} ->
Logger.error("Error while trying to get latest release, reason: #{inspect(reason)}")
%__MODULE__{}
end
end

### ==========================================================================
Expand Down
3 changes: 2 additions & 1 deletion apps/deployer/lib/deployer/status/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ defmodule Deployer.Status.Application do
end

uptime = Common.uptime_to_string(Application.get_env(:foundation, :booted_at))
{:ok, deployex_latest_release} = Github.Release.latest_release()

deployex_latest_release = Github.Release.latest_release()

%Status{
name: name,
Expand Down
21 changes: 2 additions & 19 deletions apps/deployer/test/github_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ defmodule Deployer.GithubTest do
end do
assert {:ok, pid} = Release.start_link(name: custom_name)

assert {:ok, state} = Release.latest_release(custom_name)
assert state = Release.latest_release(custom_name)
assert state.tag_name == "1.0.0"
assert state.prerelease == false
assert state.created_at == "2024-01-01T10:00:00Z"
Expand Down Expand Up @@ -144,7 +144,7 @@ defmodule Deployer.GithubTest do
end

test "fetch latest version from default module" do
assert {:ok, %Release{}} = Release.latest_release()
assert %Release{} = Release.latest_release()
end

test "handles timeout error" do
Expand All @@ -163,23 +163,6 @@ defmodule Deployer.GithubTest do
end
end

test "handles invalid JSON response" do
previous_state = %Release{tag_name: "1.0.0"}

with_mock Finch, [:passthrough],
request: fn %Finch.Request{
host: "api.github.com",
path: "/repos/thiagoesteves/deployex/releases/latest"
},
_module ->
{:ok, %{body: "invalid json {"}}
end do
assert_raise Jason.DecodeError, fn ->
Release.update_github_info(previous_state)
end
end
end

# Helper functions
defp build_mock_response(overrides \\ %{}) do
Map.merge(
Expand Down