Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update collaborators #92

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions collaborators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# THIS FILE IS OUT OF BOUNDS

import util
from typing import List
from util import HttpVerb

def update(repo: str):
collaborators = [c['login'] for c in util.request(_collaborators_url(repo)).json()]
users = util.users()
_remove_non_players(repo, users, collaborators)
_add_new_players(repo, users, collaborators)

def _remove_non_players(repo: str, users: List[str], collaborators: List[str]):
for collaborator in collaborators:
if not collaborator in users:
print('%s is a collaborator, but not a player. Removing their status as a collaborator' % collaborator)
util.request('%s/%s' % (_collaborators_url(repo), collaborator), HttpVerb.delete)

def _add_new_players(repo: str, users: List[str], collaborators: List[str]):
for user in users:
if not user in collaborators:
print('%s is a player, but not a collaborator. Adding them as a collaborator' % user)
util.request('%s/%s' % (_collaborators_url(repo), user), HttpVerb.put)

def _collaborators_url(repo: str) -> str:
return 'https://www.jefftk.com/nomic-github/repos/%s/collaborators' % repo
38 changes: 27 additions & 11 deletions util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,38 @@
import requests
import subprocess
import time
from typing import Dict, List


def request(url: str) -> requests.Response:
from enum import Enum
from typing import Callable, Dict, List

class HttpVerb(Enum):
get = 1
put = 2
delete = 3

def _request_by_verb() -> Dict[HttpVerb, Callable[[str, Dict[str, str]], requests.Response]]:
return {
HttpVerb.get: requests.get,
HttpVerb.put: requests.put,
HttpVerb.delete: requests.delete
}

def _ok_codes_by_verb() -> Dict[HttpVerb, List[int]]:
return {
HttpVerb.get: [200],
HttpVerb.put: [201],
HttpVerb.delete: [204]
}

def request(url: str, verb=HttpVerb.get) -> requests.Response:
request_headers = {'User-Agent': 'jeffkaufman/nomic'}
response = requests.get(url, headers=request_headers)

for header in ['X-RateLimit-Limit',
'X-RateLimit-Remaining',
'X-RateLimit-Reset']:
if header in response.headers:
print(' > %s: %s' % (header, response.headers[header]))
response = _request_by_verb().get(verb)(url, headers=request_headers)

if response.status_code != 200:
print(' > %s' % response.content)

if response.status_code not in _ok_codes_by_verb().get(verb):
print(' > %s' % response.content)

response.raise_for_status()
return response

Expand Down