forked from MuckRock/muckrock
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostdeploy.py
More file actions
63 lines (52 loc) · 1.89 KB
/
postdeploy.py
File metadata and controls
63 lines (52 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
""" Script that adds the MuckRock review app redirect URI to the
MuckRock staging client on Squarelet staging
"""
# Standard Library
#!/usr/bin/env python3
import os
import sys
# Third Party
import requests
SQUARELET_URL = os.environ["SQUARELET_URL"]
CLIENT_ID = 1 # Add redirect URIs to MuckRock client
REVIEW_APP_URL = (
f"https://{os.environ['HEROKU_APP_NAME']}"
".herokuapp.com/accounts/complete/squarelet"
)
GITHUB_CLIENT = os.environ["GITHUB_ACTIONS_CLIENT"]
GITHUB_SECRET = os.environ["GITHUB_ACTIONS_SECRET"]
os.environ["https_proxy"] = os.environ["FIXIE_URL"]
def get_access_token():
"""Fetch an access token using client_credentials grant"""
token_url = f"{SQUARELET_URL}/openid/token"
data = {"grant_type": "client_credentials"}
resp = requests.post(
token_url, auth=(GITHUB_CLIENT, GITHUB_SECRET), data=data, timeout=20
)
resp.raise_for_status()
return resp.json()["access_token"]
def patch_redirect_uri(client_id, redirect_uri, cmd_action):
"""PATCH the client redirect URIs on squarelet staging"""
if cmd_action not in ("add", "remove"):
raise ValueError("Action must be 'add' or 'remove'")
access_token = get_access_token()
endpoint = f"{SQUARELET_URL}/api/clients/{client_id}/redirect_uris/"
payload = {
"action": cmd_action,
"redirect_uris": [redirect_uri],
}
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json",
}
resp = requests.patch(endpoint, json=payload, headers=headers, timeout=30)
resp.raise_for_status()
print(f"Successfully {cmd_action}ed redirect URI: {redirect_uri}")
print(resp.json())
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: ./script.py <add|remove>")
sys.exit(1)
action = sys.argv[1].lower()
patch_redirect_uri(CLIENT_ID, REVIEW_APP_URL, action)