Fix json format requests where ref is in path

What does this MR do and why?

Fix json format requests where ref is in path

We use ExtractsPath to extract ref and format information from the URL. This is required because git branches and tags can include . so we can't rely on rails' default format extraction logic. To get around this we had introduced logic to extract the .atom part of the path and set the format correctly, however, we didn't do the same for .json format.

This change adds logic to extract the json format too.

It also improves the logic's performance by using find over select on ref_names. I'm not sure why we chose to use:

ref_names.select { |v| "#{id}/".starts_with?("#{v}/") }

as we only want to match exactly anyway so we can return early when looping over the ref_names using find and also remove the interpolation. Now we just check if the id is an exact match to one of the ref_names.

I've also refactored the logic a little so it is a little easier to read.

Changelog: fixed

References

Commit list returns 404 when requesting json fo... (#562696 - closed)

Screenshots or screen recordings

Before After
image image

How to set up and validate locally

  1. Open a project in GDK and go to the commits page. Modify the url and append .json to the URL e.g. https://gdk.test:3443/gitlab-org/gitlab-test/-/commits/master.json
  2. Checkout this branch git checkout fix/562696-commit-list-json-format
  3. Reload the page in GDK (might need to restart GDK)

Merge request reports

Loading