destinationTrack = track;
}
// Error checking
if (sourceTrack==null || destinationTrack==null) {
throw new InvalidUserDataException(String.format(
"Cannot find the %s or %s track on Google Play, invalid track name?",
publisherExtension.getTrack(), publisherExtension.getPromotionTrack()
));
}
if (sourceTrack.getVersionCodes().size()==0) {
throw new InvalidUserDataException(String.format(
"Cannot find a valid APK version code for the %s track, does it have at least one APK already uploaded?",
sourceTrack.getVersionCodes()
));
}
getLogger().info("Using source track {} with version codes {}",
sourceTrack.getTrack(), sourceTrack.getVersionCodes());
getLogger().info("Using destination track {} with version codes {}",
destinationTrack.getTrack(), destinationTrack.getVersionCodes());
// Find version code to promote, remove from source track and add to destination track
Integer versionCode = Collections.max(sourceTrack.getVersionCodes());
List<Integer> sourceVersionCodes = sourceTrack.getVersionCodes();
sourceVersionCodes.remove(versionCode);
sourceTrack.setVersionCodes(sourceVersionCodes);
List<Integer> destinationVersionCodes = new ArrayList<Integer>();
destinationVersionCodes.add(versionCode);
destinationTrack.setVersionCodes(destinationVersionCodes);
getLogger().info("Promoting version code {}", versionCode);
Update sourceUpdateRequest = edits
.tracks()
.update(publisherExtension.getPackageName(),
editId,
sourceTrack.getTrack(), sourceTrack);
sourceUpdateRequest.execute();
getLogger().info(String.format("Source track %s has been updated", sourceTrack.getTrack()));
Update destinationUpdateRequest = edits
.tracks()
.update(publisherExtension.getPackageName(),
editId,
destinationTrack.getTrack(), destinationTrack);
getLogger().info(String.format("Destination track %s has been updated",
destinationTrack.getTrack()));
destinationUpdateRequest.execute();
// Commit changes for edit.
Commit commitRequest = edits.commit(publisherExtension.getPackageName(), editId);
AppEdit appEdit = commitRequest.execute();
getLogger().info(String.format("App edit with id %s has been committed", appEdit.getId()));
getLogger().lifecycle("Version code {} has been promoted from the {} to the {} track",
versionCode, sourceTrack.getTrack(), destinationTrack.getTrack());
} catch (IOException e) {
throw new InvalidUserDataException(
String.format("Exception was thrown while promoting APK from the %s track to the %s track: %s",
publisherExtension.getTrack(),
publisherExtension.getPromotionTrack(),
e.getMessage()),
e);
} catch (GeneralSecurityException e) {
throw new InvalidUserDataException(
String.format("Exception was thrown while promoting APK from the %s track to the %s track: %s",
publisherExtension.getTrack(),
publisherExtension.getPromotionTrack(),
e.getMessage()),
e);