Skip to content

Commit

Permalink
better type usage
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Jun 18, 2024
1 parent 1a286c0 commit d2ecd8f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 38 deletions.
69 changes: 33 additions & 36 deletions Github/AddOrganizationMembers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
"""
mass add users to organization
python AddOrganizationMembers.py my.xlsx ~/.ssh/orgOauth -orgname myorg -col 4
python AddOrganizationMembers.py my.xlsx ~/.ssh/orgOauth myorg 4
oauth token must have "write:org" and public_repo (or repo for private) permissions
https://developer.github.com/v3/repos/#oauth-scope-requirements
Oauth permissions
https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28
https://github.com/settings/tokens/new
Note: the "fine-grained" Oauth didn't work for organizations in Nov 2022.
The "classic" Oauth does work for organizations.
* fine-grained: Oauth "Metadata" repository permissions (read)
-- but needs to be for org. Classic may be better
* classic: Oauth "write:org" and public_repo (or repo for private) permissions
"""

import pandas
Expand All @@ -19,38 +21,11 @@
from argparse import ArgumentParser


def main():
p = ArgumentParser(description="mass add team members to repos, optionally creating new repos")
p.add_argument("fn", help=".xlsx with group info")
p.add_argument("oauth", help="Oauth file")
p.add_argument("-orgname", help="Github Organization", required=True)
p.add_argument("-col", help="column for GitHub Username", type=int, required=True)
p = p.parse_args()

fn = Path(p.fn).expanduser()

match fn.suffix:
case ".xls" | ".xlsx":
users = pandas.read_excel(fn, usecols=p.col).squeeze().dropna()
case ".csv":
users = pandas.read_csv(fn, usecols=[p.col]).squeeze().dropna()
case _:
raise ValueError(f"Unknown file type {fn}")

if not users.ndim == 1:
raise ValueError("need to have member names. Check that -col argument matches spreadsheet.")
# %%
op, sess = connect(p.oauth, p.orgname)
check_api_limit(sess)

adder(users, op, sess)


def adder(users: pandas.DataFrame, op, sess):
def adder(users: pandas.DataFrame, op, sess) -> None:
members = [m.login for m in op.get_members()]
invited = [m.login for m in op.invitations()]

for u in users:
for u in users.values:
login = u.strip()
if login in members or login in invited:
continue
Expand All @@ -64,5 +39,27 @@ def adder(users: pandas.DataFrame, op, sess):
print(f"invited: {login}")


if __name__ == "__main__":
main()
p = ArgumentParser(description="mass add team members to repos, optionally creating new repos")
p.add_argument("fn", help=".xlsx with group info")
p.add_argument("oauth", help="Oauth file")
p.add_argument("orgname", help="Github Organization")
p.add_argument("col", help="column for GitHub Username", type=int)
P = p.parse_args()

fn = Path(P.fn).expanduser()

match fn.suffix:
case ".xls" | ".xlsx":
users = pandas.read_excel(fn, usecols=[P.col]).squeeze().dropna()
case ".csv":
users = pandas.read_csv(fn, usecols=[P.col]).squeeze().dropna()
case _:
raise ValueError(f"Unknown file type {fn}")

if not users.ndim == 1:
raise ValueError("need to have member names. Check that -col argument matches spreadsheet.")
# %%
op, sess = connect(P.oauth, P.orgname)
check_api_limit(sess)

adder(users, op, sess)
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ classifiers = ["Development Status :: 5 - Production/Stable",
]
requires-python = ">=3.10"
dynamic = ["readme","version"]
dependencies = ["pygithub >= 1.53", "pandas"]
dependencies = ["pygithub >= 1.53", "pandas", "openpyxl"]


[tool.setuptools.dynamic]
Expand All @@ -26,7 +26,7 @@ version = {attr = "gitbulk.__version__"}
[project.optional-dependencies]
tests = ["pytest"]
lint = ["flake8", "flake8-bugbear", "flake8-builtins", "flake8-blind-except",
"mypy", "types-requests"]
"mypy", "types-requests", "pandas-stubs"]

[tool.black]
line-length = 100
Expand Down

0 comments on commit d2ecd8f

Please sign in to comment.