diff --git a/config.go b/config.go index d4d79dc4..1cd87d53 100755 --- a/config.go +++ b/config.go @@ -23,6 +23,7 @@ type Configs struct { WhatsnewsDir string `env:"whatsnews_dir"` MappingFile string `env:"mapping_file"` ReleaseName string `env:"release_name"` + Status string `env:"status"` } // validate validates the Configs. diff --git a/main.go b/main.go index a93b03bd..87d4e0f2 100755 --- a/main.go +++ b/main.go @@ -86,7 +86,7 @@ func uploadApplications(configs Configs, service *androidpublisher.Service, appE func updateTracks(configs Configs, service *androidpublisher.Service, appEdit *androidpublisher.AppEdit, versionCodes []int64) error { editsTracksService := androidpublisher.NewEditsTracksService(service) - newRelease, err := createTrackRelease(configs.WhatsnewsDir, versionCodes, configs.UserFraction, configs.UpdatePriority, configs.ReleaseName) + newRelease, err := createTrackRelease(configs, versionCodes) if err != nil { return err } diff --git a/publish.go b/publish.go index a849ae6b..8653a314 100755 --- a/publish.go +++ b/publish.go @@ -16,6 +16,8 @@ import ( const ( releaseStatusCompleted = "completed" releaseStatusInProgress = "inProgress" + releaseStatusDraft = "draft" + releaseStatusHalted = "halted" ) // uploadExpansionFiles uploads the expansion files for given applications, like .obb files. @@ -187,24 +189,27 @@ func readLocalisedRecentChanges(recentChangesDir string) (map[string]string, err } // createTrackRelease returns a release object with the given version codes and adds the listing information. -func createTrackRelease(whatsNewsDir string, versionCodes googleapi.Int64s, userFraction float64, updatePriority int, releaseName string) (*androidpublisher.TrackRelease, error) { - status := releaseStatusFromConfig(userFraction) - +func createTrackRelease(config Configs, versionCodes googleapi.Int64s) (*androidpublisher.TrackRelease, error) { newRelease := &androidpublisher.TrackRelease{ VersionCodes: versionCodes, - Status: status, - InAppUpdatePriority: int64(updatePriority), + Status: config.Status, + InAppUpdatePriority: int64(config.UpdatePriority), } log.Infof("Release version codes are: %v", newRelease.VersionCodes) - if userFraction != 0 { - newRelease.UserFraction = userFraction + + if newRelease.Status == "" { + newRelease.Status = releaseStatusFromConfig(config.UserFraction) } - if releaseName != "" { - newRelease.Name = releaseName + if shouldApplyUserFraction(newRelease.Status) { + newRelease.UserFraction = config.UserFraction } - if err := updateListing(whatsNewsDir, newRelease); err != nil { + if config.ReleaseName != "" { + newRelease.Name = config.ReleaseName + } + + if err := updateListing(config.WhatsnewsDir, newRelease); err != nil { return nil, fmt.Errorf("failed to update listing, reason: %v", err) } @@ -219,3 +224,7 @@ func releaseStatusFromConfig(userFraction float64) string { } return releaseStatusCompleted } + +func shouldApplyUserFraction(status string) bool { + return status == releaseStatusInProgress || status == releaseStatusHalted +} diff --git a/publish_test.go b/publish_test.go index b4f600d5..e4ad600c 100644 --- a/publish_test.go +++ b/publish_test.go @@ -6,8 +6,74 @@ import ( "path/filepath" "reflect" "testing" + + "github.com/stretchr/testify/assert" ) +func Test_verifyStatusOfTheCreatedRelease(t *testing.T) { + tests := []struct { + name string + config Configs + expectedStatus string + }{ + { + "Given the user fraction is equal to 0 and the status is not set when the release is created then expect the status to be COMPLETED", + Configs{UserFraction: 0}, releaseStatusCompleted, + }, + { + "Given the user fraction is greather than 0 and the status is not set when the release is created then expect the status to be IN_PROGRESS", + Configs{UserFraction: 0.5}, releaseStatusInProgress, + }, + { + "Given the status when the release is created then expect the status to be the same as in the config", + Configs{Status: releaseStatusDraft}, releaseStatusDraft, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + trackRelease, err := createTrackRelease(tt.config, []int64{}) + + assert.NoError(t, err) + assert.Equal(t, tt.expectedStatus, trackRelease.Status) + }) + } +} + +func Test_verifyUserFractionOfTheCreatedRelease(t *testing.T) { + tests := []struct { + name string + config Configs + expectedUserFraction float64 + }{ + { + "Given status is IN_PROGRESS and the user fraction is set when the release is created then expect the user fraction to be applied", + Configs{UserFraction: 1, Status: releaseStatusInProgress}, 1, + }, + { + "Given status is HALTED and the user fraction is set when the release is created then expect the user fraction to be applied", + Configs{UserFraction: 1, Status: releaseStatusHalted}, 1, + }, + { + "Given status is DRAFT and the user fraction is set when the release is created then expect the user fraction not to be applied", + Configs{UserFraction: 1, Status: releaseStatusDraft}, 0, + }, + { + "Given status is COMPLETED and the user fraction is set when the release is created then expect the user fraction not to be applied", + Configs{UserFraction: 1, Status: releaseStatusCompleted}, 0, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + trackRelease, err := createTrackRelease(tt.config, []int64{}) + + assert.NoError(t, err) + assert.Equal(t, tt.expectedUserFraction, trackRelease.UserFraction) + }) + } +} + func Test_releaseStatusFromConfig(t *testing.T) { tests := []struct { diff --git a/step.yml b/step.yml index 4a77d3aa..fa1ba7e1 100755 --- a/step.yml +++ b/step.yml @@ -117,8 +117,17 @@ inputs: title: User Fraction description: |- Portion of the users who should get the staged version of the app. Accepts values between 0.0 and 1.0 (exclusive-exclusive). + Only applies if `Status` is `inProgress` or `halted`. + To release to all users, this input should not be defined (or should be blank). is_required: false + - status: + opts: + title: Status + description: |- + The status of a release. + For more information see here: https://developers.google.com/android-publisher/api-ref/rest/v3/edits.tracks#Status + is_required: false - release_name: opts: title: Name of the release