Skip to content

Commit

Permalink
use retryable http client (#22)
Browse files Browse the repository at this point in the history
* use retryable http client

* Update bundletool.go
  • Loading branch information
godrei authored Aug 28, 2023
1 parent a18b0a5 commit 5455537
Show file tree
Hide file tree
Showing 44 changed files with 2,958 additions and 229 deletions.
13 changes: 7 additions & 6 deletions bundletool/bundletool.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ type KeystoreConfig struct {
}

const (
githubReleaseBaseURL = "https://github.com/google/bundletool/releases/download"
// GithubReleaseBaseURL ...
GithubReleaseBaseURL = "https://github.com/google/bundletool/releases/download"
bundletoolAllJarName = "bundletool-all.jar"
)

Expand All @@ -43,15 +44,15 @@ type FileDownloader interface {
}

// New downloads the bundletool executable from Github and places it to a temporary path.
func New(version string, downloader FileDownloader) (*Tool, error) {
func New(version string, downloader FileDownloader, baseURL string) (*Tool, error) {
tmpPth, err := pathutil.NormalizedOSTempDirPath("tool")
if err != nil {
return nil, err
}

toolPath := filepath.Join(tmpPth, bundletoolAllJarName)

sources, err := sources(version)
sources, err := sources(version, baseURL)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -88,14 +89,14 @@ func (tool Tool) BuildAPKs(aabPath, apksPath string, keystoreCfg *KeystoreConfig
return tool.BuildCommand("build-apks", args...)
}

func sources(version string) ([]string, error) {
func sources(version, baseURL string) ([]string, error) {
urls := []string{}
url, err := urlutil.Join(githubReleaseBaseURL, version, "bundletool-all-"+version+".jar")
url, err := urlutil.Join(baseURL, version, "bundletool-all-"+version+".jar")
if err != nil {
return nil, err
}
urls = append(urls, url)
url, err = urlutil.Join(githubReleaseBaseURL, version, bundletoolAllJarName)
url, err = urlutil.Join(baseURL, version, bundletoolAllJarName)
if err != nil {
return nil, err
}
Expand Down
35 changes: 32 additions & 3 deletions bundletool/bundletool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,42 @@ package bundletool

import (
"errors"
"net/http"
"net/http/httptest"
"os"
"testing"

logv2 "github.com/bitrise-io/go-utils/v2/log"
"github.com/bitrise-io/go-utils/v2/retryhttp"
"github.com/bitrise-steplib/bitrise-step-export-universal-apk/filedownloader"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

func Test_Bundletool_Download(t *testing.T) {
bundletoolversion := "1.15.4"
downloader := filedownloader.New(retryhttp.NewClient(logv2.NewLogger()))

reqNum := 0
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/1.15.4/bundletool-all-1.15.4.jar" {
if reqNum == 0 {
w.WriteHeader(http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
}
reqNum++
} else {
w.WriteHeader(http.StatusBadRequest)
}

}))
defer svr.Close()

_, err := New(bundletoolversion, downloader, svr.URL)
require.NoError(t, err)
}

func Test_BuildCommand(t *testing.T) {
// Given
tool := givenTool()
Expand Down Expand Up @@ -63,7 +92,7 @@ func Test_sources(t *testing.T) {
}

// When
actualSources, err := sources(version)
actualSources, err := sources(version, GithubReleaseBaseURL)

//Then
require.NoError(t, err)
Expand All @@ -76,7 +105,7 @@ func Test_New_Success(t *testing.T) {
mockedFileDownloader.On("GetWithFallback", mock.Anything, mock.Anything, mock.Anything).Return(nil)

// When
tool, err := New("0.2.0", mockedFileDownloader)
tool, err := New("0.2.0", mockedFileDownloader, GithubReleaseBaseURL)

// Then
require.NoError(t, err)
Expand All @@ -90,7 +119,7 @@ func Test_New_Fail(t *testing.T) {
mockedFileDownloader.On("GetWithFallback", mock.Anything, mock.Anything, mock.Anything).Return(expectedError)

// When
tool, actualError := New("0.2.0", mockedFileDownloader)
tool, actualError := New("0.2.0", mockedFileDownloader, GithubReleaseBaseURL)

// Then
require.Equal(t, expectedError, actualError)
Expand Down
14 changes: 12 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
module github.com/bitrise-steplib/bitrise-step-export-universal-apk

go 1.16
go 1.20

require (
github.com/bitrise-io/go-steputils v0.0.0-20210514150206-5b6261447e77
github.com/bitrise-io/go-utils v0.0.0-20210517140706-aa64fd88ca49
github.com/stretchr/testify v1.7.0
github.com/bitrise-io/go-utils/v2 v2.0.0-alpha.19
github.com/stretchr/testify v1.8.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
19 changes: 16 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,34 @@ github.com/bitrise-io/go-steputils v0.0.0-20210514150206-5b6261447e77/go.mod h1:
github.com/bitrise-io/go-utils v0.0.0-20210507100250-37de47dfa6ce/go.mod h1:15EZZf02noI5nWFqXMZEoyb1CyqYRXTMz5Fyu4CWFzI=
github.com/bitrise-io/go-utils v0.0.0-20210517140706-aa64fd88ca49 h1:du0kI1TU7cv/Hr8U3plyAlMtyOf2kUj4CA4ku5bz20Y=
github.com/bitrise-io/go-utils v0.0.0-20210517140706-aa64fd88ca49/go.mod h1:DRx7oFuAqk0dbKpAKCqWl0TgrowfJUb/MqYPRscxJOQ=
github.com/bitrise-io/go-utils/v2 v2.0.0-alpha.19 h1:55as5Iv0N4btuRP3YwRzN+BCMtKO210MnJ8mpxmeI7o=
github.com/bitrise-io/go-utils/v2 v2.0.0-alpha.19/go.mod h1:Laih4ji980SQkRgdnMCH0g4u2GZI/5nnbqmYT9UfKFQ=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v0.9.2 h1:CG6TE5H9/JXsFWJCfoIVpKFIkFe6ysEuHirp4DxCsHI=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-retryablehttp v0.6.6/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/hashicorp/go-retryablehttp v0.7.4 h1:ZQgVdpTdAL7WpMIwLzCfbalOcSUdkDZnpUv3/+BxzFA=
github.com/hashicorp/go-retryablehttp v0.7.4/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As=
github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200220183623-bac4c82f6975/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
Expand All @@ -41,5 +53,6 @@ gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
10 changes: 6 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package main

import (
"fmt"
"net/http"
"os"
"strings"

"github.com/bitrise-io/go-steputils/stepconf"
"github.com/bitrise-io/go-steputils/tools"
"github.com/bitrise-io/go-utils/log"
logv2 "github.com/bitrise-io/go-utils/v2/log"
"github.com/bitrise-io/go-utils/v2/retryhttp"
"github.com/bitrise-steplib/bitrise-step-export-universal-apk/apkexporter"
"github.com/bitrise-steplib/bitrise-step-export-universal-apk/bundletool"
"github.com/bitrise-steplib/bitrise-step-export-universal-apk/filedownloader"
"github.com/bitrise-io/go-steputils/stepconf"
)

// Config is defining the input arguments required by the Step.
Expand All @@ -33,13 +34,14 @@ func main() {
stepconf.Print(config)
fmt.Println()

bundletoolTool, err := bundletool.New(config.BundletoolVersion, filedownloader.New(http.DefaultClient))
httpClient := retryhttp.NewClient(logv2.NewLogger())
bundletoolTool, err := bundletool.New(config.BundletoolVersion, filedownloader.New(httpClient), bundletool.GithubReleaseBaseURL)
if err != nil {
failf("Failed to initialize bundletool: %s \n", err)
}
log.Infof("bundletool path created at: %s", bundletoolTool.Path())

exporter := apkexporter.New(bundletoolTool, filedownloader.New(http.DefaultClient))
exporter := apkexporter.New(bundletoolTool, filedownloader.New(httpClient))
keystoreCfg := parseKeystoreConfig(config)
apkPath, err := exporter.ExportUniversalAPK(config.AABPath, config.DeployDir, keystoreCfg)
if err != nil {
Expand Down
22 changes: 22 additions & 0 deletions vendor/github.com/bitrise-io/go-utils/v2/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5455537

Please sign in to comment.