Skip to content

Commit c7950ca

Browse files
committed
Merge branch 'v2.0.x' into v2.x.x
* v2.0.x: Clear release notes after release Apply suggestions from code review by @ibucklaw Add missing release notes Update setup command to use a pre-existing PAT Update authentication header
2 parents d5ea29c + 7ac51e7 commit c7950ca

File tree

3 files changed

+71
-75
lines changed

3 files changed

+71
-75
lines changed

README.rst

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,29 @@ __ https://github.com/sociomantic-tsunami/git-hub/blob/master/man.rst
4545

4646
One time global setup to get the credentials
4747
--------------------------------------------
48+
49+
To use this tool you most likely will need a GitHub PAT (personal access token).
50+
If you don't have one you regularly use, you can `create a new one`__ (check
51+
`GitHub docs`__ if you need more help).
52+
53+
Make sure your PAT has at least **repo** and **user** scope.
54+
55+
Then you can use ``git hub setup`` to save it.
56+
57+
__ https://github.com/settings/tokens/new
58+
__ https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/creating-a-personal-access-token
59+
4860
::
4961

5062
$ git hub setup --global --user octocat
51-
GitHub password (will not be stored):
63+
64+
You need to use a GitHub Personal Access Token to do almost anything useful.
65+
To create one you can go to: https://github.com/settings/tokens/new.
66+
More help at: https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/creating-a-personal-access-token.
67+
68+
GitHub token: ******
69+
Saved git config hub.username
70+
Saved git config hub.oauthtoken
5271

5372
You can revoke this credentials at any time in the `GitHub Applications Settings
5473
page`__.

git-hub

Lines changed: 28 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -409,39 +409,25 @@ class Config:
409409
#
410410
# r = req.post('/repos/sociomantic-tsunami/test/labels/', name=name, color=color)
411411
#
412-
# The basic auth has priority over oauthtoken for authentication. If you want
413-
# to use OAuth just leave `basic_auth` and set `oauthtoken`. To fill the
414-
# `basic_auth` member, the `set_basic_auth()` convenient method is provided).
415-
#
416-
# See https://developer.github.com/ for more details on the GitHub API
412+
# See https://docs.github.com/en/free-pro-team@latest/rest for more details on
413+
# the GitHub API
417414
class RequestManager:
418415

419-
basic_auth = None
420416
oauthtoken = None
421417
links_re = re.compile(r'<([^>]+)>;.*rel=[\'"]?([^"]+)[\'"]?', re.M)
422418

423-
def __init__(self, base_url, oauthtoken=None,
424-
username=None, password=None):
419+
def __init__(self, base_url, oauthtoken=None):
425420
self.base_url = base_url
426421
if oauthtoken is not None:
427422
self.oauthtoken = oauthtoken
428-
elif username is not None:
429-
self.set_basic_auth(username, password)
430-
431-
# Configure the class to use basic authentication instead of OAuth
432-
def set_basic_auth(self, username, password):
433-
self.basic_auth = b"Basic " + base64.urlsafe_b64encode(
434-
_encode("%s:%s" % (username, password)))
435423

436424
# Open an URL in an authenticated manner using the specified HTTP
437425
# method. It also add other convenience headers, like Content-Type,
438426
# Accept (both to json) and Content-Length).
439427
def auth_urlopen(self, url, method, data, media_type):
440428
req = urllib.request.Request(url, _encode(data))
441-
if self.basic_auth:
442-
req.add_header("Authorization", self.basic_auth)
443-
elif self.oauthtoken:
444-
req.add_header("Authorization", "bearer " + self.oauthtoken)
429+
if self.oauthtoken:
430+
req.add_header("Authorization", "token " + self.oauthtoken)
445431
req.add_header("Content-Type", "application/json")
446432
req.add_header("Accept", media_type)
447433
req.add_header("Content-Length", str(len(data) if data else 0))
@@ -695,8 +681,8 @@ class SetupCmd (object):
695681
"will be searched and used instead, if found (for "
696682
"this to work the e-mail must be part of the public "
697683
"profile)")
698-
parser.add_argument('-p', '--password',
699-
help="GitHub's password (will not be stored)")
684+
parser.add_argument('-o', '--oauthtoken', metavar='TOKEN',
685+
help="GitHub Personal Access Token (PAT) to use")
700686
parser.add_argument('-b', '--baseurl', metavar='URL',
701687
help="GitHub's base URL to use to access the API "
702688
"(Enterprise servers usually use https://host/api/v3)")
@@ -726,62 +712,41 @@ class SetupCmd (object):
726712
infof("Using Git repository local configuration")
727713

