Skip to content

Instantly share code, notes, and snippets.

@anton-yurchenko
Last active April 28, 2023 16:19
Show Gist options
  • Save anton-yurchenko/a117a4b80111b75a6083e195941e1bbc to your computer and use it in GitHub Desktop.
Save anton-yurchenko/a117a4b80111b75a6083e195941e1bbc to your computer and use it in GitHub Desktop.
String search on GitHub organization
from requests import get
from sys import argv, exit
from os import environ
from requests.models import HTTPBasicAuth
def main():
if len(argv) < 3:
print('invalid input, expected arguments: `python3.9 script.py <account-name> <search-string>`')
exit(1)
combined = '+'.join(argv[2:])
response = get(
f"https://api.github.com/search/code?q={combined}+in:file+org:{argv[1]}",
headers={'Content-Type': 'application/json'},
auth=HTTPBasicAuth(environ['GITHUB_USERNAME'], environ['GITHUB_TOKEN']))
if response.status_code != 200:
raise Exception('error quering an api')
if len(response.json()['items']) == 0:
return ('not found', 'not found')
x = len(response.json()['items'])
print(f'found {x} affected repositories')
for item in response.json()['items']:
name = item['repository']['name']
file = item['path']
print(f'repo {name}, file {file}')
if __name__ == "__main__":
main()
@Ilaiya2023
Copy link

im getting below error.how to resolve.
C:\Users\ilraju0\Desktop>py github_search_string.py test-org sonar
Github user name is: ilraju0
200
<Response [200]>
Traceback (most recent call last):
File "C:\Users\ilraju0\Desktop\github_search_string.py", line 38, in
main()
File "C:\Users\ilraju0\Desktop\github_search_string.py", line 25, in main
if len(response.json()['items']) == 0:
File "C:\Users\ilraju0\AppData\Local\Programs\Python\Python39\lib\site-packages\requests\models.py", line 910, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\ilraju0\AppData\Local\Programs\Python\Python39\lib\json_init_.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Users\ilraju0\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\ilraju0\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 6 column 1 (char 5)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment