From d57a215ec429d6ed72f80175b7e6df5d7a324484 Mon Sep 17 00:00:00 2001 From: Robert Prehn <3952444+prehnRA@users.noreply.github.com> Date: Thu, 22 Jul 2021 09:51:44 -0500 Subject: [PATCH 01/10] fix: Report coverage properly at the umbrella level --- mix.exs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/mix.exs b/mix.exs index 98a2a7d1..3ee66836 100644 --- a/mix.exs +++ b/mix.exs @@ -11,9 +11,9 @@ defmodule Legendary.Mixfile do build_embedded: Mix.env() == :prod, start_permanent: Mix.env() == :prod, deps: deps(), - aliases: aliases(), test_coverage: [tool: ExCoveralls], - preferred_cli_env: [coveralls: :test, "coveralls.detail": :test, "coveralls.post": :test, "coveralls.html": :test], + aliases: aliases(), + preferred_cli_env: [coveralls: :test, "coveralls.detail": :test, "coveralls.post": :test, "coveralls.html": :test, "coveralls.json": :test], ] end @@ -24,10 +24,8 @@ defmodule Legendary.Mixfile do defp aliases do [ "deps.get": ["cmd mix deps.get"], - "coveralls.html": ["cmd mix coveralls.html"], "ecto.migrate": ["cmd mix ecto.migrate"], - "npm.install": ["cmd mix npm.install"], - test: ["cmd mix test"] + "npm.install": ["cmd mix npm.install"] ] end end From 702e41c8240fe7ca92472f15cf5738adac0ac1e2 Mon Sep 17 00:00:00 2001 From: Robert Prehn <3952444+prehnRA@users.noreply.github.com> Date: Thu, 22 Jul 2021 15:39:17 -0500 Subject: [PATCH 02/10] feat: Report coverage to gitlab --- .gitignore | 3 + .gitlab-ci.yml | 12 +++- mix.exs | 9 ++- script/cibuild | 2 +- script/coverage-json-to-cobertura | 105 ++++++++++++++++++++++++++++++ script/coverage-json-to-metrics | 30 +++++++++ 6 files changed, 157 insertions(+), 4 deletions(-) create mode 100755 script/coverage-json-to-cobertura create mode 100755 script/coverage-json-to-metrics diff --git a/.gitignore b/.gitignore index 2ffb1c25..170db901 100644 --- a/.gitignore +++ b/.gitignore @@ -44,3 +44,6 @@ config/*.secret.exs # Temporary CI build file build.env + +# CI metrics file +metrics.txt diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index e3ba6690..fe7cfbc9 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -26,7 +26,7 @@ variables: - name: postgres:12 script: script/cibuild -test: +test_1.10.4: <<: *test_template image: "elixir:1.10.4-alpine" @@ -34,9 +34,17 @@ test_1.11.4: <<: *test_template image: "elixir:1.11.4-alpine" -test_1.12.1: +test: <<: *test_template image: "elixir:1.12.1-alpine" + script: + - script/cibuild + - script/coverage-json-to-metrics + - script/coverage-json-to-cobertura + artifacts: + reports: + metrics: metrics.txt + cobertura: cover/cobertura.xml credo: stage: test diff --git a/mix.exs b/mix.exs index 3ee66836..dcc7b0cf 100644 --- a/mix.exs +++ b/mix.exs @@ -13,7 +13,14 @@ defmodule Legendary.Mixfile do deps: deps(), test_coverage: [tool: ExCoveralls], aliases: aliases(), - preferred_cli_env: [coveralls: :test, "coveralls.detail": :test, "coveralls.post": :test, "coveralls.html": :test, "coveralls.json": :test], + preferred_cli_env: [ + coveralls: :test, + "coveralls.detail": :test, + "coveralls.post": :test, + "coveralls.html": :test, + "coveralls.json": :test, + "coveralls.xml": :test + ], ] end diff --git a/script/cibuild b/script/cibuild index a44bc161..89d8c9c1 100755 --- a/script/cibuild +++ b/script/cibuild @@ -12,6 +12,6 @@ mix deps.get mix ecto.create mix ecto.migrate -mix test +mix coveralls.json script/restore-timestamps diff --git a/script/coverage-json-to-cobertura b/script/coverage-json-to-cobertura new file mode 100755 index 00000000..2315f920 --- /dev/null +++ b/script/coverage-json-to-cobertura @@ -0,0 +1,105 @@ +#!/usr/bin/env elixir + +Mix.install([ + {:jason, "~> 1.0"}, + {:xml_builder, "~> 2.1"} +]) + +%{"source_files" => data} = + "cover/excoveralls.json" + |> File.read!() + |> Jason.decode!() + +{total_covered, total_total} = + data + |> Enum.reduce({0,0}, fn %{"coverage" => cover}, {covered, total} -> + file_covered = + cover + |> Enum.reduce(0, &(if is_integer(&1) && &1 > 0, do: &2 + 1, else: &2)) + + file_total = + cover + |> Enum.reduce(0, &(if is_nil(&1), do: &2, else: &2 + 1)) + + {covered + file_covered, total + file_total} + end) + +ratio = total_covered / total_total + +files = + data + |> Enum.map(fn %{"coverage" => cover, "name" => name} -> + file_covered = + cover + |> Enum.reduce(0, &(if is_integer(&1) && &1 > 0, do: &2 + 1, else: &2)) + + file_total = + cover + |> Enum.reduce(0, &(if is_nil(&1), do: &2, else: &2 + 1)) + + lines = + cover + |> Enum.with_index + |> Enum.map(fn + {nil, _index} -> + nil + {line, index} -> + {:line, %{"number" => index, "hits" => line}, []} + end) + |> Enum.reject(&is_nil/1) + + ratio = + if file_total == 0 do + 1.0 + else + file_covered / file_total + end + + { + :class, + %{"filename" => name, "line-rate" => ratio}, + [ + { + :lines, + %{}, + lines + } + ] + } + end) + +buffer = + XmlBuilder.document([ + XmlBuilder.doctype( + "coverage", + system: ["http://cobertura.sourceforge.net/xml/coverage-03.dtd"] + ), + { + :coverage, + %{ + "line-rate" => ratio, + }, + [ + { + :packages, + %{}, + [ + { + :package, + %{"name" => "", "line-rate" => ratio}, + [ + { + :classes, + %{}, + files + } + ] + } + ] + } + ] + } + ]) + |> XmlBuilder.generate + +File.write!("cover/cobertura.xml", buffer) diff --git a/script/coverage-json-to-metrics b/script/coverage-json-to-metrics new file mode 100755 index 00000000..9216b5d9 --- /dev/null +++ b/script/coverage-json-to-metrics @@ -0,0 +1,30 @@ +#!/usr/bin/env elixir + +Mix.install([ + {:jason, "~> 1.0"} +]) + +%{"source_files" => data} = + "cover/excoveralls.json" + |> File.read!() + |> Jason.decode!() + +{total_covered, total_total} = + data + |> Enum.reduce({0,0}, fn %{"coverage" => cover}, {covered, total} -> + file_covered = + cover + |> Enum.reduce(0, &(if is_integer(&1) && &1 > 0, do: &2 + 1, else: &2)) + + file_total = + cover + |> Enum.reduce(0, &(if is_nil(&1), do: &2, else: &2 + 1)) + + {covered + file_covered, total + file_total} + end) + +ratio = + (total_covered / total_total) + |> Float.round(3) + +File.write!("metrics.txt", "coverage #{ratio}") From 06401909a03efcbf4b3aee5637b0c59cf9ff796e Mon Sep 17 00:00:00 2001 From: Robert Prehn <3952444+prehnRA@users.noreply.github.com> Date: Thu, 22 Jul 2021 15:52:55 -0500 Subject: [PATCH 03/10] fix: Generate coveralls.json in umbrella mode --- script/cibuild | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/cibuild b/script/cibuild index 89d8c9c1..1ef790c7 100755 --- a/script/cibuild +++ b/script/cibuild @@ -12,6 +12,6 @@ mix deps.get mix ecto.create mix ecto.migrate -mix coveralls.json +mix coveralls.json --umbrella script/restore-timestamps From 5fdaa96bb81048af638a490d6b305cb3dbc9e6b8 Mon Sep 17 00:00:00 2001 From: Robert Prehn <3952444+prehnRA@users.noreply.github.com> Date: Fri, 23 Jul 2021 09:17:24 -0500 Subject: [PATCH 04/10] fix: Add source tag to cobertura script --- script/coverage-json-to-cobertura | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/script/coverage-json-to-cobertura b/script/coverage-json-to-cobertura index 2315f920..ec0f9e7b 100755 --- a/script/coverage-json-to-cobertura +++ b/script/coverage-json-to-cobertura @@ -68,6 +68,14 @@ files = } end) +source = + if System.get_env("CI_BUILDS_DIR") do + "#{System.get_env("CI_BUILDS_DIR")}/#{System.get_env("PROJECT_FULL_PATH")}" + else + File.cwd! + end + + buffer = XmlBuilder.document([ XmlBuilder.doctype( @@ -80,6 +88,17 @@ buffer = "line-rate" => ratio, }, [ + { + :sources, + %{}, + [ + { + :source, + %{}, + source + } + ] + }, { :packages, %{}, From b9901d52be0780aa106087b4a36de6f22b1074b9 Mon Sep 17 00:00:00 2001 From: Robert Prehn <3952444+prehnRA@users.noreply.github.com> Date: Fri, 23 Jul 2021 09:30:18 -0500 Subject: [PATCH 05/10] fix: Change env var names to one defined on GitLab --- script/coverage-json-to-cobertura | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/script/coverage-json-to-cobertura b/script/coverage-json-to-cobertura index ec0f9e7b..e4424100 100755 --- a/script/coverage-json-to-cobertura +++ b/script/coverage-json-to-cobertura @@ -69,8 +69,8 @@ files = end) source = - if System.get_env("CI_BUILDS_DIR") do - "#{System.get_env("CI_BUILDS_DIR")}/#{System.get_env("PROJECT_FULL_PATH")}" + if System.get_env("CI_PROJECT_NAME") do + "#{System.get_env("CI_BUILDS_DIR")}/#{System.get_env("CI_PROJECT_NAMESPACE")}/#{System.get_env("CI_PROJECT_NAME")}" else File.cwd! end From 1a4f0120a88a326459285679b582fb7c34700468 Mon Sep 17 00:00:00 2001 From: Robert Prehn <3952444+prehnRA@users.noreply.github.com> Date: Fri, 23 Jul 2021 09:41:27 -0500 Subject: [PATCH 06/10] fix: Add lines attribute to class tags --- apps/content/lib/content/term_relationship.ex | 2 +- script/coverage-json-to-cobertura | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/content/lib/content/term_relationship.ex b/apps/content/lib/content/term_relationship.ex index 26febdc2..e3364ba3 100644 --- a/apps/content/lib/content/term_relationship.ex +++ b/apps/content/lib/content/term_relationship.ex @@ -4,7 +4,7 @@ defmodule Legendary.Content.TermRelationship do """ use Ecto.Schema import Ecto.Changeset - alias Legendary.Content.{Post} + alias Legendary.Content.Post @primary_key {:object_id, :integer, []} @primary_key {:term_taxonomy_id, :integer, []} diff --git a/script/coverage-json-to-cobertura b/script/coverage-json-to-cobertura index e4424100..9958772d 100755 --- a/script/coverage-json-to-cobertura +++ b/script/coverage-json-to-cobertura @@ -61,7 +61,7 @@ files = [ { :lines, - %{}, + %{"lines" => file_total}, lines } ] From bd4b0feba936d76606fe9da63ba93a0ae9b54e34 Mon Sep 17 00:00:00 2001 From: Robert Prehn <3952444+prehnRA@users.noreply.github.com> Date: Fri, 23 Jul 2021 10:51:28 -0500 Subject: [PATCH 07/10] fix: Remove duplicate attribute --- script/coverage-json-to-cobertura | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/coverage-json-to-cobertura b/script/coverage-json-to-cobertura index 9958772d..e4424100 100755 --- a/script/coverage-json-to-cobertura +++ b/script/coverage-json-to-cobertura @@ -61,7 +61,7 @@ files = [ { :lines, - %{"lines" => file_total}, + %{}, lines } ] From de9831c2c2c6801a79df840226e59832c7b4e4d1 Mon Sep 17 00:00:00 2001 From: Robert Prehn <3952444+prehnRA@users.noreply.github.com> Date: Fri, 23 Jul 2021 11:08:31 -0500 Subject: [PATCH 08/10] chore: Report coverage in a separate CI job --- .gitlab-ci.yml | 20 ++++++++++++++------ script/cibuild | 6 +++++- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index fe7cfbc9..a4262ab2 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -38,13 +38,9 @@ test: <<: *test_template image: "elixir:1.12.1-alpine" script: - - script/cibuild - - script/coverage-json-to-metrics - - script/coverage-json-to-cobertura + - script/cibuild covered artifacts: - reports: - metrics: metrics.txt - cobertura: cover/cobertura.xml + coverage_json: cover/excoveralls.json credo: stage: test @@ -94,6 +90,18 @@ build_image_for_commit: script: - script/ci-docker-build +report_coverage: + stage: deploy_tags + needs: ['test'] + image: "elixir:1.12.1-alpine" + script: + - script/coverage-json-to-metrics + - script/coverage-json-to-cobertura + artifacts: + reports: + metrics: metrics.txt + cobertura: cover/cobertura.xml + # If tests pass, tag the commit and update package versions deploy_to_tags: stage: deploy_tags diff --git a/script/cibuild b/script/cibuild index 1ef790c7..6544bbe7 100755 --- a/script/cibuild +++ b/script/cibuild @@ -12,6 +12,10 @@ mix deps.get mix ecto.create mix ecto.migrate -mix coveralls.json --umbrella +if [ $1 == 'covered' ]; then + mix coveralls.json --umbrella +else + mix test +fi; script/restore-timestamps From 0864dd167246cf7a7cab547ff91823517eb31beb Mon Sep 17 00:00:00 2001 From: Robert Prehn <3952444+prehnRA@users.noreply.github.com> Date: Fri, 23 Jul 2021 11:10:53 -0500 Subject: [PATCH 09/10] fix: Set proper key in artifacts --- .gitlab-ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a4262ab2..07ac3255 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -40,7 +40,8 @@ test: script: - script/cibuild covered artifacts: - coverage_json: cover/excoveralls.json + paths: + - cover/excoveralls.json credo: stage: test From 6c042da6dee6569fb691b22233c7d2adfd3a1f21 Mon Sep 17 00:00:00 2001 From: Robert Prehn <3952444+prehnRA@users.noreply.github.com> Date: Fri, 23 Jul 2021 11:17:14 -0500 Subject: [PATCH 10/10] fix: Install hex before running coverage scripts --- .gitlab-ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 07ac3255..9afe89a9 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -96,6 +96,7 @@ report_coverage: needs: ['test'] image: "elixir:1.12.1-alpine" script: + - mix local.hex --force - script/coverage-json-to-metrics - script/coverage-json-to-cobertura artifacts: