We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
search for teams by handle/slug and not name
I think this is a simple as using `team.slug` instead of `team.name` in the relevant code.

cc @isuruf 

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `conda_smithy/github.py`
Content:
```
1 import os
2 from random import choice
3 
4 from git import Repo
5 
6 from github import Github
7 from github.GithubException import GithubException
8 from github.Organization import Organization
9 from github.Team import Team
10 import github
11 
12 import conda_build.api
13 from conda_smithy.utils import get_feedstock_name_from_meta
14 
15 
16 def gh_token():
17     try:
18         with open(
19             os.path.expanduser("~/.conda-smithy/github.token"), "r"
20         ) as fh:
21             token = fh.read().strip()
22         if not token:
23             raise ValueError()
24     except (IOError, ValueError):
25         msg = (
26             "No github token. Go to https://github.com/settings/tokens/new and generate\n"
27             "a token with repo access. Put it in ~/.conda-smithy/github.token"
28         )
29         raise RuntimeError(msg)
30     return token
31 
32 
33 def create_team(org, name, description, repo_names=[]):
34     # PyGithub creates secret teams, and has no way of turning that off! :(
35     post_parameters = {
36         "name": name,
37         "description": description,
38         "privacy": "closed",
39         "permission": "push",
40         "repo_names": repo_names,
41     }
42     headers, data = org._requester.requestJsonAndCheck(
43         "POST", org.url + "/teams", input=post_parameters
44     )
45     return Team(org._requester, headers, data, completed=True)
46 
47 
48 def add_membership(team, member):
49     headers, data = team._requester.requestJsonAndCheck(
50         "PUT", team.url + "/memberships/" + member
51     )
52     return (headers, data)
53 
54 
55 def remove_membership(team, member):
56     headers, data = team._requester.requestJsonAndCheck(
57         "DELETE", team.url + "/memberships/" + member
58     )
59     return (headers, data)
60 
61 
62 def has_in_members(team, member):
63     status, headers, data = team._requester.requestJson(
64         "GET", team.url + "/members/" + member
65     )
66     return status == 204
67 
68 
69 def get_cached_team(org, team_name, description=""):
70     cached_file = os.path.expanduser(
71         "~/.conda-smithy/{}-{}-team".format(org.login, team_name)
72     )
73     try:
74         with open(cached_file, "r") as fh:
75             team_id = int(fh.read().strip())
76             return org.get_team(team_id)
77     except IOError:
78         pass
79 
80     try:
81         repo = org.get_repo("{}-feedstock".format(team_name))
82         team = next(
83             (team for team in repo.get_teams() if team.name == team_name), None
84         )
85         if team:
86             return team
87     except GithubException:
88         pass
89 
90     team = next(
91         (team for team in org.get_teams() if team.name == team_name), None
92     )
93     if not team:
94         if description:
95             team = create_team(org, team_name, description, [])
96         else:
97             raise RuntimeError("Couldn't find team {}".format(team_name))
98 
99     with open(cached_file, "w") as fh:
100         fh.write(str(team.id))
101 
102     return team
103 
104 
105 def create_github_repo(args):
106     token = gh_token()
107     meta = conda_build.api.render(
108         args.feedstock_directory,
109         permit_undefined_jinja=True,
110         finalize=False,
111         bypass_env_check=True,
112         trim_skip=False,
113     )[0][0]
114 
115     feedstock_name = get_feedstock_name_from_meta(meta)
116 
117     gh = Github(token)
118     user_or_org = None
119     if args.user is not None:
120         pass
121         # User has been defined, and organization has not.
122         user_or_org = gh.get_user()
123     else:
124         # Use the organization provided.
125         user_or_org = gh.get_organization(args.organization)
126 
127     repo_name = "{}-feedstock".format(feedstock_name)
128     try:
129         gh_repo = user_or_org.create_repo(
130             repo_name,
131             has_wiki=False,
132             private=args.private,
133             description="A conda-smithy repository for {}.".format(
134                 feedstock_name
135             ),
136         )
137         print("Created {} on github".format(gh_repo.full_name))
138     except GithubException as gh_except:
139         if (
140             gh_except.data.get("errors", [{}])[0].get("message", "")
141             != u"name already exists on this account"
142         ):
143             raise
144         gh_repo = user_or_org.get_repo(repo_name)
145         print("Github repository already exists.")
146 
147     # Now add this new repo as a remote on the local clone.
148     repo = Repo(args.feedstock_directory)
149     remote_name = args.remote_name.strip()
150     if remote_name:
151         if remote_name in [remote.name for remote in repo.remotes]:
152             existing_remote = repo.remotes[remote_name]
153             if existing_remote.url != gh_repo.ssh_url:
154                 print(
155                     "Remote {} already exists, and doesn't point to {} "
156                     "(it points to {}).".format(
157                         remote_name, gh_repo.ssh_url, existing_remote.url
158                     )
159                 )
160         else:
161             repo.create_remote(remote_name, gh_repo.ssh_url)
162 
163     if args.extra_admin_users is not None:
164         for user in args.extra_admin_users:
165             gh_repo.add_to_collaborators(user, "admin")
166 
167     if args.add_teams:
168         if isinstance(user_or_org, Organization):
169             configure_github_team(meta, gh_repo, user_or_org, feedstock_name)
170 
171 
172 def accept_all_repository_invitations(gh):
173     user = gh.get_user()
174     invitations = github.PaginatedList.PaginatedList(
175         github.Invitation.Invitation,
176         user._requester,
177         user.url + "/repository_invitations",
178         None,
179     )
180     for invite in invitations:
181         invite._requester.requestJsonAndCheck("PATCH", invite.url)
182 
183 
184 def remove_from_project(gh, org, project):
185     user = gh.get_user()
186     repo = gh.get_repo("{}/{}".format(org, project))
187     repo.remove_from_collaborators(user.login)
188 
189 
190 def configure_github_team(meta, gh_repo, org, feedstock_name):
191 
192     # Add a team for this repo and add the maintainers to it.
193     superlative = [
194         "awesome",
195         "slick",
196         "formidable",
197         "awe-inspiring",
198         "breathtaking",
199         "magnificent",
200         "wonderous",
201         "stunning",
202         "astonishing",
203         "superb",
204         "splendid",
205         "impressive",
206         "unbeatable",
207         "excellent",
208         "top",
209         "outstanding",
210         "exalted",
211         "standout",
212         "smashing",
213     ]
214 
215     maintainers = set(meta.meta.get("extra", {}).get("recipe-maintainers", []))
216     maintainers = set(maintainer.lower() for maintainer in maintainers)
217     maintainer_teams = set(m for m in maintainers if "/" in m)
218     maintainers = set(m for m in maintainers if "/" not in m)
219 
220     # Try to get team or create it if it doesn't exist.
221     team_name = feedstock_name
222     current_maintainer_teams = list(gh_repo.get_teams())
223     team = next(
224         (team for team in current_maintainer_teams if team.name == team_name),
225         None,
226     )
227     current_maintainers = set()
228     if not team:
229         team = create_team(
230             org,
231             team_name,
232             "The {} {} contributors!".format(choice(superlative), team_name),
233         )
234         team.add_to_repos(gh_repo)
235     else:
236         current_maintainers = set(
237             [e.login.lower() for e in team.get_members()]
238         )
239 
240     # Get the all-members team
241     description = "All of the awesome {} contributors!".format(org.login)
242     all_members_team = get_cached_team(org, "all-members", description)
243     new_org_members = set()
244 
245     # Add only the new maintainers to the team.
246     # Also add the new maintainers to all-members if not already included.
247     for new_maintainer in maintainers - current_maintainers:
248         add_membership(team, new_maintainer)
249 
250         if not has_in_members(all_members_team, new_maintainer):
251             print(
252                 "Adding a new member ({}) to {}. Welcome! :)".format(
253                     new_maintainer, org.login
254                 )
255             )
256             add_membership(all_members_team, new_maintainer)
257             new_org_members.add(new_maintainer)
258 
259     # Mention any maintainers that need to be removed (unlikely here).
260     for old_maintainer in current_maintainers - maintainers:
261         print(
262             "AN OLD MEMBER ({}) NEEDS TO BE REMOVED FROM {}".format(
263                 old_maintainer, gh_repo
264             )
265         )
266 
267     # Add any new maintainer team
268     maintainer_teams = set(
269         m.split("/")[1]
270         for m in maintainer_teams
271         if m.startswith(str(org.login))
272     )
273     current_maintainer_teams = [team.name for team in current_maintainer_teams]
274     for maintainer_team in maintainer_teams - set(current_maintainer_teams):
275         print(
276             "Adding a new team ({}) to {}. Welcome! :)".format(
277                 maintainer_team, org.login
278             )
279         )
280 
281         team = get_cached_team(org, maintainer_team)
282         team.add_to_repos(gh_repo)
283 
284     return maintainers, current_maintainers, new_org_members
285
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/conda_smithy/github.py b/conda_smithy/github.py
--- a/conda_smithy/github.py
+++ b/conda_smithy/github.py
@@ -187,7 +187,7 @@
     repo.remove_from_collaborators(user.login)
 
 
-def configure_github_team(meta, gh_repo, org, feedstock_name):
+def configure_github_team(meta, gh_repo, org, feedstock_name, remove=True):
 
     # Add a team for this repo and add the maintainers to it.
     superlative = [
@@ -220,21 +220,21 @@
     # Try to get team or create it if it doesn't exist.
     team_name = feedstock_name
     current_maintainer_teams = list(gh_repo.get_teams())
-    team = next(
+    fs_team = next(
         (team for team in current_maintainer_teams if team.name == team_name),
         None,
     )
     current_maintainers = set()
-    if not team:
-        team = create_team(
+    if not fs_team:
+        fs_team = create_team(
             org,
             team_name,
             "The {} {} contributors!".format(choice(superlative), team_name),
         )
-        team.add_to_repos(gh_repo)
+        fs_team.add_to_repos(gh_repo)
     else:
         current_maintainers = set(
-            [e.login.lower() for e in team.get_members()]
+            [e.login.lower() for e in fs_team.get_members()]
         )
 
     # Get the all-members team
@@ -245,40 +245,41 @@
     # Add only the new maintainers to the team.
     # Also add the new maintainers to all-members if not already included.
     for new_maintainer in maintainers - current_maintainers:
-        add_membership(team, new_maintainer)
+        add_membership(fs_team, new_maintainer)
 
         if not has_in_members(all_members_team, new_maintainer):
-            print(
-                "Adding a new member ({}) to {}. Welcome! :)".format(
-                    new_maintainer, org.login
-                )
-            )
             add_membership(all_members_team, new_maintainer)
             new_org_members.add(new_maintainer)
 
-    # Mention any maintainers that need to be removed (unlikely here).
-    for old_maintainer in current_maintainers - maintainers:
-        print(
-            "AN OLD MEMBER ({}) NEEDS TO BE REMOVED FROM {}".format(
-                old_maintainer, gh_repo
-            )
-        )
+    # Remove any maintainers that need to be removed (unlikely here).
+    if remove:
+        for old_maintainer in current_maintainers - maintainers:
+            remove_membership(fs_team, old_maintainer)
 
-    # Add any new maintainer team
+    # Add any new maintainer teams
     maintainer_teams = set(
         m.split("/")[1]
         for m in maintainer_teams
         if m.startswith(str(org.login))
     )
-    current_maintainer_teams = [team.name for team in current_maintainer_teams]
-    for maintainer_team in maintainer_teams - set(current_maintainer_teams):
-        print(
-            "Adding a new team ({}) to {}. Welcome! :)".format(
-                maintainer_team, org.login
-            )
-        )
-
-        team = get_cached_team(org, maintainer_team)
+    current_maintainer_team_objs = {
+        team.slug: team for team in current_maintainer_teams
+    }
+    current_maintainer_teams = set(
+        [team.slug for team in current_maintainer_teams]
+    )
+    for new_team in maintainer_teams - current_maintainer_teams:
+        team = org.get_team_by_slug(new_team)
         team.add_to_repos(gh_repo)
 
+    # remove any old teams
+    if remove:
+        for old_team in current_maintainer_teams - maintainer_teams:
+            team = current_maintainer_team_objs.get(
+                old_team, org.get_team_by_slug(old_team)
+            )
+            if team.name == fs_team.name:
+                continue
+            team.remove_from_repos(gh_repo)
+
     return maintainers, current_maintainers, new_org_members

{"golden_diff": "diff --git a/conda_smithy/github.py b/conda_smithy/github.py\n--- a/conda_smithy/github.py\n+++ b/conda_smithy/github.py\n@@ -187,7 +187,7 @@\n     repo.remove_from_collaborators(user.login)\n \n \n-def configure_github_team(meta, gh_repo, org, feedstock_name):\n+def configure_github_team(meta, gh_repo, org, feedstock_name, remove=True):\n \n     # Add a team for this repo and add the maintainers to it.\n     superlative = [\n@@ -220,21 +220,21 @@\n     # Try to get team or create it if it doesn't exist.\n     team_name = feedstock_name\n     current_maintainer_teams = list(gh_repo.get_teams())\n-    team = next(\n+    fs_team = next(\n         (team for team in current_maintainer_teams if team.name == team_name),\n         None,\n     )\n     current_maintainers = set()\n-    if not team:\n-        team = create_team(\n+    if not fs_team:\n+        fs_team = create_team(\n             org,\n             team_name,\n             \"The {} {} contributors!\".format(choice(superlative), team_name),\n         )\n-        team.add_to_repos(gh_repo)\n+        fs_team.add_to_repos(gh_repo)\n     else:\n         current_maintainers = set(\n-            [e.login.lower() for e in team.get_members()]\n+            [e.login.lower() for e in fs_team.get_members()]\n         )\n \n     # Get the all-members team\n@@ -245,40 +245,41 @@\n     # Add only the new maintainers to the team.\n     # Also add the new maintainers to all-members if not already included.\n     for new_maintainer in maintainers - current_maintainers:\n-        add_membership(team, new_maintainer)\n+        add_membership(fs_team, new_maintainer)\n \n         if not has_in_members(all_members_team, new_maintainer):\n-            print(\n-                \"Adding a new member ({}) to {}. Welcome! :)\".format(\n-                    new_maintainer, org.login\n-                )\n-            )\n             add_membership(all_members_team, new_maintainer)\n             new_org_members.add(new_maintainer)\n \n-    # Mention any maintainers that need to be removed (unlikely here).\n-    for old_maintainer in current_maintainers - maintainers:\n-        print(\n-            \"AN OLD MEMBER ({}) NEEDS TO BE REMOVED FROM {}\".format(\n-                old_maintainer, gh_repo\n-            )\n-        )\n+    # Remove any maintainers that need to be removed (unlikely here).\n+    if remove:\n+        for old_maintainer in current_maintainers - maintainers:\n+            remove_membership(fs_team, old_maintainer)\n \n-    # Add any new maintainer team\n+    # Add any new maintainer teams\n     maintainer_teams = set(\n         m.split(\"/\")[1]\n         for m in maintainer_teams\n         if m.startswith(str(org.login))\n     )\n-    current_maintainer_teams = [team.name for team in current_maintainer_teams]\n-    for maintainer_team in maintainer_teams - set(current_maintainer_teams):\n-        print(\n-            \"Adding a new team ({}) to {}. Welcome! :)\".format(\n-                maintainer_team, org.login\n-            )\n-        )\n-\n-        team = get_cached_team(org, maintainer_team)\n+    current_maintainer_team_objs = {\n+        team.slug: team for team in current_maintainer_teams\n+    }\n+    current_maintainer_teams = set(\n+        [team.slug for team in current_maintainer_teams]\n+    )\n+    for new_team in maintainer_teams - current_maintainer_teams:\n+        team = org.get_team_by_slug(new_team)\n         team.add_to_repos(gh_repo)\n \n+    # remove any old teams\n+    if remove:\n+        for old_team in current_maintainer_teams - maintainer_teams:\n+            team = current_maintainer_team_objs.get(\n+                old_team, org.get_team_by_slug(old_team)\n+            )\n+            if team.name == fs_team.name:\n+                continue\n+            team.remove_from_repos(gh_repo)\n+\n     return maintainers, current_maintainers, new_org_members\n", "issue": "search for teams by handle/slug and not name\nI think this is a simple as using `team.slug` instead of `team.name` in the relevant code.\r\n\r\ncc @isuruf \n", "before_files": [{"content": "import os\nfrom random import choice\n\nfrom git import Repo\n\nfrom github import Github\nfrom github.GithubException import GithubException\nfrom github.Organization import Organization\nfrom github.Team import Team\nimport github\n\nimport conda_build.api\nfrom conda_smithy.utils import get_feedstock_name_from_meta\n\n\ndef gh_token():\n    try:\n        with open(\n            os.path.expanduser(\"~/.conda-smithy/github.token\"), \"r\"\n        ) as fh:\n            token = fh.read().strip()\n        if not token:\n            raise ValueError()\n    except (IOError, ValueError):\n        msg = (\n            \"No github token. Go to https://github.com/settings/tokens/new and generate\\n\"\n            \"a token with repo access. Put it in ~/.conda-smithy/github.token\"\n        )\n        raise RuntimeError(msg)\n    return token\n\n\ndef create_team(org, name, description, repo_names=[]):\n    # PyGithub creates secret teams, and has no way of turning that off! :(\n    post_parameters = {\n        \"name\": name,\n        \"description\": description,\n        \"privacy\": \"closed\",\n        \"permission\": \"push\",\n        \"repo_names\": repo_names,\n    }\n    headers, data = org._requester.requestJsonAndCheck(\n        \"POST\", org.url + \"/teams\", input=post_parameters\n    )\n    return Team(org._requester, headers, data, completed=True)\n\n\ndef add_membership(team, member):\n    headers, data = team._requester.requestJsonAndCheck(\n        \"PUT\", team.url + \"/memberships/\" + member\n    )\n    return (headers, data)\n\n\ndef remove_membership(team, member):\n    headers, data = team._requester.requestJsonAndCheck(\n        \"DELETE\", team.url + \"/memberships/\" + member\n    )\n    return (headers, data)\n\n\ndef has_in_members(team, member):\n    status, headers, data = team._requester.requestJson(\n        \"GET\", team.url + \"/members/\" + member\n    )\n    return status == 204\n\n\ndef get_cached_team(org, team_name, description=\"\"):\n    cached_file = os.path.expanduser(\n        \"~/.conda-smithy/{}-{}-team\".format(org.login, team_name)\n    )\n    try:\n        with open(cached_file, \"r\") as fh:\n            team_id = int(fh.read().strip())\n            return org.get_team(team_id)\n    except IOError:\n        pass\n\n    try:\n        repo = org.get_repo(\"{}-feedstock\".format(team_name))\n        team = next(\n            (team for team in repo.get_teams() if team.name == team_name), None\n        )\n        if team:\n            return team\n    except GithubException:\n        pass\n\n    team = next(\n        (team for team in org.get_teams() if team.name == team_name), None\n    )\n    if not team:\n        if description:\n            team = create_team(org, team_name, description, [])\n        else:\n            raise RuntimeError(\"Couldn't find team {}\".format(team_name))\n\n    with open(cached_file, \"w\") as fh:\n        fh.write(str(team.id))\n\n    return team\n\n\ndef create_github_repo(args):\n    token = gh_token()\n    meta = conda_build.api.render(\n        args.feedstock_directory,\n        permit_undefined_jinja=True,\n        finalize=False,\n        bypass_env_check=True,\n        trim_skip=False,\n    )[0][0]\n\n    feedstock_name = get_feedstock_name_from_meta(meta)\n\n    gh = Github(token)\n    user_or_org = None\n    if args.user is not None:\n        pass\n        # User has been defined, and organization has not.\n        user_or_org = gh.get_user()\n    else:\n        # Use the organization provided.\n        user_or_org = gh.get_organization(args.organization)\n\n    repo_name = \"{}-feedstock\".format(feedstock_name)\n    try:\n        gh_repo = user_or_org.create_repo(\n            repo_name,\n            has_wiki=False,\n            private=args.private,\n            description=\"A conda-smithy repository for {}.\".format(\n                feedstock_name\n            ),\n        )\n        print(\"Created {} on github\".format(gh_repo.full_name))\n    except GithubException as gh_except:\n        if (\n            gh_except.data.get(\"errors\", [{}])[0].get(\"message\", \"\")\n            != u\"name already exists on this account\"\n        ):\n            raise\n        gh_repo = user_or_org.get_repo(repo_name)\n        print(\"Github repository already exists.\")\n\n    # Now add this new repo as a remote on the local clone.\n    repo = Repo(args.feedstock_directory)\n    remote_name = args.remote_name.strip()\n    if remote_name:\n        if remote_name in [remote.name for remote in repo.remotes]:\n            existing_remote = repo.remotes[remote_name]\n            if existing_remote.url != gh_repo.ssh_url:\n                print(\n                    \"Remote {} already exists, and doesn't point to {} \"\n                    \"(it points to {}).\".format(\n                        remote_name, gh_repo.ssh_url, existing_remote.url\n                    )\n                )\n        else:\n            repo.create_remote(remote_name, gh_repo.ssh_url)\n\n    if args.extra_admin_users is not None:\n        for user in args.extra_admin_users:\n            gh_repo.add_to_collaborators(user, \"admin\")\n\n    if args.add_teams:\n        if isinstance(user_or_org, Organization):\n            configure_github_team(meta, gh_repo, user_or_org, feedstock_name)\n\n\ndef accept_all_repository_invitations(gh):\n    user = gh.get_user()\n    invitations = github.PaginatedList.PaginatedList(\n        github.Invitation.Invitation,\n        user._requester,\n        user.url + \"/repository_invitations\",\n        None,\n    )\n    for invite in invitations:\n        invite._requester.requestJsonAndCheck(\"PATCH\", invite.url)\n\n\ndef remove_from_project(gh, org, project):\n    user = gh.get_user()\n    repo = gh.get_repo(\"{}/{}\".format(org, project))\n    repo.remove_from_collaborators(user.login)\n\n\ndef configure_github_team(meta, gh_repo, org, feedstock_name):\n\n    # Add a team for this repo and add the maintainers to it.\n    superlative = [\n        \"awesome\",\n        \"slick\",\n        \"formidable\",\n        \"awe-inspiring\",\n        \"breathtaking\",\n        \"magnificent\",\n        \"wonderous\",\n        \"stunning\",\n        \"astonishing\",\n        \"superb\",\n        \"splendid\",\n        \"impressive\",\n        \"unbeatable\",\n        \"excellent\",\n        \"top\",\n        \"outstanding\",\n        \"exalted\",\n        \"standout\",\n        \"smashing\",\n    ]\n\n    maintainers = set(meta.meta.get(\"extra\", {}).get(\"recipe-maintainers\", []))\n    maintainers = set(maintainer.lower() for maintainer in maintainers)\n    maintainer_teams = set(m for m in maintainers if \"/\" in m)\n    maintainers = set(m for m in maintainers if \"/\" not in m)\n\n    # Try to get team or create it if it doesn't exist.\n    team_name = feedstock_name\n    current_maintainer_teams = list(gh_repo.get_teams())\n    team = next(\n        (team for team in current_maintainer_teams if team.name == team_name),\n        None,\n    )\n    current_maintainers = set()\n    if not team:\n        team = create_team(\n            org,\n            team_name,\n            \"The {} {} contributors!\".format(choice(superlative), team_name),\n        )\n        team.add_to_repos(gh_repo)\n    else:\n        current_maintainers = set(\n            [e.login.lower() for e in team.get_members()]\n        )\n\n    # Get the all-members team\n    description = \"All of the awesome {} contributors!\".format(org.login)\n    all_members_team = get_cached_team(org, \"all-members\", description)\n    new_org_members = set()\n\n    # Add only the new maintainers to the team.\n    # Also add the new maintainers to all-members if not already included.\n    for new_maintainer in maintainers - current_maintainers:\n        add_membership(team, new_maintainer)\n\n        if not has_in_members(all_members_team, new_maintainer):\n            print(\n                \"Adding a new member ({}) to {}. Welcome! :)\".format(\n                    new_maintainer, org.login\n                )\n            )\n            add_membership(all_members_team, new_maintainer)\n            new_org_members.add(new_maintainer)\n\n    # Mention any maintainers that need to be removed (unlikely here).\n    for old_maintainer in current_maintainers - maintainers:\n        print(\n            \"AN OLD MEMBER ({}) NEEDS TO BE REMOVED FROM {}\".format(\n                old_maintainer, gh_repo\n            )\n        )\n\n    # Add any new maintainer team\n    maintainer_teams = set(\n        m.split(\"/\")[1]\n        for m in maintainer_teams\n        if m.startswith(str(org.login))\n    )\n    current_maintainer_teams = [team.name for team in current_maintainer_teams]\n    for maintainer_team in maintainer_teams - set(current_maintainer_teams):\n        print(\n            \"Adding a new team ({}) to {}. Welcome! :)\".format(\n                maintainer_team, org.login\n            )\n        )\n\n        team = get_cached_team(org, maintainer_team)\n        team.add_to_repos(gh_repo)\n\n    return maintainers, current_maintainers, new_org_members\n", "path": "conda_smithy/github.py"}], "after_files": [{"content": "import os\nfrom random import choice\n\nfrom git import Repo\n\nfrom github import Github\nfrom github.GithubException import GithubException\nfrom github.Organization import Organization\nfrom github.Team import Team\nimport github\n\nimport conda_build.api\nfrom conda_smithy.utils import get_feedstock_name_from_meta\n\n\ndef gh_token():\n    try:\n        with open(\n            os.path.expanduser(\"~/.conda-smithy/github.token\"), \"r\"\n        ) as fh:\n            token = fh.read().strip()\n        if not token:\n            raise ValueError()\n    except (IOError, ValueError):\n        msg = (\n            \"No github token. Go to https://github.com/settings/tokens/new and generate\\n\"\n            \"a token with repo access. Put it in ~/.conda-smithy/github.token\"\n        )\n        raise RuntimeError(msg)\n    return token\n\n\ndef create_team(org, name, description, repo_names=[]):\n    # PyGithub creates secret teams, and has no way of turning that off! :(\n    post_parameters = {\n        \"name\": name,\n        \"description\": description,\n        \"privacy\": \"closed\",\n        \"permission\": \"push\",\n        \"repo_names\": repo_names,\n    }\n    headers, data = org._requester.requestJsonAndCheck(\n        \"POST\", org.url + \"/teams\", input=post_parameters\n    )\n    return Team(org._requester, headers, data, completed=True)\n\n\ndef add_membership(team, member):\n    headers, data = team._requester.requestJsonAndCheck(\n        \"PUT\", team.url + \"/memberships/\" + member\n    )\n    return (headers, data)\n\n\ndef remove_membership(team, member):\n    headers, data = team._requester.requestJsonAndCheck(\n        \"DELETE\", team.url + \"/memberships/\" + member\n    )\n    return (headers, data)\n\n\ndef has_in_members(team, member):\n    status, headers, data = team._requester.requestJson(\n        \"GET\", team.url + \"/members/\" + member\n    )\n    return status == 204\n\n\ndef get_cached_team(org, team_name, description=\"\"):\n    cached_file = os.path.expanduser(\n        \"~/.conda-smithy/{}-{}-team\".format(org.login, team_name)\n    )\n    try:\n        with open(cached_file, \"r\") as fh:\n            team_id = int(fh.read().strip())\n            return org.get_team(team_id)\n    except IOError:\n        pass\n\n    try:\n        repo = org.get_repo(\"{}-feedstock\".format(team_name))\n        team = next(\n            (team for team in repo.get_teams() if team.name == team_name), None\n        )\n        if team:\n            return team\n    except GithubException:\n        pass\n\n    team = next(\n        (team for team in org.get_teams() if team.name == team_name), None\n    )\n    if not team:\n        if description:\n            team = create_team(org, team_name, description, [])\n        else:\n            raise RuntimeError(\"Couldn't find team {}\".format(team_name))\n\n    with open(cached_file, \"w\") as fh:\n        fh.write(str(team.id))\n\n    return team\n\n\ndef create_github_repo(args):\n    token = gh_token()\n    meta = conda_build.api.render(\n        args.feedstock_directory,\n        permit_undefined_jinja=True,\n        finalize=False,\n        bypass_env_check=True,\n        trim_skip=False,\n    )[0][0]\n\n    feedstock_name = get_feedstock_name_from_meta(meta)\n\n    gh = Github(token)\n    user_or_org = None\n    if args.user is not None:\n        pass\n        # User has been defined, and organization has not.\n        user_or_org = gh.get_user()\n    else:\n        # Use the organization provided.\n        user_or_org = gh.get_organization(args.organization)\n\n    repo_name = \"{}-feedstock\".format(feedstock_name)\n    try:\n        gh_repo = user_or_org.create_repo(\n            repo_name,\n            has_wiki=False,\n            private=args.private,\n            description=\"A conda-smithy repository for {}.\".format(\n                feedstock_name\n            ),\n        )\n        print(\"Created {} on github\".format(gh_repo.full_name))\n    except GithubException as gh_except:\n        if (\n            gh_except.data.get(\"errors\", [{}])[0].get(\"message\", \"\")\n            != u\"name already exists on this account\"\n        ):\n            raise\n        gh_repo = user_or_org.get_repo(repo_name)\n        print(\"Github repository already exists.\")\n\n    # Now add this new repo as a remote on the local clone.\n    repo = Repo(args.feedstock_directory)\n    remote_name = args.remote_name.strip()\n    if remote_name:\n        if remote_name in [remote.name for remote in repo.remotes]:\n            existing_remote = repo.remotes[remote_name]\n            if existing_remote.url != gh_repo.ssh_url:\n                print(\n                    \"Remote {} already exists, and doesn't point to {} \"\n                    \"(it points to {}).\".format(\n                        remote_name, gh_repo.ssh_url, existing_remote.url\n                    )\n                )\n        else:\n            repo.create_remote(remote_name, gh_repo.ssh_url)\n\n    if args.extra_admin_users is not None:\n        for user in args.extra_admin_users:\n            gh_repo.add_to_collaborators(user, \"admin\")\n\n    if args.add_teams:\n        if isinstance(user_or_org, Organization):\n            configure_github_team(meta, gh_repo, user_or_org, feedstock_name)\n\n\ndef accept_all_repository_invitations(gh):\n    user = gh.get_user()\n    invitations = github.PaginatedList.PaginatedList(\n        github.Invitation.Invitation,\n        user._requester,\n        user.url + \"/repository_invitations\",\n        None,\n    )\n    for invite in invitations:\n        invite._requester.requestJsonAndCheck(\"PATCH\", invite.url)\n\n\ndef remove_from_project(gh, org, project):\n    user = gh.get_user()\n    repo = gh.get_repo(\"{}/{}\".format(org, project))\n    repo.remove_from_collaborators(user.login)\n\n\ndef configure_github_team(meta, gh_repo, org, feedstock_name, remove=True):\n\n    # Add a team for this repo and add the maintainers to it.\n    superlative = [\n        \"awesome\",\n        \"slick\",\n        \"formidable\",\n        \"awe-inspiring\",\n        \"breathtaking\",\n        \"magnificent\",\n        \"wonderous\",\n        \"stunning\",\n        \"astonishing\",\n        \"superb\",\n        \"splendid\",\n        \"impressive\",\n        \"unbeatable\",\n        \"excellent\",\n        \"top\",\n        \"outstanding\",\n        \"exalted\",\n        \"standout\",\n        \"smashing\",\n    ]\n\n    maintainers = set(meta.meta.get(\"extra\", {}).get(\"recipe-maintainers\", []))\n    maintainers = set(maintainer.lower() for maintainer in maintainers)\n    maintainer_teams = set(m for m in maintainers if \"/\" in m)\n    maintainers = set(m for m in maintainers if \"/\" not in m)\n\n    # Try to get team or create it if it doesn't exist.\n    team_name = feedstock_name\n    current_maintainer_teams = list(gh_repo.get_teams())\n    fs_team = next(\n        (team for team in current_maintainer_teams if team.name == team_name),\n        None,\n    )\n    current_maintainers = set()\n    if not fs_team:\n        fs_team = create_team(\n            org,\n            team_name,\n            \"The {} {} contributors!\".format(choice(superlative), team_name),\n        )\n        fs_team.add_to_repos(gh_repo)\n    else:\n        current_maintainers = set(\n            [e.login.lower() for e in fs_team.get_members()]\n        )\n\n    # Get the all-members team\n    description = \"All of the awesome {} contributors!\".format(org.login)\n    all_members_team = get_cached_team(org, \"all-members\", description)\n    new_org_members = set()\n\n    # Add only the new maintainers to the team.\n    # Also add the new maintainers to all-members if not already included.\n    for new_maintainer in maintainers - current_maintainers:\n        add_membership(fs_team, new_maintainer)\n\n        if not has_in_members(all_members_team, new_maintainer):\n            add_membership(all_members_team, new_maintainer)\n            new_org_members.add(new_maintainer)\n\n    # Remove any maintainers that need to be removed (unlikely here).\n    if remove:\n        for old_maintainer in current_maintainers - maintainers:\n            remove_membership(fs_team, old_maintainer)\n\n    # Add any new maintainer teams\n    maintainer_teams = set(\n        m.split(\"/\")[1]\n        for m in maintainer_teams\n        if m.startswith(str(org.login))\n    )\n    current_maintainer_team_objs = {\n        team.slug: team for team in current_maintainer_teams\n    }\n    current_maintainer_teams = set(\n        [team.slug for team in current_maintainer_teams]\n    )\n    for new_team in maintainer_teams - current_maintainer_teams:\n        team = org.get_team_by_slug(new_team)\n        team.add_to_repos(gh_repo)\n\n    # remove any old teams\n    if remove:\n        for old_team in current_maintainer_teams - maintainer_teams:\n            team = current_maintainer_team_objs.get(\n                old_team, org.get_team_by_slug(old_team)\n            )\n            if team.name == fs_team.name:\n                continue\n            team.remove_from_repos(gh_repo)\n\n    return maintainers, current_maintainers, new_org_members\n", "path": "conda_smithy/github.py"}]}