Repository: repobee/repobee

Issue:
migrate does not work with GitHubAPI
#196 introduced a bug in the migrate command using the GitHubAPI class. `create_repos` expects `repo.team_id` to be not-none. If it is `None`, then the thing crashes. That is to say, if `repo.team_id` is `None` in `GitHubAPI.create_repos`, then it should not be passed on at all to the lower-level the `self._org.create_repo` call.

Discussion:
codecov-io: # [Codecov](https://codecov.io/gh/repobee/repobee/pull/200?src=pr&el=h1) Report
> Merging [#200](https://codecov.io/gh/repobee/repobee/pull/200?src=pr&el=desc) into [master](https://codecov.io/gh/repobee/repobee/commit/2af51ca3028df5676d9a0cc56696666db03a9021?src=pr&el=desc) will **increase** coverage by `0.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/repobee/repobee/pull/200/graphs/tree.svg?width=650&token=5vOKgkphZe&height=150&src=pr)](https://codecov.io/gh/repobee/repobee/pull/200?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #200      +/-   ##
==========================================
+ Coverage   92.87%   92.89%   +0.01%     
==========================================
  Files          18       18              
  Lines        1179     1182       +3     
  Branches      202      203       +1     
==========================================
+ Hits         1095     1098       +3     
  Misses         68       68              
  Partials       16       16
```


| [Impacted Files](https://codecov.io/gh/repobee/repobee/pull/200?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [repobee/github\_api.py](https://codecov.io/gh/repobee/repobee/pull/200/diff?src=pr&el=tree#diff-cmVwb2JlZS9naXRodWJfYXBpLnB5) | `92.37% <100%> (+0.09%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/repobee/repobee/pull/200?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/repobee/repobee/pull/200?src=pr&el=footer). Last update [2af51ca...d8ac7c0](https://codecov.io/gh/repobee/repobee/pull/200?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/repobee/github_api.py b/repobee/github_api.py
index 01e8353..5a698af 100644
--- a/repobee/github_api.py
+++ b/repobee/github_api.py
@@ -308,13 +308,13 @@ class GitHubAPI:
         for info in repo_infos:
             created = False
             with _try_api_request(ignore_statuses=[422]):
+                kwargs = dict(
+                    description=info.description, private=info.private
+                )
+                if info.team_id:  # using falsy results in an exception
+                    kwargs["team_id"] = info.team_id
                 repo_urls.append(
-                    self._org.create_repo(
-                        info.name,
-                        description=info.description,
-                        private=info.private,
-                        team_id=info.team_id,
-                    ).html_url
+                    self._org.create_repo(info.name, **kwargs).html_url
                 )
                 LOGGER.info("created {}/{}".format(self._org_name, info.name))
                 created = True


Test patch:
diff --git a/tests/test_github_api.py b/tests/test_github_api.py
index 5985226..f03a05a 100644
--- a/tests/test_github_api.py
+++ b/tests/test_github_api.py
@@ -438,6 +438,24 @@ class TestCreateRepos:
         for url in actual_urls:
             assert TOKEN in url
 
+    def test_create_repos_without_team_id(self, api):
+        """If there is no team id specified for the repo, then
+        github.Organization.create_repo must be called without the team_id
+        argument (because if it is called with team_id=None, there is a crash).
+        """
+        repo = tuples.Repo(
+            name="repo",
+            description="Some description",
+            private=True,
+            team_id=None,  # this is the important part!
+        )
+
+        api.create_repos([repo])
+
+        api._org.create_repo.assert_called_once_with(
+            repo.name, description=repo.description, private=repo.private
+        )
+
 
 class TestGetRepoUrls:
     """Tests for get_repo_urls."""