Extract logic who created deployment into Deployment#deployed_by

Prefer the deployable user over the deployment user.

Related to https://gitlab.com/gitlab-org/gitlab-ce/issues/66037.
This commit is contained in:
Krasimir Angelov 2019-08-20 13:29:16 +03:00
parent 51c19691e4
commit d86b77bf20
5 changed files with 42 additions and 3 deletions

View file

@ -162,6 +162,14 @@ class Deployment < ApplicationRecord
deployed_at&.to_time&.in_time_zone&.to_s(:medium) deployed_at&.to_time&.in_time_zone&.to_s(:medium)
end end
def deployed_by
# We use deployable's user if available because Ci::PlayBuildService
# does not update the deployment's user, just the one for the deployable.
# TODO: use deployment's user once https://gitlab.com/gitlab-org/gitlab-ce/issues/66442
# is completed.
deployable&.user || user
end
private private
def ref_path def ref_path

View file

@ -21,7 +21,7 @@ class DeploymentEntity < Grape::Entity
expose :finished_at expose :finished_at
expose :tag expose :tag
expose :last? expose :last?
expose :user, using: UserEntity expose :deployed_by, as: :user, using: UserEntity
expose :deployable do |deployment, opts| expose :deployable do |deployment, opts|
deployment.deployable.yield_self do |deployable| deployment.deployable.yield_self do |deployable|

View file

@ -15,10 +15,10 @@
.flex-truncate-child .flex-truncate-child
= link_to [@project.namespace.becomes(Namespace), @project, deployment.deployable], class: 'build-link' do = link_to [@project.namespace.becomes(Namespace), @project, deployment.deployable], class: 'build-link' do
#{deployment.deployable.name} (##{deployment.deployable.id}) #{deployment.deployable.name} (##{deployment.deployable.id})
- if deployment.user - if deployment.deployed_by
%div %div
by by
= user_avatar(user: deployment.user, size: 20, css_class: "mr-0 float-none") = user_avatar(user: deployment.deployed_by, size: 20, css_class: "mr-0 float-none")
.table-section.section-15{ role: 'gridcell' } .table-section.section-15{ role: 'gridcell' }
.table-mobile-header{ role: 'rowheader' }= _("Created") .table-mobile-header{ role: 'rowheader' }= _("Created")

View file

@ -0,0 +1,5 @@
---
title: Return correct user for manual deployments
merge_request: 32004
author:
type: fixed

View file

@ -322,4 +322,30 @@ describe Deployment do
end end
end end
end end
describe '#deployed_by' do
it 'returns the deployment user if there is no deployable' do
deployment_user = create(:user)
deployment = create(:deployment, deployable: nil, user: deployment_user)
expect(deployment.deployed_by).to eq(deployment_user)
end
it 'returns the deployment user if the deployable have no user' do
deployment_user = create(:user)
build = create(:ci_build, user: nil)
deployment = create(:deployment, deployable: build, user: deployment_user)
expect(deployment.deployed_by).to eq(deployment_user)
end
it 'returns the deployable user if there is one' do
build_user = create(:user)
deployment_user = create(:user)
build = create(:ci_build, user: build_user)
deployment = create(:deployment, deployable: build, user: deployment_user)
expect(deployment.deployed_by).to eq(build_user)
end
end
end end