Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
go-gitlab
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
go-gitlab
Commits
b3ac6922
Commit
b3ac6922
authored
Sep 22, 2017
by
Sander van Harmelen
Committed by
GitHub
Sep 22, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #215 from xanzy/longkey1
Refactor PR #204
parents
d6ed0f17
1a2af71a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
105 additions
and
0 deletions
+105
-0
projects.go
projects.go
+63
-0
projects_test.go
projects_test.go
+42
-0
No files found.
projects.go
View file @
b3ac6922
...
...
@@ -17,8 +17,13 @@
package
gitlab
import
(
"bytes"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/url"
"os"
"time"
)
...
...
@@ -800,3 +805,61 @@ func (s *ProjectsService) DeleteProjectForkRelation(pid int, options ...OptionFu
return
s
.
client
.
Do
(
req
,
nil
)
}
// ProjectFile represents an uploaded project file
//
// GitLab API docs: https://docs.gitlab.com/ce/api/projects.html#upload-a-file
type
ProjectFile
struct
{
Alt
string
`json:"alt"`
URL
string
`json:"url"`
Markdown
string
`json:"markdown"`
}
// UploadFile upload a file from disk
//
// GitLab API docs: https://docs.gitlab.com/ce/api/projects.html#upload-a-file
func
(
s
*
ProjectsService
)
UploadFile
(
pid
interface
{},
file
string
,
options
...
OptionFunc
)
(
*
ProjectFile
,
*
Response
,
error
)
{
project
,
err
:=
parseID
(
pid
)
if
err
!=
nil
{
return
nil
,
nil
,
err
}
u
:=
fmt
.
Sprintf
(
"projects/%s/uploads"
,
url
.
QueryEscape
(
project
))
f
,
err
:=
os
.
Open
(
file
)
if
err
!=
nil
{
return
nil
,
nil
,
err
}
defer
f
.
Close
()
b
:=
&
bytes
.
Buffer
{}
w
:=
multipart
.
NewWriter
(
b
)
fw
,
err
:=
w
.
CreateFormFile
(
"file"
,
file
)
if
err
!=
nil
{
return
nil
,
nil
,
err
}
_
,
err
=
io
.
Copy
(
fw
,
f
)
if
err
!=
nil
{
return
nil
,
nil
,
err
}
w
.
Close
()
req
,
err
:=
s
.
client
.
NewRequest
(
""
,
u
,
nil
,
options
)
if
err
!=
nil
{
return
nil
,
nil
,
err
}
req
.
Body
=
ioutil
.
NopCloser
(
b
)
req
.
ContentLength
=
int64
(
b
.
Len
())
req
.
Header
.
Set
(
"Content-Type"
,
w
.
FormDataContentType
())
req
.
Method
=
"POST"
uf
:=
&
ProjectFile
{}
resp
,
err
:=
s
.
client
.
Do
(
req
,
uf
)
if
err
!=
nil
{
return
nil
,
resp
,
err
}
return
uf
,
resp
,
nil
}
projects_test.go
View file @
b3ac6922
...
...
@@ -2,8 +2,11 @@ package gitlab
import
(
"fmt"
"io/ioutil"
"net/http"
"os"
"reflect"
"strings"
"testing"
)
...
...
@@ -161,3 +164,42 @@ func TestCreateProject(t *testing.T) {
t
.
Errorf
(
"Projects.CreateProject returned %+v, want %+v"
,
project
,
want
)
}
}
func
TestUploadFile
(
t
*
testing
.
T
)
{
mux
,
server
,
client
:=
setup
()
defer
teardown
(
server
)
tf
,
_
:=
ioutil
.
TempFile
(
os
.
TempDir
(),
"test"
)
defer
os
.
Remove
(
tf
.
Name
())
mux
.
HandleFunc
(
"/projects/1/uploads"
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
testMethod
(
t
,
r
,
http
.
MethodPost
)
if
false
==
strings
.
Contains
(
r
.
Header
.
Get
(
"Content-Type"
),
"multipart/form-data;"
)
{
t
.
Fatalf
(
"Prokects.UploadFile request content-type %+v want multipart/form-data;"
,
r
.
Header
.
Get
(
"Content-Type"
))
}
if
r
.
ContentLength
==
-
1
{
t
.
Fatalf
(
"Prokects.UploadFile request content-length is -1"
)
}
fmt
.
Fprint
(
w
,
`{
"alt": "dk",
"url": "/uploads/66dbcd21ec5d24ed6ea225176098d52b/dk.md",
"markdown": "![dk](/uploads/66dbcd21ec5d24ed6ea225176098d52b/dk.png)"
}`
)
})
want
:=
&
ProjectFile
{
Alt
:
"dk"
,
URL
:
"/uploads/66dbcd21ec5d24ed6ea225176098d52b/dk.md"
,
Markdown
:
"![dk](/uploads/66dbcd21ec5d24ed6ea225176098d52b/dk.png)"
,
}
file
,
_
,
err
:=
client
.
Projects
.
UploadFile
(
1
,
tf
.
Name
())
if
err
!=
nil
{
t
.
Fatalf
(
"Prokects.UploadFile returns an error: %v"
,
err
)
}
if
!
reflect
.
DeepEqual
(
want
,
file
)
{
t
.
Errorf
(
"Prokects.UploadFile returned %+v, want %+v"
,
file
,
want
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment