2020-09-24 18:51:20 +02:00
|
|
|
package org.mian.gitnex.activities;
|
|
|
|
|
2020-10-30 08:00:52 +01:00
|
|
|
import android.annotation.SuppressLint;
|
2020-09-24 18:51:20 +02:00
|
|
|
import android.app.DatePickerDialog;
|
|
|
|
import android.app.Dialog;
|
|
|
|
import android.graphics.Color;
|
|
|
|
import android.graphics.drawable.ColorDrawable;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.view.LayoutInflater;
|
2020-10-30 08:00:52 +01:00
|
|
|
import android.view.MotionEvent;
|
2020-09-24 18:51:20 +02:00
|
|
|
import android.view.View;
|
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import androidx.annotation.NonNull;
|
2021-02-11 11:23:35 +01:00
|
|
|
import org.gitnex.tea4j.models.Branches;
|
|
|
|
import org.gitnex.tea4j.models.CreatePullRequest;
|
|
|
|
import org.gitnex.tea4j.models.Labels;
|
|
|
|
import org.gitnex.tea4j.models.Milestones;
|
2020-09-24 18:51:20 +02:00
|
|
|
import org.mian.gitnex.R;
|
2020-10-09 17:47:52 +02:00
|
|
|
import org.mian.gitnex.actions.LabelsActions;
|
2020-09-24 18:51:20 +02:00
|
|
|
import org.mian.gitnex.adapters.LabelsListAdapter;
|
|
|
|
import org.mian.gitnex.clients.RetrofitClient;
|
|
|
|
import org.mian.gitnex.databinding.ActivityCreatePrBinding;
|
|
|
|
import org.mian.gitnex.databinding.CustomLabelsSelectionDialogBinding;
|
|
|
|
import org.mian.gitnex.helpers.AppUtil;
|
|
|
|
import org.mian.gitnex.helpers.Authorization;
|
|
|
|
import org.mian.gitnex.helpers.StaticGlobalVariables;
|
|
|
|
import org.mian.gitnex.helpers.Toasty;
|
|
|
|
import org.mian.gitnex.helpers.Version;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.List;
|
|
|
|
import okhttp3.ResponseBody;
|
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Callback;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Author M M Arif
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class CreatePullRequestActivity extends BaseActivity implements LabelsListAdapter.LabelsListAdapterListener {
|
|
|
|
|
|
|
|
private View.OnClickListener onClickListener;
|
|
|
|
private ActivityCreatePrBinding viewBinding;
|
|
|
|
private CustomLabelsSelectionDialogBinding labelsBinding;
|
|
|
|
private int resultLimit = StaticGlobalVariables.resultLimitOldGiteaInstances;
|
|
|
|
private Dialog dialogLabels;
|
|
|
|
private String labelsSetter;
|
2020-10-09 17:47:52 +02:00
|
|
|
private List<Integer> labelsIds = new ArrayList<>();
|
|
|
|
private List<String> assignees = new ArrayList<>();
|
2020-09-24 18:51:20 +02:00
|
|
|
private int milestoneId;
|
|
|
|
|
|
|
|
private String loginUid;
|
|
|
|
private String instanceToken;
|
|
|
|
private String repoOwner;
|
|
|
|
private String repoName;
|
|
|
|
|
|
|
|
private LabelsListAdapter labelsAdapter;
|
|
|
|
|
|
|
|
List<Milestones> milestonesList = new ArrayList<>();
|
|
|
|
List<Branches> branchesList = new ArrayList<>();
|
|
|
|
List<Labels> labelsList = new ArrayList<>();
|
|
|
|
|
2020-10-30 08:00:52 +01:00
|
|
|
@SuppressLint("ClickableViewAccessibility")
|
2020-09-24 18:51:20 +02:00
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
|
|
viewBinding = ActivityCreatePrBinding.inflate(getLayoutInflater());
|
2021-01-26 18:43:25 +01:00
|
|
|
setContentView(viewBinding.getRoot());
|
2020-09-24 18:51:20 +02:00
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
loginUid = tinyDB.getString("loginUid");
|
|
|
|
String repoFullName = tinyDB.getString("repoFullName");
|
2020-09-24 18:51:20 +02:00
|
|
|
String[] parts = repoFullName.split("/");
|
|
|
|
repoOwner = parts[0];
|
|
|
|
repoName = parts[1];
|
2020-11-02 16:17:00 +01:00
|
|
|
instanceToken = "token " + tinyDB.getString(loginUid + "-token");
|
2020-09-24 18:51:20 +02:00
|
|
|
|
|
|
|
// require gitea 1.12 or higher
|
2020-11-02 16:17:00 +01:00
|
|
|
if(new Version(tinyDB.getString("giteaVersion")).higherOrEqual("1.12.0")) {
|
2020-10-29 14:13:19 +01:00
|
|
|
|
2020-09-24 18:51:20 +02:00
|
|
|
resultLimit = StaticGlobalVariables.resultLimitNewGiteaInstances;
|
|
|
|
}
|
|
|
|
|
2020-10-30 08:00:52 +01:00
|
|
|
viewBinding.prBody.setOnTouchListener((touchView, motionEvent) -> {
|
|
|
|
|
|
|
|
touchView.getParent().requestDisallowInterceptTouchEvent(true);
|
|
|
|
|
|
|
|
if ((motionEvent.getAction() & MotionEvent.ACTION_UP) != 0 && (motionEvent.getActionMasked() & MotionEvent.ACTION_UP) != 0) {
|
|
|
|
|
|
|
|
touchView.getParent().requestDisallowInterceptTouchEvent(false);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
2020-10-09 17:47:52 +02:00
|
|
|
labelsAdapter = new LabelsListAdapter(labelsList, CreatePullRequestActivity.this, labelsIds);
|
2020-09-24 18:51:20 +02:00
|
|
|
|
|
|
|
ImageView closeActivity = findViewById(R.id.close);
|
|
|
|
|
|
|
|
initCloseListener();
|
|
|
|
closeActivity.setOnClickListener(onClickListener);
|
|
|
|
|
|
|
|
viewBinding.prDueDate.setOnClickListener(dueDate ->
|
|
|
|
setDueDate()
|
|
|
|
);
|
|
|
|
|
|
|
|
disableProcessButton();
|
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
getMilestones(repoOwner, repoName, resultLimit);
|
|
|
|
getBranches(repoOwner, repoName);
|
2020-09-24 18:51:20 +02:00
|
|
|
|
2020-10-29 14:13:19 +01:00
|
|
|
viewBinding.prLabels.setOnClickListener(prLabels -> showLabels());
|
2020-09-24 18:51:20 +02:00
|
|
|
|
2020-10-29 14:13:19 +01:00
|
|
|
viewBinding.createPr.setOnClickListener(createPr -> processPullRequest());
|
2020-09-24 18:51:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void processPullRequest() {
|
|
|
|
|
|
|
|
String prTitle = String.valueOf(viewBinding.prTitle.getText());
|
|
|
|
String prDescription = String.valueOf(viewBinding.prBody.getText());
|
|
|
|
String mergeInto = viewBinding.mergeIntoBranchSpinner.getText().toString();
|
|
|
|
String pullFrom = viewBinding.pullFromBranchSpinner.getText().toString();
|
|
|
|
String dueDate = String.valueOf(viewBinding.prDueDate.getText());
|
|
|
|
|
|
|
|
assignees.add("");
|
|
|
|
|
|
|
|
if (labelsIds.size() == 0) {
|
2020-10-29 14:13:19 +01:00
|
|
|
|
2020-09-24 18:51:20 +02:00
|
|
|
labelsIds.add(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dueDate.matches("")) {
|
2020-10-29 14:13:19 +01:00
|
|
|
|
2020-09-24 18:51:20 +02:00
|
|
|
dueDate = null;
|
|
|
|
}
|
|
|
|
else {
|
2020-10-29 14:13:19 +01:00
|
|
|
|
2020-09-24 18:51:20 +02:00
|
|
|
dueDate = AppUtil.customDateCombine(AppUtil.customDateFormat(dueDate));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(prTitle.matches("")) {
|
|
|
|
|
|
|
|
Toasty.error(ctx, getString(R.string.titleError));
|
|
|
|
}
|
|
|
|
else if(mergeInto.matches("")) {
|
|
|
|
|
|
|
|
Toasty.error(ctx, getString(R.string.mergeIntoError));
|
|
|
|
}
|
|
|
|
else if(pullFrom.matches("")) {
|
|
|
|
|
|
|
|
Toasty.error(ctx, getString(R.string.pullFromError));
|
|
|
|
}
|
|
|
|
else if(pullFrom.equals(mergeInto)) {
|
|
|
|
|
|
|
|
Toasty.error(ctx, getString(R.string.sameBranchesError));
|
|
|
|
}
|
|
|
|
else {
|
2020-10-29 14:13:19 +01:00
|
|
|
|
2020-09-24 18:51:20 +02:00
|
|
|
createPullRequest(prTitle, prDescription, mergeInto, pullFrom, milestoneId, dueDate, assignees);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-09 17:47:52 +02:00
|
|
|
private void createPullRequest(String prTitle, String prDescription, String mergeInto, String pullFrom, int milestoneId, String dueDate, List<String> assignees) {
|
2020-09-24 18:51:20 +02:00
|
|
|
|
|
|
|
CreatePullRequest createPullRequest = new CreatePullRequest(prTitle, prDescription, loginUid, mergeInto, pullFrom, milestoneId, dueDate, assignees, labelsIds);
|
|
|
|
|
|
|
|
Call<ResponseBody> transferCall = RetrofitClient
|
2020-11-02 16:17:00 +01:00
|
|
|
.getApiInterface(appCtx)
|
2020-09-24 18:51:20 +02:00
|
|
|
.createPullRequest(instanceToken, repoOwner, repoName, createPullRequest);
|
|
|
|
|
|
|
|
transferCall.enqueue(new Callback<ResponseBody>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<ResponseBody> call, @NonNull retrofit2.Response<ResponseBody> response) {
|
|
|
|
|
|
|
|
disableProcessButton();
|
|
|
|
|
|
|
|
if (response.code() == 201) {
|
|
|
|
|
|
|
|
Toasty.success(ctx, getString(R.string.prCreateSuccess));
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
else if (response.code() == 409 && response.message().equals("Conflict")) {
|
|
|
|
|
|
|
|
enableProcessButton();
|
|
|
|
Toasty.error(ctx, getString(R.string.prAlreadyExists));
|
|
|
|
}
|
|
|
|
else if (response.code() == 404) {
|
|
|
|
|
|
|
|
enableProcessButton();
|
|
|
|
Toasty.error(ctx, getString(R.string.apiNotFound));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
enableProcessButton();
|
|
|
|
Toasty.error(ctx, getString(R.string.genericError));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<ResponseBody> call, @NonNull Throwable t) {
|
|
|
|
|
|
|
|
enableProcessButton();
|
|
|
|
Toasty.error(ctx, getString(R.string.genericServerResponseError));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-10-09 17:47:52 +02:00
|
|
|
public void labelsInterface(List<String> data) {
|
2020-09-24 18:51:20 +02:00
|
|
|
|
|
|
|
labelsSetter = String.valueOf(data);
|
|
|
|
viewBinding.prLabels.setText(labelsSetter.replace("]", "").replace("[", ""));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-10-09 17:47:52 +02:00
|
|
|
public void labelsIdsInterface(List<Integer> data) {
|
2020-09-24 18:51:20 +02:00
|
|
|
|
|
|
|
labelsIds = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void showLabels() {
|
|
|
|
|
|
|
|
dialogLabels = new Dialog(ctx, R.style.ThemeOverlay_MaterialComponents_Dialog_Alert);
|
|
|
|
|
|
|
|
if (dialogLabels.getWindow() != null) {
|
2020-10-29 14:13:19 +01:00
|
|
|
|
2020-09-24 18:51:20 +02:00
|
|
|
dialogLabels.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
|
|
|
|
}
|
|
|
|
|
|
|
|
labelsBinding = CustomLabelsSelectionDialogBinding.inflate(LayoutInflater.from(ctx));
|
|
|
|
|
|
|
|
View view = labelsBinding.getRoot();
|
|
|
|
dialogLabels.setContentView(view);
|
|
|
|
|
2020-10-29 14:13:19 +01:00
|
|
|
labelsBinding.cancel.setOnClickListener(editProperties -> dialogLabels.dismiss());
|
2020-09-24 18:51:20 +02:00
|
|
|
|
2020-10-29 13:53:03 +01:00
|
|
|
dialogLabels.show();
|
2020-11-02 16:17:00 +01:00
|
|
|
LabelsActions.getRepositoryLabels(ctx, repoOwner, repoName, labelsList, dialogLabels, labelsAdapter, labelsBinding);
|
2020-09-24 18:51:20 +02:00
|
|
|
}
|
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
private void getBranches(String repoOwner, String repoName) {
|
2020-09-24 18:51:20 +02:00
|
|
|
|
|
|
|
Call<List<Branches>> call = RetrofitClient
|
2020-11-02 16:17:00 +01:00
|
|
|
.getApiInterface(ctx)
|
|
|
|
.getBranches(Authorization.get(ctx), repoOwner, repoName);
|
2020-09-24 18:51:20 +02:00
|
|
|
|
|
|
|
call.enqueue(new Callback<List<Branches>>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<List<Branches>> call, @NonNull retrofit2.Response<List<Branches>> response) {
|
|
|
|
|
|
|
|
if(response.isSuccessful()) {
|
2020-10-29 14:13:19 +01:00
|
|
|
|
2020-09-24 18:51:20 +02:00
|
|
|
if(response.code() == 200) {
|
|
|
|
|
|
|
|
List<Branches> branchesList_ = response.body();
|
|
|
|
assert branchesList_ != null;
|
2020-10-29 14:13:19 +01:00
|
|
|
|
2020-09-24 18:51:20 +02:00
|
|
|
if(branchesList_.size() > 0) {
|
2020-10-29 14:13:19 +01:00
|
|
|
|
2020-09-24 18:51:20 +02:00
|
|
|
for (int i = 0; i < branchesList_.size(); i++) {
|
|
|
|
|
2020-10-29 14:13:19 +01:00
|
|
|
Branches data = new Branches(branchesList_.get(i).getName());
|
2020-09-24 18:51:20 +02:00
|
|
|
branchesList.add(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayAdapter<Branches> adapter = new ArrayAdapter<>(CreatePullRequestActivity.this,
|
|
|
|
R.layout.list_spinner_items, branchesList);
|
|
|
|
|
|
|
|
viewBinding.mergeIntoBranchSpinner.setAdapter(adapter);
|
|
|
|
viewBinding.pullFromBranchSpinner.setAdapter(adapter);
|
|
|
|
enableProcessButton();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<List<Branches>> call, @NonNull Throwable t) {
|
|
|
|
|
|
|
|
Toasty.error(ctx, getString(R.string.genericServerResponseError));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-11-02 16:17:00 +01:00
|
|
|
private void getMilestones(String repoOwner, String repoName, int resultLimit) {
|
2020-09-24 18:51:20 +02:00
|
|
|
|
|
|
|
String msState = "open";
|
|
|
|
Call<List<Milestones>> call = RetrofitClient
|
2020-11-02 16:17:00 +01:00
|
|
|
.getApiInterface(appCtx)
|
|
|
|
.getMilestones(Authorization.get(ctx), repoOwner, repoName, 1, resultLimit, msState);
|
2020-09-24 18:51:20 +02:00
|
|
|
|
|
|
|
call.enqueue(new Callback<List<Milestones>>() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResponse(@NonNull Call<List<Milestones>> call, @NonNull retrofit2.Response<List<Milestones>> response) {
|
|
|
|
|
|
|
|
if(response.code() == 200) {
|
|
|
|
|
|
|
|
List<Milestones> milestonesList_ = response.body();
|
|
|
|
|
|
|
|
milestonesList.add(new Milestones(0,getString(R.string.issueCreatedNoMilestone)));
|
|
|
|
assert milestonesList_ != null;
|
2020-10-29 14:13:19 +01:00
|
|
|
|
2020-09-24 18:51:20 +02:00
|
|
|
if(milestonesList_.size() > 0) {
|
2020-10-29 14:13:19 +01:00
|
|
|
|
2020-09-24 18:51:20 +02:00
|
|
|
for (int i = 0; i < milestonesList_.size(); i++) {
|
|
|
|
|
|
|
|
//Don't translate "open" is a enum
|
|
|
|
if(milestonesList_.get(i).getState().equals("open")) {
|
|
|
|
Milestones data = new Milestones(
|
|
|
|
milestonesList_.get(i).getId(),
|
|
|
|
milestonesList_.get(i).getTitle()
|
|
|
|
);
|
|
|
|
milestonesList.add(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayAdapter<Milestones> adapter = new ArrayAdapter<>(CreatePullRequestActivity.this,
|
|
|
|
R.layout.list_spinner_items, milestonesList);
|
|
|
|
|
|
|
|
viewBinding.milestonesSpinner.setAdapter(adapter);
|
|
|
|
enableProcessButton();
|
|
|
|
|
|
|
|
viewBinding.milestonesSpinner.setOnItemClickListener ((parent, view, position, id) ->
|
|
|
|
|
|
|
|
milestoneId = milestonesList.get(position).getId()
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onFailure(@NonNull Call<List<Milestones>> call, @NonNull Throwable t) {
|
|
|
|
|
|
|
|
Toasty.error(ctx, getString(R.string.genericServerResponseError));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setDueDate() {
|
|
|
|
|
|
|
|
final Calendar c = Calendar.getInstance();
|
|
|
|
int mYear = c.get(Calendar.YEAR);
|
|
|
|
final int mMonth = c.get(Calendar.MONTH);
|
|
|
|
final int mDay = c.get(Calendar.DAY_OF_MONTH);
|
|
|
|
|
|
|
|
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
|
|
|
|
(view, year, monthOfYear, dayOfMonth) -> viewBinding.prDueDate.setText(getString(R.string.setDueDate, year, (monthOfYear + 1), dayOfMonth)), mYear, mMonth, mDay);
|
|
|
|
datePickerDialog.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initCloseListener() {
|
|
|
|
|
|
|
|
onClickListener = view -> finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void disableProcessButton() {
|
|
|
|
|
|
|
|
viewBinding.createPr.setEnabled(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void enableProcessButton() {
|
|
|
|
|
|
|
|
viewBinding.createPr.setEnabled(true);
|
|
|
|
}
|
|
|
|
}
|