-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add support for API blob upload of release attachments #29507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
cb6abf5
eedbb86
b90891b
9756bf3
4aab532
423acf8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -262,24 +262,60 @@ func TestAPIUploadAssetRelease(t *testing.T) { | |||||||
|
||||||||
filename := "image.png" | ||||||||
buff := generateImg() | ||||||||
body := &bytes.Buffer{} | ||||||||
|
||||||||
writer := multipart.NewWriter(body) | ||||||||
part, err := writer.CreateFormFile("attachment", filename) | ||||||||
assert.NoError(t, err) | ||||||||
_, err = io.Copy(part, &buff) | ||||||||
assert.NoError(t, err) | ||||||||
err = writer.Close() | ||||||||
assert.NoError(t, err) | ||||||||
assetURL := fmt.Sprintf("/api/v1/repos/%s/%s/releases/%d/assets", owner.Name, repo.Name, r.ID) | ||||||||
|
||||||||
req := NewRequestWithBody(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/releases/%d/assets?name=test-asset", owner.Name, repo.Name, r.ID), body). | ||||||||
AddTokenAuth(token) | ||||||||
req.Header.Add("Content-Type", writer.FormDataContentType()) | ||||||||
resp := MakeRequest(t, req, http.StatusCreated) | ||||||||
t.Run("multipart/form-data", func(t *testing.T) { | ||||||||
defer tests.PrintCurrentTest(t)() | ||||||||
|
||||||||
body := &bytes.Buffer{} | ||||||||
|
||||||||
writer := multipart.NewWriter(body) | ||||||||
part, err := writer.CreateFormFile("attachment", filename) | ||||||||
assert.NoError(t, err) | ||||||||
_, err = io.Copy(part, bytes.NewReader(buff.Bytes())) | ||||||||
assert.NoError(t, err) | ||||||||
err = writer.Close() | ||||||||
assert.NoError(t, err) | ||||||||
|
||||||||
req := NewRequestWithBody(t, http.MethodPost, assetURL, bytes.NewReader(body.Bytes())). | ||||||||
AddTokenAuth(token). | ||||||||
SetHeader("Content-Type", writer.FormDataContentType()) | ||||||||
resp := MakeRequest(t, req, http.StatusCreated) | ||||||||
|
||||||||
var attachment *api.Attachment | ||||||||
DecodeJSON(t, resp, &attachment) | ||||||||
|
||||||||
assert.EqualValues(t, filename, attachment.Name) | ||||||||
assert.EqualValues(t, 104, attachment.Size) | ||||||||
|
||||||||
req = NewRequestWithBody(t, http.MethodPost, assetURL+"?name=test-asset", bytes.NewReader(body.Bytes())). | ||||||||
AddTokenAuth(token). | ||||||||
SetHeader("Content-Type", writer.FormDataContentType()) | ||||||||
resp = MakeRequest(t, req, http.StatusCreated) | ||||||||
|
||||||||
var attachment2 *api.Attachment | ||||||||
DecodeJSON(t, resp, &attachment2) | ||||||||
|
||||||||
assert.EqualValues(t, "test-asset", attachment2.Name) | ||||||||
assert.EqualValues(t, 104, attachment2.Size) | ||||||||
}) | ||||||||
|
||||||||
t.Run("application/octet-stream", func(t *testing.T) { | ||||||||
defer tests.PrintCurrentTest(t)() | ||||||||
|
||||||||
req := NewRequestWithBody(t, http.MethodPost, assetURL, bytes.NewReader(buff.Bytes())). | ||||||||
AddTokenAuth(token) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Just so we test what is specced. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That's not specced. The Github docs state that you must pass the content type of the attachment. We do not use the content type, therefore There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe adding a check is better. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, we accept any content-type except multipart, so it's fine. I suppose GitHub will error when content-type is absent, but we can do that, I see no drawback. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BTW, with openapi 3.0 or higher, one could do a oneOf check on We should eventually migrate to ideally openapi 3.1, but it will require a new validator module. |
||||||||
MakeRequest(t, req, http.StatusBadRequest) | ||||||||
|
||||||||
req = NewRequestWithBody(t, http.MethodPost, assetURL+"?name=stream.bin", bytes.NewReader(buff.Bytes())). | ||||||||
AddTokenAuth(token) | ||||||||
resp := MakeRequest(t, req, http.StatusCreated) | ||||||||
|
||||||||
var attachment *api.Attachment | ||||||||
DecodeJSON(t, resp, &attachment) | ||||||||
var attachment *api.Attachment | ||||||||
DecodeJSON(t, resp, &attachment) | ||||||||
|
||||||||
assert.EqualValues(t, "test-asset", attachment.Name) | ||||||||
assert.EqualValues(t, 104, attachment.Size) | ||||||||
assert.EqualValues(t, "stream.bin", attachment.Name) | ||||||||
assert.EqualValues(t, 104, attachment.Size) | ||||||||
}) | ||||||||
} |
Uh oh!
There was an error while loading. Please reload this page.