Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
feat(snippets): Add files array to snippet creation
Browse files Browse the repository at this point in the history
This adds the new `files` array for creating snippets that replaces the
now deprecated `filename` and `contents` keys.

This is needed for creating more then one file in snippets.

Issue #1372
  • Loading branch information
zemzale committed Feb 10, 2022
1 parent 3532f54 commit 65aab58
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions project_snippets.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ type CreateProjectSnippetOptions struct {
Description *string `url:"description,omitempty" json:"description,omitempty"`
Content *string `url:"content,omitempty" json:"content,omitempty"`
Visibility *VisibilityValue `url:"visibility,omitempty" json:"visibility,omitempty"`
Files []*SnippetFile `url:"files,omitempty" json:"files,omitempty"`
}

// CreateSnippet creates a new project snippet. The user must have permission
Expand Down
17 changes: 16 additions & 1 deletion project_snippets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,13 @@ func TestProjectSnippetsService_CreateSnippet(t *testing.T) {
},
"project_id": 1,
"web_url": "http://example.com/example/example/snippets/1",
"raw_url": "http://example.com/example/example/snippets/1/raw"
"raw_url": "http://example.com/example/example/snippets/1/raw",
"files": [
{
"path": "add.rb",
"raw_url": "http://example.com/example/example/-/snippets/1/raw/main/add.rb"
}
]
}
`)
})
Expand All @@ -198,6 +204,15 @@ func TestProjectSnippetsService_CreateSnippet(t *testing.T) {
},
WebURL: "http://example.com/example/example/snippets/1",
RawURL: "http://example.com/example/example/snippets/1/raw",
Files: []struct {
Path string `json:"path"`
RawURL string `json:"raw_url"`
}{
{
Path: "add.rb",
RawURL: "http://example.com/example/example/-/snippets/1/raw/main/add.rb",
},
},
}

s, resp, err := client.ProjectSnippets.CreateSnippet(1, nil, nil, nil)
Expand Down
14 changes: 14 additions & 0 deletions snippets.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ type Snippet struct {
CreatedAt *time.Time `json:"created_at"`
WebURL string `json:"web_url"`
RawURL string `json:"raw_url"`
Files []struct {
Path string `json:"path"`
RawURL string `json:"raw_url"`
} `json:"files"`
}

func (s Snippet) String() string {
Expand Down Expand Up @@ -101,6 +105,15 @@ func (s *SnippetsService) GetSnippet(snippet int, options ...RequestOptionFunc)
return ps, resp, err
}

// SnippetFile represents the object that is used to create snippets
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/snippets.html#create-new-snippet
type SnippetFile struct {
FilePath *string `url:"file_path,omitempty" json:"file_path,omitempty"`
Content *string `url:"content,omitempty" json:"content,omitempty"`
}

// CreateSnippetOptions represents the available CreateSnippet() options.
//
// GitLab API docs:
Expand All @@ -111,6 +124,7 @@ type CreateSnippetOptions struct {
Description *string `url:"description,omitempty" json:"description,omitempty"`
Content *string `url:"content,omitempty" json:"content,omitempty"`
Visibility *VisibilityValue `url:"visibility,omitempty" json:"visibility,omitempty"`
Files []*SnippetFile `url:"files,omitempty" json:"files,omitempty"`
}

// CreateSnippet creates a new snippet. The user must have permission
Expand Down

0 comments on commit 65aab58

Please sign in to comment.