Skip to content

Commit

Permalink
Fix prompt to "update" to old release when on pre-release
Browse files Browse the repository at this point in the history
- Fix so that release candidates / betas no longer give prompt to "update" to an older release
- Change: users on a pre-release now get a prompt to update to the newest version, such as a newer release candidate
- No change for those on releases
- Close #627
  • Loading branch information
Merudo committed Oct 2, 2019
1 parent aadac1f commit 88f9d40
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/main/java/net/rptools/maptool/client/AppUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,25 @@ public class AppUpdate {
private static final Logger log = LogManager.getLogger(AppUpdate.class);

static final String GIT_HUB_API_URL = "github.api.url";
static final String GIT_HUB_RELEASES = "github.api.releases";
static final String GIT_HUB_OAUTH_TOKEN =
"github.api.oauth.token"; // Grants read-only access to public information

/**
* Look for a newer version of MapTool. If a newer release is found and the AppPreferences tell us
* the update should not be ignored, give a prompt to update. If current version is a release,
* update to the most recent release. If the current version is a pre-release, update to the most
* recent version (pre-release or release).
*
* @return has an update been made
*/
public static boolean gitHubReleases() {
// AppPreferences.setSkipAutoUpdate(false); // For testing only
if (AppPreferences.getSkipAutoUpdate()) return false;

String version = MapTool.getVersion().toLowerCase();
boolean prerelease = version.contains("rc") || version.contains("beta");

String responseBody = null;
String jarCommit = null;
String latestGitHubReleaseCommit = "";
Expand All @@ -62,11 +74,12 @@ public static boolean gitHubReleases() {
return false;
}

// if prerelease, get list of all releases, otherwise get latest release
String strURL = prerelease ? getProperty(GIT_HUB_RELEASES) : getProperty(GIT_HUB_API_URL);

try {
Request request =
new Request.Builder()
.url(getProperty(GIT_HUB_API_URL) + getProperty(GIT_HUB_OAUTH_TOKEN))
.build();
new Request.Builder().url(strURL + getProperty(GIT_HUB_OAUTH_TOKEN)).build();

Response response = new OkHttpClient().newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
Expand All @@ -75,13 +88,16 @@ public static boolean gitHubReleases() {

log.debug("GitHub API Response: " + responseBody);
} catch (IOException e) {
log.error("Unable to reach " + getProperty(GIT_HUB_API_URL), e.getLocalizedMessage());
log.error("Unable to reach " + strURL, e.getLocalizedMessage());
return false;
}

JSONObject releases = new JSONObject();
JSONObject releases;
try {
releases = JSONObject.fromObject(responseBody);
if (prerelease) {
JSONArray releasesList = JSONArray.fromObject(responseBody);
releases = JSONObject.fromObject(releasesList.get(0)); // the latest is at top of list
} else releases = JSONObject.fromObject(responseBody);
latestGitHubReleaseCommit = releases.get("target_commitish").toString();
log.info("target_commitish from GitHub: " + latestGitHubReleaseCommit);
latestGitHubReleaseTagName = releases.get("tag_name").toString();
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/github.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
github.api.url=https://api.github.com/repos/RPTools/maptool/releases/latest?access_token=
github.api.releases = https://api.github.com/repos/RPTools/maptool/releases?access_token=
github.api.oauth.token=28276e82af1de2f79f0c38ceb17d2b23640784eb

0 comments on commit 88f9d40

Please sign in to comment.