@@ -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
3673end
0 commit comments