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:
6543 2020-06-09 15:36:08 +02:00
parent 42640f2d1c
commit 50e0142f6c
3 changed files with 151 additions and 130 deletions

View File

@ -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;
@ -41,6 +42,7 @@ public class FileDiffActivity extends BaseActivity {
@Override
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,16 +78,28 @@ 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>() {
@ -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();
}

View File

@ -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 {

View File

@ -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);