Skip to content

Commit

Permalink
Merge pull request #3 from jchristgit/more-team-encouragement
Browse files Browse the repository at this point in the history
Provide more team encouragement
  • Loading branch information
jb3 authored Sep 2, 2024
2 parents b8b80d8 + 47bd230 commit 70b7653
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ You can also optionally set these variables:
|--------------------|------------------------------------------------------------------------------------------|----------------------|
| `FF_POLICY_FILE` | The file to read for policy on who can perform merges | `.github/ff-bot.yml` |
| `FF_LISTEN_PORT` | The HTTP port for the service to listen on | 4000 |
| `FF_NO_PROPAGANDA` | Disable all team encouragement messages generated by the bot on merged Pull Requests. | |
| `FF_NO_PROPAGANDA` | Disable all team encouragement messages generated by the bot. | |

## Usage

Expand Down
4 changes: 2 additions & 2 deletions lib/ff_bot/dispatch/issue_comment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule FFBot.Dispatch.IssueComment do
"""
@behaviour FFBot.Dispatch

alias FFBot.{Auth.InstallationTokenServer, GitHub, Policy}
alias FFBot.{Auth.InstallationTokenServer, GitHub, Policy, Encouragement}

require Logger

Expand Down Expand Up @@ -132,7 +132,7 @@ defmodule FFBot.Dispatch.IssueComment do
"#{comparison_details["ahead_by"]} commits and behind by " <>
"#{comparison_details["behind_by"]} commits. Please update the branch " <>
"to reflect changes on `#{repo_details["default_branch"]}` and try again, " <>
"or merge locally."
"or merge locally." <> Encouragement.maybe_encouraging_comment()
)
else
Logger.info("Triggering merger...")
Expand Down
46 changes: 46 additions & 0 deletions lib/ff_bot/encouragement.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
defmodule FFBot.Encouragement do
@moduledoc """
Encouragement functions for our Comrades.
"""

# Fractional percentage of how often the bot will generate a supportive
# comment on merge.
@support_a_comrade_margin 0.01
# Fractional percentage of how often the bot will generate an encouraging
# comment on conflicts.
@encourage_a_comrade_threshold 0.01

defp supports_comrades? do
Application.get_env(:ff_bot, :disable_propaganda?) == nil
end

defp should_generate_supportive_comment? do
:rand.uniform() < @support_a_comrade_margin
end

defp should_generate_encouraging_comment? do
:rand.uniform() < @encourage_a_comrade_threshold
end

@doc """
On a slim chance, generate a comment that expresses appreciation in the comrade's work.
"""
def maybe_supportive_comment do
if supports_comrades?() && should_generate_supportive_comment?() do
". Glory to Arstotzka!"
else
""
end
end

@doc """
On a slim chance, generate a comment that expresses encouragement for the comrade to work better.
"""
def maybe_encouraging_comment do
if supports_comrades?() && should_generate_encouraging_comment?() do
". March on, Comrade!"
else
""
end
end
end
11 changes: 2 additions & 9 deletions lib/ff_bot/github/merger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ defmodule FFBot.GitHub.Merger do

require Logger

alias FFBot.Encouragement
alias FFBot.GitHub.Request

def start_merge(token, repo, pull_request) do
Expand Down Expand Up @@ -44,7 +45,7 @@ defmodule FFBot.GitHub.Merger do
pull_request["number"],
:success,
"Successfully fast-forwarded commits from `#{pull_request["head"]["label"]}` " <>
"onto `#{repo["default_branch"]}`" <> maybe_propaganda()
"onto `#{repo["default_branch"]}`" <> Encouragement.maybe_supportive_comment()
)
end

Expand All @@ -66,12 +67,4 @@ defmodule FFBot.GitHub.Merger do
defp push_changes(cloned) do
{:ok, _} = Git.push(cloned)
end

defp maybe_propaganda do
if Application.get_env(:ff_bot, :disable_propaganda?) == nil and :rand.uniform() < 0.01 do
". Glory to Arstotzka!"
else
""
end
end
end

0 comments on commit 70b7653

Please sign in to comment.