Created
July 28, 2020 02:18
-
-
Save dehio3/833fb1486ecc60fe81001863c0c54f16 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# Note | |
# https://gist.github.com/gbaman/b3137e18c739e0cf98539bf4ec4366ad#gistcomment-2963016 | |
from requests import exceptions, request | |
class GitHubQuery: | |
BASE_URL = "https://api.github.com/graphql" | |
ORGANIZATION = "org_name" | |
def __init__( | |
self, | |
github_token=None, | |
query=None, | |
query_params=None, | |
additional_headers=None | |
): | |
self.github_token = github_token | |
self.query = query | |
self.query_params = query_params | |
self.additional_headers = additional_headers or dict() | |
@property | |
def headers(self): | |
default_headers = dict( | |
Authorization=f"token {self.github_token}", | |
) | |
return { | |
**default_headers, | |
**self.additional_headers | |
} | |
def generator(self): | |
while(True): | |
try: | |
yield request( | |
'post', | |
GitHubQuery.BASE_URL, | |
headers=self.headers, | |
json=dict(query=self.query.format_map(self.query_params)) | |
).json() | |
except exceptions.HTTPError as http_err: | |
raise http_err | |
except Exception as err: | |
raise err | |
def iterator(self): | |
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import GitHubGraphQL | |
class MemberReport(GitHubGraphQL.GitHubQuery): | |
# https://developer.github.com/v4/object/user/ | |
VULNERABILITY_QUERY = """ | |
{{ | |
organization(login: "{organization}") {{ | |
id | |
name | |
membersWithRole({args}) {{ | |
pageInfo {{ | |
hasNextPage | |
endCursor | |
}} | |
totalCount | |
edges {{ | |
role | |
node {{ | |
login | |
name | |
createdAt | |
}} | |
}} | |
}} | |
}} | |
}} | |
""" | |
QUERY_PARAMS = dict(organization='', args='first: 100') | |
ADDITIONAL_HEADERS = dict( | |
Accept="application/vnd.github.vixen-preview+json", | |
) | |
def __init__(self, github_token, github_organization): | |
super().__init__( | |
github_token=github_token, | |
query=MemberReport.VULNERABILITY_QUERY, | |
query_params=MemberReport.QUERY_PARAMS, | |
additional_headers=MemberReport.ADDITIONAL_HEADERS | |
) | |
self.organizaion = github_organization | |
def iterator(self): | |
generator = self.generator() | |
hasNextPage = True | |
repos_vulnerabilities = [] | |
self.query_params = dict( | |
organization=self.organizaion, args='first: 100') | |
while(hasNextPage): | |
response = next(generator) | |
endCursor = response["data"]["organization"]["membersWithRole"]["pageInfo"]["endCursor"] | |
arg = 'first: 100, after: "{}"'.format(endCursor) | |
self.query_params = dict(organization=self.organizaion, args=arg) | |
member = {} | |
edges = response["data"]["organization"]["membersWithRole"]["edges"] | |
for edge in edges: | |
member['organization'] = self.organizaion | |
member.update(**edge['node']) | |
member['role'] = edge['role'] | |
repos_vulnerabilities.append(member.copy()) | |
hasNextPage = response["data"]["organization"]["membersWithRole"]["pageInfo"]["hasNextPage"] | |
return (repos_vulnerabilities) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment