24_deb_pkg_gov/github_api_req.py

71 lines
2.7 KiB
Python
Raw Normal View History

2023-10-23 20:40:24 +00:00
import requests
import datetime as dt
2023-10-26 04:38:14 +00:00
import json
import os
key = os.environ.get('KKEXKEY')
2023-10-23 20:40:24 +00:00
def main(vcs, begin_date):
2023-10-24 01:11:51 +00:00
repo_uri=vcs[0]
2023-10-23 20:40:24 +00:00
gha_info = {}
#this is the entire list of Github 'milestones' grabbed from the API
2023-10-24 01:11:51 +00:00
gha_info['milestones'] = get_milestone_information(repo_uri)
2023-10-23 20:40:24 +00:00
#this is the count of milestones that occur after the cutoff date
gha_info['milestone_count'] = parse_milestones(gha_info['milestones'], begin_date)
2023-10-24 01:11:51 +00:00
#split_actors(repo_uri, actors_list)
2023-10-23 20:40:24 +00:00
return gha_info
2023-10-24 01:11:51 +00:00
2023-10-23 20:40:24 +00:00
#this simple API call has been working for now but may need to be updated as more information is desired
2023-10-24 01:11:51 +00:00
def get_milestone_information(repo_uri):
2023-10-23 20:40:24 +00:00
repo_uri_list = repo_uri.split('/')
2023-10-24 01:11:51 +00:00
print(repo_uri_list)
2023-10-23 20:40:24 +00:00
api_url = "https://api.github.com/repos/" + repo_uri_list[-2] + "/" + repo_uri_list[-1] + "/milestones"
response = requests.get(api_url)
response_dict = response.json()
return response_dict
def parse_milestones(milestones, earliest_date):
count_of_milestones = 0
for entry in milestones:
#if entry date is more recent than the earliest date we're looking at
2023-10-24 01:11:51 +00:00
# TODO: decide whether to use created_at or updated_at or closed_at
if dt.datetime.fromisoformat(entry['updated_at'][:-1]) > earliest_date:
2023-10-23 20:40:24 +00:00
count_of_milestones += 1
return count_of_milestones
2023-10-26 04:38:14 +00:00
def get_discussion_gql():
#testing
url = "https://api.github.com/graphql"
x = {"query" : "query { repository(owner: 'beekeeper-studio', name: 'beekeeper-studio') { discussions(first: 10) {totalCount}}"}
data = json.dumps(x)
headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8', 'Authorization': 'bearer ' + key}
r = requests.post(url=url, data=data, headers=headers)
print(r.content)
print(r.json())
if __name__ == "__main__":
get_discussion_gql()
2023-10-24 01:11:51 +00:00
'''
#using the github API to identify who is a collaborator on the project and who is just a contributor
def split_actors(repo_uri, actors_list):
call_sheet = {'collaborator' : [], 'contributor' : []}
repo_uri_list = repo_uri.split('/')
api_url = "https://api.github.com/repos/" + repo_uri_list[-2] + "/" + repo_uri_list[-1] + "/collaborators/"
for actor in actors_list[:2]:
actor_email = actor.split('<')[1][:-1]
print(actor_email)
actor_user = get_gh_un(actor_email)
response_dict = response.json()
print(response_dict)
2023-10-23 20:40:24 +00:00
2023-10-24 01:11:51 +00:00
#this function grabs the Github username from an associated email
def get_gh_un(email):
api_url = 'https://api.github.com/search/users?q=' + email
response = requests.get(api_url)
response_dict = response.json()
gh_username = response_dict['items'][0]['login']
return gh_username
'''