Commit 93bacb03 authored by Jeroen van Baarsen's avatar Jeroen van Baarsen

Merge pull request #8890 from sue445/feature/project_api_avatar_url

Expose avatar_url in projects API
parents 7486bc0a 51abeaa1
Pipeline #3344 passed with stage
...@@ -10,6 +10,7 @@ v 7.9.0 (unreleased) ...@@ -10,6 +10,7 @@ v 7.9.0 (unreleased)
- Save web edit in new branch - Save web edit in new branch
- Fix ordering of imported but unchanged projects (Marco Wessel) - Fix ordering of imported but unchanged projects (Marco Wessel)
- Mobile UI improvements: make aside content expandable - Mobile UI improvements: make aside content expandable
- Expose avatar_url in projects API
- Generalize image upload in drag and drop in markdown to all files (Hannes Rosenögger) - Generalize image upload in drag and drop in markdown to all files (Hannes Rosenögger)
- Fix mass-unassignment of issues (Robert Speicher) - Fix mass-unassignment of issues (Robert Speicher)
- Allow user confirmation to be skipped for new users via API - Allow user confirmation to be skipped for new users via API
......
...@@ -58,10 +58,8 @@ module ApplicationHelper ...@@ -58,10 +58,8 @@ module ApplicationHelper
Project.find_with_namespace(project_id) Project.find_with_namespace(project_id)
end end
if project.avatar.present? if project.avatar_url
image_tag project.avatar.url, options image_tag project.avatar_url, options
elsif project.avatar_in_git
image_tag namespace_project_avatar_path(project.namespace, project), options
else # generated icon else # generated icon
project_identicon(project, options) project_identicon(project, options)
end end
......
...@@ -37,6 +37,8 @@ class Project < ActiveRecord::Base ...@@ -37,6 +37,8 @@ class Project < ActiveRecord::Base
include Gitlab::ShellAdapter include Gitlab::ShellAdapter
include Gitlab::VisibilityLevel include Gitlab::VisibilityLevel
include Gitlab::ConfigHelper include Gitlab::ConfigHelper
include Rails.application.routes.url_helpers
extend Gitlab::ConfigHelper extend Gitlab::ConfigHelper
extend Enumerize extend Enumerize
...@@ -408,6 +410,14 @@ class Project < ActiveRecord::Base ...@@ -408,6 +410,14 @@ class Project < ActiveRecord::Base
@avatar_file @avatar_file
end end
def avatar_url
if avatar.present?
[gitlab_config.url, avatar.url].join
elsif avatar_in_git
[gitlab_config.url, namespace_project_avatar_path(namespace, self)].join
end
end
# For compatibility with old code # For compatibility with old code
def code def code
path path
......
...@@ -68,7 +68,8 @@ Parameters: ...@@ -68,7 +68,8 @@ Parameters:
"path": "diaspora", "path": "diaspora",
"updated_at": "2013-09-30T13: 46: 02Z" "updated_at": "2013-09-30T13: 46: 02Z"
}, },
"archived": false "archived": false,
"avatar_url": "http://example.com/uploads/project/avatar/4/uploads/avatar.png"
}, },
{ {
"id": 6, "id": 6,
...@@ -103,7 +104,8 @@ Parameters: ...@@ -103,7 +104,8 @@ Parameters:
"path": "brightbox", "path": "brightbox",
"updated_at": "2013-09-30T13:46:02Z" "updated_at": "2013-09-30T13:46:02Z"
}, },
"archived": false "archived": false,
"avatar_url": null
} }
] ]
``` ```
...@@ -195,7 +197,8 @@ Parameters: ...@@ -195,7 +197,8 @@ Parameters:
"notification_level": 3 "notification_level": 3
} }
}, },
"archived": false "archived": false,
"avatar_url": "http://example.com/uploads/project/avatar/3/uploads/avatar.png"
} }
``` ```
......
...@@ -56,6 +56,7 @@ module API ...@@ -56,6 +56,7 @@ module API
expose :issues_enabled, :merge_requests_enabled, :wiki_enabled, :snippets_enabled, :created_at, :last_activity_at expose :issues_enabled, :merge_requests_enabled, :wiki_enabled, :snippets_enabled, :created_at, :last_activity_at
expose :namespace expose :namespace
expose :forked_from_project, using: Entities::ForkedFromProject, if: lambda{ | project, options | project.forked? } expose :forked_from_project, using: Entities::ForkedFromProject, if: lambda{ | project, options | project.forked? }
expose :avatar_url
end end
class ProjectMember < UserBasic class ProjectMember < UserBasic
......
...@@ -64,8 +64,9 @@ describe ApplicationHelper do ...@@ -64,8 +64,9 @@ describe ApplicationHelper do
project = create(:project) project = create(:project)
project.avatar = File.open(avatar_file_path) project.avatar = File.open(avatar_file_path)
project.save! project.save!
avatar_url = "http://localhost/uploads/project/avatar/#{ project.id }/gitlab_logo.png"
expect(project_icon("#{project.namespace.to_param}/#{project.to_param}").to_s).to eq( expect(project_icon("#{project.namespace.to_param}/#{project.to_param}").to_s).to eq(
"<img alt=\"Gitlab logo\" src=\"/uploads/project/avatar/#{ project.id }/gitlab_logo.png\" />" "<img alt=\"Gitlab logo\" src=\"#{avatar_url}\" />"
) )
end end
...@@ -75,8 +76,9 @@ describe ApplicationHelper do ...@@ -75,8 +76,9 @@ describe ApplicationHelper do
allow_any_instance_of(Project).to receive(:avatar_in_git).and_return(true) allow_any_instance_of(Project).to receive(:avatar_in_git).and_return(true)
avatar_url = 'http://localhost' + namespace_project_avatar_path(project.namespace, project)
expect(project_icon("#{project.namespace.to_param}/#{project.to_param}").to_s).to match( expect(project_icon("#{project.namespace.to_param}/#{project.to_param}").to_s).to match(
image_tag(namespace_project_avatar_path(project.namespace, project))) image_tag(avatar_url))
end end
end end
......
...@@ -326,4 +326,35 @@ describe Project do ...@@ -326,4 +326,35 @@ describe Project do
expect(project.avatar_type).to eq(['only images allowed']) expect(project.avatar_type).to eq(['only images allowed'])
end end
end end
describe :avatar_url do
subject { project.avatar_url }
let(:project) { create(:project) }
context 'When avatar file is uploaded' do
before do
project.update_columns(avatar: 'uploads/avatar.png')
allow(project.avatar).to receive(:present?) { true }
end
let(:avatar_path) do
"/uploads/project/avatar/#{project.id}/uploads/avatar.png"
end
it { should eq "http://localhost#{avatar_path}" }
end
context 'When avatar file in git' do
before do
allow(project).to receive(:avatar_in_git) { true }
end
let(:avatar_path) do
"/#{project.namespace.name}/#{project.path}/avatar"
end
it { should eq "http://localhost#{avatar_path}" }
end
end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment