Commit b9d0d013 authored by Amparo Luna's avatar Amparo Luna Committed by Dmytro Zaporozhets

Add blobs to SnippetType

parent a7fe3895
......@@ -65,6 +65,11 @@ module Types
calls_gitaly: true,
null: false
field :blobs, type: [Types::Snippets::BlobType],
description: 'Snippet blobs',
calls_gitaly: true,
null: false
field :ssh_url_to_repo, type: GraphQL::STRING_TYPE,
description: 'SSH URL to the snippet repository',
calls_gitaly: true,
......
......@@ -36,10 +36,14 @@ class SnippetPresenter < Gitlab::View::Presenter::Delegated
end
def blob
blobs.first
end
def blobs
if snippet.empty_repo?
snippet.blob
[snippet.blob]
else
snippet.blobs.first
snippet.blobs
end
end
......
---
title: Add blobs field to SnippetType in GraphQL
merge_request: 33657
author:
type: changed
......@@ -11003,6 +11003,11 @@ type Snippet implements Noteable {
"""
blob: SnippetBlob!
"""
Snippet blobs
"""
blobs: [SnippetBlob!]!
"""
Timestamp this snippet was created
"""
......
......@@ -32475,6 +32475,32 @@
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "blobs",
"description": "Snippet blobs",
"args": [
],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "OBJECT",
"name": "SnippetBlob",
"ofType": null
}
}
}
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "createdAt",
"description": "Timestamp this snippet was created",
......@@ -1603,6 +1603,7 @@ Represents a snippet entry
| --- | ---- | ---------- |
| `author` | User! | The owner of the snippet |
| `blob` | SnippetBlob! | Snippet blob |
| `blobs` | SnippetBlob! => Array | Snippet blobs |
| `createdAt` | Time! | Timestamp this snippet was created |
| `description` | String | Description of the snippet |
| `descriptionHtml` | String | The GitLab Flavored Markdown rendering of `description` |
......
......@@ -11,7 +11,7 @@ describe GitlabSchema.types['Snippet'] do
:visibility_level, :created_at, :updated_at,
:web_url, :raw_url, :ssh_url_to_repo, :http_url_to_repo,
:notes, :discussions, :user_permissions,
:description_html, :blob]
:description_html, :blob, :blobs]
expect(described_class).to have_graphql_fields(*expected_fields)
end
......@@ -76,30 +76,14 @@ describe GitlabSchema.types['Snippet'] do
describe '#blob' do
let(:query_blob) { subject.dig('data', 'snippets', 'edges')[0]['node']['blob'] }
let(:query) do
%(
{
snippets {
edges {
node {
blob {
name
path
}
}
}
}
}
)
end
subject { GitlabSchema.execute(query, context: { current_user: user }).as_json }
subject { GitlabSchema.execute(snippet_query_for(field: 'blob'), context: { current_user: user }).as_json }
context 'when snippet has repository' do
let!(:snippet) { create(:personal_snippet, :repository, :public, author: user) }
let(:blob) { snippet.blobs.first }
it 'returns blob from the repository' do
it 'returns the first blob from the repository' do
expect(query_blob['name']).to eq blob.name
expect(query_blob['path']).to eq blob.path
end
......@@ -115,4 +99,58 @@ describe GitlabSchema.types['Snippet'] do
end
end
end
describe '#blobs' do
let_it_be(:snippet) { create(:personal_snippet, :public, author: user) }
let(:query_blobs) { subject.dig('data', 'snippets', 'edges')[0]['node']['blobs'] }
subject { GitlabSchema.execute(snippet_query_for(field: 'blobs'), context: { current_user: user }).as_json }
shared_examples 'an array' do
it 'returns an array of snippet blobs' do
expect(query_blobs).to be_an(Array)
end
end
context 'when snippet does not have a repository' do
let(:blob) { snippet.blob }
it_behaves_like 'an array'
it 'contains the first blob from the snippet' do
expect(query_blobs.first['name']).to eq blob.name
expect(query_blobs.first['path']).to eq blob.path
end
end
context 'when snippet has repository' do
let_it_be(:snippet) { create(:personal_snippet, :repository, :public, author: user) }
let(:blobs) { snippet.blobs }
it_behaves_like 'an array'
it 'contains all the blobs from the repository' do
resulting_blobs_names = query_blobs.map { |b| b['name'] }
expect(resulting_blobs_names).to match_array(blobs.map(&:name))
end
end
end
def snippet_query_for(field:)
%(
{
snippets {
edges {
node {
#{field} {
name
path
}
}
}
}
}
)
end
end
......@@ -163,4 +163,25 @@ describe SnippetPresenter do
end
end
end
describe '#blobs' do
let(:snippet) { personal_snippet }
subject { presenter.blobs }
context 'when snippet does not have a repository' do
it 'returns an array with one SnippetBlob' do
expect(subject.size).to eq(1)
expect(subject.first).to eq(snippet.blob)
end
end
context 'when snippet has a repository' do
let(:snippet) { create(:snippet, :repository, author: user) }
it 'returns an array with all repository blobs' do
expect(subject).to match_array(snippet.blobs)
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