Skip to content

Commit 27ec3e7

Browse files
authored
Merge pull request #137 from yknx4/feature/use-integration-for-check-suite
feature: use integration for check suite
2 parents 8f94a39 + 1cce405 commit 27ec3e7

File tree

10 files changed

+49
-16
lines changed

10 files changed

+49
-16
lines changed

config/config.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ config :event_bus,
5959
:pull_request_synced,
6060
:inserted,
6161
:updated,
62-
:build_finished
62+
:build_finished,
63+
:check_suite_requested
6364
]
6465

6566
config :ueberauth, Ueberauth,

lib/librecov_web/live/build_live/job_row.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ defmodule Librecov.RepositoryLive.JobRow do
2929
</span>
3030
</td>
3131
<td class="text-start d-none d-sm-table-cell">
32-
{@job.run_at |> human_time_ago}
32+
{(@job.run_at || @job.updated_at) |> human_time_ago}
3333
</td>
3434
<td class="text-center d-none d-sm-table-cell">
3535
{@job.files_count}

lib/opencov/services/github/auth.ex

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ defmodule Librecov.Services.Github.Auth do
1818

1919
def with_auth_data(nil, _), do: {:error, :nil_input}
2020

21+
def with_auth_data(
22+
%{
23+
"installation" => %{"id" => installation_id},
24+
"repository" => %{"name" => repo, "owner" => %{"login" => owner}}
25+
},
26+
block
27+
) do
28+
with {:ok, token} <- login_token_for(:installation_id, installation_id) do
29+
apply(block, [%AuthData{token: token, owner: owner, repo: repo}])
30+
end
31+
end
32+
2133
def with_auth_data(%Project{} = project, block) do
2234
with {owner, repo} <- Project.name_and_owner(project) do
2335
with_auth_data(owner, repo, block)
@@ -53,6 +65,15 @@ defmodule Librecov.Services.Github.Auth do
5365
jwt
5466
end
5567

68+
def login_token_for(:installation_id, installation_id) do
69+
with {:ok, token} <-
70+
app_token()
71+
|> Connection.new()
72+
|> Apps.apps_create_installation_access_token(installation_id) do
73+
{:ok, token.token}
74+
end
75+
end
76+
5677
def login_token(login) do
5778
with {:ok, installations} <-
5879
app_token()

lib/opencov/services/github/checks.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ defmodule Librecov.Services.Github.Checks do
6363
}
6464
) do
6565
Logger.info("Finished check of commit #{commit} with diff: #{cov_dif} coverage: #{cov}.")
66+
Logger.info("#{commit_check |> inspect() |> Jason.encode!(pretty: true)}")
67+
Logger.info("#{diff_check |> inspect() |> Jason.encode!(pretty: true)}")
6668
{:ok, [commit_check, diff_check]}
6769
end
6870
end

lib/opencov/subscribers/github_subscriber.ex

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ defmodule Librecov.Subscriber.GithubSubscriber do
1010
alias Librecov.Templates.CommentTemplate
1111
alias Librecov.Services.Github.AuthData
1212

13-
@topics [:build_finished, :pull_request_synced]
13+
@topics [:build_finished, :pull_request_synced, :check_suite_requested]
1414

1515
def topics, do: @topics
1616

@@ -39,13 +39,10 @@ defmodule Librecov.Subscriber.GithubSubscriber do
3939
end)
4040
end
4141

42-
def process(%Event{topic: :pull_request_synced, data: payload}) do
43-
with %{"after" => commit, "repository" => repo} <- payload,
44-
%{"name" => repo, "owner" => %{"login" => owner}} <- repo do
45-
Auth.with_auth_data(owner, repo, fn auth ->
46-
Checks.create_check(auth, commit)
47-
end)
48-
end
42+
def process(%Event{topic: :check_suite_requested, data: payload}) do
43+
Auth.with_auth_data(payload, fn auth ->
44+
Checks.create_check(auth, payload["check_suite"]["head_sha"])
45+
end)
4946
end
5047

5148
def process(%Event{}) do

lib/opencov/templates/comment_template.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ defmodule Librecov.Templates.CommentTemplate do
2323
}
2424
} = build,
2525
%{
26-
head: %{user: %{login: username}},
26+
user: %{login: username},
2727
base: %{ref: base_branch, sha: base_commit}
2828
}
2929
) do

lib/web/services/github_service.ex

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,22 @@ defmodule Librecov.GithubService do
1010
install(payload["repository"])
1111
end
1212

13+
def handle("check_suite", payload) do
14+
handle_check_suite(payload["action"], payload)
15+
end
16+
1317
def handle(event, _payload) do
1418
Logger.debug("Unhandled event: #{event}")
1519
end
1620

21+
defp handle_check_suite(action, payload) when action in ["rerequested", "requested"] do
22+
%Event{id: UUID.uuid1(), topic: :check_suite_requested, data: payload} |> EventBus.notify()
23+
end
24+
25+
defp handle_check_suite(action, _payload) do
26+
Logger.debug("Unhandled action: #{action}")
27+
end
28+
1729
def handle_pr("synchronize", payload) do
1830
%Event{id: UUID.uuid1(), topic: :pull_request_synced, data: payload} |> EventBus.notify()
1931
end

test/lib/templates/comments_template_test.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ defmodule Librecov.Templates.CommentTemplateTest do
1717
test "barebones template #1" do
1818
template =
1919
CommentTemplate.coverage_message(@base_build, %{
20-
head: %{user: %{login: "github"}},
20+
user: %{login: "github"},
2121
base: %{ref: "potato", sha: "chettos"}
2222
})
2323

@@ -28,7 +28,7 @@ defmodule Librecov.Templates.CommentTemplateTest do
2828
test "barebones template #2" do
2929
template =
3030
CommentTemplate.coverage_message(%Build{@base_build | coverage: 50.1234}, %{
31-
head: %{user: %{login: "github"}},
31+
user: %{login: "github"},
3232
base: %{ref: "potato", sha: "chettos"}
3333
})
3434

@@ -41,7 +41,7 @@ defmodule Librecov.Templates.CommentTemplateTest do
4141
CommentTemplate.coverage_message(
4242
%Build{@base_build | coverage: 50.1234, previous_coverage: 30.2345},
4343
%{
44-
head: %{user: %{login: "github"}},
44+
user: %{login: "github"},
4545
base: %{ref: "potato", sha: "chettos"}
4646
}
4747
)

test/librecov_web/live/build_live_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ defmodule LibrecovWeb.BuildLiveTest do
2424
commit_sha: "",
2525
commit_message: "oie shi",
2626
branch: "main",
27-
inserted_at: Timex.now() |> Timex.beginning_of_day() |> Timex.to_datetime(),
27+
inserted_at: Timex.now() |> Timex.shift(hours: -18) |> Timex.to_datetime(),
2828
project: project
2929
)
3030

test/librecov_web/live/repository_live_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ defmodule Librecov.RepositoryLiveTest do
334334
commit_sha: "",
335335
commit_message: "oie shi",
336336
branch: "main",
337-
inserted_at: Timex.now() |> Timex.beginning_of_day() |> Timex.to_datetime(),
337+
inserted_at: Timex.now() |> Timex.shift(hours: -18) |> Timex.to_datetime(),
338338
project: p
339339
)
340340

0 commit comments

Comments
 (0)