Skip to content

Commit

Permalink
added dockerfile and refactoring -> 0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanodallapalma committed Nov 7, 2020
1 parent 73a3734 commit f98cb1e
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 24 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# CHANGELOG

## [0.3.0]
- Renamed score_repository parameter path_to_repo -> clone_to
- Added entrypoint main.py
- Added Dockerfile

## [0.2.0]
- Renamed module radonscorer to reposcorer.

Expand Down
13 changes: 13 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM python:3.8-buster

MAINTAINER Stefano Dalla Palma

RUN python3.8 -m pip install --upgrade pip

COPY . /app

WORKDIR /app

RUN pip install -r requirements.txt

ENTRYPOINT ["python3.8", "main.py"]
30 changes: 26 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,37 @@ report = score_repository(path_to_repo='path/to/cloned/repo',
**Output**
```text
{
'continuous_integration': <bool>,
'percent_comment': <float in [0,1]>,
'has_ci': <bool>,
'comments_ratio': <float in [0,1]>,
'commit_frequency': <float>,
'core_contributors': <int>,
'iac_ratio': <float in [0,1]>,
'issue_frequency': <float>,
'license': <bool>,
'has_license': <bool>,
'repository_size': <int>
}
```

See [CHANGELOG](CHANGELOG.md) for logs detail about releases.

## How to build the Docker image

`docker build -t repo-scorer:latest .`

## How to pull from Dockerhub

`docker pull radonconsortium/repo-scorer:latest`

## How to use Docker image

`docker run repo-scorer:latest <host> <repo full name> <clone to>`

**Example**

`docker run repo-scorer:latest github UoW-CPC/COLARepo /tmp/`

*Output:*

`{"has_ci": false, "percent_comment": 0.0389, "commit_frequency": 4.56, "core_contributors": 3, "iac_ratio": 0.96, "issue_frequency": 0.0, "has_license": false, "repository_size": 2224}`


See [CHANGELOG](CHANGELOG.md) for logs detail about releases.
7 changes: 7 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import json
import sys
from reposcorer.scorer import score_repository

if __name__ == '__main__':
scores = score_repository(sys.argv[1], sys.argv[2], sys.argv[3])
print(json.dumps(scores))
51 changes: 32 additions & 19 deletions reposcorer/scorer.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,60 @@
import git
import os

from typing import Union

from .attributes.community import core_contributors
from .attributes.continuous_integration import has_continuous_integration
from .attributes.history import commit_frequency
from .attributes.iac import iac_ratio
from .attributes.issues import github_issue_event_frequency, gitlab_issue_event_frequency
from .attributes.licensing import has_license
from .attributes.loc_info import loc_info
from reposcorer.attributes.community import core_contributors
from reposcorer.attributes.continuous_integration import has_continuous_integration
from reposcorer.attributes.history import commit_frequency
from reposcorer.attributes.iac import iac_ratio
from reposcorer.attributes.issues import github_issue_event_frequency, gitlab_issue_event_frequency
from reposcorer.attributes.licensing import has_license
from reposcorer.attributes.loc_info import loc_info


def score_repository(path_to_repo: str,
full_name_or_id: Union[str, int],
host: str):
def score_repository(host: str,
full_name: Union[str, int],
clone_to: str,):
"""
Score a repository to identify well-engineered projects.
:param path_to_repo: path to the local repository
:param access_token: Github access token
:param full_name_or_id: the full name of a repository or its id (e.g., radon-h2020/radon-repository-scorer)
:param host: the SVM hosting platform. That is, github or gitlab
:param full_name: the full name of a repository (e.g., radon-h2020/radon-repository-scorer)
:param clone_to: directory to clone the repository
:return: a dictionary with a score for every indicator
"""

issues = 0
if host == 'github':
issues = github_issue_event_frequency(full_name_or_id)
issues = github_issue_event_frequency(full_name)

if not os.path.isdir(full_name):
git.Git(clone_to).clone(f'https://github.com/{full_name}.git')

elif host == 'gitlab':
issues = gitlab_issue_event_frequency(full_name_or_id)
issues = gitlab_issue_event_frequency(full_name)
if not os.path.isdir(full_name):
git.Git(clone_to).clone(f'https://github.com/{full_name}.git')
else:
raise ValueError(f'{host} not supported. Please select github or gitlab')

path_to_repo = os.path.join(clone_to, full_name.split('/')[-1])

history = commit_frequency(path_to_repo)
community = core_contributors(path_to_repo)
ci = has_continuous_integration(path_to_repo)
license = has_license(path_to_repo)
license_ = has_license(path_to_repo)
cloc, sloc = loc_info(path_to_repo)
ratio_comments = cloc / (cloc + sloc) if (cloc + sloc) != 0 else 0
ratio_iac = iac_ratio(path_to_repo)

return {
'continuous_integration': ci,
'percent_comment': round(ratio_comments, 4),
'has_ci': ci,
'comments_ratio': round(ratio_comments, 4),
'commit_frequency': round(history, 2),
'core_contributors': community,
'iac_ratio': round(ratio_iac, 4),
'issue_frequency': round(issues, 2),
'license': license,
'has_license': license_,
'repository_size': sloc
}
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
GitPython~=3.1.11
python-dotenv~=0.14.0
PyDriller~=1.15.2
PyGithub~=1.53
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

setup(
name='repository-scorer',
version="0.2.1",
version="0.3.0",
description='A python package to compute a repository best engineering practices indicators',
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
32 changes: 32 additions & 0 deletions tests/test_scorer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os
import shutil
import unittest

from dotenv import load_dotenv
from reposcorer.scorer import score_repository


class AttributesTestCase(unittest.TestCase):

@classmethod
def setUpClass(cls):
load_dotenv()
cls.tmp_dir = os.path.join('test_data', 'tmp')
os.mkdir(cls.tmp_dir)

@classmethod
def tearDownClass(cls):
shutil.rmtree(cls.tmp_dir)

def test_score_repository(self):
scores = score_repository(host='github', full_name='UoW-CPC/COLARepo', clone_to=self.tmp_dir)

self.assertAlmostEqual(scores['commit_frequency'], 4.5, 0)
self.assertEqual(scores['issue_frequency'], 0)
self.assertEqual(scores['core_contributors'], 3)
self.assertFalse(scores['has_ci'])
self.assertFalse(scores['has_license'])


if __name__ == '__main__':
unittest.main()

0 comments on commit f98cb1e

Please sign in to comment.