Skip to content

Commit

Permalink
Merge fec08bc into 0f45bd1
Browse files Browse the repository at this point in the history
  • Loading branch information
MarekSuchanek committed Jul 6, 2018
2 parents 0f45bd1 + fec08bc commit eb3c9ff
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 35 deletions.
28 changes: 28 additions & 0 deletions migrations/versions/891cf9712a20_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Add topics of Repository
Revision ID: 891cf9712a20
Revises: dc286086230c
Create Date: 2018-07-06 09:52:31.204206
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '891cf9712a20'
down_revision = 'dc286086230c'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('Repository', sa.Column('topics', sa.UnicodeText(), nullable=True))
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('Repository', 'topics')
# ### end Alembic commands ###
2 changes: 1 addition & 1 deletion migrations/versions/dc286086230c_.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""empty message
"""Initial DB
Revision ID: dc286086230c
Revises:
Expand Down
30 changes: 25 additions & 5 deletions repocribro/filters/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,34 @@ def repo_link(repo, show_secret=False):
return jinja2.Markup('<a href="{0}">{0}</a>'.format(url))


def repo_languages(languages):
def repo_languages(repo):
"""Filter for languages to get rid of unrecognized as None
:param languages: representation of repo languages
:type languages: string or None
:param repo: Repository to show its languages
:type repo: ``repocribro.models.Repository``
:return: string representation of languages
:rtype: str
"""
if languages is not str:
if not isinstance(repo.languages, str):
return 'unrecognized'
return languages
return repo.languages


def repo_topics(repo):
"""Filter for topics as list of badges
:param repo: Repository to show its topics
:type repo: ``repocribro.models.Repository``
:return: HTML code with topics as badges
:rtype: ``jinja2.Markup``
"""
if repo.topics is None:
return ''
ds_topics = repo.topics.split(' ')
return jinja2.Markup(' '.join([
'<span class ="badge badge-secondary">{}</span>'.format(topic)
for topic in ds_topics
]))


def gh_user_link(user):
Expand Down Expand Up @@ -114,6 +133,7 @@ def gh_repo_visibility(repo):
'repo_visibility': repo_visibility,
'repo_link': repo_link,
'repo_languages': repo_languages,
'repo_topics': repo_topics,
'gh_user_link': gh_user_link,
'gh_repo_link': gh_repo_link,
'gh_push_url': gh_push_url,
Expand Down
17 changes: 9 additions & 8 deletions repocribro/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def __init__(self, client_id, client_secret, webhooks_secret,
self.token = token
self.scope = []

def _get_auth_header(self):
def _get_headers(self):
"""Prepare auth header fields (empty if no token provided)
:return: Headers for the request
Expand All @@ -154,7 +154,8 @@ def _get_auth_header(self):
if self.token is None:
return {}
return {
'Authorization': 'token {}'.format(self.token)
'Authorization': 'token {}'.format(self.token),
'Accept': 'application/vnd.github.mercy-preview+json'
}

def get_auth_url(self):
Expand Down Expand Up @@ -208,10 +209,10 @@ def get(self, what, page=0):
uri += '?page={}'.format(page)
return GitHubResponse(self.session.get(
uri,
headers=self._get_auth_header()
headers=self._get_headers()
))

def webhook_get(self, full_name, id):
def webhook_get(self, full_name, hook_id):
"""Perform GET request for repo's webhook
:param full_name: Full name of repository that contains the hook
Expand All @@ -221,7 +222,7 @@ def webhook_get(self, full_name, id):
:return: Data of the webhook
:rtype: ``repocribro.github.GitHubResponse``
"""
return self.get('/repos/{}/hooks/{}'.format(full_name, id))
return self.get('/repos/{}/hooks/{}'.format(full_name, hook_id))

