Pull Diff View: Use new API if gitea >= 1.13.0 (#536)
right arg order & right instanceUrl use new API if gitea >= 1.13.0 correct name format code Co-authored-by: 6543 <6543@obermui.de> Reviewed-by: opyale <opyale@noreply.codeberg.org>
This commit is contained in:
		| @@ -16,6 +16,7 @@ import org.mian.gitnex.adapters.FilesDiffAdapter; | ||||
| import org.mian.gitnex.clients.RetrofitClient; | ||||
| import org.mian.gitnex.helpers.AlertDialogs; | ||||
| import org.mian.gitnex.helpers.Toasty; | ||||
| import org.mian.gitnex.helpers.Version; | ||||
| import org.mian.gitnex.models.FileDiffView; | ||||
| import org.mian.gitnex.util.AppUtil; | ||||
| import org.mian.gitnex.util.TinyDB; | ||||
| @@ -40,7 +41,8 @@ public class FileDiffActivity extends BaseActivity { | ||||
| 	private Context appCtx; | ||||
|  | ||||
| 	@Override | ||||
|     protected int getLayoutResourceId(){ | ||||
| 	protected int getLayoutResourceId() { | ||||
|  | ||||
| 		return R.layout.activity_file_diff; | ||||
| 	} | ||||
|  | ||||
| @@ -58,7 +60,6 @@ public class FileDiffActivity extends BaseActivity { | ||||
| 		String[] parts = repoFullName.split("/"); | ||||
| 		final String repoOwner = parts[0]; | ||||
| 		final String repoName = parts[1]; | ||||
|         final String instanceUrl = tinyDb.getString("instanceUrl"); | ||||
| 		final String loginUid = tinyDb.getString("loginUid"); | ||||
| 		final String instanceToken = "token " + tinyDb.getString(loginUid + "-token"); | ||||
|  | ||||
| @@ -77,23 +78,35 @@ public class FileDiffActivity extends BaseActivity { | ||||
|  | ||||
| 		String pullIndex = tinyDb.getString("issueNumber"); | ||||
|  | ||||
|         getPullDiffContent(tinyDb.getString("instanceUrlWithProtocol"), repoOwner, repoName, pullIndex); | ||||
| 		boolean apiCall = true; | ||||
| 		String instanceUrl = tinyDb.getString("instanceUrl"); | ||||
|  | ||||
| 		// fallback for old gitea instances | ||||
| 		if(new Version(tinyDb.getString("giteaVersion")).less("1.13.0")) { | ||||
| 			apiCall = true; | ||||
| 			instanceUrl = tinyDb.getString("instanceUrlWithProtocol"); | ||||
| 		} | ||||
|  | ||||
| 		getPullDiffContent(instanceUrl, repoOwner, repoName, pullIndex, instanceToken, apiCall); | ||||
|  | ||||
| 	} | ||||
|  | ||||
|     private void getPullDiffContent(String instanceUrl, String owner, String repo, String filename) { | ||||
| 	private void getPullDiffContent(String instanceUrl, String owner, String repo, String pullIndex, String token, boolean apiCall) { | ||||
|  | ||||
|         Call<ResponseBody> call = RetrofitClient | ||||
|                 .getInstance(instanceUrl, ctx) | ||||
|                 .getWebInterface() | ||||
|                 .getPullDiffContent(owner, repo, filename); | ||||
| 		Call<ResponseBody> call; | ||||
| 		if(apiCall) { | ||||
| 			call = RetrofitClient.getInstance(instanceUrl, ctx).getApiInterface().getPullDiffContent(token, owner, repo, pullIndex); | ||||
| 		} | ||||
| 		else { | ||||
| 			call = RetrofitClient.getInstance(instanceUrl, ctx).getWebInterface().getPullDiffContent(owner, repo, pullIndex); | ||||
| 		} | ||||
|  | ||||
| 		call.enqueue(new Callback<ResponseBody>() { | ||||
|  | ||||
| 			@Override | ||||
| 			public void onResponse(@NonNull Call<ResponseBody> call, @NonNull retrofit2.Response<ResponseBody> response) { | ||||
|  | ||||
|                 if (response.code() == 200) { | ||||
| 				if(response.code() == 200) { | ||||
|  | ||||
| 					try { | ||||
| 						assert response.body() != null; | ||||
| @@ -105,7 +118,7 @@ public class FileDiffActivity extends BaseActivity { | ||||
|  | ||||
| 						if(lines.length > 0) { | ||||
|  | ||||
|                             for (int i = 1; i < lines.length; i++) { | ||||
| 							for(int i = 1; i < lines.length; i++) { | ||||
|  | ||||
| 								if(lines[i].contains("@@ -")) { | ||||
|  | ||||
| @@ -119,16 +132,16 @@ public class FileDiffActivity extends BaseActivity { | ||||
| 									StringBuilder fileContentsFinal = new StringBuilder(fileContents_[1]); | ||||
|  | ||||
| 									if(level2nd.length > 2) { | ||||
|                                         for (int j = 2; j < level2nd.length; j++) { | ||||
| 										for(int j = 2; j < level2nd.length; j++) { | ||||
| 											fileContentsFinal.append(level2nd[j]); | ||||
| 										} | ||||
| 									} | ||||
|  | ||||
| 									String fileExtension = FileUtils.getExtension(fileNameFinal); | ||||
|  | ||||
|                                     String fileContentsFinalWithBlankLines = fileContentsFinal.toString().replaceAll( ".*@@.*", "" ); | ||||
|                                     String fileContentsFinalWithoutBlankLines = fileContentsFinal.toString().replaceAll( ".*@@.*(\r?\n|\r)?", "" ); | ||||
|                                     fileContentsFinalWithoutBlankLines = fileContentsFinalWithoutBlankLines.replaceAll( ".*\\ No newline at end of file.*(\r?\n|\r)?", "" ); | ||||
| 									String fileContentsFinalWithBlankLines = fileContentsFinal.toString().replaceAll(".*@@.*", ""); | ||||
| 									String fileContentsFinalWithoutBlankLines = fileContentsFinal.toString().replaceAll(".*@@.*(\r?\n|\r)?", ""); | ||||
| 									fileContentsFinalWithoutBlankLines = fileContentsFinalWithoutBlankLines.replaceAll(".*\\ No newline at end of file.*(\r?\n|\r)?", ""); | ||||
|  | ||||
| 									fileContentsArray.add(new FileDiffView(fileNameFinal, appUtil.imageExtension(fileExtension), fileInfoFinal, fileContentsFinalWithoutBlankLines)); | ||||
| 								} | ||||
| @@ -140,8 +153,8 @@ public class FileDiffActivity extends BaseActivity { | ||||
| 									String getFileNameFinal = getFileName_[0].trim(); | ||||
|  | ||||
| 									String[] binaryFile = getFileName_[1].split("GIT binary patch"); | ||||
|                                     String binaryFileRaw = binaryFile[1].substring(binaryFile[1].indexOf('\n')+1); | ||||
|                                     String binaryFileFinal = binaryFile[1].substring(binaryFileRaw.indexOf('\n')+1); | ||||
| 									String binaryFileRaw = binaryFile[1].substring(binaryFile[1].indexOf('\n') + 1); | ||||
| 									String binaryFileFinal = binaryFile[1].substring(binaryFileRaw.indexOf('\n') + 1); | ||||
|  | ||||
| 									String fileExtension = FileUtils.getExtension(getFileNameFinal); | ||||
|  | ||||
| @@ -169,17 +182,15 @@ public class FileDiffActivity extends BaseActivity { | ||||
|  | ||||
| 						mProgressBar.setVisibility(View.GONE); | ||||
|  | ||||
|                     } catch (IOException e) { | ||||
| 					} | ||||
| 					catch(IOException e) { | ||||
| 						e.printStackTrace(); | ||||
| 					} | ||||
|  | ||||
| 				} | ||||
| 				else if(response.code() == 401) { | ||||
|  | ||||
|                     AlertDialogs.authorizationTokenRevokedDialog(ctx, getResources().getString(R.string.alertDialogTokenRevokedTitle), | ||||
|                             getResources().getString(R.string.alertDialogTokenRevokedMessage), | ||||
|                             getResources().getString(R.string.alertDialogTokenRevokedCopyNegativeButton), | ||||
|                             getResources().getString(R.string.alertDialogTokenRevokedCopyPositiveButton)); | ||||
| 					AlertDialogs.authorizationTokenRevokedDialog(ctx, getResources().getString(R.string.alertDialogTokenRevokedTitle), getResources().getString(R.string.alertDialogTokenRevokedMessage), getResources().getString(R.string.alertDialogTokenRevokedCopyNegativeButton), getResources().getString(R.string.alertDialogTokenRevokedCopyPositiveButton)); | ||||
|  | ||||
| 				} | ||||
| 				else if(response.code() == 403) { | ||||
| @@ -202,6 +213,7 @@ public class FileDiffActivity extends BaseActivity { | ||||
|  | ||||
| 			@Override | ||||
| 			public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) { | ||||
|  | ||||
| 				Log.e("onFailure", t.toString()); | ||||
| 			} | ||||
| 		}); | ||||
| @@ -209,9 +221,12 @@ public class FileDiffActivity extends BaseActivity { | ||||
| 	} | ||||
|  | ||||
| 	private void initCloseListener() { | ||||
|  | ||||
| 		onClickListener = new View.OnClickListener() { | ||||
|  | ||||
| 			@Override | ||||
| 			public void onClick(View view) { | ||||
|  | ||||
| 				getIntent().removeExtra("singleFileName"); | ||||
| 				finish(); | ||||
| 			} | ||||
|   | ||||
| @@ -64,7 +64,10 @@ public class BottomSheetSingleIssueFragment extends BottomSheetDialogFragment { | ||||
| 				mergePullRequest.setVisibility(View.VISIBLE); | ||||
| 			} | ||||
|  | ||||
| 			if(tinyDB.getString("repoType").equals("public")) { | ||||
| 			if(new Version(tinyDB.getString("giteaVersion")).higherOrEqual("1.13.0")) { | ||||
| 				openFilesDiff.setVisibility(View.VISIBLE); | ||||
| 			} | ||||
| 			else if(tinyDB.getString("repoType").equals("public")) { | ||||
| 				openFilesDiff.setVisibility(View.VISIBLE); | ||||
| 			} | ||||
| 			else { | ||||
|   | ||||
| @@ -267,6 +267,9 @@ public interface ApiInterface { | ||||
|     @GET("repos/{owner}/{repo}/pulls") // get repository pull requests | ||||
|     Call<List<PullRequests>> getPullRequests(@Header("Authorization") String token, @Path("owner") String owner, @Path("repo") String repo, @Query("page") int page, @Query("state") String state, @Query("limit") int limit); | ||||
|  | ||||
|     @GET("repos/{owner}/{repo}/pulls/{index}.diff") // get pull diff file contents | ||||
|     Call<ResponseBody> getPullDiffContent(@Header("Authorization") String token, @Path("owner") String owner, @Path("repo") String repo, @Path("index") String pullIndex); | ||||
|  | ||||
|     @POST("repos/{owner}/{repo}/pulls/{index}/merge") // merge a pull request | ||||
|     Call<ResponseBody> mergePullRequest(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName, @Path("index") int index, @Body MergePullRequest jsonStr); | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user