Skip to content

Commit

Permalink
Introduce status input (#116)
Browse files Browse the repository at this point in the history
* Status input added

* State strings replaced with const

* Status changes implemented

* Tests updated

* Status options removed

* Step.yml updated

* Condition fixed

* Step.yml fix

* Support halted

* Condition fixed

* Google API documentation linked to the status input in the step.yml
  • Loading branch information
istvankovacs-bitrise authored Mar 23, 2021
1 parent 727e315 commit 4fc0eb0
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 11 deletions.
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
29 changes: 19 additions & 10 deletions publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}

Expand All @@ -219,3 +224,7 @@ func releaseStatusFromConfig(userFraction float64) string {
}
return releaseStatusCompleted
}

func shouldApplyUserFraction(status string) bool {
return status == releaseStatusInProgress || status == releaseStatusHalted
}
66 changes: 66 additions & 0 deletions publish_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions step.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4fc0eb0

Please sign in to comment.