Commit 7b8ff999 authored by Vamshi Krishna's avatar Vamshi Krishna

Filter commits by author

Closes #128
parent d9a9b26d
Pipeline #21710311 passed with stages
in 1 minute
......@@ -94,28 +94,37 @@ class GitHubRepository(GitHubMixin, Repository):
"""
return self._repository or self.data['full_name']
@property
def commits(self):
def filter_commits(self, author: Optional[str]=None):
"""
Retrieves the set of commits in this repository.
Filter commits based on properties.
:author: Author username of the commit.
:return: A set of GitHubCommit objects.
"""
# Don't move to module, leads to circular imports
from IGitt.GitHub.GitHubCommit import GitHubCommit
data = {'author': author}
try:
return {GitHubCommit.from_data(commit,
self._token,
self.full_name,
commit['sha'])
for commit in get(self._token, self.url + '/commits')}
for commit in get(self._token, self.url + '/commits', data)}
except RuntimeError as ex:
# Repository is empty. GitHub returns 409.
if ex.args[1] == 409:
return set()
raise ex # dont cover, this is the real exception
@property
def commits(self):
"""
Retrieves the set of commits in this repository.
:return: A set of GitHubCommit objects.
"""
return self.filter_commits()
@property
def clone_url(self):
......
......@@ -7,6 +7,7 @@ from typing import Optional
from typing import Set
from typing import Union
from urllib.parse import quote_plus
from functools import lru_cache
from IGitt import ElementAlreadyExistsError, ElementDoesntExistError
from IGitt.GitLab import GitLabMixin
......@@ -124,22 +125,41 @@ class GitLabRepository(GitLabMixin, Repository):
"""
return self._repository or self.data['path_with_namespace']
@property
def commits(self):
@lru_cache(None)
def filter_commits(self, author: Optional[str]=None):
"""
Retrieves the set of commits in this repository.
Filter commits based on properties.
:author: Author username of the commit.
:return: A set of GitLabCommit objects.
"""
# Don't move to module, leads to circular imports
from IGitt.GitLab.GitLabCommit import GitLabCommit
commits = get(self._token, self.url + '/repository/commits')
if author is not None:
data = {'username': author}
user = get(self._token, self.absolute_url('/users'), data)
if user:
author_name = user[0]['name']
else:
return None
commits = [commit for commit in commits
if commit['author_name'] == author_name]
return {GitLabCommit.from_data(commit,
self._token,
self.full_name,
commit['id'])
for commit in get(self._token,
self.url + '/repository/commits')}
for commit in commits}
@property
def commits(self):
"""
Retrieves the set of commits in this repository.
:return: A set of GitLabCommit objects.
"""
return self.filter_commits()
@property
def clone_url(self) -> str:
......
......@@ -206,6 +206,16 @@ class Repository(IGittObject):
"""
raise NotImplementedError
@property
def filter_commits(self, author: Optional[str]=None):
"""
Filter commits based on properties.
:author: Author of the commit.
:return: Set of Commit objects.
"""
raise NotImplementedError
@property
def commits(self):
"""
......
......@@ -125,4 +125,100 @@ interactions:
X-Runtime-rack: ['0.029745']
X-XSS-Protection: [1; mode=block]
status: {code: 409, message: Conflict}
- request:
body: '{}'
headers:
Accept: ['*/*']
Accept-Encoding: ['gzip, deflate']
Connection: [keep-alive]
Content-Length: ['2']
Content-Type: [application/json]
If-None-Match: [W/"87faaee61a980dd1e396246ac0ae5566"]
User-Agent: [IGitt]
method: GET
uri: https://api.github.com/repos/gitmate-test-user/test/commits?author=sils&per_page=100
response:
body:
string: !!binary |
H4sIAAAAAAAAA+1WTW/bMAz9K4XOSWQ7lr+AYRuw3rbLPi4rikCRaFuALRuSnKIL8t9HOWnaBFjr
tNitN9nkIx+fSEk3W2JrTgqSxCxPQhFkccizWIRSRpxlbMk4TwMuY7aGOBNxQmZEdG2rHCm2hA+u
7oxfad4CRvnKrYWrH6IelGm51ugNLVcNmhpv+mSPpoUGh2bJnQdGQZjMg2ge5T9DVgRpweLfZPeQ
y8F/TdKCtbzyNH71no+8+n79+cu360UrkaEzgKYHnaDMlwELSxkEpQCRJlDKHEqRxFmZMhaE5TLM
yzRD4GB83bVzvS0o5b1aVMrVw3qBAlIDfWcp/mgx4dyBdfPBgqF+5X9Tn9bSyelQq7fl2++qpRc2
Ami3Et2gsR+iGdmAUaUS3KlOe8323yBJUfLGwowY4NabyKCtqjRaZsQvuBsMqqyHppmRnt83HUeQ
/9y9qbJXVFW7tlmdavlk3/6xY/s8rxDPnqW6pE0uLY56AG6YRdEfR7fpKuV3xKrGGxTqztIwYVGA
XhvuuDmnOP60waGdfduKTjsMPHb2QA/wj5sPMQaszCGKj0xemgsfzdIDl+e34uBUdk3T3SHqnObp
wD0GpkcEktmvla4uRCNiSztXA6qDlHe+UGXddBKj9xZH3bqVkh5vUVUDcjKRgz/SuNPIYDueKWOg
YW2FUb2fwumETlAYpTMV1+rPOMvToyDKd9F4vE2uZPRGFGx8d06G7d23tDdqw8W9L92AALVBES8M
dYbDSO6+H28E3FwvqXKw4rL1kzIeZed30/sUdXfvU4Qn19PZe5+il6eo52a8koqb41M0jeM8K2Uc
MlFyKZZMMFhH2TrLWQgZPryWLBVJieq+/slzvDunJ3v+NnrhYTA1ze52d/sXGb57t5ULAAA=
headers:
Access-Control-Allow-Origin: ['*']
Access-Control-Expose-Headers: ['ETag, Link, Retry-After, X-GitHub-OTP, X-RateLimit-Limit,
X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes,
X-Poll-Interval']
Cache-Control: ['private, max-age=60, s-maxage=60']
Content-Encoding: [gzip]
Content-Security-Policy: [default-src 'none']
Content-Type: [application/json; charset=utf-8]
Date: ['Mon, 07 May 2018 10:07:59 GMT']
ETag: [W/"f27b1cbdd505cb5a7f0bf106ae72d63d"]
Last-Modified: ['Mon, 29 Feb 2016 15:07:54 GMT']
Referrer-Policy: ['origin-when-cross-origin, strict-origin-when-cross-origin']
Server: [GitHub.com]
Status: [200 OK]
Strict-Transport-Security: [max-age=31536000; includeSubdomains; preload]
Vary: ['Accept, Authorization, Cookie, X-GitHub-OTP']
X-Accepted-OAuth-Scopes: ['']
X-Content-Type-Options: [nosniff]
X-Frame-Options: [deny]
X-GitHub-Media-Type: [github.v3; format=json]
X-GitHub-Request-Id: ['FC5D:2CD3:2EF0219:5F43664:5AF0257E']
X-OAuth-Scopes: ['admin:gpg_key, admin:org, admin:org_hook, admin:public_key,
admin:repo_hook, gist, notifications, repo, user']
X-RateLimit-Limit: ['5000']
X-RateLimit-Remaining: ['4998']
X-RateLimit-Reset: ['1525691184']
X-Runtime-rack: ['0.087516']
X-XSS-Protection: [1; mode=block]
status: {code: 200, message: OK}
- request:
body: '{}'
headers:
Accept: ['*/*']
Accept-Encoding: ['gzip, deflate']
Connection: [keep-alive]
Content-Length: ['2']
Content-Type: [application/json]
User-Agent: [IGitt]
method: GET
uri: https://api.github.com/repos/gitmate-test-user/empty/commits?author=sils&per_page=100
response:
body: {string: '{"message":"Git Repository is empty.","documentation_url":"https://developer.github.com/v3/repos/commits/#list-commits-on-a-repository"}'}
headers:
Access-Control-Allow-Origin: ['*']
Access-Control-Expose-Headers: ['ETag, Link, Retry-After, X-GitHub-OTP, X-RateLimit-Limit,
X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes,
X-Poll-Interval']
Content-Length: ['136']
Content-Security-Policy: [default-src 'none']
Content-Type: [application/json; charset=utf-8]
Date: ['Mon, 07 May 2018 10:08:01 GMT']
Referrer-Policy: ['origin-when-cross-origin, strict-origin-when-cross-origin']
Server: [GitHub.com]
Status: [409 Conflict]
Strict-Transport-Security: [max-age=31536000; includeSubdomains; preload]
X-Accepted-OAuth-Scopes: ['']
X-Content-Type-Options: [nosniff]
X-Frame-Options: [deny]
X-GitHub-Media-Type: [github.v3; format=json]
X-GitHub-Request-Id: ['FC5E:2CD0:212FBC8:4C9F538:5AF0257F']
X-OAuth-Scopes: ['admin:gpg_key, admin:org, admin:org_hook, admin:public_key,
admin:repo_hook, gist, notifications, repo, user']
X-RateLimit-Limit: ['5000']
X-RateLimit-Remaining: ['4997']
X-RateLimit-Reset: ['1525691184']
X-Runtime-rack: ['0.063124']
X-XSS-Protection: [1; mode=block]
status: {code: 409, message: Conflict}
version: 1
......@@ -189,8 +189,12 @@ class GitHubRepositoryTest(IGittTestCase):
'703c4badc774c9659a3909e203b2da96c97b44fc',
'500050498474c746349ccb0ac8972e77813d2e9b',
'674498fd415cfadc35c5eb28b8951e800f357c6f'})
self.assertEqual({commit.sha
for commit in self.repo.filter_commits('sils')},
{'645961c0841a84c1dd2a58535aa70ad45be48c46'})
repo = GitHubRepository(self.token, 'gitmate-test-user/empty')
self.assertEqual(repo.commits, set())
self.assertEqual(repo.filter_commits('sils'), set())
def test_get_permission_level(self):
sils = GitHubUser(self.token, 'sils')
......
......@@ -192,8 +192,13 @@ class GitLabRepositoryTest(IGittTestCase):
'515280bfe8488e1b403e0dd95c41a404355ca184',
'05a9faff56fd9bdb25d18a554bb2f3320de3fc6f',
'ed5fb0a1cc38a078a6f72b3523b6bce8c14be9b8'})
self.assertEqual({commit.sha
for commit in self.repo.filter_commits('sils')},
{'645961c0841a84c1dd2a58535aa70ad45be48c46'})
repo = GitLabRepository(self.token, 'gitmate-test-user/empty')
self.assertEqual(repo.commits, set())
self.assertEqual(repo.filter_commits('sils'), set())
self.assertEqual(repo.filter_commits('nonenone'), None)
def test_get_permission_level(self):
sils = GitLabUser(self.token, 104269)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment