Skip to content
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
17 changes: 17 additions & 0 deletions app/controllers/health_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class HealthController < ApplicationController
skip_before_action :authenticate_user!
skip_after_action :verify_authorized
before_action :verify_token_for_gc_stats, only: [:gc]

def index
respond_to do |format|
Expand All @@ -12,6 +13,14 @@ def index
end
end

def gc
render body: JSON.pretty_generate([
Time.now.in_time_zone("Central Time (US & Canada)").strftime("%H"),
GC.stat
]),
content_type: "application/json"
end

def case_contacts_creation_times_in_last_week
case_contacts_created_in_last_week = CaseContact.where("created_at >= ?", 1.week.ago)

Expand Down Expand Up @@ -67,4 +76,12 @@ def monthly_unique_users_graph_data

render json: monthly_line_graph_combined_data
end

private

def verify_token_for_gc_stats
gc_access_token = ENV["GC_ACCESS_TOKEN"]

head :forbidden unless params[:token] == gc_access_token && !gc_access_token.nil?
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
resources :health, only: %i[index] do
collection do
get :case_contacts_creation_times_in_last_week
get :gc
get :monthly_line_graph_data
get :monthly_unique_users_graph_data
end
Expand Down
14 changes: 11 additions & 3 deletions lib/tasks/post_gc_stat_to_discord.rake
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
desc "Post gc stats to discord channel"

task post_gc_stat_to_discord: :environment do
stats = GC.stat
require "net/http"

url = URI("https://casavolunteertracking.org/health/gc?token=#{ENV["GC_ACCESS_TOKEN"]}")
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This rake task hardcodes the production host and interpolates the token directly into the URL. This makes the task non-portable across environments and risks leaking the token via logs/proxies (query string) as well as incorrect encoding. Prefer deriving the base URL from configuration (e.g., an env var) and building the query via URI.encode_www_form / URI::HTTPS.build, or sending the token in a header instead of the query string.

Suggested change
url = URI("https://casavolunteertracking.org/health/gc?token=#{ENV["GC_ACCESS_TOKEN"]}")
base_url = ENV.fetch("GC_BASE_URL", "https://casavolunteertracking.org")
url = URI.join(base_url, "/health/gc")
url.query = URI.encode_www_form(token: ENV["GC_ACCESS_TOKEN"])

Copilot uses AI. Check for mistakes.
response = Net::HTTP.get_response(url)

unless response.is_a?(Net::HTTPSuccess)
raise "Failed to fetch GC stats. HTTP status code:#{response.code}"
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The raised error message is missing a space after the colon (code:) and omits the response body, which can make diagnosing failures harder. Consider including a space and (optionally) a truncated body or response.message for context.

Suggested change
raise "Failed to fetch GC stats. HTTP status code:#{response.code}"
raise "Failed to fetch GC stats. HTTP status code: #{response.code} #{response.message}. Body: #{response.body&.slice(0, 200)}"

Copilot uses AI. Check for mistakes.
end

stats = response.body

unless ENV["DISCORD_WEBHOOK_URL"].nil?
formatted_stats = JSON.pretty_generate(stats)
discord_message = <<~MULTILINE
```json
#{formatted_stats}
#{stats}
```
Comment on lines +13 to 19
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stats = response.body is posted to Discord as-is. If the endpoint returns structured JSON (array/hash), consider parsing and pretty-printing it here so the Discord message is consistently readable, and so the task fails fast on invalid JSON (instead of posting an error page/HTML).

Copilot uses AI. Check for mistakes.
MULTILINE

Expand Down
Loading