Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: do not spam comments #143

Merged
merged 4 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config/prod.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
9 changes: 1 addition & 8 deletions lib/opencov/services/github/auth.ex
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
7 changes: 4 additions & 3 deletions lib/opencov/services/github/checks.ex
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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: %{
Expand All @@ -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: %{
Expand All @@ -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.",
Expand Down
22 changes: 19 additions & 3 deletions lib/opencov/services/github/comments.ex
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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}"
Expand Down
10 changes: 10 additions & 0 deletions lib/opencov/services/github/config.ex
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions test/lib/services/github/comments_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down