728714
username = args.username
729-
password = args.password
730-
if (username is None or password is None) and \
731-
not sys.stdin.isatty():
732-
die("Can't perform an interactive setup outside a tty")
733715
if username is None:
734-
username = config.username or getpass.getuser()
716+
username = config.username
735717
reply = user_input('GitHub username [%s]: ' % username)
736718
if reply:
737719
username = reply
738-
if password is None:
720+
721+
oauthtoken = args.oauthtoken
722+
if oauthtoken is None:
723+
infof("\nYou need to use a GitHub Personal Access Token to do "
724+
"almost anything useful. To create one you can go to: "
725+
"https://github.com/settings/tokens/new.\nMore help at: "
726+
"https://docs.github.com/en/free-pro-team@latest/github/"
727+
"authenticating-to-github/creating-a-personal-access-token.\n")
739728
try:
740-
password = getpass.getpass('GitHub '
741-
'password (will not be stored): ')
742-
except EOFError:
729+
oauthtoken = getpass.getpass('GitHub token: ')
730+
except EOFError as e:
743731
sys.stdout.write('\n')
744-
password = ''
732+
warnf("Couldn't read Personal Access Token: {}", e)
733+
if oauthtoken == "":
734+
oauthtoken = None
735+
warnf("Token will not be used because it's empty.")
736+
745737
if '@' in username:
746738
infof("E-mail used to authenticate, trying to "
747739
"retrieve the GitHub username...")
748740
username = cls.find_username(username)
749741
infof("Found: {}", username)
750742

751-
req.set_basic_auth(username, password)
752-
753-
note = 'git-hub'
754-
if not is_global and config.forkrepo:
755-
proj = config.forkrepo.split('/', 1)[1]
756-
note += ' (%s)' % proj
757-
758-
while True:
759-
infof("Looking for GitHub authorization token...")
760-
auths = dict(
761-
[(a['note'], a) for a in req.get('/authorizations')])
762-
763-
if note not in auths:
764-
break
765-
766-
errf("The OAuth token with name '{}' already exists.", note)
767-
infof("If you want to create a new one, enter a "
768-
"name for it. Otherwise you can go to "
769-
"https://github.com/settings/tokens to "
770-
"regenerate or delete the token '{}'", note)
771-
note = user_input("Enter a new token name (an empty "
772-
"name cancels the setup): ")
773-
774-
if not note:
775-
sys.exit(0)
776-
777-
infof("Creating auth token '{}'", note)
778-
auth = req.post('/authorizations', note=note,
779-
scopes=['user', 'repo'])
780-
781-
set_config = lambda k, v: git_config(k, value=v, opts=args.opts)
743+
def set_config(k, v):
744+
git_config(k, value=v, opts=args.opts)
745+
infof("Saved git config {}{}", GIT_CONFIG_PREFIX, k)
782746

783747
set_config('username', username)
784-
set_config('oauthtoken', auth['token'])
748+
if oauthtoken is not None:
749+
set_config('oauthtoken', oauthtoken)
785750
if args.baseurl is not None:
786751
set_config('baseurl', args.baseurl)
787752

man.rst

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,33 @@ COMMANDS
5555

5656
`setup`
5757
This command performs an initial setup to connect to GitHub. It basically
58-
asks GitHub for an authorization token and stores it in the Git configuration
59-
variable `hub.oauthtoken` for future use so you don't need to type your
60-
password each time (or store it in the config). The username is also stored
61-
for future use in the `hub.username` variable. If the base URL is specified,
62-
it is stored in `hub.baseurl` too. By default configuration is stored in the
63-
repository's ``.git/config`` file (using ``git config``). If you want your
64-
configuration to be global to your user or system-wide, use the ``--global``
65-
or ``--system`` option respectively. These options are passed straight to
66-
``git config``.
58+
asks for a username and a GitHub Personal Access Token (PAT), which is
59+
needed to perform most actions.
60+
61+
The token will be stored it in the Git configuration variable
62+
`hub.oauthtoken` for future use. If you don't have one, you can `create it`__
63+
(check `GitHub docs`__ if you need more help). Make sure your PAT has at
64+
least **repo** and **user** scope.
65+
66+
The username is also stored for future use in the `hub.username` variable. If
67+
the base URL is specified, it is stored in `hub.baseurl` too.
68+
69+
By default configuration is stored in the repository's ``.git/config`` file
70+
(using ``git config``). If you want your configuration to be global to your
71+
user or system-wide, use the ``--global`` or ``--system`` option
72+
respectively. These options are passed straight to ``git config``.
6773

6874
\-u USERNAME, --username=USERNAME
6975
GitHub's username (login name), will be stored in the configuration
7076
variable `hub.username`. If an e-mail is provided, then a username matching
7177
that e-mail will be searched and used instead, if found (for this to work
7278
the e-mail must be part of the public profile).
7379

74-
\-p PASSWORD, --password=PASSWORD
75-
GitHub's password (will not be stored).
80+
\-o TOKEN, --oauthtoken=TOKEN
81+
GitHub's Personal Access Token (PAT), will be stored in the configuration
82+
variable `hub.username`. If an e-mail is provided, then a username matching
83+
that e-mail will be searched and used instead, if found (for this to work
84+
the e-mail must be part of the public profile).
7685

7786
\-b URL, --baseurl=URL
7887
GitHub's base URL to use to access the API. Set this when your GitHub API is
@@ -87,6 +96,9 @@ COMMANDS
8796
Store settings in the system configuration (see --system option in `git
8897
config(1)` for details).
8998

99+
__ https://github.com/settings/tokens/new
100+
__ https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/creating-a-personal-access-token
101+
90102
`clone` REPO [DEST]
91103
This command is used to clone **REPO**, a GitHub repository, to a **DEST**
92104
directory (defaults to the name of the project being cloned). If the

0 commit comments

Comments
 (0)