NoMethodError in Commits API: each_with_index called on String instead of Array

Summary

A NoMethodError is occurring in the Commits API when attrs[:actions] is a String instead of an Array, causing the validation to fail.

Error Details

Sentry error: https://new-sentry.gitlab.net/organizations/gitlab/issues/2951206

NoMethodError: undefined method `each_with_index' for "[\n  ... { }\n]":String (NoMethodError)

        attrs[:actions].each_with_index do |action, index|
                       ^^^^^^^^^^^^^^^^
Did you mean?  each_line
  from lib/api/commits.rb:82:in `validate_commits_attrs!'
  from lib/api/commits.rb:273:in `block (2 levels) in <class:Commits>'

Root Cause

The error occurs at line 82 in lib/api/commits.rb where the code expects attrs[:actions] to be an Array but receives a String representation of JSON instead. This suggests that the JSON parsing is not happening correctly, and the actions parameter is being passed as a raw string.

Likely Cause

This issue is probably related to the changes made in #574324. The new multipart upload flow may not be properly parsing the JSON content from the uploaded file.

Expected Behavior

The attrs[:actions] should be parsed as an Array of action objects before reaching the validate_commits_attrs! method.

Actual Behavior

The attrs[:actions] is a String containing JSON, causing each_with_index to fail since String doesn't have that method.

Affected Code

File: lib/api/commits.rb Line: 82

attrs[:actions].each_with_index do |action, index|
  bad_request!("actions[#{index}][action] is required") if action[:action].blank?
  bad_request!("actions[#{index}][file_path] is required") if action[:file_path].blank?

  err = validate_commit_action!(action, index)

  bad_request!(err) if err
end

Suggested Fix

The file_params_from_body_upload helper (likely in Helpers::CommitsBodyUploaderHelper) should ensure that the JSON content is properly parsed before returning the attributes. The actions field should be parsed from JSON string to Array.

  • #574324
Edited by 🤖 GitLab Bot 🤖