forked from mozilla/github-org-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_search.py
78 lines (59 loc) · 1.81 KB
/
code_search.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
__doc__ = """Searches for code across multiple github orgs."""
import argparse
import jsonstreams
import sys
from client import (
get_github3_client,
sleep_if_rate_limited,
)
DEFAULT_ORGS = [
"mozilla",
"mozilla-conduit",
"mozilla-platform-ops",
"mozilla-releng",
"mozilla-services",
"taskcluster",
]
def parse_args():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"query",
type=str,
help="code search query. syntax at https://help.github.com/articles/searching-code/",
)
parser.add_argument(
"--orgs",
default=DEFAULT_ORGS,
nargs="*",
help=f"organizations to search (defaults to {DEFAULT_ORGS})",
)
parser.add_argument(
"--json", help="path to output json results", type=str, default=None
)
parser.add_argument(
"--verbose", action="store_true", help="print logins for all changes"
)
return parser.parse_args()
def main():
args = parse_args()
gh = get_github3_client()
json_fout = (
jsonstreams.Stream(jsonstreams.Type.array, args.json, indent=4)
if args.json
else None
)
for org in args.orgs:
full_query = f"org:{org} {args.query}"
if args.verbose:
print(f"searching with query {full_query}")
sleep_if_rate_limited(gh, verbose=args.verbose)
print("{:<16}{:<32}{:<64}".format("org", "repo", "file path"))
for result in gh.search_code(full_query):
print("{0:<16}{1.repository.name:<32}{1.path:<64}".format(org, result))
if json_fout:
json_fout.write(result.to_json())
if json_fout:
# must explicitly close -- that's what outputs the closing delimiter
json_fout.close()
if __name__ == "__main__":
main()