Refactor Version Checks (#472)
Merge branch 'master' into refactor-versionCheck
Merge branch 'master' into refactor-versionCheck
Revert "test CI"
This reverts commit b56eff0920.
test CI
Reformat LoginActivity.java
cleanup
migrate
Working + Tests
add first UnitTest
wip ...
enum2class
Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitnex/GitNex/pulls/472
Reviewed-by: M M Arif <mmarif@swatian.com>
			
			
This commit is contained in:
		| @@ -24,7 +24,7 @@ import org.mian.gitnex.clients.AppApiService; | ||||
| import org.mian.gitnex.helpers.Authorization; | ||||
| import org.mian.gitnex.helpers.StaticGlobalVariables; | ||||
| import org.mian.gitnex.helpers.Toasty; | ||||
| import org.mian.gitnex.helpers.VersionCheck; | ||||
| import org.mian.gitnex.helpers.Version; | ||||
| import org.mian.gitnex.interfaces.ApiInterface; | ||||
| import org.mian.gitnex.models.Commits; | ||||
| import org.mian.gitnex.util.TinyDB; | ||||
| @@ -92,7 +92,7 @@ public class CommitsActivity extends BaseActivity { | ||||
| 		closeActivity.setOnClickListener(onClickListener); | ||||
|  | ||||
| 		// if gitea is 1.12 or higher use the new limit (resultLimitNewGiteaInstances) | ||||
| 		if(VersionCheck.compareVersion("1.12.0", tinyDb.getString("giteaVersion")) >= 1) { | ||||
| 		if(new Version(tinyDb.getString("giteaVersion")).higherOrEqual("1.12")) { | ||||
| 			resultLimit = StaticGlobalVariables.resultLimitNewGiteaInstances; | ||||
| 		} | ||||
|  | ||||
|   | ||||
| @@ -18,7 +18,7 @@ import org.mian.gitnex.R; | ||||
| import org.mian.gitnex.clients.RetrofitClient; | ||||
| import org.mian.gitnex.helpers.AlertDialogs; | ||||
| import org.mian.gitnex.helpers.Authorization; | ||||
| import org.mian.gitnex.helpers.VersionCheck; | ||||
| import org.mian.gitnex.helpers.Version; | ||||
| import org.mian.gitnex.helpers.Toasty; | ||||
| import org.mian.gitnex.models.Milestones; | ||||
| import org.mian.gitnex.util.AppUtil; | ||||
| @@ -134,7 +134,7 @@ public class CreateMilestoneActivity extends BaseActivity implements View.OnClic | ||||
|         String finalMilestoneDueDate = null; | ||||
|         if(!newMilestoneDueDate.isEmpty()) { | ||||
|             finalMilestoneDueDate = (AppUtil.customDateCombine(AppUtil.customDateFormat(newMilestoneDueDate))); | ||||
|         } else if (VersionCheck.compareVersion("1.10.0", tinyDb.getString("giteaVersion")) > 1) { | ||||
|         } else if (new Version(tinyDb.getString("giteaVersion")).less("1.10.0")) { | ||||
|             // if Gitea version is less than 1.10.0 DueDate is required | ||||
|             Toasty.info(ctx, getString(R.string.milestoneDateEmpty)); | ||||
|             return; | ||||
|   | ||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							| @@ -23,7 +23,7 @@ import org.mian.gitnex.clients.RetrofitClient; | ||||
| import org.mian.gitnex.helpers.AlertDialogs; | ||||
| import org.mian.gitnex.helpers.Authorization; | ||||
| import org.mian.gitnex.helpers.Toasty; | ||||
| import org.mian.gitnex.helpers.VersionCheck; | ||||
| import org.mian.gitnex.helpers.Version; | ||||
| import org.mian.gitnex.models.Collaborators; | ||||
| import org.mian.gitnex.models.MergePullRequest; | ||||
| import org.mian.gitnex.models.MergePullRequestSpinner; | ||||
| @@ -110,7 +110,7 @@ public class MergePullRequestActivity extends BaseActivity { | ||||
|  | ||||
| 		if(!tinyDb.getString("issueTitle").isEmpty()) { | ||||
| 			toolbar_title.setText(tinyDb.getString("issueTitle")); | ||||
| 			mergeTitle.setText(tinyDb.getString("issueTitle") + " (#" + tinyDb.getString("issueNumber")+ ")"); | ||||
| 			mergeTitle.setText(tinyDb.getString("issueTitle") + " (#" + tinyDb.getString("issueNumber") + ")"); | ||||
| 		} | ||||
|  | ||||
| 		initCloseListener(); | ||||
| @@ -140,8 +140,8 @@ public class MergePullRequestActivity extends BaseActivity { | ||||
| 		mergeList.add(new MergePullRequestSpinner("merge", getResources().getString(R.string.mergeOptionMerge))); | ||||
| 		mergeList.add(new MergePullRequestSpinner("rebase", getResources().getString(R.string.mergeOptionRebase))); | ||||
| 		mergeList.add(new MergePullRequestSpinner("rebase-merge", getResources().getString(R.string.mergeOptionRebaseCommit))); | ||||
| 		//squash merge works only on gitea v1.11.5 and higher due to a bug | ||||
| 		if(VersionCheck.compareVersion("1.11.5", tinyDb.getString("giteaVersion")) < 1) { | ||||
| 		//squash merge works only on gitea > v1.11.4 due to a bug | ||||
| 		if(new Version(tinyDb.getString("giteaVersion")).higher("1.11.4")) { | ||||
| 			mergeList.add(new MergePullRequestSpinner("squash", getResources().getString(R.string.mergeOptionSquash))); | ||||
| 		} | ||||
|  | ||||
|   | ||||
| @@ -38,7 +38,7 @@ import org.mian.gitnex.fragments.PullRequestsFragment; | ||||
| import org.mian.gitnex.fragments.ReleasesFragment; | ||||
| import org.mian.gitnex.fragments.RepoInfoFragment; | ||||
| import org.mian.gitnex.helpers.Authorization; | ||||
| import org.mian.gitnex.helpers.VersionCheck; | ||||
| import org.mian.gitnex.helpers.Version; | ||||
| import org.mian.gitnex.models.UserRepositories; | ||||
| import org.mian.gitnex.models.WatchRepository; | ||||
| import org.mian.gitnex.util.AppUtil; | ||||
| @@ -222,7 +222,7 @@ public class RepoDetailActivity extends BaseActivity implements BottomSheetRepoF | ||||
| 			} | ||||
|  | ||||
| 			// release count | ||||
| 			if(VersionCheck.compareVersion("1.11.5", tinyDb.getString("giteaVersion")) < 1) { | ||||
| 			if(new Version("1.11.4").less(tinyDb.getString("giteaVersion"))) { | ||||
| 				if(textViewBadgeRelease.getText() != "") { // only show if API returned a number | ||||
| 					Objects.requireNonNull(tabLayout.getTabAt(5)).setCustomView(tabHeader6); | ||||
| 					TabLayout.Tab tabOpenRelease = tabLayout.getTabAt(5); | ||||
|   | ||||
| @@ -26,7 +26,7 @@ import org.mian.gitnex.clients.AppApiService; | ||||
| import org.mian.gitnex.helpers.Authorization; | ||||
| import org.mian.gitnex.helpers.StaticGlobalVariables; | ||||
| import org.mian.gitnex.helpers.Toasty; | ||||
| import org.mian.gitnex.helpers.VersionCheck; | ||||
| import org.mian.gitnex.helpers.Version; | ||||
| import org.mian.gitnex.interfaces.ApiInterface; | ||||
| import org.mian.gitnex.models.Issues; | ||||
| import org.mian.gitnex.util.TinyDB; | ||||
| @@ -76,7 +76,7 @@ public class IssuesFragment extends Fragment { | ||||
| 		final SwipeRefreshLayout swipeRefresh = v.findViewById(R.id.pullToRefresh); | ||||
|  | ||||
| 		// if gitea is 1.12 or higher use the new limit | ||||
| 		if(VersionCheck.compareVersion("1.12.0", tinyDb.getString("giteaVersion")) >= 1) { | ||||
| 		if(new Version(tinyDb.getString("giteaVersion")).higherOrEqual("1.12.0")) { | ||||
| 			resultLimit = StaticGlobalVariables.resultLimitNewGiteaInstances; | ||||
| 		} | ||||
|  | ||||
|   | ||||
| @@ -27,7 +27,7 @@ import org.mian.gitnex.clients.AppApiService; | ||||
| import org.mian.gitnex.helpers.Authorization; | ||||
| import org.mian.gitnex.helpers.StaticGlobalVariables; | ||||
| import org.mian.gitnex.helpers.Toasty; | ||||
| import org.mian.gitnex.helpers.VersionCheck; | ||||
| import org.mian.gitnex.helpers.Version; | ||||
| import org.mian.gitnex.interfaces.ApiInterface; | ||||
| import org.mian.gitnex.models.PullRequests; | ||||
| import org.mian.gitnex.util.TinyDB; | ||||
| @@ -76,7 +76,7 @@ public class PullRequestsFragment extends Fragment { | ||||
| 		final SwipeRefreshLayout swipeRefresh = v.findViewById(R.id.pullToRefresh); | ||||
|  | ||||
| 		// if gitea is 1.12 or higher use the new limit | ||||
| 		if(VersionCheck.compareVersion("1.12.0", tinyDb.getString("giteaVersion")) >= 1) { | ||||
| 		if(new Version(tinyDb.getString("giteaVersion")).higherOrEqual("1.12.0")) { | ||||
| 			resultLimit = StaticGlobalVariables.resultLimitNewGiteaInstances; | ||||
| 		} | ||||
|  | ||||
|   | ||||
							
								
								
									
										222
									
								
								app/src/main/java/org/mian/gitnex/helpers/Version.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										222
									
								
								app/src/main/java/org/mian/gitnex/helpers/Version.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,222 @@ | ||||
| package org.mian.gitnex.helpers; | ||||
|  | ||||
| import org.jetbrains.annotations.NotNull; | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| import java.util.regex.Matcher; | ||||
| import java.util.regex.Pattern; | ||||
|  | ||||
| /** | ||||
|  * Author 6543 | ||||
|  */ | ||||
|  | ||||
| public class Version { | ||||
|  | ||||
| 	// the raw String | ||||
| 	private String raw; | ||||
| 	// the version numbers in its order (dot separated) | ||||
| 	private List<Integer> values; | ||||
|  | ||||
| 	public Version(String value) { | ||||
|  | ||||
| 		raw = value; | ||||
| 		this.init(); | ||||
|  | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * init parse and store values for other functions of an Version instance | ||||
| 	 * it use the raw variable as base | ||||
| 	 * | ||||
| 	 * @return if parse was successfully | ||||
| 	 */ | ||||
| 	private void init() { | ||||
|  | ||||
| 		final Pattern pattern_valid = Pattern.compile("^[v,V]?(\\d+)+(\\.(\\d+))*([_,\\-,+][\\w,\\d,_,\\-,+]*)?$"); | ||||
| 		final Pattern pattern_number_dot_number = Pattern.compile("^\\d+(\\.(\\d)+)*"); | ||||
|  | ||||
| 		if(!pattern_valid.matcher(raw).find()) { | ||||
| 			throw new IllegalArgumentException("Invalid version format"); | ||||
| 		} | ||||
|  | ||||
| 		if(raw.charAt(0) == 'v' || raw.charAt(0) == 'V') { | ||||
| 			raw = raw.substring(1); | ||||
| 		} | ||||
|  | ||||
| 		values = new ArrayList<Integer>(); | ||||
| 		Matcher match = pattern_number_dot_number.matcher(raw); | ||||
| 		match.find(); | ||||
| 		for(String i : match.group().split("\\.")) { | ||||
| 			values.add(Integer.parseInt(i)); | ||||
| 		} | ||||
|  | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * equal return true if version is the same | ||||
| 	 * | ||||
| 	 * @param value | ||||
| 	 * @return true/false | ||||
| 	 */ | ||||
| 	public boolean equal(String value) { | ||||
|  | ||||
| 		return this.equal(new Version(value)); | ||||
|  | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * equal return true if version is the same | ||||
| 	 * | ||||
| 	 * @param v | ||||
| 	 * @return | ||||
| 	 */ | ||||
| 	public boolean equal(@NotNull Version v) { | ||||
|  | ||||
| 		int rounds = Math.min(this.values.size(), v.values.size()); | ||||
| 		for(int i = 0; i < rounds; i++) { | ||||
| 			if(this.values.get(i) != v.values.get(i)) { | ||||
| 				return false; | ||||
| 			} | ||||
| 		} | ||||
| 		return true; | ||||
|  | ||||
| 	} | ||||
|  | ||||
|  | ||||
| 	/** | ||||
| 	 * less return true if version is less | ||||
| 	 * | ||||
| 	 * @param value | ||||
| 	 * @return true/false | ||||
| 	 */ | ||||
| 	public boolean less(String value) { | ||||
|  | ||||
| 		return this.less(new Version(value)); | ||||
|  | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * less return true if version is less | ||||
| 	 * | ||||
| 	 * @param v | ||||
| 	 * @return | ||||
| 	 */ | ||||
| 	public boolean less(@NotNull Version v) { | ||||
|  | ||||
| 		int rounds = Math.min(this.values.size(), v.values.size()); | ||||
| 		for(int i = 0; i < rounds; i++) { | ||||
| 			if(i + 1 == rounds) { | ||||
| 				if(this.values.get(i) >= v.values.get(i)) { | ||||
| 					return false; | ||||
| 				} | ||||
| 			} | ||||
| 			else { | ||||
| 				if(this.values.get(i) > v.values.get(i)) { | ||||
| 					return false; | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 		return true; | ||||
|  | ||||
| 	} | ||||
|  | ||||
|  | ||||
| 	/** | ||||
| 	 * higher return true if version is higher | ||||
| 	 * | ||||
| 	 * @param value | ||||
| 	 * @return true/false | ||||
| 	 */ | ||||
| 	public boolean higher(String value) { | ||||
|  | ||||
| 		return this.higher(new Version(value)); | ||||
|  | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * higher return true if version is higher | ||||
| 	 * | ||||
| 	 * @param v | ||||
| 	 * @return | ||||
| 	 */ | ||||
| 	public boolean higher(@NotNull Version v) { | ||||
|  | ||||
| 		int rounds = Math.min(this.values.size(), v.values.size()); | ||||
| 		for(int i = 0; i < rounds; i++) { | ||||
| 			if(i + 1 == rounds) { | ||||
| 				if(this.values.get(i) <= v.values.get(i)) { | ||||
| 					return false; | ||||
| 				} | ||||
| 			} | ||||
| 			else { | ||||
| 				if(this.values.get(i) < v.values.get(i)) { | ||||
| 					return false; | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 		return true; | ||||
|  | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * lessOrEqual return true if version is less or equal | ||||
| 	 * | ||||
| 	 * @param value | ||||
| 	 * @return true/false | ||||
| 	 */ | ||||
| 	public boolean lessOrEqual(String value) { | ||||
|  | ||||
| 		return this.lessOrEqual(new Version(value)); | ||||
|  | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * lessOrEqual return true if version is less or equal | ||||
| 	 * | ||||
| 	 * @param v | ||||
| 	 * @return | ||||
| 	 */ | ||||
| 	public boolean lessOrEqual(@NotNull Version v) { | ||||
|  | ||||
| 		int rounds = Math.min(this.values.size(), v.values.size()); | ||||
| 		for(int i = 0; i < rounds; i++) { | ||||
| 			if(this.values.get(i) > v.values.get(i)) { | ||||
| 				return false; | ||||
| 			} | ||||
| 		} | ||||
| 		return true; | ||||
|  | ||||
| 	} | ||||
|  | ||||
|  | ||||
| 	/** | ||||
| 	 * higherOrEqual return true if version is higher or equal | ||||
| 	 * | ||||
| 	 * @param value | ||||
| 	 * @return true/false | ||||
| 	 */ | ||||
| 	public boolean higherOrEqual(String value) { | ||||
|  | ||||
| 		return this.higherOrEqual(new Version(value)); | ||||
|  | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * higherOrEqual return true if version is higher or equal | ||||
| 	 * | ||||
| 	 * @param v | ||||
| 	 * @return | ||||
| 	 */ | ||||
| 	public boolean higherOrEqual(@NotNull Version v) { | ||||
|  | ||||
| 		int rounds = Math.min(this.values.size(), v.values.size()); | ||||
| 		for(int i = 0; i < rounds; i++) { | ||||
| 			if(this.values.get(i) < v.values.get(i)) { | ||||
| 				return false; | ||||
| 			} | ||||
| 		} | ||||
| 		return true; | ||||
|  | ||||
| 	} | ||||
|  | ||||
| } | ||||
| @@ -1,151 +0,0 @@ | ||||
| package org.mian.gitnex.helpers; | ||||
|  | ||||
| import java.util.regex.Matcher; | ||||
| import java.util.regex.Pattern; | ||||
|  | ||||
| /** | ||||
|  * Author 6543 | ||||
|  */ | ||||
|  | ||||
| public enum VersionCheck { | ||||
|  | ||||
|     UNKNOWN, | ||||
|     SUPPORTED_LATEST, | ||||
|     SUPPORTED_OLD, | ||||
|     DEVELOPMENT, | ||||
|     UNSUPPORTED_OLD, | ||||
|     UNSUPPORTED_NEW; | ||||
|  | ||||
|     public static VersionCheck check(String min, String last, String value) { | ||||
|  | ||||
|         final Pattern pattern_stable_release = Pattern.compile("^(\\d)\\.(\\d+)\\.(\\d+)$"); | ||||
|         final Pattern pattern_dev_release = Pattern.compile("^(\\d).(\\d+).(\\d+)(\\D)(.+)"); | ||||
|         Matcher match; | ||||
|  | ||||
|         if (!pattern_stable_release.matcher(min).find() || !pattern_stable_release.matcher(last).find()) { | ||||
|             throw new IllegalArgumentException("VersionCheck: wrong format for min or last version given"); | ||||
|         } | ||||
|  | ||||
|         match = pattern_stable_release.matcher(value); | ||||
|         if (match.find()) { | ||||
|  | ||||
|             switch (correlate(min, last, match.group())){ | ||||
|                 case 0: | ||||
|                     return UNSUPPORTED_OLD; | ||||
|                 case 1: | ||||
|                     return SUPPORTED_OLD; | ||||
|                 case 2: | ||||
|                     return SUPPORTED_LATEST; | ||||
|                 default: | ||||
|                     return UNSUPPORTED_NEW; | ||||
|             } | ||||
|  | ||||
|         } | ||||
|  | ||||
|         match = pattern_dev_release.matcher(value); | ||||
|         if (match.find()) { | ||||
|  | ||||
|             match = Pattern.compile("^(\\d)\\.(\\d+)\\.(\\d+)").matcher(value); | ||||
|             match.find(); | ||||
|  | ||||
|             if (correlate(min, last, match.group())>0) { | ||||
|                 return DEVELOPMENT; | ||||
|             } | ||||
|             else { | ||||
|                 return UNSUPPORTED_OLD; | ||||
|             } | ||||
|  | ||||
|         } | ||||
|  | ||||
|         return UNKNOWN; | ||||
|  | ||||
|     } | ||||
|  | ||||
|     //helper | ||||
|     // 0 to less | ||||
|     // 1 in range | ||||
|     // 2 at the top | ||||
|     // 3 above | ||||
|     private static int correlate(String min, String last, String value){ | ||||
|         int min_check = compareVersion(value,min); | ||||
|         int max_check = compareVersion(value,last); | ||||
|         int range_check = compareVersion(min,last); | ||||
|  | ||||
|         switch (range_check) { | ||||
|             case 2: | ||||
|                 throw new IllegalArgumentException("Minimum Version higher than Last Version"); | ||||
|             case 1: //min == last | ||||
|                 switch (min_check) { | ||||
|                     case 0: | ||||
|                         return 0; | ||||
|                     case 1: | ||||
|                         return 2; | ||||
|                     default: | ||||
|                         return 3; | ||||
|                 } | ||||
|             default: | ||||
|                 if (max_check >1) return 3; | ||||
|                 if (max_check == 1) return 2; | ||||
|                 if (min_check < 1) return 0; | ||||
|                 return 1; | ||||
|         } | ||||
|  | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @description compare doted formatted Versions | ||||
|      * @param A doted formatted Versions | ||||
|      * @param B doted formatted Versions | ||||
|      * @return 0|1|2 | ||||
|      * 0 = less | ||||
|      * 1 = same | ||||
|      * 2 = more | ||||
|      */ | ||||
|     public static int compareVersion(String A, String B) { | ||||
|         final Pattern pattern_stable_release = Pattern.compile("^(\\d)\\.(\\d+)\\.(\\d+)"); | ||||
|         final Pattern pattern_dev_release = Pattern.compile("^(\\d).(\\d+).(\\d+)(\\D)(.+)"); | ||||
|         Matcher match; | ||||
|         match = pattern_dev_release.matcher(A); | ||||
|         if (match.find()) { | ||||
|             match = pattern_stable_release.matcher(A); | ||||
|             match.find(); | ||||
|             A = match.group(); | ||||
|         } | ||||
|         match = pattern_dev_release.matcher(B); | ||||
|         if (match.find()) { | ||||
|             match = pattern_stable_release.matcher(B); | ||||
|             match.find(); | ||||
|             B = match.group(); | ||||
|         } | ||||
|  | ||||
|         //throw new IllegalArgumentException | ||||
|         if((!A.matches("[0-9]+(\\.[0-9]+)*")) || (!B.matches("[0-9]+(\\.[0-9]+)*"))) throw new IllegalArgumentException("Invalid version format"); | ||||
|  | ||||
|         if (A.contains(".") || B.contains(".")) { | ||||
|             // example 2 vs 1.3 | ||||
|             if (!(A.contains(".") && B.contains("."))) { | ||||
|                 if (A.contains(".")) { | ||||
|                     return compareVersion(A,B + ".0"); | ||||
|                 } | ||||
|                 if (B.contains(".")) { | ||||
|                     return compareVersion(A + ".0",B); | ||||
|                 } | ||||
|             } | ||||
|  | ||||
|             //normal compare | ||||
|             int a = Integer.parseInt(A.substring(0,A.indexOf("."))); | ||||
|             int b = Integer.parseInt(B.substring(0,B.indexOf("."))); | ||||
|             if (a < b) return 0; | ||||
|             if (a == b) return compareVersion(A.substring(A.indexOf(".")+1),B.substring(B.indexOf(".")+1)); | ||||
|             return 2; //if (a > b) | ||||
|         } | ||||
|         else { | ||||
|             int a = Integer.parseInt(A); | ||||
|             int b = Integer.parseInt(B); | ||||
|             if (a < b) return 0; | ||||
|             if (a == b) return 1; | ||||
|             return 2; //if (a > b) | ||||
|         } | ||||
|     } | ||||
|  | ||||
| } | ||||
		Reference in New Issue
	
	Block a user