forked from orrc/google-play-android-publisher-plugin
-
Notifications
You must be signed in to change notification settings - Fork 48
/
ApkPublisher.java
451 lines (372 loc) · 17.1 KB
/
ApkPublisher.java
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package org.jenkinsci.plugins.googleplayandroidpublisher;
import com.google.jenkins.plugins.credentials.oauth.GoogleRobotCredentials;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractDescribableImpl;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Descriptor;
import hudson.model.Result;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Publisher;
import hudson.util.ComboBoxModel;
import hudson.util.FormValidation;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.export.Exported;
import java.io.IOException;
import java.io.PrintStream;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static hudson.Util.fixEmptyAndTrim;
import static hudson.Util.join;
import static hudson.Util.tryParseNumber;
import static org.jenkinsci.plugins.googleplayandroidpublisher.ReleaseTrack.PRODUCTION;
import static org.jenkinsci.plugins.googleplayandroidpublisher.ReleaseTrack.fromConfigValue;
import static org.jenkinsci.plugins.googleplayandroidpublisher.ReleaseTrack.getConfigValues;
import static org.jenkinsci.plugins.googleplayandroidpublisher.Util.REGEX_LANGUAGE;
import static org.jenkinsci.plugins.googleplayandroidpublisher.Util.REGEX_VARIABLE;
import static org.jenkinsci.plugins.googleplayandroidpublisher.Util.SUPPORTED_LANGUAGES;
import static org.jenkinsci.plugins.googleplayandroidpublisher.Util.expand;
import static org.jenkinsci.plugins.googleplayandroidpublisher.Util.getApplicationId;
import static org.jenkinsci.plugins.googleplayandroidpublisher.Util.getVersionCode;
/** Uploads Android application files to the Google Play Developer Console. */
public class ApkPublisher extends GooglePlayPublisher {
/** Expansion file type: main */
public static final String TYPE_MAIN = "main";
/** Expansion file type: patch */
public static final String TYPE_PATCH = "patch";
public static final DecimalFormat PERCENTAGE_FORMATTER = new DecimalFormat("#.#");
/** Allowed percentage values when doing a staged rollout to production. */
private static final double[] ROLLOUT_PERCENTAGES = { 0.5, 1, 5, 10, 20, 50, 100 };
private static final double DEFAULT_PERCENTAGE = 100;
/** File name pattern which expansion files must match. */
private static final Pattern OBB_FILE_REGEX =
Pattern.compile("^(main|patch)\\.([0-9]+)\\.([._a-z0-9]+)\\.obb$", Pattern.CASE_INSENSITIVE);
@DataBoundSetter
private String apkFilesPattern;
@DataBoundSetter
private String expansionFilesPattern;
@DataBoundSetter
private boolean usePreviousExpansionFilesIfMissing;
@DataBoundSetter
private String trackName;
@DataBoundSetter
private String rolloutPercentage;
@DataBoundSetter
private RecentChanges[] recentChangeList;
@DataBoundConstructor
public ApkPublisher() {}
public String getApkFilesPattern() {
return fixEmptyAndTrim(apkFilesPattern);
}
private String getExpandedApkFilesPattern(EnvVars env) {
return expand(env, getApkFilesPattern());
}
public String getExpansionFilesPattern() {
return fixEmptyAndTrim(expansionFilesPattern);
}
private String getExpandedExpansionFilesPattern(EnvVars env) {
return expand(env, getExpansionFilesPattern());
}
public boolean getUsePreviousExpansionFilesIfMissing() {
return usePreviousExpansionFilesIfMissing;
}
public String getTrackName() {
return fixEmptyAndTrim(trackName);
}
private String getCanonicalTrackName(EnvVars env) {
String name = expand(env, getTrackName());
if (name == null) {
return null;
}
return name.toLowerCase(Locale.ENGLISH);
}
public String getRolloutPercentage() {
return fixEmptyAndTrim(rolloutPercentage);
}
private double getRolloutPercentageValue(EnvVars env) {
String pct = getRolloutPercentage();
if (pct != null) {
// Allow % characters in the config
pct = pct.replace("%", "");
}
// If no valid numeric value was set, we will roll out to 100%
return tryParseNumber(expand(env, pct), DEFAULT_PERCENTAGE).doubleValue();
}
public RecentChanges[] getRecentChangeList() {
return recentChangeList;
}
private RecentChanges[] getExpandedRecentChangesList(EnvVars env) {
if (recentChangeList == null) {
return null;
}
RecentChanges[] expanded = new RecentChanges[recentChangeList.length];
for (int i = 0; i < recentChangeList.length; i++) {
RecentChanges r = recentChangeList[i];
expanded[i] = new RecentChanges(expand(env, r.language), expand(env, r.text));
}
return expanded;
}
private boolean isConfigValid(PrintStream logger, EnvVars env) {
final List<String> errors = new ArrayList<String>();
// Check whether a file pattern was provided
if (getExpandedApkFilesPattern(env) == null) {
errors.add("Path or pattern to APK file was not specified");
}
// Track name is also required
final String trackName = getCanonicalTrackName(env);
final ReleaseTrack track = fromConfigValue(trackName);
if (trackName == null) {
errors.add("Release track was not specified");
} else if (track == null) {
errors.add(String.format("'%s' is not a valid release track", trackName));
} else if (track == PRODUCTION) {
// Check for valid rollout percentage
double pct = getRolloutPercentageValue(env);
if (Arrays.binarySearch(ROLLOUT_PERCENTAGES, pct) < 0) {
errors.add(String.format("%s%% is not a valid rollout percentage", PERCENTAGE_FORMATTER.format(pct)));
}
}
// Print accumulated errors
if (!errors.isEmpty()) {
logger.println("Cannot upload to Google Play:");
for (String error : errors) {
logger.print("- ");
logger.println(error);
}
}
return errors.isEmpty();
}
@Override
public boolean perform(AbstractBuild build, Launcher launcher,
BuildListener listener) throws IOException, InterruptedException {
PrintStream logger = listener.getLogger();
// Check whether we should execute at all
final Result buildResult = build.getResult();
if (buildResult != null && buildResult.isWorseThan(Result.UNSTABLE)) {
logger.println("Skipping upload to Google Play due to build result");
return true;
}
// Check that the job has been configured correctly
final EnvVars env = build.getEnvironment(listener);
if (!isConfigValid(logger, env)) {
return false;
}
// Find the APK filename(s) which match the pattern after variable expansion
final String filesPattern = getExpandedApkFilesPattern(env);
final FilePath ws = build.getWorkspace();
List<String> relativePaths = ws.act(new FindFilesTask(filesPattern));
if (relativePaths.isEmpty()) {
logger.println(String.format("No APK files matching the pattern '%s' could be found", filesPattern));
return false;
}
// Get the full remote path in the workspace for each filename
final List<FilePath> apkFiles = new ArrayList<FilePath>();
final Set<String> applicationIds = new HashSet<String>();
final Set<Integer> versionCodes = new TreeSet<Integer>();
for (String path : relativePaths) {
FilePath apk = ws.child(path);
applicationIds.add(getApplicationId(apk));
versionCodes.add(getVersionCode(apk));
apkFiles.add(apk);
}
// If there are multiple APKs, ensure that all have the same application ID
if (applicationIds.size() != 1) {
logger.println("Multiple APKs were found but they have inconsistent application IDs:");
for (String id : applicationIds) {
logger.print("- ");
logger.println(id);
}
return false;
}
final String applicationId = applicationIds.iterator().next();
// Find the expansion filename(s) which match the pattern after variable expansion
final Map<Integer, ExpansionFileSet> expansionFiles = new TreeMap<Integer, ExpansionFileSet>();
final String expansionPattern = getExpandedExpansionFilesPattern(env);
if (expansionPattern != null) {
List<String> expansionPaths = ws.act(new FindFilesTask(expansionPattern));
// Check that the expansion files found apply to the APKs to be uploaded
for (String path : expansionPaths) {
FilePath file = ws.child(path);
// Check that the filename is in the right format
Matcher matcher = OBB_FILE_REGEX.matcher(file.getName());
if (!matcher.matches()) {
logger.println(String.format("Expansion file '%s' doesn't match the required naming scheme", path));
return false;
}
// We can only associate expansion files with the application ID we're going to upload
final String appId = matcher.group(3);
if (!applicationId.equals(appId)) {
logger.println(String.format("Expansion filename '%s' doesn't match the application ID to be " +
"uploaded: %s", path, applicationId));
return false;
}
// We can only associate expansion files with version codes we're going to upload
final int versionCode = Integer.parseInt(matcher.group(2));
if (!versionCodes.contains(versionCode)) {
logger.println(String.format("Expansion filename '%s' doesn't match the versionCode of any of " +
"APK(s) to be uploaded: %s", path, join(versionCodes, ", ")));
return false;
}
// File looks good, so add it to the fileset for this version code
final String type = matcher.group(1).toLowerCase(Locale.ENGLISH);
ExpansionFileSet fileSet = expansionFiles.get(versionCode);
if (fileSet == null) {
fileSet = new ExpansionFileSet();
expansionFiles.put(versionCode, fileSet);
}
if (type.equals(TYPE_MAIN)) {
fileSet.setMainFile(file);
} else {
fileSet.setPatchFile(file);
}
}
// If there are patch files, make sure that each has a main file, or "use previous if missing" is enabled
for (ExpansionFileSet fileSet : expansionFiles.values()) {
if (!usePreviousExpansionFilesIfMissing && fileSet.getPatchFile() != null && fileSet.getMainFile() == null) {
logger.println(String.format("Patch expansion file '%s' was provided, but no main expansion file " +
"was provided, and the option to reuse a pre-existing expansion file was " +
"disabled.\nGoogle Play requires that each APK with a patch file also has a main " +
"file.", fileSet.getPatchFile().getName()));
return false;
}
}
}
// Upload the file(s) from the workspace
try {
GoogleRobotCredentials credentials = getServiceAccountCredentials();
return build.getWorkspace()
.act(new ApkUploadTask(listener, credentials, applicationId, apkFiles, expansionFiles,
usePreviousExpansionFilesIfMissing, fromConfigValue(getCanonicalTrackName(env)),
getRolloutPercentageValue(env), getExpandedRecentChangesList(env)));
} catch (UploadException e) {
logger.println(String.format("Upload failed: %s", getPublisherErrorMessage(e)));
logger.println("- No changes have been applied to the Google Play account");
}
return false;
}
static final class ExpansionFileSet implements Serializable {
private static final long serialVersionUID = 1;
FilePath mainFile;
FilePath patchFile;
public FilePath getMainFile() {
return mainFile;
}
public void setMainFile(FilePath mainFile) {
this.mainFile = mainFile;
}
public FilePath getPatchFile() {
return patchFile;
}
public void setPatchFile(FilePath patchFile) {
this.patchFile = patchFile;
}
}
public static final class RecentChanges extends AbstractDescribableImpl<RecentChanges> implements Serializable {
private static final long serialVersionUID = 1;
@Exported
public final String language;
@Exported
public final String text;
@DataBoundConstructor
public RecentChanges(String language, String text) {
this.language = language;
this.text = text;
}
@Extension
public static class DescriptorImpl extends Descriptor<RecentChanges> {
@Override
public String getDisplayName() {
return null;
}
public ComboBoxModel doFillLanguageItems() {
return new ComboBoxModel(SUPPORTED_LANGUAGES);
}
public FormValidation doCheckLanguage(@QueryParameter String value) {
value = fixEmptyAndTrim(value);
if (value != null && !value.matches(REGEX_LANGUAGE) && !value.matches(REGEX_VARIABLE)) {
return FormValidation.warning("Should be a language code like 'be' or 'en-GB'");
}
return FormValidation.ok();
}
public FormValidation doCheckText(@QueryParameter String value) {
value = fixEmptyAndTrim(value);
if (value != null && value.length() > 500) {
return FormValidation.error("Recent changes text must be 500 characters or fewer");
}
return FormValidation.ok();
}
}
}
@Extension
public static final class DescriptorImpl extends BuildStepDescriptor<Publisher> {
public DescriptorImpl() {
load();
}
public String getDisplayName() {
return "Upload Android APK to Google Play";
}
public FormValidation doCheckApkFiles(@QueryParameter String value) {
if (fixEmptyAndTrim(value) == null) {
return FormValidation.error("An APK file path or pattern is required");
}
return FormValidation.ok();
}
public FormValidation doCheckTrackName(@QueryParameter String value) {
if (fixEmptyAndTrim(value) == null) {
return FormValidation.error("A release track is required");
}
return FormValidation.ok();
}
public FormValidation doCheckRolloutPercentage(@QueryParameter String value) {
value = fixEmptyAndTrim(value);
if (value == null || value.matches(REGEX_VARIABLE)) {
return FormValidation.ok();
}
final double lowest = ROLLOUT_PERCENTAGES[0];
final double highest = DEFAULT_PERCENTAGE;
double pct = tryParseNumber(value.replace("%", ""), highest).doubleValue();
if (Double.compare(pct, 0.5) < 0 || Double.compare(pct, DEFAULT_PERCENTAGE) > 0) {
return FormValidation.error("Percentage value must be between %s and %s%%",
PERCENTAGE_FORMATTER.format(lowest), PERCENTAGE_FORMATTER.format(highest));
}
return FormValidation.ok();
}
public ComboBoxModel doFillTrackNameItems() {
return new ComboBoxModel(getConfigValues());
}
public ComboBoxModel doFillRolloutPercentageItems() {
ComboBoxModel list = new ComboBoxModel();
for (double pct : ROLLOUT_PERCENTAGES) {
list.add(String.format("%s%%", PERCENTAGE_FORMATTER.format(pct)));
}
return list;
}
public boolean isApplicable(Class<? extends AbstractProject> c) {
return true;
}
@Override
public boolean configure(StaplerRequest req, JSONObject formData) throws FormException {
save();
return super.configure(req, formData);
}
}
}