From de0aeb762ea7c6ab0701b71dde49efa9613090dd Mon Sep 17 00:00:00 2001 From: Ale Ornelas Date: Wed, 29 Sep 2021 15:35:22 -0500 Subject: [PATCH 1/4] feat: update existing comment if it exists --- config/prod.exs | 3 ++- lib/opencov/services/github/auth.ex | 9 +-------- lib/opencov/services/github/comments.ex | 24 +++++++++++++++++++++--- lib/opencov/services/github/config.ex | 10 ++++++++++ 4 files changed, 34 insertions(+), 12 deletions(-) create mode 100644 lib/opencov/services/github/config.ex 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/comments.ex b/lib/opencov/services/github/comments.ex index 7fe62afd..1758d004 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,27 @@ 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) + + IO.inspect(existing_message) + {: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 From 865db2070a54391029abbfe1ae501780b5081dae Mon Sep 17 00:00:00 2001 From: Ale Ornelas Date: Wed, 29 Sep 2021 15:47:58 -0500 Subject: [PATCH 2/4] fix: remove io inspect --- lib/opencov/services/github/comments.ex | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/opencov/services/github/comments.ex b/lib/opencov/services/github/comments.ex index 1758d004..3c9f3167 100644 --- a/lib/opencov/services/github/comments.ex +++ b/lib/opencov/services/github/comments.ex @@ -61,8 +61,6 @@ defmodule Librecov.Services.Github.Comments do existing_message = messages |> Enum.find(fn m -> m.user.login == "#{github_app_name()}[bot]" end) - IO.inspect(existing_message) - {:ok, %{id: id} = comment} = if is_nil(existing_message) do conn From d426cb3d085785b9a7b8b16e0024c83ddcedae0c Mon Sep 17 00:00:00 2001 From: Ale Ornelas Date: Wed, 29 Sep 2021 15:52:45 -0500 Subject: [PATCH 3/4] test: add missing mock --- test/lib/services/github/comments_test.exs | 7 +++++++ 1 file changed, 7 insertions(+) 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" From 5a1bf0f8f40adbce4604c0938b225bcd9f36dbd7 Mon Sep 17 00:00:00 2001 From: Ale Ornelas Date: Wed, 29 Sep 2021 15:55:58 -0500 Subject: [PATCH 4/4] feat: use dynamic check name --- lib/opencov/services/github/checks.ex | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) 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.",