Skip to content
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

[PLANG-1212] Add content_type parameter for POST artifact.json API call #155

Merged
merged 14 commits into from
Apr 11, 2022
Merged
5 changes: 3 additions & 2 deletions uploaders/aabuploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,13 @@ func DeployAAB(pth string, artifacts []string, buildURL, token, bundletoolVersio

// ---

uploadURL, artifactID, err := createArtifact(buildURL, token, pth, "android-apk")
const AABContentType = "application/octet-stream aab"
uploadURL, artifactID, err := createArtifact(buildURL, token, pth, "android-apk", AABContentType)
if err != nil {
return ArtifactURLs{}, fmt.Errorf("failed to create apk artifact, error: %s", err)
}

if err := uploadArtifact(uploadURL, pth, "application/octet-stream aab"); err != nil {
if err := uploadArtifact(uploadURL, pth, AABContentType); err != nil {
return ArtifactURLs{}, fmt.Errorf("failed to upload apk artifact, error: %s", err)
}
artifactURLs, err := finishArtifact(buildURL, token, artifactID, string(artifactInfoBytes), "", "", "false")
Expand Down
5 changes: 3 additions & 2 deletions uploaders/apkuploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@ func DeployAPK(pth string, artifacts []string, buildURL, token, notifyUserGroups

// ---

uploadURL, artifactID, err := createArtifact(buildURL, token, pth, "android-apk")
const APKContentType = "application/vnd.android.package-archive"
uploadURL, artifactID, err := createArtifact(buildURL, token, pth, "android-apk", APKContentType)
if err != nil {
return ArtifactURLs{}, fmt.Errorf("failed to create apk artifact, error: %s", err)
}

if err := uploadArtifact(uploadURL, pth, "application/vnd.android.package-archive"); err != nil {
if err := uploadArtifact(uploadURL, pth, APKContentType); err != nil {
return ArtifactURLs{}, fmt.Errorf("failed to upload apk artifact, error: %s", err)
}

Expand Down
6 changes: 4 additions & 2 deletions uploaders/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"

Expand All @@ -24,7 +25,7 @@ type ArtifactURLs struct {
PermanentDownloadURL string
}

func createArtifact(buildURL, token, artifactPth, artifactType string) (string, string, error) {
func createArtifact(buildURL, token, artifactPth, artifactType, contentType string) (string, string, error) {
log.Printf("creating artifact")

// create form data
Expand All @@ -49,6 +50,7 @@ func createArtifact(buildURL, token, artifactPth, artifactType string) (string,
"filename": {artifactName},
"artifact_type": {artifactType},
"file_size_bytes": {fmt.Sprintf("%d", int(fileSize))},
"content_type": {contentType},
}
// ---

Expand Down Expand Up @@ -132,7 +134,6 @@ func uploadArtifact(uploadURL, artifactPth, contentType string) error {
}
}()

// Set Content Length manually (https://stackoverflow.com/a/39764726), as it is part of signature in signed URL
fileInfo, err := file.Stat()
if err != nil {
return fmt.Errorf("failed to get file info for %s, error: %s", artifactPth, err)
Expand All @@ -153,6 +154,7 @@ func uploadArtifact(uploadURL, artifactPth, contentType string) error {
request.Header.Add("Content-Type", contentType)
}

request.Header.Add("X-Upload-Content-Length", strconv.FormatInt(fileInfo.Size(), 10)) // header used by Google Cloud Storage signed URLs
request.ContentLength = fileInfo.Size()

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
Expand Down
5 changes: 5 additions & 0 deletions uploaders/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"net/http/httptest"
"path/filepath"
"strconv"
"testing"
)

Expand Down Expand Up @@ -56,6 +57,10 @@ func Test_uploadArtifact(t *testing.T) {
t.Fatalf("httptest: Content-length got %d want %d", r.ContentLength, wantFileSize)
}

if r.Header.Get("X-Upload-Content-Length") != strconv.FormatInt(wantFileSize, 10) {
t.Fatalf("httptest: X-Upload-Content-Length got %s want %d", r.Header.Get("X-Upload-Content-Length"), wantFileSize)
}

if r.Header.Get("Content-Type") != contentType {
t.Fatalf("httptest: content type got: %s want: %s", r.Header.Get("Content-Type"), contentType)
}
Expand Down
2 changes: 1 addition & 1 deletion uploaders/fileuploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "fmt"

// DeployFile ...
func DeployFile(pth, buildURL, token string) (ArtifactURLs, error) {
uploadURL, artifactID, err := createArtifact(buildURL, token, pth, "file")
uploadURL, artifactID, err := createArtifact(buildURL, token, pth, "file", "")
if err != nil {
return ArtifactURLs{}, fmt.Errorf("failed to create file artifact, error: %s", err)
}
Expand Down
5 changes: 3 additions & 2 deletions uploaders/ipauploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,13 @@ func DeployIPA(pth, buildURL, token, notifyUserGroups, notifyEmails, isEnablePub

// ---

uploadURL, artifactID, err := createArtifact(buildURL, token, pth, "ios-ipa")
const IPAContentType = "application/octet-stream ipa"
uploadURL, artifactID, err := createArtifact(buildURL, token, pth, "ios-ipa", IPAContentType)
if err != nil {
return ArtifactURLs{}, fmt.Errorf("failed to create ipa artifact, error: %s", err)
}

if err := uploadArtifact(uploadURL, pth, "application/octet-stream ipa"); err != nil {
if err := uploadArtifact(uploadURL, pth, IPAContentType); err != nil {
return ArtifactURLs{}, fmt.Errorf("failed to upload ipa artifact, error: %s", err)
}

Expand Down
2 changes: 1 addition & 1 deletion uploaders/xcarchiveuploader.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func DeployXcarchive(pth, buildURL, token string) (ArtifactURLs, error) {

log.Printf(" xcarchive infos: %v", appInfo)

uploadURL, artifactID, err := createArtifact(buildURL, token, pth, "ios-xcarchive")
uploadURL, artifactID, err := createArtifact(buildURL, token, pth, "ios-xcarchive", "")
if err != nil {
return ArtifactURLs{}, fmt.Errorf("failed to create xcarchive artifact, error: %s", err)
}
Expand Down