This is an automated email from the ASF dual-hosted git repository.

mgrund pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark-connect-go.git


The following commit(s) were added to refs/heads/master by this push:
     new 146b076  [MINOR] Allowing the merge tool to use JIRA Token
146b076 is described below

commit 146b0761f47b967ba05392109904a35f562bc3dd
Author: Martin Grund <[email protected]>
AuthorDate: Thu Aug 8 16:14:31 2024 +0200

    [MINOR] Allowing the merge tool to use JIRA Token
    
    ### What changes were proposed in this pull request?
    Updating the merge PR tool to allow using the JIRA access token instead of 
just username and password.
    
    ### Why are the changes needed?
    Usability
    
    ### Does this PR introduce _any_ user-facing change?
    No
    
    ### How was this patch tested?
    N/A
    
    Closes #37 from grundprinzip/minor_merge.
    
    Authored-by: Martin Grund <[email protected]>
    Signed-off-by: Martin Grund <[email protected]>
---
 merge_connect_go_pr.py | 50 ++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 42 insertions(+), 8 deletions(-)

diff --git a/merge_connect_go_pr.py b/merge_connect_go_pr.py
index bb514a8..8ff7d2a 100755
--- a/merge_connect_go_pr.py
+++ b/merge_connect_go_pr.py
@@ -52,6 +52,8 @@ PUSH_REMOTE_NAME = os.environ.get("PUSH_REMOTE_NAME", 
"apache")
 JIRA_USERNAME = os.environ.get("JIRA_USERNAME", "")
 # ASF JIRA password
 JIRA_PASSWORD = os.environ.get("JIRA_PASSWORD", "")
+# ASF JIRA Token
+JIRA_ACCESS_TOKEN = os.environ.get("JIRA_ACCESS_TOKEN")
 # OAuth key used for issuing requests against the GitHub API. If this is not 
defined, then requests
 # will be unauthenticated. You should only need to configure this if you find 
yourself regularly
 # exceeding your IP's unauthenticated request rate limit. You can create an 
OAuth key at
@@ -67,6 +69,8 @@ JIRA_API_BASE = "https://issues.apache.org/jira";
 BRANCH_PREFIX = "PR_TOOL"
 
 
+asf_jira = None
+
 def get_json(url):
     try:
         request = Request(url)
@@ -247,9 +251,7 @@ def fix_version_from_branch(branch, versions):
 
 
 def resolve_jira_issue(merge_branches, comment, default_jira_id=""):
-    asf_jira = jira.client.JIRA(
-        {"server": JIRA_API_BASE}, basic_auth=(JIRA_USERNAME, JIRA_PASSWORD)
-    )
+    global asf_jira
 
     jira_id = input("Enter a JIRA id [%s]: " % default_jira_id)
     if jira_id == "":
@@ -465,16 +467,48 @@ def get_current_ref():
         return ref
 
 
+def initialize_jira():
+    global asf_jira
+    jira_server = {"server": JIRA_API_BASE}
+
+    if not JIRA_IMPORTED:
+        print_error("ERROR finding jira library. Run 'pip3 install jira' to 
install.")
+        continue_maybe("Continue without jira?")
+    elif JIRA_ACCESS_TOKEN:
+        client = jira.client.JIRA(jira_server, token_auth=JIRA_ACCESS_TOKEN)
+        try:
+            # Eagerly check if the token is valid to align with the behavior 
of username/password
+            # authn
+            client.current_user()
+            asf_jira = client
+        except Exception as e:
+            if e.__class__.__name__ == "JIRAError" and getattr(e, 
"status_code", None) == 401:
+                msg = (
+                    "ASF JIRA could not authenticate with the invalid or 
expired token '%s'"
+                    % JIRA_ACCESS_TOKEN
+                )
+                fail(msg)
+            else:
+                raise e
+    elif JIRA_USERNAME and JIRA_PASSWORD:
+        print("You can use JIRA_ACCESS_TOKEN instead of 
JIRA_USERNAME/JIRA_PASSWORD.")
+        print("Visit https://issues.apache.org/jira/secure/ViewProfile.jspa ")
+        print("and click 'Personal Access Tokens' menu to manage your own 
tokens.")
+        asf_jira = jira.client.JIRA(jira_server, basic_auth=(JIRA_USERNAME, 
JIRA_PASSWORD))
+    else:
+        print("Neither JIRA_ACCESS_TOKEN nor JIRA_USERNAME/JIRA_PASSWORD are 
set.")
+        continue_maybe("Continue without jira?")
+
+
 def main():
     global original_head
+    global asf_jira
+
+    initialize_jira()
 
     os.chdir(SPARK_CONNECT_GO_HOME)
     original_head = get_current_ref()
 
-    # Check this up front to avoid failing the JIRA update at the very end
-    if not JIRA_USERNAME or not JIRA_PASSWORD:
-        continue_maybe("The env-vars JIRA_USERNAME and/or JIRA_PASSWORD are 
not set. Continue?")
-
     branches = get_json("%s/branches" % GITHUB_API_BASE)
     branch_names = list(filter(lambda x: x.startswith("branch-"), [x["name"] 
for x in branches]))
     # Assumes branch names can be sorted lexicographically
@@ -577,7 +611,7 @@ def main():
         merged_refs = merged_refs + [cherry_pick(pr_num, merge_hash, 
latest_branch)]
 
     if JIRA_IMPORTED:
-        if JIRA_USERNAME and JIRA_PASSWORD:
+        if asf_jira is not None:
             continue_maybe("Would you like to update an associated JIRA?")
             jira_comment = "Issue resolved by pull request %s\n[%s/%s]" % (
                 pr_num,


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to