From 50e0142f6ce31991132aa9d8df91126a11c406b0 Mon Sep 17 00:00:00 2001 From: 6543 <6543@noreply.codeberg.org> Date: Tue, 9 Jun 2020 15:36:08 +0200 Subject: [PATCH] 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 --- .../gitnex/activities/FileDiffActivity.java | 273 +++++++++--------- .../BottomSheetSingleIssueFragment.java | 5 +- .../mian/gitnex/interfaces/ApiInterface.java | 3 + 3 files changed, 151 insertions(+), 130 deletions(-) diff --git a/app/src/main/java/org/mian/gitnex/activities/FileDiffActivity.java b/app/src/main/java/org/mian/gitnex/activities/FileDiffActivity.java index e855a8f9..bf421a43 100644 --- a/app/src/main/java/org/mian/gitnex/activities/FileDiffActivity.java +++ b/app/src/main/java/org/mian/gitnex/activities/FileDiffActivity.java @@ -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; @@ -32,191 +33,205 @@ import retrofit2.Callback; public class FileDiffActivity extends BaseActivity { - private View.OnClickListener onClickListener; - private TextView toolbar_title; - private ListView mListView; - private ProgressBar mProgressBar; - final Context ctx = this; - private Context appCtx; + private View.OnClickListener onClickListener; + private TextView toolbar_title; + private ListView mListView; + private ProgressBar mProgressBar; + final Context ctx = this; + private Context appCtx; - @Override - protected int getLayoutResourceId(){ - return R.layout.activity_file_diff; - } + @Override + protected int getLayoutResourceId() { - @Override - public void onCreate(Bundle savedInstanceState) { + return R.layout.activity_file_diff; + } - super.onCreate(savedInstanceState); - appCtx = getApplicationContext(); + @Override + public void onCreate(Bundle savedInstanceState) { - Toolbar toolbar = findViewById(R.id.toolbar); - setSupportActionBar(toolbar); + super.onCreate(savedInstanceState); + appCtx = getApplicationContext(); - final TinyDB tinyDb = new TinyDB(appCtx); - String repoFullName = tinyDb.getString("repoFullName"); - 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"); + Toolbar toolbar = findViewById(R.id.toolbar); + setSupportActionBar(toolbar); - ImageView closeActivity = findViewById(R.id.close); - toolbar_title = findViewById(R.id.toolbar_title); - mListView = findViewById(R.id.listView); - mProgressBar = findViewById(R.id.progress_bar); + final TinyDB tinyDb = new TinyDB(appCtx); + String repoFullName = tinyDb.getString("repoFullName"); + String[] parts = repoFullName.split("/"); + final String repoOwner = parts[0]; + final String repoName = parts[1]; + final String loginUid = tinyDb.getString("loginUid"); + final String instanceToken = "token " + tinyDb.getString(loginUid + "-token"); - mListView.setDivider(null); + ImageView closeActivity = findViewById(R.id.close); + toolbar_title = findViewById(R.id.toolbar_title); + mListView = findViewById(R.id.listView); + mProgressBar = findViewById(R.id.progress_bar); - toolbar_title.setText(R.string.processingText); - initCloseListener(); - closeActivity.setOnClickListener(onClickListener); + mListView.setDivider(null); - mProgressBar.setVisibility(View.VISIBLE); + toolbar_title.setText(R.string.processingText); + initCloseListener(); + closeActivity.setOnClickListener(onClickListener); - String pullIndex = tinyDb.getString("issueNumber"); + mProgressBar.setVisibility(View.VISIBLE); - getPullDiffContent(tinyDb.getString("instanceUrlWithProtocol"), repoOwner, repoName, pullIndex); + String pullIndex = tinyDb.getString("issueNumber"); - } + boolean apiCall = true; + String instanceUrl = tinyDb.getString("instanceUrl"); - private void getPullDiffContent(String instanceUrl, String owner, String repo, String filename) { + // fallback for old gitea instances + if(new Version(tinyDb.getString("giteaVersion")).less("1.13.0")) { + apiCall = true; + instanceUrl = tinyDb.getString("instanceUrlWithProtocol"); + } - Call call = RetrofitClient - .getInstance(instanceUrl, ctx) - .getWebInterface() - .getPullDiffContent(owner, repo, filename); + getPullDiffContent(instanceUrl, repoOwner, repoName, pullIndex, instanceToken, apiCall); - call.enqueue(new Callback() { + } - @Override - public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { + private void getPullDiffContent(String instanceUrl, String owner, String repo, String pullIndex, String token, boolean apiCall) { - if (response.code() == 200) { + Call 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); + } - try { - assert response.body() != null; + call.enqueue(new Callback() { - AppUtil appUtil = new AppUtil(); - List fileContentsArray = new ArrayList<>(); + @Override + public void onResponse(@NonNull Call call, @NonNull retrofit2.Response response) { - String[] lines = response.body().string().split("diff"); + if(response.code() == 200) { - if(lines.length > 0) { + try { + assert response.body() != null; - for (int i = 1; i < lines.length; i++) { + AppUtil appUtil = new AppUtil(); + List fileContentsArray = new ArrayList<>(); - if(lines[i].contains("@@ -")) { + String[] lines = response.body().string().split("diff"); - String[] level2nd = lines[i].split("@@ -"); // main content part of single diff view + if(lines.length > 0) { - String[] fileName_ = level2nd[0].split("\\+\\+\\+ b/"); // filename part - String fileNameFinal = fileName_[1]; + for(int i = 1; i < lines.length; i++) { - String[] fileContents_ = level2nd[1].split("@@"); // file info / content part - String fileInfoFinal = fileContents_[0]; - StringBuilder fileContentsFinal = new StringBuilder(fileContents_[1]); + if(lines[i].contains("@@ -")) { - if(level2nd.length > 2) { - for (int j = 2; j < level2nd.length; j++) { - fileContentsFinal.append(level2nd[j]); - } - } + String[] level2nd = lines[i].split("@@ -"); // main content part of single diff view - String fileExtension = FileUtils.getExtension(fileNameFinal); + String[] fileName_ = level2nd[0].split("\\+\\+\\+ b/"); // filename part + String fileNameFinal = fileName_[1]; - 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[] fileContents_ = level2nd[1].split("@@"); // file info / content part + String fileInfoFinal = fileContents_[0]; + StringBuilder fileContentsFinal = new StringBuilder(fileContents_[1]); - fileContentsArray.add(new FileDiffView(fileNameFinal, appUtil.imageExtension(fileExtension), fileInfoFinal, fileContentsFinalWithoutBlankLines)); - } - else { + if(level2nd.length > 2) { + for(int j = 2; j < level2nd.length; j++) { + fileContentsFinal.append(level2nd[j]); + } + } - String[] getFileName = lines[i].split("--git a/"); + String fileExtension = FileUtils.getExtension(fileNameFinal); - String[] getFileName_ = getFileName[1].split("b/"); - String getFileNameFinal = getFileName_[0].trim(); + 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[] 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); + fileContentsArray.add(new FileDiffView(fileNameFinal, appUtil.imageExtension(fileExtension), fileInfoFinal, fileContentsFinalWithoutBlankLines)); + } + else { - String fileExtension = FileUtils.getExtension(getFileNameFinal); + String[] getFileName = lines[i].split("--git a/"); - if(appUtil.imageExtension(FileUtils.getExtension(getFileNameFinal))) { + String[] getFileName_ = getFileName[1].split("b/"); + String getFileNameFinal = getFileName_[0].trim(); - fileContentsArray.add(new FileDiffView(getFileNameFinal, appUtil.imageExtension(fileExtension), "", binaryFileFinal)); - } + 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 fileExtension = FileUtils.getExtension(getFileNameFinal); - } + if(appUtil.imageExtension(FileUtils.getExtension(getFileNameFinal))) { - } + fileContentsArray.add(new FileDiffView(getFileNameFinal, appUtil.imageExtension(fileExtension), "", binaryFileFinal)); + } - int filesCount = fileContentsArray.size(); - if(filesCount > 1) { - toolbar_title.setText(getResources().getString(R.string.fileDiffViewHeader, Integer.toString(filesCount))); - } - else { - toolbar_title.setText(getResources().getString(R.string.fileDiffViewHeaderSingle, Integer.toString(filesCount))); - } + } - FilesDiffAdapter adapter = new FilesDiffAdapter(ctx, fileContentsArray); - mListView.setAdapter(adapter); + } - mProgressBar.setVisibility(View.GONE); + } - } catch (IOException e) { - e.printStackTrace(); - } + int filesCount = fileContentsArray.size(); + if(filesCount > 1) { + toolbar_title.setText(getResources().getString(R.string.fileDiffViewHeader, Integer.toString(filesCount))); + } + else { + toolbar_title.setText(getResources().getString(R.string.fileDiffViewHeaderSingle, Integer.toString(filesCount))); + } - } - else if(response.code() == 401) { + FilesDiffAdapter adapter = new FilesDiffAdapter(ctx, fileContentsArray); + mListView.setAdapter(adapter); - AlertDialogs.authorizationTokenRevokedDialog(ctx, getResources().getString(R.string.alertDialogTokenRevokedTitle), - getResources().getString(R.string.alertDialogTokenRevokedMessage), - getResources().getString(R.string.alertDialogTokenRevokedCopyNegativeButton), - getResources().getString(R.string.alertDialogTokenRevokedCopyPositiveButton)); + mProgressBar.setVisibility(View.GONE); - } - else if(response.code() == 403) { + } + catch(IOException e) { + e.printStackTrace(); + } - Toasty.info(ctx, ctx.getString(R.string.authorizeError)); + } + else if(response.code() == 401) { - } - else if(response.code() == 404) { + AlertDialogs.authorizationTokenRevokedDialog(ctx, getResources().getString(R.string.alertDialogTokenRevokedTitle), getResources().getString(R.string.alertDialogTokenRevokedMessage), getResources().getString(R.string.alertDialogTokenRevokedCopyNegativeButton), getResources().getString(R.string.alertDialogTokenRevokedCopyPositiveButton)); - Toasty.info(ctx, ctx.getString(R.string.apiNotFound)); + } + else if(response.code() == 403) { - } - else { + Toasty.info(ctx, ctx.getString(R.string.authorizeError)); - Toasty.info(ctx, getString(R.string.labelGeneralError)); + } + else if(response.code() == 404) { - } + Toasty.info(ctx, ctx.getString(R.string.apiNotFound)); - } + } + else { - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - Log.e("onFailure", t.toString()); - } - }); + Toasty.info(ctx, getString(R.string.labelGeneralError)); - } + } - private void initCloseListener() { - onClickListener = new View.OnClickListener() { - @Override - public void onClick(View view) { - getIntent().removeExtra("singleFileName"); - finish(); - } - }; - } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + + Log.e("onFailure", t.toString()); + } + }); + + } + + private void initCloseListener() { + + onClickListener = new View.OnClickListener() { + + @Override + public void onClick(View view) { + + getIntent().removeExtra("singleFileName"); + finish(); + } + }; + } } diff --git a/app/src/main/java/org/mian/gitnex/fragments/BottomSheetSingleIssueFragment.java b/app/src/main/java/org/mian/gitnex/fragments/BottomSheetSingleIssueFragment.java index ca88af63..2e8cc667 100644 --- a/app/src/main/java/org/mian/gitnex/fragments/BottomSheetSingleIssueFragment.java +++ b/app/src/main/java/org/mian/gitnex/fragments/BottomSheetSingleIssueFragment.java @@ -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 { diff --git a/app/src/main/java/org/mian/gitnex/interfaces/ApiInterface.java b/app/src/main/java/org/mian/gitnex/interfaces/ApiInterface.java index 1a7d41d9..9a8bd49b 100644 --- a/app/src/main/java/org/mian/gitnex/interfaces/ApiInterface.java +++ b/app/src/main/java/org/mian/gitnex/interfaces/ApiInterface.java @@ -267,6 +267,9 @@ public interface ApiInterface { @GET("repos/{owner}/{repo}/pulls") // get repository pull requests Call> 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 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 mergePullRequest(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName, @Path("index") int index, @Body MergePullRequest jsonStr);