From c8386d5d25008c864cc77c87d4c320fd81a12f29 Mon Sep 17 00:00:00 2001 From: Alper Akgun <aakgun@gitlab.com> Date: Thu, 23 Jan 2025 22:05:03 +0300 Subject: [PATCH] Fix error for anonymous users on Projects::Ml::CandidatesController#show Changelog: fixed --- .../ml/candidate_details_presenter.rb | 24 ++++++++++--------- .../ml/candidate_details_presenter_spec.rb | 10 ++++++++ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/app/presenters/ml/candidate_details_presenter.rb b/app/presenters/ml/candidate_details_presenter.rb index cb4baf1190dc..420488afd161 100644 --- a/app/presenters/ml/candidate_details_presenter.rb +++ b/app/presenters/ml/candidate_details_presenter.rb @@ -2,8 +2,6 @@ module Ml class CandidateDetailsPresenter - include Rails.application.routes.url_helpers - def initialize(candidate, current_user) @candidate = candidate @current_user = current_user @@ -26,7 +24,7 @@ def present created_at: candidate.created_at, author_web_url: candidate.user&.namespace&.web_url, author_name: candidate.user&.name, - promote_path: promote_project_ml_candidate_path(candidate.project, candidate.iid), + promote_path: url_helpers.promote_project_ml_candidate_path(candidate.project, candidate.iid), can_promote: can_promote }, params: candidate.params, @@ -34,7 +32,7 @@ def present metadata: candidate.metadata, projectPath: candidate.project.full_path, can_write_model_experiments: current_user&.can?(:write_model_experiments, candidate.project), - markdown_preview_path: project_preview_markdown_path(candidate.project), + markdown_preview_path: url_helpers.project_preview_markdown_path(candidate.project), model_gid: candidate.experiment.model&.to_global_id.to_s, latest_version: candidate.experiment.model&.latest_version&.version } @@ -51,12 +49,12 @@ def present_as_json attr_reader :candidate, :current_user def job_info - return unless candidate.from_ci? && current_user.can?(:read_build, candidate.ci_build) + return unless candidate.from_ci? && current_user&.can?(:read_build, candidate.ci_build) build = candidate.ci_build { - path: project_job_path(build.project, build), + path: url_helpers.project_job_path(build.project, build), name: build.name, **user_info(build.user) || {}, **mr_info(build.pipeline.merge_request) || {} @@ -68,7 +66,7 @@ def user_info(user) { user: { - path: user_path(user), + path: url_helpers.user_path(user), username: user.username, name: user.name, avatar: user.avatar_url @@ -81,7 +79,7 @@ def mr_info(mr) { merge_request: { - path: project_merge_request_path(mr.project, mr), + path: url_helpers.project_merge_request_path(mr.project, mr), iid: mr.iid, title: mr.title } @@ -93,15 +91,19 @@ def link_to_artifact return unless artifact.present? - project_package_path(candidate.project, artifact) + url_helpers.project_package_path(candidate.project, artifact) end def link_to_details - project_ml_candidate_path(candidate.project, candidate.iid) + url_helpers.project_ml_candidate_path(candidate.project, candidate.iid) end def link_to_experiment - project_ml_experiment_path(candidate.project, candidate.experiment.iid) + url_helpers.project_ml_experiment_path(candidate.project, candidate.experiment.iid) + end + + def url_helpers + Gitlab::Routing.url_helpers end def can_promote diff --git a/spec/presenters/ml/candidate_details_presenter_spec.rb b/spec/presenters/ml/candidate_details_presenter_spec.rb index b959d2f143c0..4e8358a478fd 100644 --- a/spec/presenters/ml/candidate_details_presenter_spec.rb +++ b/spec/presenters/ml/candidate_details_presenter_spec.rb @@ -186,6 +186,16 @@ expect(subject.dig('info', 'canPromote')).to be(false) end end + + context 'when user is nil' do + subject(:present_as_json) do + Gitlab::Json.parse(described_class.new(candidate, nil).present_as_json)['candidate'] + end + + it 'ciJob is nil' do + expect(present_as_json.dig('info', 'ciJob')).to be_nil + end + end end end end -- GitLab