def webhooks_get(self, full_name):
"""GET all webhooks of the repository
Expand Down Expand Up @@ -260,7 +261,7 @@ def webhook_create(self, full_name, hook_url, events=None):
response = self.session.post(
self.API_URL + '/repos/{}/hooks'.format(full_name),
data=json.dumps(data),
headers=self._get_auth_header()
headers=self._get_headers()
)
if response.status_code == 201:
return response.json()
Expand All @@ -280,7 +281,7 @@ def webhook_tests(self, full_name, hook_id):
self.API_URL + '/repos/{}/hooks/{}/tests'.format(
full_name, hook_id
),
headers=self._get_auth_header()
headers=self._get_headers()
)
return response.status_code == 204

Expand All @@ -298,7 +299,7 @@ def webhook_delete(self, full_name, hook_id):
self.API_URL + '/repos/{}/hooks/{}'.format(
full_name, hook_id
),
headers=self._get_auth_header()
headers=self._get_headers()
)
return response.status_code == 204

Expand Down
22 changes: 20 additions & 2 deletions repocribro/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,8 @@ class Repository(db.Model, SearchableMixin, SerializableMixin):
url = sqlalchemy.Column(sqlalchemy.UnicodeText)
# Description of the project in repo
description = sqlalchemy.Column(sqlalchemy.UnicodeText)
# Topics (tags) of repo
topics = sqlalchemy.Column(sqlalchemy.UnicodeText)
# GitHub visibility
private = sqlalchemy.Column(sqlalchemy.Boolean)
# Internal visibility within app
Expand Down Expand Up @@ -541,15 +543,16 @@ class Repository(db.Model, SearchableMixin, SerializableMixin):
VISIBILITY_HIDDEN = 2

def __init__(self, github_id, fork_of, full_name, name, languages, url,
description, private, webhook_id, owner, visibility_type,
secret=None):
description, topics, private, webhook_id, owner,
visibility_type, secret=None):
self.github_id = github_id
self.fork_of = fork_of
self.full_name = full_name
self.name = name
self.languages = languages
self.url = url
self.description = description
self.topics = topics
self.private = private
self.webhook_id = webhook_id
self.owner = owner
Expand Down Expand Up @@ -587,6 +590,7 @@ def create_from_dict(repo_dict, owner, webhook_id=None,
repo_dict['language'],
repo_dict['html_url'],
repo_dict['description'],
Repository.serialize_topics(repo_dict.get('topics', None)),
repo_dict['private'],
webhook_id,
owner,
Expand All @@ -605,6 +609,7 @@ def update_from_dict(self, repo_dict):
self.languages = repo_dict['language']
self.url = repo_dict['html_url']
self.description = repo_dict['description']
self.topics = self.serialize_topics(repo_dict.get('topics', None))
self.private = repo_dict['private']

def update_languages(self, languages_dict):
Expand Down Expand Up @@ -634,6 +639,19 @@ def make_full_name(login, reponame):
"""
return '{}/{}'.format(login, reponame)

@staticmethod
def serialize_topics(topics):
"""Make string from topics list from GitHub
:param topics: List of topics (strings without whitespaces)
:type topics: list of str
:return: Serialized list of topics
:rtype: str
"""
if topics is None or len(topics) == 0:
return None
return ' '.join(topics)

