Repository: pypi/warehouse

Issue:
Automagic GitHub repo stats display doesn't find www.github.com/user/repo URLs
As noted [here](https://github.com/pypa/warehouse/pull/2163#pullrequestreview-111691465), the automagic sidebar stats feature when a GitHub repo is the project homepage doesn't activate if the URL is written using `www.github.com`; it only finds `github.com` URLs.


Discussion:
We probably should make sure that this works for URLs like `github.com` and `www.github.com`, but not something like `thisisnotgithub.com`.

---

**Good First Issue**: This issue is good for first time contributors. If you've already contributed to Warehouse, please work on [another issue without this label](https://github.com/pypa/warehouse/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+-label%3A%22good+first+issue%22) instead. If there is not a corresponding pull request for this issue, it is up for grabs. For directions for getting set up, see our [Getting Started Guide](https://warehouse.pypa.io/development/getting-started/). If you are working on this issue and have questions, please feel free to ask them here, [`#pypa-dev` on Freenode](https://webchat.freenode.net/?channels=%23pypa-dev), or the [pypa-dev mailing list](https://groups.google.com/forum/#!forum/pypa-dev).
Mm, <nod>... `.endswith('.github.com')` might suffice?
> Mm, ... `.endswith('.github.com')` might suffice?

I don't think this would work for a URL like `https://github.com/pypa/warehouse`
Indeed. Breaky-breaky. Might not be able to avoid a compound condition?

    if (parsed.netloc == 'github.com' or parsed.netloc.endswith('.github.com')) and len(segments) >= 2:


Are there any other common `*.github.com` URLs that would ***not*** be composed as `*.github.com/user/repo/...`, and that someone might want to use as a homepage URL?

If so, a pattern matching condition might get pretty messy.  A narrower compound condition might be better.

    if (parsed.netloc == 'github.com' or parsed.netloc == 'www.github.com') and ...

@bskinn Hope you don't mind -- since this is an improvement to a new feature, I'm marking it as something we can do after we shut down the legacy PyPI site.
@brainwane Nope! I figured it would get slotted for further down the pipeline, since it's a minor glitch.
To be clear: if you made a PR to fix this I would merge it, it's just not going to get fixed by one of the maintainers soon.
@di I can put together a PR to fix it, no problem; but I'm not set up with docker &c. to run the test suite locally.  Can I just rely on Travis to test it for me?

~Also, if I'd need to add a test for the new behavior, I'd need guidance as to where in the test suite to add it.~ Nevermind, [found the current test](https://github.com/pypa/warehouse/blob/a4ca4b580d74c68afe082ca6cff05c9e92ca1865/tests/unit/packaging/test_models.py#L292-L330).
@bskinn Travis will definitely test it for you. You probably don't need to be able to run the tests locally to fix this issue, but it's not too complicated and we have a pretty good guide: https://warehouse.readthedocs.io/development/getting-started/

Resolving patch:
diff --git a/warehouse/packaging/models.py b/warehouse/packaging/models.py
--- a/warehouse/packaging/models.py
+++ b/warehouse/packaging/models.py
@@ -437,7 +437,8 @@ def urls(self):
     def github_repo_info_url(self):
         for parsed in [urlparse(url) for url in self.urls.values()]:
             segments = parsed.path.strip('/').rstrip('/').split('/')
-            if parsed.netloc == 'github.com' and len(segments) >= 2:
+            if (parsed.netloc == 'github.com' or
+                    parsed.netloc == 'www.github.com') and len(segments) >= 2:
                 user_name, repo_name = segments[:2]
                 return f"https://api.github.com/repos/{user_name}/{repo_name}"
 


Test patch:
diff --git a/tests/unit/packaging/test_models.py b/tests/unit/packaging/test_models.py
--- a/tests/unit/packaging/test_models.py
+++ b/tests/unit/packaging/test_models.py
@@ -305,6 +305,10 @@ def test_acl(self, db_session):
                 "https://github.com/pypa/warehouse/tree/master",
                 "https://api.github.com/repos/pypa/warehouse"
             ),
+            (
+                "https://www.github.com/pypa/warehouse",
+                "https://api.github.com/repos/pypa/warehouse"
+            ),
             (
                 "https://github.com/pypa/",
                 None