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:
parent
42640f2d1c
commit
50e0142f6c
@ -16,6 +16,7 @@ import org.mian.gitnex.adapters.FilesDiffAdapter;
|
|||||||
import org.mian.gitnex.clients.RetrofitClient;
|
import org.mian.gitnex.clients.RetrofitClient;
|
||||||
import org.mian.gitnex.helpers.AlertDialogs;
|
import org.mian.gitnex.helpers.AlertDialogs;
|
||||||
import org.mian.gitnex.helpers.Toasty;
|
import org.mian.gitnex.helpers.Toasty;
|
||||||
|
import org.mian.gitnex.helpers.Version;
|
||||||
import org.mian.gitnex.models.FileDiffView;
|
import org.mian.gitnex.models.FileDiffView;
|
||||||
import org.mian.gitnex.util.AppUtil;
|
import org.mian.gitnex.util.AppUtil;
|
||||||
import org.mian.gitnex.util.TinyDB;
|
import org.mian.gitnex.util.TinyDB;
|
||||||
@ -40,7 +41,8 @@ public class FileDiffActivity extends BaseActivity {
|
|||||||
private Context appCtx;
|
private Context appCtx;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected int getLayoutResourceId(){
|
protected int getLayoutResourceId() {
|
||||||
|
|
||||||
return R.layout.activity_file_diff;
|
return R.layout.activity_file_diff;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -58,7 +60,6 @@ public class FileDiffActivity extends BaseActivity {
|
|||||||
String[] parts = repoFullName.split("/");
|
String[] parts = repoFullName.split("/");
|
||||||
final String repoOwner = parts[0];
|
final String repoOwner = parts[0];
|
||||||
final String repoName = parts[1];
|
final String repoName = parts[1];
|
||||||
final String instanceUrl = tinyDb.getString("instanceUrl");
|
|
||||||
final String loginUid = tinyDb.getString("loginUid");
|
final String loginUid = tinyDb.getString("loginUid");
|
||||||
final String instanceToken = "token " + tinyDb.getString(loginUid + "-token");
|
final String instanceToken = "token " + tinyDb.getString(loginUid + "-token");
|
||||||
|
|
||||||
@ -77,23 +78,35 @@ public class FileDiffActivity extends BaseActivity {
|
|||||||
|
|
||||||
String pullIndex = tinyDb.getString("issueNumber");
|
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
|
Call<ResponseBody> call;
|
||||||
.getInstance(instanceUrl, ctx)
|
if(apiCall) {
|
||||||
.getWebInterface()
|
call = RetrofitClient.getInstance(instanceUrl, ctx).getApiInterface().getPullDiffContent(token, owner, repo, pullIndex);
|
||||||
.getPullDiffContent(owner, repo, filename);
|
}
|
||||||
|
else {
|
||||||
|
call = RetrofitClient.getInstance(instanceUrl, ctx).getWebInterface().getPullDiffContent(owner, repo, pullIndex);
|
||||||
|
}
|
||||||
|
|
||||||
call.enqueue(new Callback<ResponseBody>() {
|
call.enqueue(new Callback<ResponseBody>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onResponse(@NonNull Call<ResponseBody> call, @NonNull retrofit2.Response<ResponseBody> response) {
|
public void onResponse(@NonNull Call<ResponseBody> call, @NonNull retrofit2.Response<ResponseBody> response) {
|
||||||
|
|
||||||
if (response.code() == 200) {
|
if(response.code() == 200) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
assert response.body() != null;
|
assert response.body() != null;
|
||||||
@ -105,7 +118,7 @@ public class FileDiffActivity extends BaseActivity {
|
|||||||
|
|
||||||
if(lines.length > 0) {
|
if(lines.length > 0) {
|
||||||
|
|
||||||
for (int i = 1; i < lines.length; i++) {
|
for(int i = 1; i < lines.length; i++) {
|
||||||
|
|
||||||
if(lines[i].contains("@@ -")) {
|
if(lines[i].contains("@@ -")) {
|
||||||
|
|
||||||
@ -119,16 +132,16 @@ public class FileDiffActivity extends BaseActivity {
|
|||||||
StringBuilder fileContentsFinal = new StringBuilder(fileContents_[1]);
|
StringBuilder fileContentsFinal = new StringBuilder(fileContents_[1]);
|
||||||
|
|
||||||
if(level2nd.length > 2) {
|
if(level2nd.length > 2) {
|
||||||
for (int j = 2; j < level2nd.length; j++) {
|
for(int j = 2; j < level2nd.length; j++) {
|
||||||
fileContentsFinal.append(level2nd[j]);
|
fileContentsFinal.append(level2nd[j]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String fileExtension = FileUtils.getExtension(fileNameFinal);
|
String fileExtension = FileUtils.getExtension(fileNameFinal);
|
||||||
|
|
||||||
String fileContentsFinalWithBlankLines = fileContentsFinal.toString().replaceAll( ".*@@.*", "" );
|
String fileContentsFinalWithBlankLines = fileContentsFinal.toString().replaceAll(".*@@.*", "");
|
||||||
String fileContentsFinalWithoutBlankLines = fileContentsFinal.toString().replaceAll( ".*@@.*(\r?\n|\r)?", "" );
|
String fileContentsFinalWithoutBlankLines = fileContentsFinal.toString().replaceAll(".*@@.*(\r?\n|\r)?", "");
|
||||||
fileContentsFinalWithoutBlankLines = fileContentsFinalWithoutBlankLines.replaceAll( ".*\\ No newline at end of file.*(\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));
|
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 getFileNameFinal = getFileName_[0].trim();
|
||||||
|
|
||||||
String[] binaryFile = getFileName_[1].split("GIT binary patch");
|
String[] binaryFile = getFileName_[1].split("GIT binary patch");
|
||||||
String binaryFileRaw = binaryFile[1].substring(binaryFile[1].indexOf('\n')+1);
|
String binaryFileRaw = binaryFile[1].substring(binaryFile[1].indexOf('\n') + 1);
|
||||||
String binaryFileFinal = binaryFile[1].substring(binaryFileRaw.indexOf('\n')+1);
|
String binaryFileFinal = binaryFile[1].substring(binaryFileRaw.indexOf('\n') + 1);
|
||||||
|
|
||||||
String fileExtension = FileUtils.getExtension(getFileNameFinal);
|
String fileExtension = FileUtils.getExtension(getFileNameFinal);
|
||||||
|
|
||||||
@ -169,17 +182,15 @@ public class FileDiffActivity extends BaseActivity {
|
|||||||
|
|
||||||
mProgressBar.setVisibility(View.GONE);
|
mProgressBar.setVisibility(View.GONE);
|
||||||
|
|
||||||
} catch (IOException e) {
|
}
|
||||||
|
catch(IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
else if(response.code() == 401) {
|
else if(response.code() == 401) {
|
||||||
|
|
||||||
AlertDialogs.authorizationTokenRevokedDialog(ctx, getResources().getString(R.string.alertDialogTokenRevokedTitle),
|
AlertDialogs.authorizationTokenRevokedDialog(ctx, getResources().getString(R.string.alertDialogTokenRevokedTitle), getResources().getString(R.string.alertDialogTokenRevokedMessage), getResources().getString(R.string.alertDialogTokenRevokedCopyNegativeButton), getResources().getString(R.string.alertDialogTokenRevokedCopyPositiveButton));
|
||||||
getResources().getString(R.string.alertDialogTokenRevokedMessage),
|
|
||||||
getResources().getString(R.string.alertDialogTokenRevokedCopyNegativeButton),
|
|
||||||
getResources().getString(R.string.alertDialogTokenRevokedCopyPositiveButton));
|
|
||||||
|
|
||||||
}
|
}
|
||||||
else if(response.code() == 403) {
|
else if(response.code() == 403) {
|
||||||
@ -202,6 +213,7 @@ public class FileDiffActivity extends BaseActivity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
|
public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
|
||||||
|
|
||||||
Log.e("onFailure", t.toString());
|
Log.e("onFailure", t.toString());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -209,9 +221,12 @@ public class FileDiffActivity extends BaseActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void initCloseListener() {
|
private void initCloseListener() {
|
||||||
|
|
||||||
onClickListener = new View.OnClickListener() {
|
onClickListener = new View.OnClickListener() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View view) {
|
public void onClick(View view) {
|
||||||
|
|
||||||
getIntent().removeExtra("singleFileName");
|
getIntent().removeExtra("singleFileName");
|
||||||
finish();
|
finish();
|
||||||
}
|
}
|
||||||
|
@ -64,7 +64,10 @@ public class BottomSheetSingleIssueFragment extends BottomSheetDialogFragment {
|
|||||||
mergePullRequest.setVisibility(View.VISIBLE);
|
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);
|
openFilesDiff.setVisibility(View.VISIBLE);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -267,6 +267,9 @@ public interface ApiInterface {
|
|||||||
@GET("repos/{owner}/{repo}/pulls") // get repository pull requests
|
@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);
|
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
|
@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);
|
Call<ResponseBody> mergePullRequest(@Header("Authorization") String token, @Path("owner") String ownerName, @Path("repo") String repoName, @Path("index") int index, @Body MergePullRequest jsonStr);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user