diff --git a/config/prod.exs b/config/prod.exs index effc0fd8..48836df4 100644 --- a/config/prod.exs +++ b/config/prod.exs @@ -66,7 +66,8 @@ config :event_bus, config :librecov, :github, app_id: System.get_env("GITHUB_APP_ID"), client_id: System.get_env("GITHUB_CLIENT_ID"), - client_secret: System.get_env("GITHUB_CLIENT_SECRET") + client_secret: System.get_env("GITHUB_CLIENT_SECRET"), + app_name: System.get_env("GITHUB_APP_NAME") config :ueberauth, Ueberauth.Strategy.Github.OAuth, client_id: System.get_env("GITHUB_CLIENT_ID"), diff --git a/lib/opencov/services/github/auth.ex b/lib/opencov/services/github/auth.ex index 0b1ac291..52dd1a97 100644 --- a/lib/opencov/services/github/auth.ex +++ b/lib/opencov/services/github/auth.ex @@ -1,4 +1,5 @@ defmodule Librecov.Services.Github.Auth do + import Librecov.Services.Github.Config alias ExOctocat.Connection alias ExOctocat.Api.Apps alias ExOctocat.Model.Installation @@ -102,12 +103,4 @@ defmodule Librecov.Services.Github.Auth do ) |> OAuth2.Client.put_serializer("application/json", Jason) end - - defp config do - Application.get_env(:librecov, :github, []) - end - - defp github_app_id, do: config() |> Keyword.get(:app_id, "") - defp github_client_id, do: config() |> Keyword.get(:client_id, "") - defp github_client_secret, do: config() |> Keyword.get(:client_secret, "") end diff --git a/lib/opencov/services/github/checks.ex b/lib/opencov/services/github/checks.ex index 99555d2a..f09198d5 100644 --- a/lib/opencov/services/github/checks.ex +++ b/lib/opencov/services/github/checks.ex @@ -1,5 +1,6 @@ defmodule Librecov.Services.Github.Checks do require Logger + import Librecov.Services.Github.Config alias ExOctocat.Connection alias ExOctocat.Api.Checks alias Librecov.Build @@ -40,7 +41,7 @@ defmodule Librecov.Services.Github.Checks do conn |> Checks.checks_create(owner, repo, body: %{ - name: "LibreCov/commit", + name: "#{github_app_name()}/commit", head_sha: commit, conclusion: "success", output: %{ @@ -53,7 +54,7 @@ defmodule Librecov.Services.Github.Checks do conn |> Checks.checks_create(owner, repo, body: %{ - name: "LibreCov/diff", + name: "#{github_app_name()}/diff", head_sha: commit, conclusion: coverage_diff(coverage, real_previous_coverage) |> diff_conclusion(), output: %{ @@ -80,7 +81,7 @@ defmodule Librecov.Services.Github.Checks do |> Connection.new() |> Checks.checks_create(owner, repo, body: %{ - name: "LibreCov/commit", + name: "#{github_app_name()}/commit", head_sha: commit, output: %{ title: "Waiting for tests to finish.", diff --git a/lib/opencov/services/github/comments.ex b/lib/opencov/services/github/comments.ex index 7fe62afd..3c9f3167 100644 --- a/lib/opencov/services/github/comments.ex +++ b/lib/opencov/services/github/comments.ex @@ -1,5 +1,6 @@ defmodule Librecov.Services.Github.Comments do require Logger + import Librecov.Services.Github.Config alias ExOctocat.Connection alias ExOctocat.Api.Issues alias Librecov.Services.Github.PullRequests @@ -51,10 +52,25 @@ defmodule Librecov.Services.Github.Comments do }) do Logger.info("Sending pr_message to #{owner}/#{repo}##{issue_number}.") + conn = token |> Connection.new() + + {:ok, messages} = + existing_messages = + conn |> Issues.issues_list_comments(owner, repo, issue_number, per_page: 100) + + existing_message = + messages |> Enum.find(fn m -> m.user.login == "#{github_app_name()}[bot]" end) + {:ok, %{id: id} = comment} = - token - |> Connection.new() - |> Issues.issues_create_comment(owner, repo, issue_number, body: %{body: pr_message}) + if is_nil(existing_message) do + conn + |> Issues.issues_create_comment(owner, repo, issue_number, body: %{body: pr_message}) + else + conn + |> Issues.issues_update_comment(owner, repo, existing_message.id, + body: %{body: pr_message} + ) + end Logger.info( "Succesfully sent message to #{owner}/#{repo}##{issue_number}. IssueComment##{id}" diff --git a/lib/opencov/services/github/config.ex b/lib/opencov/services/github/config.ex new file mode 100644 index 00000000..7eb45da7 --- /dev/null +++ b/lib/opencov/services/github/config.ex @@ -0,0 +1,10 @@ +defmodule Librecov.Services.Github.Config do + def config do + Application.get_env(:librecov, :github, []) + end + + def github_app_id, do: config() |> Keyword.get(:app_id, "") + def github_client_id, do: config() |> Keyword.get(:client_id, "") + def github_client_secret, do: config() |> Keyword.get(:client_secret, "") + def github_app_name, do: config() |> Keyword.get(:app_name, "") +end diff --git a/test/lib/services/github/comments_test.exs b/test/lib/services/github/comments_test.exs index fc46274b..f70b3a0e 100644 --- a/test/lib/services/github/comments_test.exs +++ b/test/lib/services/github/comments_test.exs @@ -590,6 +590,13 @@ defmodule Librecov.Services.Github.CommentsTests do } -> json([], status: 200) + %{ + method: :get, + url: "https://api.github.com/repos/octocat/Hello-World/issues/1347/comments", + query: [per_page: 100] + } -> + json([], status: 200) + %{ method: :post, url: "https://api.github.com/repos/octocat/Hello-World/issues/1347/comments"