Skip to content

Commit 69382a0

Browse files
authored
Add GH service support for repo integrity content (#20)
1 parent 968ad2f commit 69382a0

File tree

5 files changed

+65
-12
lines changed

5 files changed

+65
-12
lines changed

CHANGES.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 1.1.0
2+
3+
- [NEW] Added repository details retrieval to Github service class.
4+
- [NEW] Added recent commit details retrieval to Github service class.
5+
- [NEW] Added repository branch protection details retrieval to Github service class.
6+
17
# 1.0.2
28

39
- [FIXED] Added PyYAML library as a dependency to resolve Github service issue.

compliance/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
# limitations under the License.
1515
"""Compliance automation package."""
1616

17-
__version__ = '1.0.2'
17+
__version__ = '1.1.0'

compliance/notify.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
from compliance.config import get_config
2727
from compliance.utils.services import pagerduty
28-
from compliance.utils.services.github import GitHub
28+
from compliance.utils.services.github import Github
2929
from compliance.utils.test import parse_test_id
3030

3131
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
@@ -413,7 +413,7 @@ def __init__(self, results, controls):
413413
# Using the locker repo url to define the base url. The expectation
414414
# is that the Github issues repository will share the base url.
415415
parsed_locker_url = urlparse(get_config().get('locker.repo_url'))
416-
self._github = GitHub(
416+
self._github = Github(
417417
get_config().creds,
418418
f'{parsed_locker_url.scheme}://{parsed_locker_url.hostname}'
419419
)

compliance/utils/services/github.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import yaml
3030

3131

32-
class GitHub(object):
32+
class Github(object):
3333
"""Github service helper class."""
3434

3535
def __init__(self, config=None, base_url='https://github.com'):
@@ -484,6 +484,53 @@ def remove_labels(self, repo, issue, *labels):
484484
# Each response has all the labels, so only return the last one
485485
return response
486486

487+
def get_repo_details(self, repo):
488+
"""
489+
Retrieve a repository's metadata.
490+
491+
:param repo: the organization/repository as a string.
492+
493+
:returns: the repository's metadata details.
494+
"""
495+
self.session.headers.update(
496+
{'Accept': 'application/vnd.github.v3+json'}
497+
)
498+
return self._make_request('get', f'repos/{repo}')
499+
500+
def get_commit_details(self, repo, since):
501+
"""
502+
Retrieve a repository's commit details since a given date/time.
503+
504+
:param repo: the organization/repository as a string.
505+
:param since: the starting date/time as a datetime.
506+
507+
:returns: the repository's commit details since a given date/time.
508+
"""
509+
self.session.headers.update(
510+
{'Accept': 'application/vnd.github.v3+json'}
511+
)
512+
return self._make_request(
513+
'get',
514+
f'repos/{repo}/commits',
515+
params={'since': since.strftime('%Y-%m-%dT%H:%M:%SZ')}
516+
)
517+
518+
def get_branch_protection_details(self, repo, branch='master'):
519+
"""
520+
Retrieve a repository branch's branch protection details.
521+
522+
:param repo: the organization/repository as a string.
523+
:param branch: the branch as a string.
524+
525+
:returns: the repository branch's branch protection details.
526+
"""
527+
self.session.headers.update(
528+
{'Accept': 'application/vnd.github.luke-cage-preview+json'}
529+
)
530+
return self._make_request(
531+
'get', f'repos/{repo}/branches/{branch}/protection'
532+
)
533+
487534
def _make_request(self, method, url, parse=True, **kwargs):
488535
r = self.session.request(method, url, **kwargs)
489536
r.raise_for_status()

test/t_compliance/t_notify/test_gh_notifier.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,21 @@ def setUp(self):
3535
for status in ['pass', 'fail', 'error', 'warn']:
3636
name, result = self._generate_result(status)
3737
self.results[name] = result
38-
self.si_patcher = patch('compliance.notify.GitHub.search_issues')
38+
self.si_patcher = patch('compliance.notify.Github.search_issues')
3939
self.search_issues_mock = self.si_patcher.start()
40-
self.ai_patcher = patch('compliance.notify.GitHub.add_issue')
40+
self.ai_patcher = patch('compliance.notify.Github.add_issue')
4141
self.add_issue_mock = self.ai_patcher.start()
42-
self.pi_patcher = patch('compliance.notify.GitHub.patch_issue')
42+
self.pi_patcher = patch('compliance.notify.Github.patch_issue')
4343
self.patch_issue_mock = self.pi_patcher.start()
44-
self.aic_patcher = patch('compliance.notify.GitHub.add_issue_comment')
44+
self.aic_patcher = patch('compliance.notify.Github.add_issue_comment')
4545
self.add_issue_comment_mock = self.aic_patcher.start()
46-
self.gap_patcher = patch('compliance.notify.GitHub.get_all_projects')
46+
self.gap_patcher = patch('compliance.notify.Github.get_all_projects')
4747
self.get_all_projects_mock = self.gap_patcher.start()
48-
self.gc_patcher = patch('compliance.notify.GitHub.get_columns')
48+
self.gc_patcher = patch('compliance.notify.Github.get_columns')
4949
self.get_columns_mock = self.gc_patcher.start()
50-
self.gac_patcher = patch('compliance.notify.GitHub.get_all_cards')
50+
self.gac_patcher = patch('compliance.notify.Github.get_all_cards')
5151
self.get_all_cards_mock = self.gac_patcher.start()
52-
self.ac_patcher = patch('compliance.notify.GitHub.add_card')
52+
self.ac_patcher = patch('compliance.notify.Github.add_card')
5353
self.add_card_mock = self.ac_patcher.start()
5454

5555
def tearDown(self):

0 commit comments

Comments
 (0)