Skip to content

Commit 9c9b51c

Browse files
committed
feat: add diff and better messaging
1 parent d971910 commit 9c9b51c

File tree

3 files changed

+92
-19
lines changed

3 files changed

+92
-19
lines changed

lib/opencov/services/github/checks.ex

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,57 @@ defmodule Librecov.Services.Github.Checks do
22
require Logger
33
alias ExOctocat.Connection
44
alias ExOctocat.Api.Checks
5+
alias Librecov.Build
56

6-
def finish_check(token, commit, owner, repo, coverage) do
7-
token
8-
|> Connection.new()
9-
|> Checks.checks_create(owner, repo,
10-
body: %{
11-
name: "Open Coverage",
12-
head_sha: commit,
13-
conclusion: "success",
14-
output: %{
15-
title: "Coverage at #{coverage}%",
16-
summary: "#{coverage}%"
17-
}
18-
}
19-
)
7+
def finish_check(token, owner, repo, %Build{
8+
coverage: coverage,
9+
previous_coverage: previous_coverage,
10+
commit_sha: commit
11+
}) do
12+
cov_dif = coverage_diff(coverage, previous_coverage) |> format_coverage()
13+
cov = coverage |> format_coverage()
14+
15+
conn =
16+
token
17+
|> Connection.new()
18+
19+
with {:ok, commit_check} <-
20+
conn
21+
|> Checks.checks_create(owner, repo,
22+
body: %{
23+
name: "LibreCov/commit",
24+
head_sha: commit,
25+
conclusion: "success",
26+
output: %{
27+
title: "Coverage at #{cov} (#{cov_dif})",
28+
summary: "#{cov}% (#{cov_dif})"
29+
}
30+
}
31+
),
32+
{:ok, diff_check} <-
33+
conn
34+
|> Checks.checks_create(owner, repo,
35+
body: %{
36+
name: "LibreCov/diff",
37+
head_sha: commit,
38+
conclusion: coverage_diff(coverage, previous_coverage) |> diff_conclusion(),
39+
output: %{
40+
title: "Coverage changed #{cov_dif}",
41+
summary: "changed #{cov_dif}"
42+
}
43+
}
44+
) do
45+
Logger.info("Finished check of commit #{commit} with diff: #{cov_dif} coverage: #{cov}.")
46+
{:ok, [commit_check, diff_check]}
47+
end
2048
end
2149

2250
def create_check(token, commit, owner, repo) do
2351
token
2452
|> Connection.new()
2553
|> Checks.checks_create(owner, repo,
2654
body: %{
27-
name: "Open Coverage",
55+
name: "LibreCov/commit",
2856
head_sha: commit,
2957
output: %{
3058
title: "Waiting for tests to finish.",
@@ -33,4 +61,13 @@ defmodule Librecov.Services.Github.Checks do
3361
}
3462
)
3563
end
64+
65+
defp diff_conclusion(diff) when diff == 0, do: "neutral"
66+
defp diff_conclusion(diff) when diff < 0, do: "failure"
67+
defp diff_conclusion(diff) when diff > 0, do: "success"
68+
69+
defp format_coverage(coverage), do: "#{Float.round(coverage, 2)}%"
70+
71+
defp coverage_diff(coverage, nil), do: coverage
72+
defp coverage_diff(coverage, previous_coverage), do: coverage - previous_coverage
3673
end

test/lib/services/github/checks_test.exs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ defmodule Librecov.Services.Github.ChecksTests do
33
import Tesla.Mock
44

55
alias Librecov.Services.Github.Checks
6+
alias Librecov.Build
67

78
@create_check %{
89
"id" => 4,
@@ -111,8 +112,43 @@ defmodule Librecov.Services.Github.ChecksTests do
111112
assert Map.has_key?(project, :id)
112113
end
113114

114-
test "it finished a check" do
115-
{:ok, project} = Checks.finish_check("asdfasdf", "qwerqwer", "github", "hello-world", 54.4444)
116-
assert Map.has_key?(project, :id)
115+
@base_build %Build{
116+
commit_sha: "ce587453ced02b1526dfb4cb910479d431683101",
117+
coverage: 97.12345
118+
}
119+
120+
test "it finishes a check with nil previous coverage" do
121+
{:ok, checks} = Checks.finish_check("asdfasdf", "github", "hello-world", @base_build)
122+
checks |> Enum.each(fn check -> assert Map.has_key?(check, :id) end)
123+
end
124+
125+
test "it finishes a check with 0 diff coverage" do
126+
{:ok, checks} =
127+
Checks.finish_check("asdfasdf", "github", "hello-world", %Build{
128+
@base_build
129+
| previous_coverage: @base_build.coverage
130+
})
131+
132+
checks |> Enum.each(fn check -> assert Map.has_key?(check, :id) end)
133+
end
134+
135+
test "it finishes a check with positive diff coverage" do
136+
{:ok, checks} =
137+
Checks.finish_check("asdfasdf", "github", "hello-world", %Build{
138+
@base_build
139+
| previous_coverage: 99.99999
140+
})
141+
142+
checks |> Enum.each(fn check -> assert Map.has_key?(check, :id) end)
143+
end
144+
145+
test "it finishes a check with negative diff coverage" do
146+
{:ok, checks} =
147+
Checks.finish_check("asdfasdf", "github", "hello-world", %Build{
148+
@base_build
149+
| previous_coverage: 90.4321
150+
})
151+
152+
checks |> Enum.each(fn check -> assert Map.has_key?(check, :id) end)
117153
end
118154
end

web/managers/project_manager.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ defmodule Librecov.ProjectManager do
8080

8181
with {owner, name} <- Project.name_and_owner(project),
8282
{:ok, token} <- Auth.login_token(owner) do
83-
Checks.finish_check(token, build.commit_sha, owner, name, job.coverage)
83+
Checks.finish_check(token, owner, name, build)
8484
end
8585

8686
{build, job}

0 commit comments

Comments
 (0)