fix: handle empty requests gracefully (#608)

This commit is contained in:
Echo
2025-11-04 14:46:45 -05:00
committed by GitHub
parent d99e40c6ed
commit 5f6f195fcf

View File

@@ -1,7 +1,7 @@
class Api::Hackatime::V1::HackatimeController < ApplicationController
before_action :set_user
skip_before_action :verify_authenticity_token
before_action :set_raw_heartbeat_upload, only: [ :push_heartbeats ]
before_action :set_raw_heartbeat_upload, only: [ :push_heartbeats ], if: :is_blank?
def push_heartbeats
# Handle both single and bulk heartbeats based on format
@@ -17,6 +17,11 @@ class Api::Hackatime::V1::HackatimeController < ApplicationController
# ]
# }
heartbeat_array = heartbeat_bulk_params[:heartbeats].map(&:to_h)
if heartbeat_array.empty?
return render json: { error: "No data provided..." }, status: :bad_request
end
render json: { responses: handle_heartbeat(heartbeat_array) }, status: :created
else
# POST /api/hackatime/v1/users/:id/heartbeats
@@ -26,6 +31,11 @@ class Api::Hackatime::V1::HackatimeController < ApplicationController
# ...heartbeat_data
# }
heartbeat_array = Array(heartbeat_params)
if heartbeat_array.empty? || heartbeat_params.blank?
return render json: { error: "No data provided..." }, status: :bad_request
end
new_heartbeat = handle_heartbeat(heartbeat_array)&.first&.first
render json: new_heartbeat, status: :accepted
end
@@ -138,6 +148,11 @@ class Api::Hackatime::V1::HackatimeController < ApplicationController
private
def is_blank?
body = body_to_json
body.present? && (body.is_a?(Array) ? body.any? : true)
end
def calculate_category_stats(heartbeats, category)
return [] if heartbeats.empty?