-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
242 lines (191 loc) · 7.24 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package main
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/bitrise-io/appcenter"
"github.com/bitrise-io/appcenter/client"
"github.com/bitrise-io/appcenter/model"
"github.com/bitrise-io/go-steputils/stepconf"
"github.com/bitrise-io/go-steputils/tools"
"github.com/bitrise-io/go-utils/log"
)
const statusEnvKey = "APPCENTER_DEPLOY_STATUS"
type config struct {
Debug bool `env:"debug,required"`
AppPath string `env:"app_path,file"`
AppName string `env:"app_name,required"`
APIToken stepconf.Secret `env:"api_token,required"`
OwnerName string `env:"owner_name,required"`
Mandatory bool `env:"mandatory,required"`
MappingPath string `env:"mapping_path"`
ReleaseNotes string `env:"release_notes"`
NotifyTesters bool `env:"notify_testers,required"`
DistributeAllGroup bool `env:"all_distribution_groups"`
DistributionGroup string `env:"distribution_group"`
DistributionStore string `env:"distribution_store"`
DistributionTester string `env:"distribution_tester"`
}
func main() {
var cfg config
if err := stepconf.Parse(&cfg); err != nil {
failf("Issue with input: %s", err)
}
stepconf.Print(cfg)
fmt.Println()
app := model.App{
Owner: cfg.OwnerName,
AppName: cfg.AppName,
AppType: model.AppTypeAndroid,
}
releaseOptions := model.ReleaseOptions{
GroupNames: strings.Split(cfg.DistributionGroup, "\n"),
Mandatory: cfg.Mandatory,
NotifyTesters: cfg.NotifyTesters,
FilePath: cfg.AppPath,
App: app,
}
api := client.CreateAPIWithClientParams(string(cfg.APIToken))
appAPI := appcenter.CreateApplicationAPI(api, releaseOptions)
log.SetEnableDebugLog(cfg.Debug)
log.Infof("Uploading binary")
release, err := appAPI.NewRelease()
if err != nil {
failf("Failed to create new release, error: %s", err)
}
log.Donef("- Done")
fmt.Println()
releaseAPI := appcenter.CreateReleaseAPI(api, release, releaseOptions)
// We think there is a limitation in the App Center API where release notes can only be modified
// after a release has been created (using separate endpoints). This leads to a race condition
// where the generated email for the release does not necessarily contain the release notes
// because it was added later via a separate API call. Anecdotally, this timing issue can be somewhat mitigated
// by adding the release notes as soon as possible after the release was created (i.e. calling the endpoints right after each other).
// In the future, we should investigate if there is a solution for updating the release notes reliably,
// or switch entirely to the App Center CLI which might be able to handle this correctly.
if len(cfg.ReleaseNotes) > 0 {
log.Infof("Setting release notes")
if err := releaseAPI.SetReleaseNote(cfg.ReleaseNotes); err != nil {
failf("Failed to set release note, error: %s", err)
}
log.Donef("- Done")
fmt.Println()
}
log.Infof("Setting distribution group(s)")
err = releaseAPI.AddGroupsToRelease(releaseOptions.GroupNames)
if err != nil {
failf("Failed to set groups on the release %s, groups: %s, error: %s", release.ID, releaseOptions.GroupNames, err)
}
if len(cfg.MappingPath) > 0 {
log.Infof("Uploading mapping file")
if err := releaseAPI.UploadSymbol(cfg.MappingPath); err != nil {
failf("Failed to upload symbol file(%s), error: %s", cfg.MappingPath, err)
}
log.Donef("- Done")
fmt.Println()
}
log.Infof("Gatehering public group(s)")
var publicGroup []string
if cfg.DistributeAllGroup {
log.Infof("Gatehering all public group(s)")
groups, err := appAPI.AllGroups()
if err != nil {
failf("Failed to fetch groups, error: %s", err)
}
for _, group := range groups {
if err := releaseAPI.AddGroup(group); err != nil {
failf("Failed to add group(%s) to the release, error: %s", group.DisplayName, err)
}
if group.IsPublic {
publicGroup = append(publicGroup, group.DisplayName)
}
}
} else {
log.Infof("Gatehering config public group(s)")
err = releaseAPI.AddGroupsToRelease(releaseOptions.GroupNames)
if err != nil {
failf("Failed to set groups on the release %s, groups: %s, error: %s", release.ID, releaseOptions.GroupNames, err)
}
for _, groupName := range releaseOptions.GroupNames {
groupName = strings.TrimSpace(groupName)
if len(groupName) == 0 {
continue
}
log.Printf("- %s", groupName)
group, err := appAPI.Groups(groupName)
if err != nil {
failf("Failed to fetch group with name: (%s), error: %s", groupName, err)
}
log.Debugf("%+v", group)
if group.IsPublic {
publicGroup = append(publicGroup, groupName)
}
}
}
log.Donef("- Done")
fmt.Println()
log.Infof("Setting distribution store(s)")
for _, storeName := range strings.Split(cfg.DistributionStore, "\n") {
storeName = strings.TrimSpace(storeName)
if len(storeName) == 0 {
continue
}
log.Printf("- %s", storeName)
store, err := appAPI.Stores(storeName)
if err != nil {
failf("Failed to fetch store with name: (%s), error: %s", storeName, err)
}
if err := releaseAPI.AddStore(store); err != nil {
failf("Failed to add store(%s) to the release, error: %s", storeName, err)
}
}
log.Donef("- Done")
fmt.Println()
log.Infof("Setting distribution tester(s)")
for _, email := range strings.Split(cfg.DistributionTester, "\n") {
email = strings.TrimSpace(email)
if len(email) == 0 {
continue
}
log.Printf("- %s", email)
if err := releaseAPI.AddTester(email); err != nil {
failf("Failed to add tester(%s) to the release, error: %s", email, err)
}
}
log.Donef("- Done")
fmt.Println()
log.Infof("Exporting outputs")
var groupUrls []string
for _, groupName := range publicGroup {
groupUrls = append(groupUrls, fmt.Sprintf("https://install.appcenter.ms/users/%s/apps/%s/distribution_groups/%s", cfg.OwnerName, cfg.AppName, groupName))
}
var outputs = map[string]string{
statusEnvKey: "success",
"APPCENTER_DEPLOY_INSTALL_URL": release.InstallURL,
"APPCENTER_DEPLOY_DOWNLOAD_URL": release.DownloadURL,
"APPCENTER_RELEASE_PAGE_URL": fmt.Sprintf("https://appcenter.ms/orgs/%s/apps/%s/distribute/releases/%d", cfg.OwnerName, cfg.AppName, release.ID),
"APPCENTER_DEPLOY_RELEASE_ID": strconv.Itoa(release.ID),
}
if len(publicGroup) > 0 {
outputs["APPCENTER_PUBLIC_INSTALL_PAGE_URL"] = fmt.Sprintf("https://install.appcenter.ms/users/%s/apps/%s/distribution_groups/%s", cfg.OwnerName, cfg.AppName, publicGroup[0])
outputs["APPCENTER_PUBLIC_INSTALL_PAGE_URLS"] = strings.Join(groupUrls, ", ")
} else {
outputs["APPCENTER_PUBLIC_INSTALL_PAGE_URL"] = ""
outputs["APPCENTER_PUBLIC_INSTALL_PAGE_URLS"] = ""
}
for key, value := range outputs {
log.Printf("- %s: %s", key, value)
if err := tools.ExportEnvironmentWithEnvman(key, value); err != nil {
failf("Failed to export environment variable: %s with value: %s. Error: %s", key, value, err)
}
}
log.Donef("- Done")
}
func failf(f string, args ...interface{}) {
log.Errorf(f, args...)
if err := tools.ExportEnvironmentWithEnvman(statusEnvKey, "failed"); err != nil {
log.Errorf("Failed to export environment variable: %s with value: %s. Error: %s", statusEnvKey, "failed", err)
}
os.Exit(1)
}