2019-11-27 16:09:26 +01:00
|
|
|
package org.mian.gitnex.activities;
|
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
import android.annotation.SuppressLint;
|
2019-11-27 16:09:26 +01:00
|
|
|
import android.content.Context;
|
|
|
|
import android.graphics.drawable.GradientDrawable;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.View;
|
2020-03-29 16:24:38 +02:00
|
|
|
import android.view.inputmethod.InputMethodManager;
|
2020-04-11 22:20:26 +02:00
|
|
|
import android.widget.AdapterView;
|
2019-11-27 16:09:26 +01:00
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
import android.widget.Button;
|
2020-04-11 22:20:26 +02:00
|
|
|
import android.widget.EditText;
|
2019-11-27 16:09:26 +01:00
|
|
|
import android.widget.ImageView;
|
2020-04-11 22:20:26 +02:00
|
|
|
import android.widget.Spinner;
|
2019-11-27 16:09:26 +01:00
|
|
|
import android.widget.TextView;
|
2020-04-11 22:20:26 +02:00
|
|
|
import androidx.annotation.NonNull;
|
2019-11-27 16:09:26 +01:00
|
|
|
import com.hendraanggrian.appcompat.socialview.Mention;
|
|
|
|
import com.hendraanggrian.appcompat.widget.MentionArrayAdapter;
|
|
|
|
import com.hendraanggrian.appcompat.widget.SocialAutoCompleteTextView;
|
|
|
|
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.Toasty;
|
2020-04-11 22:20:26 +02:00
|
|
|
import org.mian.gitnex.helpers.VersionCheck;
|
2019-11-27 16:09:26 +01:00
|
|
|
import org.mian.gitnex.models.Collaborators;
|
|
|
|
import org.mian.gitnex.models.MergePullRequest;
|
2020-04-11 22:20:26 +02:00
|
|
|
import org.mian.gitnex.models.MergePullRequestSpinner;
|
2019-11-27 16:09:26 +01:00
|
|
|
import org.mian.gitnex.util.AppUtil;
|
|
|
|
import org.mian.gitnex.util.TinyDB;
|
2020-04-11 22:20:26 +02:00
|
|
|
import java.util.ArrayList;
|
2019-11-27 16:09:26 +01:00
|
|
|
import java.util.List;
|
|
|
|
import okhttp3.ResponseBody;
|
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
|
|
|
import retrofit2.Response;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Author M M Arif
|
|
|
|
*/
|
|
|
|
|
2020-03-04 20:37:28 +01:00
|
|
|
public class MergePullRequestActivity extends BaseActivity {
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
public ImageView closeActivity;
|
|
|
|
private View.OnClickListener onClickListener;
|
|
|
|
|
|
|
|
final Context ctx = this;
|
2020-04-28 15:47:25 +02:00
|
|
|
private Context appCtx;
|
2020-04-11 22:20:26 +02:00
|
|
|
|
|
|
|
private SocialAutoCompleteTextView mergeDescription;
|
|
|
|
private EditText mergeTitle;
|
|
|
|
private Spinner mergeModeSpinner;
|
|
|
|
private ArrayAdapter<Mention> defaultMentionAdapter;
|
|
|
|
private Button mergeButton;
|
|
|
|
private String Do;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected int getLayoutResourceId() {
|
|
|
|
|
|
|
|
return R.layout.activity_merge_pull_request;
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressLint("SetTextI18n")
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
|
|
|
super.onCreate(savedInstanceState);
|
2020-04-28 15:47:25 +02:00
|
|
|
appCtx = getApplicationContext();
|
2020-04-11 22:20:26 +02:00
|
|
|
|
2020-04-28 15:47:25 +02:00
|
|
|
boolean connToInternet = AppUtil.haveNetworkConnection(appCtx);
|
|
|
|
TinyDB tinyDb = new TinyDB(appCtx);
|
2020-04-11 22:20:26 +02:00
|
|
|
|
|
|
|
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
|
|
|
|
|
|
mergeModeSpinner = findViewById(R.id.mergeSpinner);
|
|
|
|
mergeDescription = findViewById(R.id.mergeDescription);
|
|
|
|
mergeTitle = findViewById(R.id.mergeTitle);
|
|
|
|
|
|
|
|
mergeTitle.requestFocus();
|
|
|
|
assert imm != null;
|
|
|
|
imm.showSoftInput(mergeTitle, InputMethodManager.SHOW_IMPLICIT);
|
|
|
|
|
|
|
|
setMergeAdapter();
|
|
|
|
|
|
|
|
mergeModeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
|
|
|
|
|
|
|
MergePullRequestSpinner mergeId = (MergePullRequestSpinner) parent.getSelectedItem();
|
|
|
|
Do = mergeId.getId();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onNothingSelected(AdapterView<?> parent) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
defaultMentionAdapter = new MentionArrayAdapter<>(this);
|
|
|
|
loadCollaboratorsList();
|
|
|
|
|
|
|
|
mergeDescription.setMentionAdapter(defaultMentionAdapter);
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
closeActivity = findViewById(R.id.close);
|
|
|
|
TextView toolbar_title = findViewById(R.id.toolbar_title);
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
if(!tinyDb.getString("issueTitle").isEmpty()) {
|
|
|
|
toolbar_title.setText(tinyDb.getString("issueTitle"));
|
|
|
|
mergeTitle.setText(tinyDb.getString("issueTitle") + " (#" + tinyDb.getString("issueNumber")+ ")");
|
|
|
|
}
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
initCloseListener();
|
|
|
|
closeActivity.setOnClickListener(onClickListener);
|
2020-03-04 20:37:28 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
mergeButton = findViewById(R.id.mergeButton);
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
if(!connToInternet) {
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
disableProcessButton();
|
2020-03-29 16:24:38 +02:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
mergeButton.setOnClickListener(mergePullRequest);
|
2020-03-29 16:24:38 +02:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
}
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
}
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
private void setMergeAdapter() {
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-28 15:47:25 +02:00
|
|
|
TinyDB tinyDb = new TinyDB(appCtx);
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
ArrayList<MergePullRequestSpinner> mergeList = new ArrayList<>();
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
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) {
|
|
|
|
mergeList.add(new MergePullRequestSpinner("squash", getResources().getString(R.string.mergeOptionSquash)));
|
|
|
|
}
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-28 12:36:07 +02:00
|
|
|
ArrayAdapter<MergePullRequestSpinner> adapter = new ArrayAdapter<>(MergePullRequestActivity.this, R.layout.spinner_item, mergeList);
|
2020-04-11 22:20:26 +02:00
|
|
|
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
|
|
|
|
mergeModeSpinner.setAdapter(adapter);
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
}
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
public void loadCollaboratorsList() {
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-28 15:47:25 +02:00
|
|
|
final TinyDB tinyDb = new TinyDB(appCtx);
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
final String instanceUrl = tinyDb.getString("instanceUrl");
|
|
|
|
final String loginUid = tinyDb.getString("loginUid");
|
|
|
|
final String instanceToken = "token " + tinyDb.getString(loginUid + "-token");
|
|
|
|
String repoFullName = tinyDb.getString("repoFullName");
|
|
|
|
String[] parts = repoFullName.split("/");
|
|
|
|
final String repoOwner = parts[0];
|
|
|
|
final String repoName = parts[1];
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-28 15:47:25 +02:00
|
|
|
Call<List<Collaborators>> call = RetrofitClient.getInstance(instanceUrl, ctx).getApiInterface().getCollaborators(Authorization.returnAuthentication(ctx, loginUid, instanceToken), repoOwner, repoName);
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
call.enqueue(new Callback<List<Collaborators>>() {
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<List<Collaborators>> call, @NonNull Response<List<Collaborators>> response) {
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
if(response.isSuccessful()) {
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
assert response.body() != null;
|
|
|
|
String fullName = "";
|
|
|
|
for(int i = 0; i < response.body().size(); i++) {
|
|
|
|
if(!response.body().get(i).getFull_name().equals("")) {
|
|
|
|
fullName = response.body().get(i).getFull_name();
|
|
|
|
}
|
|
|
|
defaultMentionAdapter.add(new Mention(response.body().get(i).getUsername(), fullName, response.body().get(i).getAvatar_url()));
|
|
|
|
}
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
Log.i("onResponse", String.valueOf(response.code()));
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
}
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
}
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<List<Collaborators>> call, @NonNull Throwable t) {
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
Log.i("onFailure", t.toString());
|
|
|
|
}
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
});
|
|
|
|
}
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
private void initCloseListener() {
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
onClickListener = view -> finish();
|
|
|
|
}
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
private View.OnClickListener mergePullRequest = v -> processMergePullRequest();
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
private void processMergePullRequest() {
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
String mergePRDesc = mergeDescription.getText().toString();
|
|
|
|
String mergePRTitle = mergeTitle.getText().toString();
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-28 15:47:25 +02:00
|
|
|
boolean connToInternet = AppUtil.haveNetworkConnection(appCtx);
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
if(!connToInternet) {
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-28 15:47:25 +02:00
|
|
|
Toasty.info(ctx, getResources().getString(R.string.checkNetConnection));
|
2020-04-11 22:20:26 +02:00
|
|
|
return;
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
}
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
disableProcessButton();
|
|
|
|
mergeFunction(Do, mergePRDesc, mergePRTitle);
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
}
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
private void mergeFunction(String Do, String mergePRDT, String mergeTitle) {
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-28 15:47:25 +02:00
|
|
|
final TinyDB tinyDb = new TinyDB(appCtx);
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
final String instanceUrl = tinyDb.getString("instanceUrl");
|
|
|
|
final String loginUid = tinyDb.getString("loginUid");
|
|
|
|
final String instanceToken = "token " + tinyDb.getString(loginUid + "-token");
|
|
|
|
String repoFullName = tinyDb.getString("repoFullName");
|
|
|
|
String[] parts = repoFullName.split("/");
|
|
|
|
final String repoOwner = parts[0];
|
|
|
|
final String repoName = parts[1];
|
|
|
|
final int prIndex = Integer.parseInt(tinyDb.getString("issueNumber"));
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
MergePullRequest mergePR = new MergePullRequest(Do, mergePRDT, mergeTitle);
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-28 15:47:25 +02:00
|
|
|
Call<ResponseBody> call = RetrofitClient.getInstance(instanceUrl, ctx).getApiInterface().mergePullRequest(Authorization.returnAuthentication(ctx, loginUid, instanceToken), repoOwner, repoName, prIndex, mergePR);
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
call.enqueue(new Callback<ResponseBody>() {
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<ResponseBody> call, @NonNull retrofit2.Response<ResponseBody> response) {
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
if(response.code() == 200) {
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-28 15:47:25 +02:00
|
|
|
Toasty.info(ctx, getString(R.string.mergePRSuccessMsg));
|
2020-04-11 22:20:26 +02:00
|
|
|
tinyDb.putBoolean("prMerged", true);
|
|
|
|
tinyDb.putBoolean("resumePullRequests", true);
|
|
|
|
finish();
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
}
|
|
|
|
else if(response.code() == 401) {
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
enableProcessButton();
|
|
|
|
AlertDialogs.authorizationTokenRevokedDialog(ctx, getResources().getString(R.string.alertDialogTokenRevokedTitle), getResources().getString(R.string.alertDialogTokenRevokedMessage), getResources().getString(R.string.alertDialogTokenRevokedCopyNegativeButton), getResources().getString(R.string.alertDialogTokenRevokedCopyPositiveButton));
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
}
|
|
|
|
else if(response.code() == 404) {
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
enableProcessButton();
|
2020-04-28 15:47:25 +02:00
|
|
|
Toasty.info(ctx, getString(R.string.mergePR404ErrorMsg));
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
enableProcessButton();
|
2020-04-28 15:47:25 +02:00
|
|
|
Toasty.info(ctx, getString(R.string.genericError));
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
}
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
}
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
Log.e("onFailure", t.toString());
|
|
|
|
enableProcessButton();
|
|
|
|
}
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
});
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
}
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
private void disableProcessButton() {
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
mergeButton.setEnabled(false);
|
|
|
|
GradientDrawable shape = new GradientDrawable();
|
|
|
|
shape.setCornerRadius(8);
|
|
|
|
shape.setColor(getResources().getColor(R.color.hintColor));
|
|
|
|
mergeButton.setBackground(shape);
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
}
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
private void enableProcessButton() {
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
mergeButton.setEnabled(true);
|
|
|
|
GradientDrawable shape = new GradientDrawable();
|
|
|
|
shape.setCornerRadius(8);
|
|
|
|
shape.setColor(getResources().getColor(R.color.btnBackground));
|
|
|
|
mergeButton.setBackground(shape);
|
2019-11-27 16:09:26 +01:00
|
|
|
|
2020-04-11 22:20:26 +02:00
|
|
|
}
|
2019-11-27 16:09:26 +01:00
|
|
|
|
|
|
|
}
|