Skip to content

Commit 021a827

Browse files
authored
Merge pull request #143 from yknx4/features/single-comment
feat: do not spam comments
2 parents 2064bbf + 5a1bf0f commit 021a827

File tree

6 files changed

+43
-15
lines changed

6 files changed

+43
-15
lines changed

config/prod.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ config :event_bus,
6666
config :librecov, :github,
6767
app_id: System.get_env("GITHUB_APP_ID"),
6868
client_id: System.get_env("GITHUB_CLIENT_ID"),
69-
client_secret: System.get_env("GITHUB_CLIENT_SECRET")
69+
client_secret: System.get_env("GITHUB_CLIENT_SECRET"),
70+
app_name: System.get_env("GITHUB_APP_NAME")
7071

7172
config :ueberauth, Ueberauth.Strategy.Github.OAuth,
7273
client_id: System.get_env("GITHUB_CLIENT_ID"),

lib/opencov/services/github/auth.ex

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
defmodule Librecov.Services.Github.Auth do
2+
import Librecov.Services.Github.Config
23
alias ExOctocat.Connection
34
alias ExOctocat.Api.Apps
45
alias ExOctocat.Model.Installation
@@ -102,12 +103,4 @@ defmodule Librecov.Services.Github.Auth do
102103
)
103104
|> OAuth2.Client.put_serializer("application/json", Jason)
104105
end
105-
106-
defp config do
107-
Application.get_env(:librecov, :github, [])
108-
end
109-
110-
defp github_app_id, do: config() |> Keyword.get(:app_id, "")
111-
defp github_client_id, do: config() |> Keyword.get(:client_id, "")
112-
defp github_client_secret, do: config() |> Keyword.get(:client_secret, "")
113106
end

lib/opencov/services/github/checks.ex

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule Librecov.Services.Github.Checks do
22
require Logger
3+
import Librecov.Services.Github.Config
34
alias ExOctocat.Connection
45
alias ExOctocat.Api.Checks
56
alias Librecov.Build
@@ -40,7 +41,7 @@ defmodule Librecov.Services.Github.Checks do
4041
conn
4142
|> Checks.checks_create(owner, repo,
4243
body: %{
43-
name: "LibreCov/commit",
44+
name: "#{github_app_name()}/commit",
4445
head_sha: commit,
4546
conclusion: "success",
4647
output: %{
@@ -53,7 +54,7 @@ defmodule Librecov.Services.Github.Checks do
5354
conn
5455
|> Checks.checks_create(owner, repo,
5556
body: %{
56-
name: "LibreCov/diff",
57+
name: "#{github_app_name()}/diff",
5758
head_sha: commit,
5859
conclusion: coverage_diff(coverage, real_previous_coverage) |> diff_conclusion(),
5960
output: %{
@@ -80,7 +81,7 @@ defmodule Librecov.Services.Github.Checks do
8081
|> Connection.new()
8182
|> Checks.checks_create(owner, repo,
8283
body: %{
83-
name: "LibreCov/commit",
84+
name: "#{github_app_name()}/commit",
8485
head_sha: commit,
8586
output: %{
8687
title: "Waiting for tests to finish.",

lib/opencov/services/github/comments.ex

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
defmodule Librecov.Services.Github.Comments do
22
require Logger
3+
import Librecov.Services.Github.Config
34
alias ExOctocat.Connection
45
alias ExOctocat.Api.Issues
56
alias Librecov.Services.Github.PullRequests
@@ -51,10 +52,25 @@ defmodule Librecov.Services.Github.Comments do
5152
}) do
5253
Logger.info("Sending pr_message to #{owner}/#{repo}##{issue_number}.")
5354

55+
conn = token |> Connection.new()
56+
57+
{:ok, messages} =
58+
existing_messages =
59+
conn |> Issues.issues_list_comments(owner, repo, issue_number, per_page: 100)
60+
61+
existing_message =
62+
messages |> Enum.find(fn m -> m.user.login == "#{github_app_name()}[bot]" end)
63+
5464
{:ok, %{id: id} = comment} =
55-
token
56-
|> Connection.new()
57-
|> Issues.issues_create_comment(owner, repo, issue_number, body: %{body: pr_message})
65+
if is_nil(existing_message) do
66+
conn
67+
|> Issues.issues_create_comment(owner, repo, issue_number, body: %{body: pr_message})
68+
else
69+
conn
70+
|> Issues.issues_update_comment(owner, repo, existing_message.id,
71+
body: %{body: pr_message}
72+
)
73+
end
5874

5975
Logger.info(
6076
"Succesfully sent message to #{owner}/#{repo}##{issue_number}. IssueComment##{id}"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
defmodule Librecov.Services.Github.Config do
2+
def config do
3+
Application.get_env(:librecov, :github, [])
4+
end
5+
6+
def github_app_id, do: config() |> Keyword.get(:app_id, "")
7+
def github_client_id, do: config() |> Keyword.get(:client_id, "")
8+
def github_client_secret, do: config() |> Keyword.get(:client_secret, "")
9+
def github_app_name, do: config() |> Keyword.get(:app_name, "")
10+
end

test/lib/services/github/comments_test.exs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,13 @@ defmodule Librecov.Services.Github.CommentsTests do
590590
} ->
591591
json([], status: 200)
592592

593+
%{
594+
method: :get,
595+
url: "https://api.github.com/repos/octocat/Hello-World/issues/1347/comments",
596+
query: [per_page: 100]
597+
} ->
598+
json([], status: 200)
599+
593600
%{
594601
method: :post,
595602
url: "https://api.github.com/repos/octocat/Hello-World/issues/1347/comments"

0 commit comments

Comments
 (0)