GitNex-Android-App/app/src/main/java/org/mian/gitnex/activities/MergePullRequestActivity.java

269 lines
7.8 KiB
Java
Raw Normal View History

2019-11-27 16:09:26 +01:00
package org.mian.gitnex.activities;
import android.annotation.SuppressLint;
Don't use TinyDB as cache (#1034) Do not use TinyDB as a cache or a way to send data between activities. ### How is this working Instead of saving everything into the TinyDB, I created three `Context`s (a `RepositoryContext`, an `IssueContext` and an `AccountContext`). All are used to store things like API or database values/models and additional data, e.g. the `RepositoryContext` also contains information about the current filter state of a repository (issues, pull requests, releases/tags and milestones). These are sent using `Intent`s and `Bundle`s between activities and fragments. Changing a field (e.g. filter state) in any fragment changes it also for the whole repository (or at least it should do so). Due to the size of the changes (after https://codeberg.org/gitnex/GitNex/commit/c9172f85efafd9f25739fdd8385e1904b711ea41, Git says `154 files changed, 3318 insertions(+), 3835 deletions(-)`) **I highly recommend you to create a beta/pre release before releasing a stable version**. Additional changes: * after logging out, the account remains in the account list (with a note) and you can log in again (you can't switch to this account) * repositories and organizations are clickable on user profiles * deleted two unused classes Once finished, hopefully * closes #354 * replaces #897 * fixes #947 * closes #1001 * closes #1015 * marks #876 and #578 as `Wontfix` since they are not necessary at this point * and all the other TinyDB issues Co-authored-by: qwerty287 <ndev@web.de> Co-authored-by: M M Arif <mmarif@noreply.codeberg.org> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/1034 Reviewed-by: 6543 <6543@noreply.codeberg.org> Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org> Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2022-03-13 03:59:13 +01:00
import android.content.Intent;
2019-11-27 16:09:26 +01:00
import android.os.Bundle;
import android.os.Handler;
import android.view.MenuItem;
2019-11-27 16:09:26 +01:00
import android.view.View;
import android.widget.ArrayAdapter;
import androidx.annotation.NonNull;
import java.util.ArrayList;
import java.util.Objects;
import org.gitnex.tea4j.v2.models.MergePullRequestOption;
2019-11-27 16:09:26 +01:00
import org.mian.gitnex.R;
import org.mian.gitnex.actions.PullRequestActions;
2019-11-27 16:09:26 +01:00
import org.mian.gitnex.clients.RetrofitClient;
import org.mian.gitnex.databinding.ActivityMergePullRequestBinding;
Don't use TinyDB as cache (#1034) Do not use TinyDB as a cache or a way to send data between activities. ### How is this working Instead of saving everything into the TinyDB, I created three `Context`s (a `RepositoryContext`, an `IssueContext` and an `AccountContext`). All are used to store things like API or database values/models and additional data, e.g. the `RepositoryContext` also contains information about the current filter state of a repository (issues, pull requests, releases/tags and milestones). These are sent using `Intent`s and `Bundle`s between activities and fragments. Changing a field (e.g. filter state) in any fragment changes it also for the whole repository (or at least it should do so). Due to the size of the changes (after https://codeberg.org/gitnex/GitNex/commit/c9172f85efafd9f25739fdd8385e1904b711ea41, Git says `154 files changed, 3318 insertions(+), 3835 deletions(-)`) **I highly recommend you to create a beta/pre release before releasing a stable version**. Additional changes: * after logging out, the account remains in the account list (with a note) and you can log in again (you can't switch to this account) * repositories and organizations are clickable on user profiles * deleted two unused classes Once finished, hopefully * closes #354 * replaces #897 * fixes #947 * closes #1001 * closes #1015 * marks #876 and #578 as `Wontfix` since they are not necessary at this point * and all the other TinyDB issues Co-authored-by: qwerty287 <ndev@web.de> Co-authored-by: M M Arif <mmarif@noreply.codeberg.org> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/1034 Reviewed-by: 6543 <6543@noreply.codeberg.org> Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org> Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2022-03-13 03:59:13 +01:00
import org.mian.gitnex.fragments.PullRequestsFragment;
2019-11-27 16:09:26 +01:00
import org.mian.gitnex.helpers.AlertDialogs;
import org.mian.gitnex.helpers.MergePullRequestSpinner;
import org.mian.gitnex.helpers.SnackBar;
Don't use TinyDB as cache (#1034) Do not use TinyDB as a cache or a way to send data between activities. ### How is this working Instead of saving everything into the TinyDB, I created three `Context`s (a `RepositoryContext`, an `IssueContext` and an `AccountContext`). All are used to store things like API or database values/models and additional data, e.g. the `RepositoryContext` also contains information about the current filter state of a repository (issues, pull requests, releases/tags and milestones). These are sent using `Intent`s and `Bundle`s between activities and fragments. Changing a field (e.g. filter state) in any fragment changes it also for the whole repository (or at least it should do so). Due to the size of the changes (after https://codeberg.org/gitnex/GitNex/commit/c9172f85efafd9f25739fdd8385e1904b711ea41, Git says `154 files changed, 3318 insertions(+), 3835 deletions(-)`) **I highly recommend you to create a beta/pre release before releasing a stable version**. Additional changes: * after logging out, the account remains in the account list (with a note) and you can log in again (you can't switch to this account) * repositories and organizations are clickable on user profiles * deleted two unused classes Once finished, hopefully * closes #354 * replaces #897 * fixes #947 * closes #1001 * closes #1015 * marks #876 and #578 as `Wontfix` since they are not necessary at this point * and all the other TinyDB issues Co-authored-by: qwerty287 <ndev@web.de> Co-authored-by: M M Arif <mmarif@noreply.codeberg.org> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/1034 Reviewed-by: 6543 <6543@noreply.codeberg.org> Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org> Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2022-03-13 03:59:13 +01:00
import org.mian.gitnex.helpers.contexts.IssueContext;
2019-11-27 16:09:26 +01:00
import retrofit2.Call;
import retrofit2.Callback;
/**
* @author M M Arif
2019-11-27 16:09:26 +01:00
*/
2020-03-04 20:37:28 +01:00
public class MergePullRequestActivity extends BaseActivity {
2019-11-27 16:09:26 +01:00
Don't use TinyDB as cache (#1034) Do not use TinyDB as a cache or a way to send data between activities. ### How is this working Instead of saving everything into the TinyDB, I created three `Context`s (a `RepositoryContext`, an `IssueContext` and an `AccountContext`). All are used to store things like API or database values/models and additional data, e.g. the `RepositoryContext` also contains information about the current filter state of a repository (issues, pull requests, releases/tags and milestones). These are sent using `Intent`s and `Bundle`s between activities and fragments. Changing a field (e.g. filter state) in any fragment changes it also for the whole repository (or at least it should do so). Due to the size of the changes (after https://codeberg.org/gitnex/GitNex/commit/c9172f85efafd9f25739fdd8385e1904b711ea41, Git says `154 files changed, 3318 insertions(+), 3835 deletions(-)`) **I highly recommend you to create a beta/pre release before releasing a stable version**. Additional changes: * after logging out, the account remains in the account list (with a note) and you can log in again (you can't switch to this account) * repositories and organizations are clickable on user profiles * deleted two unused classes Once finished, hopefully * closes #354 * replaces #897 * fixes #947 * closes #1001 * closes #1015 * marks #876 and #578 as `Wontfix` since they are not necessary at this point * and all the other TinyDB issues Co-authored-by: qwerty287 <ndev@web.de> Co-authored-by: M M Arif <mmarif@noreply.codeberg.org> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/1034 Reviewed-by: 6543 <6543@noreply.codeberg.org> Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org> Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2022-03-13 03:59:13 +01:00
private IssueContext issue;
private ActivityMergePullRequestBinding viewBinding;
private String Do;
@SuppressLint("SetTextI18n")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewBinding = ActivityMergePullRequestBinding.inflate(getLayoutInflater());
setContentView(viewBinding.getRoot());
Don't use TinyDB as cache (#1034) Do not use TinyDB as a cache or a way to send data between activities. ### How is this working Instead of saving everything into the TinyDB, I created three `Context`s (a `RepositoryContext`, an `IssueContext` and an `AccountContext`). All are used to store things like API or database values/models and additional data, e.g. the `RepositoryContext` also contains information about the current filter state of a repository (issues, pull requests, releases/tags and milestones). These are sent using `Intent`s and `Bundle`s between activities and fragments. Changing a field (e.g. filter state) in any fragment changes it also for the whole repository (or at least it should do so). Due to the size of the changes (after https://codeberg.org/gitnex/GitNex/commit/c9172f85efafd9f25739fdd8385e1904b711ea41, Git says `154 files changed, 3318 insertions(+), 3835 deletions(-)`) **I highly recommend you to create a beta/pre release before releasing a stable version**. Additional changes: * after logging out, the account remains in the account list (with a note) and you can log in again (you can't switch to this account) * repositories and organizations are clickable on user profiles * deleted two unused classes Once finished, hopefully * closes #354 * replaces #897 * fixes #947 * closes #1001 * closes #1015 * marks #876 and #578 as `Wontfix` since they are not necessary at this point * and all the other TinyDB issues Co-authored-by: qwerty287 <ndev@web.de> Co-authored-by: M M Arif <mmarif@noreply.codeberg.org> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/1034 Reviewed-by: 6543 <6543@noreply.codeberg.org> Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org> Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2022-03-13 03:59:13 +01:00
issue = IssueContext.fromIntent(getIntent());
setMergeAdapter();
if (!issue.getPullRequest().getTitle().isEmpty()) {
viewBinding.topAppBar.setTitle(issue.getPullRequest().getTitle());
viewBinding.mergeTitle.setText(
issue.getPullRequest().getTitle() + " (#" + issue.getIssueIndex() + ")");
}
2019-11-27 16:09:26 +01:00
viewBinding.topAppBar.setNavigationOnClickListener(v -> finish());
MenuItem attachment = viewBinding.topAppBar.getMenu().getItem(0);
MenuItem markdown = viewBinding.topAppBar.getMenu().getItem(1);
MenuItem create = viewBinding.topAppBar.getMenu().getItem(2);
attachment.setVisible(false);
markdown.setVisible(false);
create.setTitle(getString(R.string.mergePullRequestButtonText));
// if gitea version is greater/equal(1.12.0) than user installed version
// (installed.higherOrEqual(compareVer))
if (getAccount().requiresVersion("1.12.0")) {
viewBinding.deleteBranch.setVisibility(View.VISIBLE);
}
if (!issue.getPullRequest().isMergeable()) {
viewBinding.mergeInfoDisabledMessage.setVisibility(View.VISIBLE);
create.setVisible(false);
} else {
viewBinding.mergeInfoDisabledMessage.setVisibility(View.GONE);
create.setVisible(true);
}
2020-03-04 20:37:28 +01:00
if (issue.prIsFork()) {
viewBinding.deleteBranchForkInfo.setVisibility(View.VISIBLE);
} else {
viewBinding.deleteBranchForkInfo.setVisibility(View.GONE);
}
2019-11-27 16:09:26 +01:00
if (!(issue.getPullRequest().getHead().getRepo() != null
? issue.getPullRequest().getHead().getRepo().getPermissions().isPush()
: false)) {
Hide actions if the user can't use them (#977) closes #919 TODO - [X] repo and file actions (create release, label...) - [X] allow for repo admins - [x] label actions (edit, delete) - [x] allow for repo admins - [X] issue/pr action that are available for creators (close...) - [X] allow for creator - [X] allow for repo admins - [x] pr actions that are allowed when having push access to pr source (there was a bug in Gitea that makes that this was not working and these features were not accessible -> https://github.com/go-gitea/gitea/issues/17181) - [x] allow deleting of head branch/updates only if you can do this - [x] issue/pr action that are available for repo admins (merge) - [x] allow for repo admins - [x] milestone actions (close/reopen) - [x] allow for repo admins - [x] comment actions (delete, edit...) - [X] allow for creators - [x] allow for repo admins - [x] org actions (create label, team, repo; req gitea 1.16.0) - [x] actions when creating/editing issues - [x] delete head when merging - [x] All actions available to instance admins? (handled through API) Maybe as extras: - [x] Allow close/reopen also for PRs - [x] Improve handling of these (multiple btns for the same action) Co-authored-by: qwerty287 <ndev@web.de> Co-authored-by: M M Arif <mmarif@noreply.codeberg.org> Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/977 Reviewed-by: M M Arif <mmarif@noreply.codeberg.org> Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org> Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2022-02-11 15:27:31 +01:00
viewBinding.deleteBranch.setVisibility(View.GONE);
viewBinding.deleteBranchForkInfo.setVisibility(View.GONE);
}
viewBinding.topAppBar.setOnMenuItemClickListener(
menuItem -> {
int id = menuItem.getItemId();
if (id == R.id.create) {
processMergePullRequest();
return true;
} else {
return super.onOptionsItemSelected(menuItem);
}
});
}
2019-11-27 16:09:26 +01:00
private void setMergeAdapter() {
2019-11-27 16:09:26 +01:00
ArrayList<MergePullRequestSpinner> mergeList = new ArrayList<>();
2019-11-27 16:09:26 +01: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.4 due to a bug
if (getAccount().requiresVersion("1.12.0")) {
mergeList.add(
new MergePullRequestSpinner(
"squash", getResources().getString(R.string.mergeOptionSquash)));
}
2019-11-27 16:09:26 +01:00
ArrayAdapter<MergePullRequestSpinner> adapter =
new ArrayAdapter<>(
MergePullRequestActivity.this, R.layout.list_spinner_items, mergeList);
viewBinding.mergeSpinner.setAdapter(adapter);
2019-11-27 16:09:26 +01:00
viewBinding.mergeSpinner.setOnItemClickListener(
(parent, view, position, id) -> {
Do = mergeList.get(position).getId();
});
}
2019-11-27 16:09:26 +01:00
private void processMergePullRequest() {
2019-11-27 16:09:26 +01:00
String mergePRDesc =
Objects.requireNonNull(viewBinding.mergeDescription.getText()).toString();
String mergePRTitle = Objects.requireNonNull(viewBinding.mergeTitle.getText()).toString();
boolean deleteBranch = viewBinding.deleteBranch.isChecked();
2019-11-27 16:09:26 +01:00
if (Do == null) {
2019-11-27 16:09:26 +01:00
SnackBar.error(
ctx,
findViewById(android.R.id.content),
getString(R.string.selectMergeStrategy));
} else {
mergeFunction(Do, mergePRDesc, mergePRTitle, deleteBranch);
}
}
2019-11-27 16:09:26 +01:00
private void mergeFunction(
String Do, String mergePRDT, String mergeTitle, boolean deleteBranch) {
2019-11-27 16:09:26 +01:00
MergePullRequestOption mergePR = new MergePullRequestOption();
mergePR.setDeleteBranchAfterMerge(deleteBranch);
mergePR.setMergeTitleField(mergeTitle);
mergePR.setMergeMessageField(mergePRDT);
switch (Do) {
case "merge":
mergePR.setDo(MergePullRequestOption.DoEnum.MERGE);
break;
case "rebase":
mergePR.setDo(MergePullRequestOption.DoEnum.REBASE);
break;
case "rebase-merge":
mergePR.setDo(MergePullRequestOption.DoEnum.REBASE_MERGE);
break;
case "squash":
mergePR.setDo(MergePullRequestOption.DoEnum.SQUASH);
break;
}
2019-11-27 16:09:26 +01:00
Call<Void> call =
RetrofitClient.getApiInterface(ctx)
.repoMergePullRequest(
issue.getRepository().getOwner(),
issue.getRepository().getName(),
(long) issue.getIssueIndex(),
mergePR);
call.enqueue(
new Callback<>() {
@Override
public void onResponse(
@NonNull Call<Void> call, @NonNull retrofit2.Response<Void> response) {
if (response.code() == 200) {
if (deleteBranch) {
if (issue.prIsFork()) {
String repoFullName =
issue.getPullRequest()
.getHead()
.getRepo()
.getFullName();
String[] parts = repoFullName.split("/");
final String repoOwner = parts[0];
final String repoName = parts[1];
PullRequestActions.deleteHeadBranch(
ctx,
repoOwner,
repoName,
issue.getPullRequest().getHead().getRef(),
false);
} else {
PullRequestActions.deleteHeadBranch(
ctx,
issue.getRepository().getOwner(),
issue.getRepository().getName(),
issue.getPullRequest().getHead().getRef(),
false);
}
}
SnackBar.success(
ctx,
findViewById(android.R.id.content),
getString(R.string.mergePRSuccessMsg));
Intent result = new Intent();
PullRequestsFragment.resumePullRequests = true;
IssueDetailActivity.singleIssueUpdate = true;
RepoDetailActivity.updateRepo = true;
setResult(200, result);
new Handler().postDelayed(() -> finish(), 3000);
} else if (response.code() == 401) {
AlertDialogs.authorizationTokenRevokedDialog(ctx);
} else if (response.code() == 404) {
SnackBar.error(
ctx,
findViewById(android.R.id.content),
getString(R.string.mergePR404ErrorMsg));
} else if (response.code() == 405) {
SnackBar.error(
ctx,
findViewById(android.R.id.content),
getString(R.string.mergeNotAllowed));
} else {
SnackBar.error(
ctx,
findViewById(android.R.id.content),
getString(R.string.genericError));
}
}
@Override
public void onFailure(@NonNull Call<Void> call, @NonNull Throwable t) {}
});
}
2019-11-27 16:09:26 +01:00
Don't use TinyDB as cache (#1034) Do not use TinyDB as a cache or a way to send data between activities. ### How is this working Instead of saving everything into the TinyDB, I created three `Context`s (a `RepositoryContext`, an `IssueContext` and an `AccountContext`). All are used to store things like API or database values/models and additional data, e.g. the `RepositoryContext` also contains information about the current filter state of a repository (issues, pull requests, releases/tags and milestones). These are sent using `Intent`s and `Bundle`s between activities and fragments. Changing a field (e.g. filter state) in any fragment changes it also for the whole repository (or at least it should do so). Due to the size of the changes (after https://codeberg.org/gitnex/GitNex/commit/c9172f85efafd9f25739fdd8385e1904b711ea41, Git says `154 files changed, 3318 insertions(+), 3835 deletions(-)`) **I highly recommend you to create a beta/pre release before releasing a stable version**. Additional changes: * after logging out, the account remains in the account list (with a note) and you can log in again (you can't switch to this account) * repositories and organizations are clickable on user profiles * deleted two unused classes Once finished, hopefully * closes #354 * replaces #897 * fixes #947 * closes #1001 * closes #1015 * marks #876 and #578 as `Wontfix` since they are not necessary at this point * and all the other TinyDB issues Co-authored-by: qwerty287 <ndev@web.de> Co-authored-by: M M Arif <mmarif@noreply.codeberg.org> Co-authored-by: 6543 <6543@obermui.de> Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/1034 Reviewed-by: 6543 <6543@noreply.codeberg.org> Co-authored-by: qwerty287 <qwerty287@noreply.codeberg.org> Co-committed-by: qwerty287 <qwerty287@noreply.codeberg.org>
2022-03-13 03:59:13 +01:00
@Override
public void onResume() {
super.onResume();
issue.getRepository().checkAccountSwitch(this);
}
2019-11-27 16:09:26 +01:00
}