-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate-users.py
executable file
·172 lines (131 loc) · 4.54 KB
/
update-users.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python
"""
Copyright (C) 2020-2022 Vanessa Sochat.
This Source Code Form is subject to the terms of the
Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
"""
import argparse
import json
import os
import requests
import sys
here = os.path.dirname(os.path.abspath(__file__))
print("Present working directory is %s" % here)
def get_parser():
parser = argparse.ArgumentParser(description="Open Source Heartbeat")
parser.add_argument(
"--version",
dest="version",
help="suppress additional output.",
default=False,
action="store_true",
)
parser.add_argument(
"--users-file",
dest="users_file",
help="The users.txt file with GitHub usernames on lines.",
default=None,
)
parser.add_argument(
"--exclude-users-file",
dest="exclude_users_file",
help="A list of users to never add (that might still be discovered).",
default=None,
)
parser.add_argument(
"--user-query",
dest="user_query",
help="The string portion of a user query, generate with https://github.com/search/advanced.",
default=None,
)
parser.add_argument(
"--org-users-file",
dest="org_users_file",
help="A filename with organization names to get public users from",
default=None,
)
return parser
def get_headers():
"""Get headers with an authentication token if one is found in the
environment
"""
# Add token to increase API limits
token = os.environ.get("GITHUB_TOKEN")
headers = {"Accept": "application/vnd.github.v3+json"}
if token:
headers = {"Authorization": f"token {token}"}
return headers
def get_org_users(orgs):
"""
Given a list of orgs, retrieve public users
"""
users = []
for org in orgs:
org = org.strip()
if not org:
continue
res = requests.get(f"https://api.github.com/orgs/{org}/members")
if res.status_code != 200:
print(f"{org} {res.status_code}: {res.reason}")
continue
users += [x["login"] for x in res.json()]
return users
def search_users(query, search_type="users"):
"""
Return a subset of users that match a particular query (up to 1000)
The example here searches for location Stanford
"""
url = "https://api.github.com/search/%s?q=%s" % (search_type, query)
response = requests.get(url, headers=get_headers())
# Exit early if issue with request
if response.status_code != 200:
sys.exit(f"{response.status_code}: {response.reason}")
return [item["login"] for item in response.json().get("items", [])]
def read_file(filename):
with open(filename, "r") as fd:
content = fd.read()
return content
def write_file(content, filename):
with open(filename, "w") as fd:
fd.writelines(content)
def main():
parser = get_parser()
args, extra = parser.parse_known_args()
# GitHub Workflow - we get variables from environment
USERS_FILE = args.users_file or os.environ.get("INPUT_USERS_FILE", "users.txt")
SKIP_USERS_FILE = args.exclude_users_file or os.environ.get(
"INPUT_EXCLUDE_USERS_FILE", "exclude-users.txt"
)
USERS_FROM_ORGS_FILE = args.org_users_file or os.environ.get(
"INPUT_USERS_FROM_ORGS_FILE", "org-users.txt"
)
# Debugging for the user
print(f"users file: {USERS_FILE}")
print(f"skips file: {SKIP_USERS_FILE}")
print(f"users from orgs file {USERS_FROM_ORGS_FILE}")
# Update users to file (so they can remove later)
SEARCH_QUERY = os.environ.get("INPUT_QUERY", args.user_query)
users = []
if os.path.exists(USERS_FILE):
users = [u for u in read_file(USERS_FILE).split("\n") if u]
# Get public users for an org
if os.path.exists(USERS_FROM_ORGS_FILE):
users += get_org_users(read_file(USERS_FROM_ORGS_FILE).split("\n"))
# Unique
users = list(set(users))
# If skip users is defined
skip_users = []
if os.path.exists(SKIP_USERS_FILE):
skip_users = [u for u in read_file(USERS_FILE).split("\n") if u]
# To we want to further filter?
new_users = []
if SEARCH_QUERY:
new_users = search_users(SEARCH_QUERY)
users = users + new_users
# Update users
users = [user for user in users if user not in skip_users]
print(f"Found {len(new_users)} users!")
write_file("\n".join(users), USERS_FILE)
if __name__ == "__main__":
main()