@property
def owner_login(self):
"""Get owner login from full name of repository
Expand Down
6 changes: 5 additions & 1 deletion repocribro/templates/admin/repo.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ <h1>{{ repo.full_name }}
</tr>
<tr>
<th>Languages:</th>
<td>{{ repo.languages }}</td>
<td>{{ repo|repo_languages }}</td>
</tr>
<tr>
<th>Description:</th>
<td>{{ repo.description }}</td>
</tr>
<tr>
<th>Topics:</th>
<td>{{ repo|repo_topics }}</td>
</tr>
<tr>
<th>Visibility @GitHub:</th>
<td>{{ repo|gh_repo_visibility }}</td>
Expand Down
6 changes: 5 additions & 1 deletion repocribro/templates/core/repo/details_tab.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ <h2>Details</h2>
</tr>
<tr>
<th>Languages</th>
<td>{{ repo.languages|repo_languages }}</td>
<td>{{ repo|repo_languages }}</td>
</tr>
<tr>
<th>Topics</th>
<td>{{ repo|repo_topics }}</td>
</tr>
{# TODO: fork_of #}
</table>
Expand Down
10 changes: 7 additions & 3 deletions repocribro/templates/manage/repo.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ <h1>{{ repo.full_name }}
<th>Full name:</th>
<td>{{ repo|gh_repo_link }}</td>
</tr>
<tr>
<th>Description:</th>
<td>{{ repo.description }}</td>
</tr>
<tr>
<th>Languages:</th>
<td>{{ repo.languages|repo_languages }}</td>
<td>{{ repo|repo_languages }}</td>
</tr>
<tr>
<th>Description:</th>
<td>{{ repo.description }}</td>
<th>Topics:</th>
<td>{{ repo|repo_topics }}</td>
</tr>
<tr>
<th>Visibility @GitHub:</th>
Expand Down
11 changes: 7 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,14 +194,17 @@ def filled_db_session(empty_db_session):
session.add(org1)

repo1 = Repository(100, None, 'regular/repo1', 'repo1', 'Python', '',
'', False, None, user2, Repository.VISIBILITY_PUBLIC)
'', '', False, None, user2,
Repository.VISIBILITY_PUBLIC)
session.add(repo1)
repo2 = Repository(101, None, 'regular/repo2', 'repo2', 'Python', '',
'', False, None, user2, Repository.VISIBILITY_HIDDEN)
'', 'python testing-it awesome', False, None, user2,
Repository.VISIBILITY_HIDDEN)
repo2.generate_secret()
session.add(repo2)
repo3 = Repository(102, None, 'regular/repo3', 'repo3', 'Haskell', '',
'', False, None, user2, Repository.VISIBILITY_PRIVATE)
'', '', False, None, user2,
Repository.VISIBILITY_PRIVATE)
session.add(repo3)

release = Release(666, 'v1.0', '', '', '', False, False, 'First release',
Expand Down Expand Up @@ -236,7 +239,7 @@ class FakeGitHubAPI:
{
'id': 101, 'full_name': 'regular/repo2', 'name': 'repo2',
'language': 'Python', 'html_url': '', 'description': '',
'private': False
'topics': ['python', 'testing-it'], 'private': False
},
{
'id': 102, 'full_name': 'regular/repo3', 'name': 'repo3',
Expand Down
2 changes: 1 addition & 1 deletion tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_repocheck_invalid(filled_db_session, app_client):
_repocheck('nonexistent')
assert exodus.value.code == 1

repo = Repository(105, None, 'regular/repo4', 'repo4', 'Python', '',
repo = Repository(105, None, 'regular/repo4', 'repo4', 'Python', '', '',
'', False, None, None, Repository.VISIBILITY_PUBLIC)
filled_db_session.add(repo)
filled_db_session.commit()
Expand Down
34 changes: 28 additions & 6 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import datetime
import jinja2
import pytest

from repocribro.filters.common import *
Expand Down Expand Up @@ -45,7 +44,7 @@ def test_flash_class(category, css_class):


def test_repo_visibility():
repo = Repository(777, None, 'some/repo', 'repo', 'C++', '', '',
repo = Repository(777, None, 'some/repo', 'repo', 'C++', '', '', '',
False, None, None, Repository.VISIBILITY_PRIVATE)
assert repo_visibility(repo) == 'Private'
repo.visibility_type = Repository.VISIBILITY_HIDDEN
Expand All @@ -55,7 +54,7 @@ def test_repo_visibility():


def test_repo_link():
repo = Repository(777, None, 'some/project', 'project', 'C++', '', '',
repo = Repository(777, None, 'some/project', 'project', 'C++', '', '', '',
False, None, None, Repository.VISIBILITY_PUBLIC)
result = repo_link(repo, False)
assert 'a href="' in result
Expand All @@ -81,6 +80,29 @@ def test_repo_link():
assert isinstance(result, jinja2.Markup)


def test_repo_languages():
repo = Repository(777, None, 'some/repo', 'repo', 'C++, Python', '', '',
'', False, None, None, Repository.VISIBILITY_PRIVATE)
assert repo_languages(repo) == 'C++, Python'
repo.languages = ''
assert repo_languages(repo) == ''
repo.languages = None
assert repo_languages(repo) == 'unrecognized'


def test_repo_topics():
repo = Repository(777, None, 'some/repo', 'repo', '', '', '',
'github testing-it',
False, None, None, Repository.VISIBILITY_PRIVATE)
assert 'github' in repo_topics(repo)
assert 'testing-it' in repo_topics(repo)
repo.topics = 'cool'
assert 'cool' in repo_topics(repo)
assert 'testing-it' not in repo_topics(repo)
repo.topics = None
assert repo_topics(repo) == ''


def test_gh_user_link():
user = User(111, 'some', '', '', None, '', '', None, None, None, None)
result = gh_user_link(user)
Expand All @@ -95,7 +117,7 @@ def test_gh_user_link():


def test_gh_repo_link():
repo = Repository(777, None, 'some/repo', 'repo', 'C++', '', '',
repo = Repository(777, None, 'some/repo', 'repo', 'C++', '', '', '',
False, None, None, Repository.VISIBILITY_PRIVATE)
result = gh_repo_link(repo)
assert repo.full_name in result
Expand All @@ -104,7 +126,7 @@ def test_gh_repo_link():


def test_gh_push_url():
repo = Repository(777, None, 'some/repo', 'repo', 'C++', '', '',
repo = Repository(777, None, 'some/repo', 'repo', 'C++', '', '', '',
False, None, None, Repository.VISIBILITY_PRIVATE)
push = Push(484, 'refs/heads/changes', 'abc', 'def', 1, 1,
datetime.datetime.now(), 'sender_login', 'sender_id', repo)
Expand All @@ -115,7 +137,7 @@ def test_gh_push_url():


def test_gh_repo_visibility():
repo = Repository(777, None, 'some/repo', 'repo', 'C++', '', '',
repo = Repository(777, None, 'some/repo', 'repo', 'C++', '', '', '',
False, None, None, Repository.VISIBILITY_PRIVATE)
assert gh_repo_visibility(repo) == 'Public'
repo.private = True
Expand Down

0 comments on commit eb3c9ff

Please sign in to comment.