Skip to content

Commit 0ab5b99

Browse files
Merge pull request #3 from Dalee/v4-new-support
v3 and v4:legacy has same method for accessing files
2 parents 65db43e + c5f6189 commit 0ab5b99

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

pkg/client/gitlab/client_base.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,37 @@ func (c *Client) GetArchive(project *Project, ref string) ([]byte, error) {
107107
// for v4 file_path is not a parameter but part of URI. Should be encoded anyway.
108108
// update, right now this one doesn't seem's to work.
109109
//
110-
// KEEP EYE ON THIS METHOD, v4 right now doesn't work as expected.
110+
// GitLab < v9.4.2: v4 method doesn't work as documented, uses v3 signature.
111+
// GitLab >= v9.4.2: v4 work as documented.
112+
//
113+
// To maintain compatibility between all v3, v4-pre and v4 versions,
114+
// one extra HEAD request should be executed.
111115
//
112116
func (c *Client) GetFile(project *Project, path, ref string) ([]byte, error) {
117+
var endpoint string
113118

114-
endpoint := fmt.Sprintf(
119+
// v3 and v4:legacy method for accessing files
120+
endpoint = fmt.Sprintf(
115121
"projects/%d/repository/files?file_path=%s&ref=%s",
116122
project.ID,
117123
url.QueryEscape(path),
118124
url.QueryEscape(ref),
119125
)
126+
127+
if c.HasV4Support {
128+
// check broken v4 api
129+
r, _ := c.executeHead(endpoint)
130+
if r.StatusCode() != 200 {
131+
// ok, gitlab has correct v4 support
132+
endpoint = fmt.Sprintf(
133+
"projects/%d/repository/files/%s?ref=%s",
134+
project.ID,
135+
url.QueryEscape(path),
136+
url.QueryEscape(ref),
137+
)
138+
}
139+
}
140+
120141
pageList, err := c.executeAPIMethod(endpoint)
121142
if err != nil {
122143
return nil, err

0 commit comments

Comments
 (0)