|
| 1 | +// |
| 2 | +// Copyright 2024, Sander van Harmelen |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +package gitlab |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "fmt" |
| 22 | + "io" |
| 23 | + "net/http" |
| 24 | + "time" |
| 25 | +) |
| 26 | + |
| 27 | +// ProjectMarkdownUploadsService handles communication with the project markdown uploads |
| 28 | +// related methods of the GitLab API. |
| 29 | +// |
| 30 | +// Gitlab API docs: https://docs.gitlab.com/ee/api/project_markdown_uploads.html |
| 31 | +type ProjectMarkdownUploadsService struct { |
| 32 | + client *Client |
| 33 | +} |
| 34 | + |
| 35 | +// ProjectMarkdownUploadedFile represents a single project markdown uploaded file. |
| 36 | +// |
| 37 | +// Gitlab API docs: https://docs.gitlab.com/ee/api/project_markdown_uploads.html |
| 38 | +type ProjectMarkdownUploadedFile struct { |
| 39 | + ID int `json:"id"` |
| 40 | + Alt string `json:"alt"` |
| 41 | + URL string `json:"url"` |
| 42 | + FullPath string `json:"full_path"` |
| 43 | + Markdown string `json:"markdown"` |
| 44 | +} |
| 45 | + |
| 46 | +// ProjectMarkdownUpload represents a single project markdown upload. |
| 47 | +// |
| 48 | +// Gitlab API docs: https://docs.gitlab.com/ee/api/project_markdown_uploads.html |
| 49 | +type ProjectMarkdownUpload struct { |
| 50 | + ID int `json:"id"` |
| 51 | + Size int `json:"size"` |
| 52 | + Filename string `json:"filename"` |
| 53 | + CreatedAt *time.Time `json:"created_at"` |
| 54 | + UploadedBy *User `json:"uploaded_by"` |
| 55 | +} |
| 56 | + |
| 57 | +// Gets a string representation of a ProjectMarkdownUpload. |
| 58 | +// |
| 59 | +// GitLab API docs: https://docs.gitlab.com/ee/api/project_markdown_uploads.html |
| 60 | +func (m ProjectMarkdownUpload) String() string { |
| 61 | + return Stringify(m) |
| 62 | +} |
| 63 | + |
| 64 | +// UploadProjectMarkdown uploads a markdown file to a project. |
| 65 | +// |
| 66 | +// GitLab docs: |
| 67 | +// https://docs.gitlab.com/ee/api/project_markdown_uploads.html#upload-a-file |
| 68 | +func (s *ProjectMarkdownUploadsService) UploadProjectMarkdown(pid interface{}, content io.Reader, options ...RequestOptionFunc) (*ProjectMarkdownUploadedFile, *Response, error) { |
| 69 | + project, err := parseID(pid) |
| 70 | + if err != nil { |
| 71 | + return nil, nil, err |
| 72 | + } |
| 73 | + u := fmt.Sprintf("projects/%s/uploads", PathEscape(project)) |
| 74 | + |
| 75 | + // We need to create the request as a GET request to make sure the options |
| 76 | + // are set correctly. After the request is created we will overwrite both |
| 77 | + // the method and the body. |
| 78 | + req, err := s.client.NewRequest(http.MethodPost, u, nil, options) |
| 79 | + if err != nil { |
| 80 | + return nil, nil, err |
| 81 | + } |
| 82 | + |
| 83 | + // Overwrite the method and body. |
| 84 | + req.Method = http.MethodPost |
| 85 | + req.SetBody(content) |
| 86 | + |
| 87 | + f := new(ProjectMarkdownUploadedFile) |
| 88 | + resp, err := s.client.Do(req, f) |
| 89 | + if err != nil { |
| 90 | + return nil, resp, err |
| 91 | + } |
| 92 | + |
| 93 | + return f, resp, nil |
| 94 | +} |
| 95 | + |
| 96 | +// ListProjectMarkdownUploads gets all markdown uploads for a project. |
| 97 | +// |
| 98 | +// GitLab API Docs: |
| 99 | +// https://docs.gitlab.com/ee/api/project_markdown_uploads.html#list-uploads |
| 100 | +func (s *ProjectMarkdownUploadsService) ListProjectMarkdownUploads(pid interface{}, options ...RequestOptionFunc) ([]*ProjectMarkdownUpload, *Response, error) { |
| 101 | + project, err := parseID(pid) |
| 102 | + if err != nil { |
| 103 | + return nil, nil, err |
| 104 | + } |
| 105 | + u := fmt.Sprintf("projects/%s/uploads", PathEscape(project)) |
| 106 | + |
| 107 | + req, err := s.client.NewRequest(http.MethodGet, u, nil, options) |
| 108 | + if err != nil { |
| 109 | + return nil, nil, err |
| 110 | + } |
| 111 | + |
| 112 | + var uploads []*ProjectMarkdownUpload |
| 113 | + resp, err := s.client.Do(req, &uploads) |
| 114 | + if err != nil { |
| 115 | + return nil, resp, err |
| 116 | + } |
| 117 | + |
| 118 | + return uploads, resp, err |
| 119 | +} |
| 120 | + |
| 121 | +// DownloadProjectMarkdownUploadByID downloads a specific upload by ID. |
| 122 | +// |
| 123 | +// GitLab API Docs: |
| 124 | +// https://docs.gitlab.com/ee/api/project_markdown_uploads.html#download-an-uploaded-file-by-id |
| 125 | +func (s *ProjectMarkdownUploadsService) DownloadProjectMarkdownUploadByID(pid interface{}, uploadID int, options ...RequestOptionFunc) ([]byte, *Response, error) { |
| 126 | + project, err := parseID(pid) |
| 127 | + if err != nil { |
| 128 | + return nil, nil, err |
| 129 | + } |
| 130 | + u := fmt.Sprintf("projects/%s/uploads/%d", PathEscape(project), uploadID) |
| 131 | + |
| 132 | + req, err := s.client.NewRequest(http.MethodGet, u, nil, options) |
| 133 | + if err != nil { |
| 134 | + return nil, nil, err |
| 135 | + } |
| 136 | + |
| 137 | + var f bytes.Buffer |
| 138 | + resp, err := s.client.Do(req, &f) |
| 139 | + if err != nil { |
| 140 | + return nil, resp, err |
| 141 | + } |
| 142 | + |
| 143 | + return f.Bytes(), resp, err |
| 144 | +} |
| 145 | + |
| 146 | +// DownloadProjectMarkdownUploadBySecretAndFilename downloads a specific upload |
| 147 | +// by secret and filename. |
| 148 | +// |
| 149 | +// GitLab API Docs: |
| 150 | +// https://docs.gitlab.com/ee/api/project_markdown_uploads.html#download-an-uploaded-file-by-secret-and-filename |
| 151 | +func (s *ProjectMarkdownUploadsService) DownloadProjectMarkdownUploadBySecretAndFilename(pid interface{}, secret string, filename string, options ...RequestOptionFunc) ([]byte, *Response, error) { |
| 152 | + project, err := parseID(pid) |
| 153 | + if err != nil { |
| 154 | + return nil, nil, err |
| 155 | + } |
| 156 | + u := fmt.Sprintf("projects/%s/uploads/%s/%s", PathEscape(project), PathEscape(secret), PathEscape(filename)) |
| 157 | + |
| 158 | + req, err := s.client.NewRequest(http.MethodGet, u, nil, options) |
| 159 | + if err != nil { |
| 160 | + return nil, nil, err |
| 161 | + } |
| 162 | + |
| 163 | + var f bytes.Buffer |
| 164 | + resp, err := s.client.Do(req, &f) |
| 165 | + if err != nil { |
| 166 | + return nil, resp, err |
| 167 | + } |
| 168 | + |
| 169 | + return f.Bytes(), resp, err |
| 170 | +} |
| 171 | + |
| 172 | +// DeleteProjectMarkdownUploadByID deletes an upload by ID. |
| 173 | +// |
| 174 | +// GitLab API Docs: |
| 175 | +// https://docs.gitlab.com/ee/api/project_markdown_uploads.html#delete-an-uploaded-file-by-id |
| 176 | +func (s *ProjectMarkdownUploadsService) DeleteProjectMarkdownUploadByID(pid interface{}, uploadID int, options ...RequestOptionFunc) (*Response, error) { |
| 177 | + project, err := parseID(pid) |
| 178 | + if err != nil { |
| 179 | + return nil, err |
| 180 | + } |
| 181 | + u := fmt.Sprintf("projects/%s/uploads/%d", PathEscape(project), uploadID) |
| 182 | + |
| 183 | + req, err := s.client.NewRequest(http.MethodDelete, u, nil, options) |
| 184 | + if err != nil { |
| 185 | + return nil, err |
| 186 | + } |
| 187 | + |
| 188 | + return s.client.Do(req, nil) |
| 189 | +} |
| 190 | + |
| 191 | +// DeleteProjectMarkdownUploadBySecretAndFilename deletes an upload |
| 192 | +// by secret and filename. |
| 193 | +// |
| 194 | +// GitLab API Docs: |
| 195 | +// https://docs.gitlab.com/ee/api/project_markdown_uploads.html#delete-an-uploaded-file-by-secret-and-filename |
| 196 | +func (s *ProjectMarkdownUploadsService) DeleteProjectMarkdownUploadBySecretAndFilename(pid interface{}, secret string, filename string, options ...RequestOptionFunc) (*Response, error) { |
| 197 | + project, err := parseID(pid) |
| 198 | + if err != nil { |
| 199 | + return nil, err |
| 200 | + } |
| 201 | + u := fmt.Sprintf("projects/%s/uploads/%s/%s", |
| 202 | + PathEscape(project), PathEscape(secret), PathEscape(filename)) |
| 203 | + |
| 204 | + req, err := s.client.NewRequest(http.MethodDelete, u, nil, options) |
| 205 | + if err != nil { |
| 206 | + return nil, err |
| 207 | + } |
| 208 | + |
| 209 | + return s.client.Do(req, nil) |
| 210 | +} |
0 commit comments