From cc0254d0017a43fecd4068769e16441241c56ad8 Mon Sep 17 00:00:00 2001 From: Katie Wright Date: Wed, 11 Jan 2023 11:09:33 +1300 Subject: [PATCH] Add collector and version to all analytics API payloads --- lib/buildkite_test_collector/ci_env.ex | 9 +++++++++ lib/buildkite_test_collector/ci_env/buildkite.ex | 6 ++++++ lib/buildkite_test_collector/ci_env/circle_ci.ex | 6 ++++++ lib/buildkite_test_collector/ci_env/generic.ex | 6 ++++++ lib/buildkite_test_collector/ci_env/github_actions.ex | 6 ++++++ lib/buildkite_test_collector/ci_env/local.ex | 6 ++++++ lib/buildkite_test_collector/payload.ex | 6 ++++-- mix.exs | 7 +++++-- test/buildkite_test_collector/payload_test.exs | 2 ++ 9 files changed, 50 insertions(+), 4 deletions(-) diff --git a/lib/buildkite_test_collector/ci_env.ex b/lib/buildkite_test_collector/ci_env.ex index efe9a3d..0e2b4a7 100644 --- a/lib/buildkite_test_collector/ci_env.ex +++ b/lib/buildkite_test_collector/ci_env.ex @@ -52,6 +52,15 @@ defmodule BuildkiteTestCollector.CiEnv do """ @callback message() :: nil | String.t() + @doc """ + Name of test collector + """ + @callback collector() :: String.t() + @doc """ + Version of test collector + """ + @callback version() :: String.t() + @optional_callbacks url: 0, branch: 0, commit_sha: 0, number: 0, job_id: 0, message: 0 @doc """ diff --git a/lib/buildkite_test_collector/ci_env/buildkite.ex b/lib/buildkite_test_collector/ci_env/buildkite.ex index 030b027..4f5db80 100644 --- a/lib/buildkite_test_collector/ci_env/buildkite.ex +++ b/lib/buildkite_test_collector/ci_env/buildkite.ex @@ -35,4 +35,10 @@ defmodule BuildkiteTestCollector.CiEnv.Buildkite do @impl true def message, do: System.get_env("BUILDKITE_MESSAGE") + + @impl true + def collector, do: BuildkiteTestCollector.MixProject.collector_name() + + @impl true + def version, do: BuildkiteTestCollector.MixProject.version() end diff --git a/lib/buildkite_test_collector/ci_env/circle_ci.ex b/lib/buildkite_test_collector/ci_env/circle_ci.ex index d61faef..9ffeb1f 100644 --- a/lib/buildkite_test_collector/ci_env/circle_ci.ex +++ b/lib/buildkite_test_collector/ci_env/circle_ci.ex @@ -34,4 +34,10 @@ defmodule BuildkiteTestCollector.CiEnv.CircleCi do @impl true def number, do: System.get_env("CIRCLE_BUILD_NUM") + + @impl true + def collector, do: BuildkiteTestCollector.MixProject.collector_name() + + @impl true + def version, do: BuildkiteTestCollector.MixProject.version() end diff --git a/lib/buildkite_test_collector/ci_env/generic.ex b/lib/buildkite_test_collector/ci_env/generic.ex index 74568e9..3f619ee 100644 --- a/lib/buildkite_test_collector/ci_env/generic.ex +++ b/lib/buildkite_test_collector/ci_env/generic.ex @@ -20,4 +20,10 @@ defmodule BuildkiteTestCollector.CiEnv.Generic do @impl true def key, do: Ecto.UUID.generate() + + @impl true + def collector, do: BuildkiteTestCollector.MixProject.collector_name() + + @impl true + def version, do: BuildkiteTestCollector.MixProject.version() end diff --git a/lib/buildkite_test_collector/ci_env/github_actions.ex b/lib/buildkite_test_collector/ci_env/github_actions.ex index c247a4d..8075a6c 100644 --- a/lib/buildkite_test_collector/ci_env/github_actions.ex +++ b/lib/buildkite_test_collector/ci_env/github_actions.ex @@ -37,4 +37,10 @@ defmodule BuildkiteTestCollector.CiEnv.GithubActions do @impl true def number, do: System.get_env("GITHUB_RUN_NUMBER") + + @impl true + def collector, do: BuildkiteTestCollector.MixProject.collector_name() + + @impl true + def version, do: BuildkiteTestCollector.MixProject.version() end diff --git a/lib/buildkite_test_collector/ci_env/local.ex b/lib/buildkite_test_collector/ci_env/local.ex index 074fc15..4051376 100644 --- a/lib/buildkite_test_collector/ci_env/local.ex +++ b/lib/buildkite_test_collector/ci_env/local.ex @@ -20,4 +20,10 @@ defmodule BuildkiteTestCollector.CiEnv.Local do @impl true def key, do: Ecto.UUID.generate() + + @impl true + def collector, do: BuildkiteTestCollector.MixProject.collector_name() + + @impl true + def version, do: BuildkiteTestCollector.MixProject.version() end diff --git a/lib/buildkite_test_collector/payload.ex b/lib/buildkite_test_collector/payload.ex index 721538c..1bbf207 100644 --- a/lib/buildkite_test_collector/payload.ex +++ b/lib/buildkite_test_collector/payload.ex @@ -22,7 +22,9 @@ defmodule BuildkiteTestCollector.Payload do optional(:branch) => String.t(), optional(:commit_sha) => String.t(), optional(:message) => String.t(), - optional(:url) => String.t() + optional(:url) => String.t(), + required(:collector) => String.t(), + required(:version) => String.t() } @doc """ @@ -52,7 +54,7 @@ defmodule BuildkiteTestCollector.Payload do def set_start_time(%Payload{} = payload, started_at), do: %{payload | started_at: started_at} defp serialise_env(ci_env_mod) do - ~w[CI key number job_id branch commit_sha message url]a + ~w[CI key number job_id branch commit_sha message url collector version]a |> Enum.reduce(%{}, fn :CI, env -> Map.put(env, :CI, ci_env_mod.ci()) key, env -> Map.put(env, key, apply(ci_env_mod, key, [])) diff --git a/mix.exs b/mix.exs index 67ff860..09f0530 100644 --- a/mix.exs +++ b/mix.exs @@ -2,11 +2,14 @@ defmodule BuildkiteTestCollector.MixProject do use Mix.Project @moduledoc false - @version "0.2.0" + @version "0.2.2" + def version, do: @version + @collector_name :buildkite_test_collector + def collector_name, do: "elixir-#{@collector_name}" def project do [ - app: :buildkite_test_collector, + app: @collector_name, description: "Official Buildkite Test Analytics Collector", version: @version, elixir: "~> 1.13", diff --git a/test/buildkite_test_collector/payload_test.exs b/test/buildkite_test_collector/payload_test.exs index d24d3f3..4405ad3 100644 --- a/test/buildkite_test_collector/payload_test.exs +++ b/test/buildkite_test_collector/payload_test.exs @@ -22,6 +22,8 @@ defmodule BuildkiteTestCollector.PayloadTest do assert run_env.message == env["BUILDKITE_MESSAGE"] assert run_env.url =~ "http" assert run_env.url =~ env["BUILDKITE_BUILD_ID"] + assert run_env.collector == BuildkiteTestCollector.MixProject.collector_name() + assert run_env.version == BuildkiteTestCollector.MixProject.version() end test "it initialises with empty data" do