Skip to content

Commit de0aeb7

Browse files
committed
feat: update existing comment if it exists
1 parent 2064bbf commit de0aeb7

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
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/comments.ex

Lines changed: 21 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,27 @@ 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+
64+
IO.inspect(existing_message)
65+
5466
{:ok, %{id: id} = comment} =
55-
token
56-
|> Connection.new()
57-
|> Issues.issues_create_comment(owner, repo, issue_number, body: %{body: pr_message})
67+
if is_nil(existing_message) do
68+
conn
69+
|> Issues.issues_create_comment(owner, repo, issue_number, body: %{body: pr_message})
70+
else
71+
conn
72+
|> Issues.issues_update_comment(owner, repo, existing_message.id,
73+
body: %{body: pr_message}
74+
)
75+
end
5876

5977
Logger.info(
6078
"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

0 commit comments

Comments
 (0)