]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
script/build-integration-branch: support fetching github token from netrc 54723/head
authorJohn Mulligan <jmulligan@redhat.com>
Wed, 29 Nov 2023 20:45:15 +0000 (15:45 -0500)
committerJohn Mulligan <jmulligan@redhat.com>
Mon, 4 Dec 2023 19:00:58 +0000 (14:00 -0500)
I constantly forget to update the special place that this script wants
to read the github token from. Other tools I use can read from the .netrc file.
Try reading the token from netrc before falling back to this script's
traditional location.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
src/script/build-integration-branch

index b5f53aa210177b5cb3eb7818d844e4b35c80bbab..c4e1d604f0c8448f36a8ba8b24be31e42a80b6e3 100755 (executable)
@@ -7,7 +7,12 @@ Builds integration branches. Something similar to
   >   git pull b
   > done
 
-Requires `~/.github_token`.
+Requires either `~/.github_token` containing ONLY the token
+OR adding an entry like the following to `~/.netrc`:
+  ```
+  machine github.com
+  password ghp_E7ln0tAR34LtoK3nIsw34RyTve2moM3BvK
+  ```
 
 
 Usage:
@@ -24,6 +29,7 @@ import os
 import requests
 import sys
 import time
+import netrc
 
 from subprocess import call, check_output
 from urllib.parse import urljoin
@@ -52,9 +58,28 @@ except ImportError:
     assert len(sys.argv) == 2
     branch = label + postfix
 
-
-with open(os.path.expanduser('~/.github_token')) as myfile:
-    token = myfile.readline().strip()
+token = ''
+try:
+    nrc = netrc.netrc()
+    nrauth = nrc.authenticators("api.github.com")
+    if nrauth:
+        token = nrauth[2]
+    if not token:
+        nrauth = nrc.authenticators("github.com")
+        if nrauth:
+            token = nrauth[2]
+except FileNotFoundError:
+    pass
+if not token:
+    try:
+        with open(os.path.expanduser('~/.github_token')) as myfile:
+            token = myfile.readline().strip()
+    except FileNotFoundError:
+        pass
+if not token:
+    print('No github api access token found')
+    print('  Add a token to .netrc for [api.]github.com')
+    print('  OR add a token to $HOME/.github_token')
 
 # get prs
 baseurl = urljoin('https://api.github.com',