Skip to content

Commit

Permalink
Use personal access tokens & automatic nudge
Browse files Browse the repository at this point in the history
  • Loading branch information
Undo1 committed Jul 21, 2020
1 parent 9d93a70 commit e3e35ba
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
31 changes: 22 additions & 9 deletions gitmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,17 @@ def _anchor(str_to_anchor, blacklist_type):


class GitHubManager:
auth = HTTPBasicAuth(GlobalVars.github_username, GlobalVars.github_password)
still_using_usernames = GlobalVars.github_access_token is None

if still_using_usernames:
still_using_usernames_nudge = " By the way, tell my owner to [set up personal access tokens]" \
"(//chat.stackexchange.com/transcript/message/55007870#55007870)" \
" when they have time :)"
auth_args = {"auth": HTTPBasicAuth(GlobalVars.github_username, GlobalVars.github_password)}
else:
still_using_usernames_nudge = None
auth_args = {"headers": {'Authorization': 'token {}'.format(GlobalVars.github_access_token)}}

repo = GlobalVars.bot_repo_slug

@classmethod
Expand All @@ -45,14 +55,14 @@ def create_pull_request(cls, payload):
if isinstance(payload, dict):
payload = json.dumps(payload)
response = requests.post("https://api.github.com/repos/{}/pulls".format(cls.repo),
auth=cls.auth, data=payload)
data=payload, **cls.auth_args)
return response.json()

@classmethod
def comment_on_thread(cls, thread_id, body):
url = "https://api.github.com/repos/{}/issues/{}/comments".format(cls.repo, thread_id)
payload = json.dumps({'body': body})
response = requests.post(url, auth=cls.auth, data=payload)
response = requests.post(url, data=payload, **cls.auth_args)
return response.json()


Expand Down Expand Up @@ -159,8 +169,9 @@ def add_to_blacklist(cls, blacklist='', item_to_blacklist='', username='', chat_
git.push(origin_or_auth, branch)
git.checkout("master")

if GlobalVars.github_username is None or GlobalVars.github_password is None:
return (False, "Tell someone to set a GH password")
if ((GlobalVars.github_username is None or GlobalVars.github_password is None)
and (GlobalVars.github_access_token is None)):
return (False, "Tell someone to set a GH token")

payload = {"title": "{0}: {1} {2}".format(username, op.title(), item),
"body": "[{0}]({1}) requests the {2} of the {3} `{4}`. See the MS search [here]"
Expand All @@ -183,15 +194,17 @@ def add_to_blacklist(cls, blacklist='', item_to_blacklist='', username='', chat_
git.checkout("deploy") # Return to deploy, pending the accept of the PR in Master.
git.branch('-D', branch) # Delete the branch in the local git tree since we're done with it.
url, pr_num = response["html_url"], response["number"]

if metasmoke_down:
return (True,
"MS is not reachable, so I can't see if you have blacklist manager privileges, but "
"I've [created PR#{1} for you]({0}).".format(
url, pr_num))
"I've [created PR#{1} for you]({0}).{2}".format(
url, pr_num, GitHubManager.still_using_usernames_nudge))
else:
return (True,
"You don't have blacklist manager privileges, but I've [created PR#{1} for you]({0})."
.format(url, pr_num))
"You don't have blacklist manager privileges,"
"but I've [created PR#{1} for you]({0}).{2}"
.format(url, pr_num, GitHubManager.still_using_usernames_nudge))

except KeyError:
git.checkout("deploy") # Return to deploy
Expand Down
1 change: 1 addition & 0 deletions globalvars.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ def reset_ms_status():

github_username = config.get("github_username")
github_password = config.get("github_password")
github_access_token = config.get("github_access_token")

perspective_key = config.get("perspective_key")

Expand Down
1 change: 1 addition & 0 deletions helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ def redact(text, redact_str, replace_with):
value = regex.sub(r"((?:https?|ftp):\/\/)[^@:\/]*:[^@:\/]*(?=@)", r"\1[REDACTED URL USERNAME AND PASSWORD]", value)
# In case these are somewhere else.
value = redact(value, GlobalVars.github_password, "[GITHUB PASSWORD REDACTED]")
value = redact(value, GlobalVars.github_access_token, "[GITHUB ACCESS TOKEN REDACTED]")
value = redact(value, GlobalVars.chatexchange_p, "[CHAT PASSWORD REDACTED]")
value = redact(value, GlobalVars.metasmoke_key, "[METASMOKE KEY REDACTED]")
value = redact(value, GlobalVars.perspective_key, "[PERSPECTIVE KEY REDACTED]")
Expand Down

0 comments on commit e3e35ba

Please sign in to comment.