Skip to content

Commit

Permalink
shorten deploy notification commit list
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielburnworth committed Aug 24, 2024
1 parent 7e83684 commit 36511b5
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions lib/tasks/hook.rake
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
COMPARE_URL_API = "https://api.github.com/repos/Farmbot/Farmbot-Web-App/compare/"
BASE_URL_API = "https://api.github.com/repos/Farmbot/Farmbot-Web-App/"
COMPARE_URL_API = "#{BASE_URL_API}compare/"
DEPLOYS_URL_API = "#{BASE_URL_API}deployments"
COMPARE_URL_WEB = "https://github.com/Farmbot/Farmbot-Web-App/compare/"
COMMIT_SHA = ENV["HEROKU_BUILD_COMMIT"]
DESCRIPTION = ENV["HEROKU_BUILD_DESCRIPTION"]
Expand All @@ -8,24 +10,44 @@ def open_json(url)
begin
JSON.parse(URI.parse(url).open.read)
rescue *[OpenURI::HTTPError, SocketError] => exception
puts exception.message
puts exception.message + ": #{url}"
return {}
end
end

def commit_messages
def commits_since_main
base_head = "main...#{COMMIT_SHA}"
url = "#{COMPARE_URL_API}#{base_head}"
data = open_json(url)
commits = data.fetch("commits", [])
web_url = "#{COMPARE_URL_WEB}#{base_head}"
data.fetch("commits", [])
end

def last_deploy_commit
data = open_json(DEPLOYS_URL_API)
(data[0] || {}).fetch("sha", nil)
end

def commits_since_last_deploy
last_sha_deployed = last_deploy_commit()
commits = []
commits_since_main.reverse.map do |commit|
if commit.fetch("sha") == last_sha_deployed
break
end
commits.push(commit["commit"]["message"].gsub("\n", " "))
end
commits
end

def details
output = "\n\n"
if !DESCRIPTION.nil?
output += "#{DESCRIPTION}\n\n"
end
output += "<#{web_url}|compare>\n"
messages = commits.map do |x|
output += "\n + #{x["commit"]["message"].gsub("\n", " ")}"
web_compare_url = "#{COMPARE_URL_WEB}#{COMMIT_SHA}...#{last_deploy_commit}"
output += "<#{web_compare_url}|compare>\n"
messages = commits_since_last_deploy.reverse.map do |commit|
output += "\n + #{commit}"
end
output
end
Expand All @@ -35,7 +57,7 @@ namespace :hook do
task release_info: :environment do
if WEBHOOK_URL
notification_text = "A new release has been deployed to the server."
info = notification_text + commit_messages
info = notification_text + details
payload = {
"mrkdwn": true,
"text": notification_text,
Expand Down

0 comments on commit 36511b5

Please sign in to comment.