From b5cc8829d31e56503bc917fb060c3b644a88f15a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pablo=20Correa=20G=C3=B3mez?= <ablocorrea@hotmail.com>
Date: Wed, 11 Sep 2024 17:48:25 +0200
Subject: [PATCH] mrhlpr: add push command (MR 48)

Fixes #21
---
 mrhlpr/frontend.py |  6 ++++++
 mrhlpr/mr.py       | 20 ++++++++++++--------
 2 files changed, 18 insertions(+), 8 deletions(-)

diff --git a/mrhlpr/frontend.py b/mrhlpr/frontend.py
index d16f03c..77603cd 100644
--- a/mrhlpr/frontend.py
+++ b/mrhlpr/frontend.py
@@ -230,6 +230,9 @@ def parse_args() -> argparse.Namespace:
         help=f"add message to last commit: {ci_labels['skip_vercheck']}",
     )
 
+    # push
+    sub.add_parser("push", help="Push the MR to gitlab")
+
     # finish
     finish = sub.add_parser("finish", help="help finishing the MR (push, approve, merge...)")
     finish.add_argument(
@@ -265,6 +268,9 @@ def main():
         mr_id = mr.checked_out()
         mr.fixmsg(mr_id, args.skip_build, args.ignore_count, args.skip_vercheck)
         print_status(mr_id)
+    elif args.action == "push":
+        mr_id = mr.checked_out()
+        mr.push(mr_id, args.no_cache)
     elif args.action == "finish":
         mr_id = mr.checked_out()
         # TODO: Check that everything passed
diff --git a/mrhlpr/mr.py b/mrhlpr/mr.py
index 349112e..3ab89d0 100644
--- a/mrhlpr/mr.py
+++ b/mrhlpr/mr.py
@@ -445,6 +445,16 @@ def fixmsg(
         run_git_filter_branch("msg_filter_add_label.py", "HEAD~1", label="skip_vercheck")
 
 
+def push(mr_id: int, no_cache: bool = False) -> None:
+    print("Pushing changes...")
+    status = get_status(mr_id, no_cache)
+    origin = gitlab.parse_git_origin()
+    remote_local = status.source.split("/", 1)[0]
+    if remote_local == origin.project:
+        remote_local = "origin"
+    git.push_head(remote_local, status.source_branch)
+
+
 def finish(
     mr_id: int, comment: Optional[str], no_cache: bool = False, skip_approve: bool = False
 ) -> None:
@@ -460,14 +470,7 @@ def finish(
         )
         exit(1)
 
-    print("Pushing changes...")
-    status = get_status(mr_id, no_cache)
-    origin = gitlab.parse_git_origin()
-    remote_local = status.source.split("/", 1)[0]
-    if remote_local == origin.project:
-        remote_local = "origin"
-    git.push_head(remote_local, status.source_branch)
-
+    push(mr_id, no_cache)
     gl = gitlab_api.Gitlab(url="https://gitlab.com", private_token=gitlab_token)
     try:
         gl.auth()
@@ -478,6 +481,7 @@ def finish(
         )
         exit(1)
 
+    origin = gitlab.parse_git_origin()
     project = gl.projects.get(origin.project_id)
     mr = project.mergerequests.get(mr_id)
     if skip_approve or (gl.user and mr.author and gl.user.id == mr.author.get("id")):
-- 
GitLab