Skip to content

Commit

Permalink
Merge pull request #610 from dreispt/dr-fix-login
Browse files Browse the repository at this point in the history
[FIX] github_login: token only Github auth
  • Loading branch information
sbidoul committed May 1, 2024
2 parents 251e678 + 6c50fa2 commit 2d50c39
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 59 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ They are used by OCA maintainers to address common operations across all repos.

**Prerequisite**

Get a token from Github.
Github authentication uses a token, that must be previously created on Github, at
[Settings > Developer settings > Personal access tokens](https://github.com/settings/tokens).

$ oca-github-login USERNAME

Set and store the token to be used for Github auth using:

NOTE: you may have to delete the existing one from
"Account settings -> Developer Settings -> Personal Access Tokens".
$ oca-github-login

Alternatively, the token can be set on the GITHUB_TOKEN environment variable.


### Sync team users from community.odoo.com to GitHub teams
Expand Down Expand Up @@ -155,9 +157,9 @@ egg.
$ tox -e py27 # python 2.7
$ tox -- -k readme -v # run tests containing 'readme' in their name, verbose

**Get a token from Github**
**Set the client token to use for Github* authentication*

$ python -m tools.github_login USERNAME
$ python -m tools.github_login

**Run a script**

Expand Down
70 changes: 17 additions & 53 deletions tools/github_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import argparse
import os
import sys
from getpass import getpass
import github3
from .config import read_config, write_config
Expand All @@ -29,67 +28,32 @@ def login():
return github3.login(token=token)


def authorize_token(user):
def store_token():
config = read_config()
if config.get("GitHub", "token"):
print("The token already exists.")
sys.exit()

password = getpass("Password for {0}: ".format(user))

note = "OCA (odoo community association) Maintainers Tools"
note_url = "https://github.com/OCA/maintainers-tools"
scopes = ["repo", "read:org", "write:org", "admin:org"]

try:
# Python 2
prompt = raw_input
except NameError:
# Python 3
prompt = input

def two_factor_prompt():
code = ""
while not code:
# The user could accidentally press Enter before being ready,
# let's protect them from doing that.
code = prompt("Enter 2FA code: ")
return code

try:
auth = github3.authorize(
user,
password,
scopes,
note,
note_url,
two_factor_callback=two_factor_prompt,
)
except github3.GitHubError as err:
if err.code == 422:
for error in err.errors:
if error["code"] == "already_exists":
msg = (
"The 'OCA (odoo community association) Maintainers "
"Tools' token already exists. You will find it at "
"https://github.com/settings/tokens and can "
"revoke it or set the token manually in the "
"configuration file."
)
sys.exit(msg)
raise

config.set("GitHub", "token", auth.token)
print("Note: a token already exists and will be replaced.")
token = getpass("Enter Github Client Token: ")
auth = github3.login(token=token)
config.set("GitHub", "token", token)
write_config(config)
print("Token stored in configuration file")
print("Token stored in configuration file.")
return auth


def main():
parser = argparse.ArgumentParser()
parser.add_argument("username", help="GitHub Username")
parser.add_argument(
"--test",
action="store_true",
help="Test the Github authentication.",
)
args = parser.parse_args()

authorize_token(args.username)
if args.test:
auth = login()
else:
auth = store_token()
print("Authenticated as %s." % auth.me().login)


if __name__ == "__main__":
Expand Down

0 comments on commit 2d50c39

Please sign in to comment.