Skip to content

Commit

Permalink
Fix prompt to "update" to old version when on pre-release (#757)
Browse files Browse the repository at this point in the history
* Fix prompt to "update" to old release when on pre-release

- Fix so that a release candidate / alpha / beta no longer gives 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

* Change pre-release detection to use github API

- Change so that pre-release status is detected by parsing the json of all releases and finding the one corresponding to the commit SHA
  • Loading branch information
Merudo authored and JamzTheMan committed Oct 3, 2019
1 parent aadac1f commit 6a642b5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 32 deletions.
96 changes: 65 additions & 31 deletions src/main/java/net/rptools/maptool/client/AppUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
*/
package net.rptools.maptool.client;

import com.jayway.jsonpath.JsonPath;
import java.io.*;
import java.net.*;
import java.util.List;
import java.util.Properties;
import java.util.jar.*;
import javax.swing.*;
Expand All @@ -31,18 +33,27 @@
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_OAUTH_TOKEN =
private static final String GIT_HUB_RELEASES = "github.api.releases";
private 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 strURL = getProperty(GIT_HUB_RELEASES);
String strRequest = strURL + getProperty(GIT_HUB_OAUTH_TOKEN);

String responseBody = null;
String jarCommit = null;
String latestGitHubReleaseCommit = "";
String latestGitHubReleaseTagName = "";
String jarCommit;
String latestGitHubReleaseCommit;
String latestGitHubReleaseTagName;

// Default for Linux?
String DOWNLOAD_EXTENSION = ".deb";
Expand All @@ -55,36 +66,32 @@ public static boolean gitHubReleases() {

// If we don't have a commit attribute from JAR, we're done!
if (jarCommit == null) {
log.info(
"No commit SHA (running in DEVELOPMENT mode?): "
+ getProperty(GIT_HUB_API_URL)
+ getProperty(GIT_HUB_OAUTH_TOKEN));
log.info("No commit SHA (running in DEVELOPMENT mode?): " + strRequest);
return false;
}

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

Response response = new OkHttpClient().newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

responseBody = response.body().string();
String strReleases = getReleases();
// If can't access the list of releases, we're done
if (strReleases == null) return false;

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

JSONObject releases = new JSONObject();
JSONObject release;
try {
releases = JSONObject.fromObject(responseBody);
latestGitHubReleaseCommit = releases.get("target_commitish").toString();
// Get pre-release information regarding MapTool version from github list
String path = "$.[?(@.target_commitish == '" + jarCommit + "')].prerelease";
List<Boolean> listMatches = JsonPath.parse(strReleases).read(path);
boolean prerelease = listMatches.isEmpty() || listMatches.get(0);

if (prerelease) {
JSONArray releasesList = JSONArray.fromObject(strReleases);
release = JSONObject.fromObject(releasesList.get(0)); // the latest is at top of list
} else {
path = "$.[?(@.prerelease == false)]";
listMatches = JsonPath.parse(strReleases).read(path); // get sublist of releases
release = JSONObject.fromObject(listMatches.get(0)); // the latest is at top of list
}
latestGitHubReleaseCommit = release.get("target_commitish").toString();
log.info("target_commitish from GitHub: " + latestGitHubReleaseCommit);
latestGitHubReleaseTagName = releases.get("tag_name").toString();
latestGitHubReleaseTagName = release.get("tag_name").toString();
log.info("tag_name from GitHub: " + latestGitHubReleaseTagName);
} catch (Exception e) {
log.error("Unable to parse JSON payload from GitHub...", e);
Expand All @@ -95,7 +102,7 @@ public static boolean gitHubReleases() {
if (jarCommit.equals(latestGitHubReleaseCommit)
|| AppPreferences.getSkipAutoUpdateCommit().equals(latestGitHubReleaseCommit)) return false;

JSONArray releaseAssets = releases.getJSONArray("assets");
JSONArray releaseAssets = release.getJSONArray("assets");
String assetDownloadURL = null;
JSONObject asset;

Expand Down Expand Up @@ -132,6 +139,33 @@ public static boolean gitHubReleases() {
return false;
}

/**
* Get the String containing the list of the releases and pre-releases from github.
*
* @return the String with the list of releases, or null if IOException
*/
private static String getReleases() {
String strURL = getProperty(GIT_HUB_RELEASES);
String strRequest = strURL + getProperty(GIT_HUB_OAUTH_TOKEN);
try {
Request request = new Request.Builder().url(strRequest).build();
Response response = new OkHttpClient().newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

String responseBody = response.body().string();
log.debug("GitHub API Response: " + responseBody);
return responseBody;
} catch (IOException e) {
log.error("Unable to reach " + strURL, e.getLocalizedMessage());
return null;
}
}

/**
* Get the latest commit SHA from MANIFEST.MF
*
* @return the String of the commit SHA, or null if IOException
*/
public static String getCommitSHA() {
String jarCommit = "";

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/github.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
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 6a642b5

Please sign in to comment.