From 95aea16a078bc120933b61def29d9303185c8a08 Mon Sep 17 00:00:00 2001 From: qwerty287 Date: Tue, 14 Sep 2021 16:18:28 +0200 Subject: [PATCH] Add option to update PR (#973) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### Describe what your pull request does and which issue you’re targeting Closes #967

- [X] I carefully read the [contribution guidelines](https://codeberg.org/GitNex/GitNex/src/branch/main/CONTRIBUTING.md). - [X] I'm following the code standards as defined [here](https://codeberg.org/gitnex/GitNex/wiki/Code-Standards). - [X] By submitting this pull request, I permit GitNex to license my work under the [GNU General Public License v3](https://codeberg.org/GitNex/GitNex/src/branch/main/LICENSE). Co-authored-by: qwerty287 Co-authored-by: M M Arif Reviewed-on: https://codeberg.org/gitnex/GitNex/pulls/973 Reviewed-by: M M Arif Co-authored-by: qwerty287 Co-committed-by: qwerty287 --- app/build.gradle | 2 +- .../gitnex/actions/PullRequestActions.java | 40 +++++++++++ .../BottomSheetSingleIssueFragment.java | 14 ++++ .../org/mian/gitnex/helpers/AlertDialogs.java | 28 ++++++++ app/src/main/res/drawable/ic_update.xml | 27 +++++++ .../res/layout/bottom_sheet_single_issue.xml | 15 ++++ .../custom_pr_update_strategy_dialog.xml | 71 +++++++++++++++++++ app/src/main/res/values/strings.xml | 6 ++ 8 files changed, 202 insertions(+), 1 deletion(-) create mode 100644 app/src/main/res/drawable/ic_update.xml create mode 100644 app/src/main/res/layout/custom_pr_update_strategy_dialog.xml diff --git a/app/build.gradle b/app/build.gradle index d6196514..6e4210e2 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -109,7 +109,7 @@ dependencies { implementation "androidx.work:work-runtime:$work_version" implementation "io.mikael:urlbuilder:2.0.9" implementation "org.codeberg.gitnex-garage:emoji-java:v5.1.2" - implementation "org.codeberg.gitnex:tea4j:1.0.18" + implementation "org.codeberg.gitnex:tea4j:1.0.20" coreLibraryDesugaring "com.android.tools:desugar_jdk_libs:1.1.5" implementation 'androidx.biometric:biometric:1.1.0' implementation 'com.github.chrisvest:stormpot:2.4.2' diff --git a/app/src/main/java/org/mian/gitnex/actions/PullRequestActions.java b/app/src/main/java/org/mian/gitnex/actions/PullRequestActions.java index 61c2e687..6dfcce45 100644 --- a/app/src/main/java/org/mian/gitnex/actions/PullRequestActions.java +++ b/app/src/main/java/org/mian/gitnex/actions/PullRequestActions.java @@ -10,6 +10,7 @@ import org.mian.gitnex.helpers.Authorization; import org.mian.gitnex.helpers.Toasty; import retrofit2.Call; import retrofit2.Callback; +import retrofit2.Response; /** * Author qwerty287 @@ -59,4 +60,43 @@ public class PullRequestActions { }); } + public static void updatePr(Context context, String repoOwner, String repoName, String index, Boolean rebase) { + String strategy; + if(rebase == null) { + strategy = null; + } + else if(!rebase) { + strategy = "merge"; + } + else { + strategy = "rebase"; + } + RetrofitClient.getApiInterface(context).updatePullRequest(Authorization.get(context), repoOwner, repoName, Integer.parseInt(index), strategy) + .enqueue(new Callback() { + + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if(response.isSuccessful()) { + Toasty.success(context, context.getString(R.string.updatePrSuccess)); + } + else { + if(response.code() == 403) { + Toasty.error(context, context.getString(R.string.authorizeError)); + } + else if(response.code() == 409) { + Toasty.error(context, context.getString(R.string.updatePrConflict)); + } + else { + Toasty.error(context, context.getString(R.string.genericError)); + } + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + Toasty.error(context, context.getString(R.string.genericError)); + } + }); + } + } diff --git a/app/src/main/java/org/mian/gitnex/fragments/BottomSheetSingleIssueFragment.java b/app/src/main/java/org/mian/gitnex/fragments/BottomSheetSingleIssueFragment.java index b678c96a..fa7bd4b0 100644 --- a/app/src/main/java/org/mian/gitnex/fragments/BottomSheetSingleIssueFragment.java +++ b/app/src/main/java/org/mian/gitnex/fragments/BottomSheetSingleIssueFragment.java @@ -56,6 +56,7 @@ public class BottomSheetSingleIssueFragment extends BottomSheetDialogFragment { TextView addRemoveAssignees = bottomSheetSingleIssueBinding.addRemoveAssignees; TextView copyIssueUrl = bottomSheetSingleIssueBinding.copyIssueUrl; TextView openFilesDiff = bottomSheetSingleIssueBinding.openFilesDiff; + TextView updatePullRequest = bottomSheetSingleIssueBinding.updatePullRequest; TextView mergePullRequest = bottomSheetSingleIssueBinding.mergePullRequest; TextView deletePullRequestBranch = bottomSheetSingleIssueBinding.deletePrHeadBranch; TextView shareIssue = bottomSheetSingleIssueBinding.shareIssue; @@ -92,10 +93,12 @@ public class BottomSheetSingleIssueFragment extends BottomSheetDialogFragment { shareIssue.setText(R.string.sharePr); if(tinyDB.getBoolean("prMerged") || tinyDB.getString("repoPrState").equals("closed")) { + updatePullRequest.setVisibility(View.GONE); mergePullRequest.setVisibility(View.GONE); deletePullRequestBranch.setVisibility(View.VISIBLE); } else { + updatePullRequest.setVisibility(View.VISIBLE); mergePullRequest.setVisibility(View.VISIBLE); deletePullRequestBranch.setVisibility(View.GONE); } @@ -113,10 +116,21 @@ public class BottomSheetSingleIssueFragment extends BottomSheetDialogFragment { } else { + updatePullRequest.setVisibility(View.GONE); mergePullRequest.setVisibility(View.GONE); deletePullRequestBranch.setVisibility(View.GONE); } + updatePullRequest.setOnClickListener(v -> { + if(new Version(tinyDB.getString("giteaVersion")).higherOrEqual("1.16.0")) { + AlertDialogs.selectPullUpdateStrategy(requireContext(), parts[0], parts[1], tinyDB.getString("issueNumber")); + } + else { + PullRequestActions.updatePr(requireContext(), parts[0], parts[1], tinyDB.getString("issueNumber"), null); + } + dismiss(); + }); + mergePullRequest.setOnClickListener(v13 -> { startActivity(new Intent(ctx, MergePullRequestActivity.class)); diff --git a/app/src/main/java/org/mian/gitnex/helpers/AlertDialogs.java b/app/src/main/java/org/mian/gitnex/helpers/AlertDialogs.java index 77a108e5..00fecf0e 100644 --- a/app/src/main/java/org/mian/gitnex/helpers/AlertDialogs.java +++ b/app/src/main/java/org/mian/gitnex/helpers/AlertDialogs.java @@ -1,10 +1,15 @@ package org.mian.gitnex.helpers; +import android.app.Dialog; import android.content.Context; import android.content.Intent; +import android.graphics.Color; +import android.graphics.drawable.ColorDrawable; +import android.widget.Button; import androidx.appcompat.app.AlertDialog; import org.mian.gitnex.R; import org.mian.gitnex.actions.CollaboratorActions; +import org.mian.gitnex.actions.PullRequestActions; import org.mian.gitnex.actions.TeamActions; import org.mian.gitnex.activities.CreateLabelActivity; import org.mian.gitnex.activities.LoginActivity; @@ -112,4 +117,27 @@ public class AlertDialogs { } + public static void selectPullUpdateStrategy(Context context, String repoOwner, String repo, String issueNumber) { + Dialog dialog = new Dialog(context, R.style.ThemeOverlay_MaterialComponents_Dialog_Alert); + + if (dialog.getWindow() != null) { + dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); + } + + dialog.setContentView(R.layout.custom_pr_update_strategy_dialog); + Button mergeBtn = dialog.findViewById(R.id.updatePullMerge); + Button rebaseBtn = dialog.findViewById(R.id.updatePullRebase); + Button cancelBtn = dialog.findViewById(R.id.cancelPullUpdate); + mergeBtn.setOnClickListener((v) -> { + PullRequestActions.updatePr(context, repoOwner, repo, issueNumber, false); + dialog.dismiss(); + }); + rebaseBtn.setOnClickListener((v) -> { + PullRequestActions.updatePr(context, repoOwner, repo, issueNumber, true); + dialog.dismiss(); + }); + cancelBtn.setOnClickListener((v) -> dialog.dismiss()); + dialog.show(); + } + } diff --git a/app/src/main/res/drawable/ic_update.xml b/app/src/main/res/drawable/ic_update.xml new file mode 100644 index 00000000..6188f3b0 --- /dev/null +++ b/app/src/main/res/drawable/ic_update.xml @@ -0,0 +1,27 @@ + + + + + diff --git a/app/src/main/res/layout/bottom_sheet_single_issue.xml b/app/src/main/res/layout/bottom_sheet_single_issue.xml index e6278303..45866cd3 100644 --- a/app/src/main/res/layout/bottom_sheet_single_issue.xml +++ b/app/src/main/res/layout/bottom_sheet_single_issue.xml @@ -58,6 +58,21 @@ android:textSize="16sp" app:drawableStartCompat="@drawable/ic_pull_request" /> + + + + + + + + +