Skip to content
Closed
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
30 changes: 30 additions & 0 deletions tests/unit/packaging/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,36 @@ def test_github_repo_info_url(self, db_session, home_page, expected):
release = DBReleaseFactory.create(home_page=home_page)
assert release.github_repo_info_url == expected

@pytest.mark.parametrize(
("home_page", "expected"),
[
(None, None),
(
"https://gitlab.com/user/project",
"https://gitlab.com/api/v4/projects/user%2Fproject",
),
(
"https://gitlab.com/user/project/",
"https://gitlab.com/api/v4/projects/user%2Fproject",
),
(
"https://gitlab.com/user/project/tree/master",
"https://gitlab.com/api/v4/projects/user%2Fproject",
),
(
"https://www.gitlab.com/user/project",
"https://gitlab.com/api/v4/projects/user%2Fproject",
),
("https://gitlab.com/user/", None),
("https://google.com/user/project/tree/master", None),
("https://google.com", None),
("incorrect url", None),
],
)
def test_gitlab_repo_info_url(self, db_session, home_page, expected):
release = DBReleaseFactory.create(home_page=home_page)
assert release.gitlab_repo_info_url == expected


class TestFile:
def test_requires_python(self, db_session):
Expand Down
1 change: 1 addition & 0 deletions tests/unit/test_csp.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ def test_includeme():
"connect-src": [
"'self'",
"https://api.github.com/repos/",
"https://gitlab.com/api/v4/projects/",
"*.fastly-insights.com",
"sentry.io",
"https://2p66nmmycsj3.statuspage.io",
Expand Down
1 change: 1 addition & 0 deletions warehouse/csp.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def includeme(config):
"connect-src": [
SELF,
"https://api.github.com/repos/",
"https://gitlab.com/api/v4/projects/",
"*.fastly-insights.com",
"sentry.io",
]
Expand Down
11 changes: 11 additions & 0 deletions warehouse/packaging/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,17 @@ def github_repo_info_url(self):
user_name, repo_name = segments[:2]
return f"https://api.github.com/repos/{user_name}/{repo_name}"

@property
def gitlab_repo_info_url(self):
for parsed in [urlparse(url) for url in self.urls.values()]:
segments = parsed.path.strip("/").rstrip("/").split("/")

if (
parsed.netloc == "gitlab.com" or parsed.netloc == "www.gitlab.com"
) and len(segments) >= 2:
user_name, repo_name = segments[:2]
return f"https://gitlab.com/api/v4/projects/{user_name}%2F{repo_name}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this property has a lot of common code with the above. It's not a blocker for the PR, but it'd be nice if we could refactor the common logic into a helper function.


@property
def has_meta(self):
return any(
Expand Down
10 changes: 5 additions & 5 deletions warehouse/static/js/warehouse/utils/repository-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@
*/

export default () => {
const repoInfoContainer = document.querySelector(".github-repo-info");
if (repoInfoContainer !== null){
const repoInfoContainer = document.querySelector(".repo-info");
if (repoInfoContainer !== null) {
const url = repoInfoContainer.dataset.url;
fetch(url, {
method: "GET",
mode: "cors",
}).then((response) => {
if (response.ok){
if (response.ok) {
return response.json();
} else {
return null;
}
}).then((json) => {
if (json === null){
if (json === null) {
return;
}
repoInfoContainer.classList.remove("hidden");
const items = document.querySelectorAll(".github-repo-info__item");
const items = document.querySelectorAll(".js-repo-data");
items.forEach(function(elem) {
const jsonKey = elem.dataset.key;
let jsonValue = json[jsonKey];
Expand Down
45 changes: 45 additions & 0 deletions warehouse/static/sass/blocks/_repo-info.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*!
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/*
Styling for repository statistics.

<div class="repo-info">
<em>Service statistics:</em> // GitHub or GitLab
<span class="repo-info__item">
<i class="fa fa-star" aria-hidden="true"></i> // fa icon
<strong>Star: </strong> // item title
<span>1</span> // item value
</span>
</div>

*/
.repo-info {
&__item {
@include clearfix;
display: block;
padding: $spacing-unit / 4 $spacing-unit / 2;

&:hover,
&:focus {
text-decoration: none;
}

i {
width: 20px;
text-align: center;
margin-right: 5px;
}
}
}
1 change: 1 addition & 0 deletions warehouse/static/sass/warehouse.scss
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
@import "blocks/project-description";
@import "blocks/release";
@import "blocks/release-timeline";
@import "blocks/repo-info";
@import "blocks/search-form";
@import "blocks/sidebar-section";
@import "blocks/site-header";
Expand Down
76 changes: 50 additions & 26 deletions warehouse/templates/includes/packaging/project-data.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,60 @@ <h3 class="sidebar-section__title">Project links</h3>
</div>
{% endif %}

{% macro repo_item(title, json_data_key, json_base_url_key=None, url_ending=None, icon=None) %}
{% if not icon %}
{% if title == 'Stars' %}
{% set icon = 'fa-star' %}
{% elif title == 'Forks' %}
{% set icon = 'fa-code-branch' %}
{% elif title == 'Open issues / PRs' %}
{% set icon = 'fa-exclamation-circle' %}
{% endif %}
{% endif %}
{% if json_base_url_key %}
<a class="repo-info__item js-repo-data"
data-key="{{ json_base_url_key }}"
data-attr="href"
data-supplement="{{ url_ending }}"
rel="noopener"
target="_blank">
<i class="fa {{ icon }}" aria-hidden="true"></i>
<strong>{{ title }}: </strong>
<span class="js-repo-data" data-key="{{ json_data_key }}"></span>
</a>
{% else %}
<span class="repo-info__item">
<i class="fa {{ icon }}" aria-hidden="true"></i>
<strong>{{ title }}: </strong>
<span class="js-repo-data" data-key="{{ json_data_key }}"></span>
</span>
{% endif %}
{% endmacro %}

<div class="sidebar-section">
<h3 class="sidebar-section__title">Statistics</h3>
{% if release.github_repo_info_url %}
<div class="github-repo-info hidden" data-url="{{release.github_repo_info_url}}">
GitHub statistics:
<a class="vertical-tabs__tab vertical-tabs__tab--with-icon vertical-tabs__tab--condensed github-repo-info__item"
data-key="html_url" data-attr="href" data-supplement="/stargazers" rel="noopener"
target="_blank">
<i class="fa fa-star" aria-hidden="true"></i>
<strong>Stars: </strong>
<span class="github-repo-info__item" data-key="stargazers_count"></span>
</a>
<a class="vertical-tabs__tab vertical-tabs__tab--with-icon vertical-tabs__tab--condensed github-repo-info__item"
data-key="html_url" data-attr="href" data-supplement="/network" rel="noopener"
target="_blank">
<i class="fa fa-code-branch" aria-hidden="true"></i>
<strong>Forks: </strong>
<span class="github-repo-info__item" data-key="forks_count"></span>
</a>
<a class="vertical-tabs__tab vertical-tabs__tab--with-icon vertical-tabs__tab--condensed github-repo-info__item"
data-key="html_url" data-attr="href" data-supplement="/issues" rel="noopener"
target="_blank">
<i class="fa fa-exclamation-circle" aria-hidden="true"></i>
<strong>Open issues/PRs: </strong>
<span class="github-repo-info__item" data-key="open_issues_count"></span>
</a>
<h3 class="sidebar-section__title">
<i class="fab fa-github" aria-hidden="true"></i>
GitHub statistics
</h3>
<div class="repo-info hidden" data-url="{{release.github_repo_info_url}}">
{{ repo_item('Stars', 'stargazers_count', 'html_url', '/stargazers') }}
{{ repo_item('Forks', 'forks_count', 'html_url', '/network') }}
{{ repo_item('Open issues / PRs', 'open_issues_count', 'html_url', '/issues') }}
</div>
{% elif release.gitlab_repo_info_url %}
<h3 class="sidebar-section__title">
<i class="fab fa-gitlab" aria-hidden="true"></i>
GitLab statistics
</h3>
<div class="repo-info hidden" data-url="{{release.gitlab_repo_info_url}}">
{{ repo_item('Stars', 'star_count') }}
{{ repo_item('Forks', 'forks_count', 'web_url', '/forks') }}
</div>
{% endif %}
<p>View statistics for this project via <a
href="https://libraries.io/pypi/{{ release.project.name }}">Libraries.io</a>, or by using
<br>
<p>View other statistics for this project via <a
href="https://libraries.io/pypi/{{ release.project.name }}">Libraries.io</a>, or by using
<a href="https://packaging.python.org/guides/analyzing-pypi-package-downloads/">Google
BigQuery</a></p>
</div>
Expand Down