Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding default parameters #105

Merged
merged 1 commit into from
Apr 12, 2022
Merged
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
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ by [Rob Pike](https://github.com/robpike):

<img src="https://raw.githubusercontent.com/ossf/criticality_score/main/images/formula.png" width="359" height="96">

We use the following parameters to derive the criticality score for an
We use the following default parameters to derive the criticality score for an
open source project:

| Parameter (S<sub>i</sub>) | Weight (&alpha;<sub>i</sub>) | Max threshold (T<sub>i</sub>) | Description | Reasoning |
Expand All @@ -38,6 +38,7 @@ open source project:

**NOTE**:

- You can override those defaut values at runtime as described below.
- We are looking for community ideas to improve upon these parameters.
- There will always be exceptions to the individual reasoning rules.

Expand Down Expand Up @@ -71,7 +72,15 @@ You can add your own parameters to the criticality score calculation. For
example, you can add internal project usage data to re-adjust the project's
criticality score for your prioritization needs. This can be done by adding
the `--params <param1_value>:<param1_weight>:<param1_max_threshold> ...`
argument on the command line.
argument on the command line. You cannot specify the parameter names and
these won't be listed in the results but they will be included in the
score calculation.

You can override the default values for the weight and threshold of the
built-in parameters to match your needs. This can be done by adding the
`--overrides <param1_name>:<param1_weight>:<param1_max_threshold> ...`
argument on the command line, where param1_name refers to the name of the
parameter you want to override.

### Authentication

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# 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.
"""Constants used in OSS criticality score calculation."""
"""Defaults used in OSS criticality score calculation."""
import re

# Weights for various parameters.
Expand Down
66 changes: 65 additions & 1 deletion criticality_score/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import gitlab
import requests

from .constants import * # pylint: disable=wildcard-import
from .defaults import * # pylint: disable=wildcard-import

logger = logging.getLogger()

Expand Down Expand Up @@ -608,6 +608,62 @@ def initialize_logging_handlers():
logging.getLogger('').addHandler(console)


def override_params(override_params):
calebbrown marked this conversation as resolved.
Show resolved Hide resolved
for override_param in override_params:
temp = override_param.split(':',1)
param_name = temp[0]
try:
weight, threshold = [
float(i) for i in temp[1].split(':')
]
except ValueError:
logger.error('Override parameter in bad format: ' + override_param)
sys.exit(1)
if param_name == 'created_since':
global CREATED_SINCE_WEIGHT, CREATED_SINCE_THRESHOLD
CREATED_SINCE_WEIGHT = weight
CREATED_SINCE_THRESHOLD = threshold
elif param_name == 'updated_since':
global UPDATED_SINCE_WEIGHT, UPDATED_SINCE_THRESHOLD
UPDATED_SINCE_WEIGHT = weight
UPDATED_SINCE_THRESHOLD = threshold
elif param_name == 'contributor_count':
global CONTRIBUTOR_COUNT_WEIGHT, CONTRIBUTOR_COUNT_THRESHOLD
CONTRIBUTOR_COUNT_WEIGHT = weight
CONTRIBUTOR_COUNT_THRESHOLD = threshold
elif param_name == 'org_count':
global ORG_COUNT_WEIGHT, ORG_COUNT_THRESHOLD
ORG_COUNT_WEIGHT = weight
ORG_COUNT_THRESHOLD = threshold
elif param_name == 'commit_frequency':
global COMMIT_FREQUENCY_WEIGHT, COMMIT_FREQUENCY_THRESHOLD
COMMIT_FREQUENCY_WEIGHT = weight
COMMIT_FREQUENCY_THRESHOLD = threshold
elif param_name == 'recent_releases_count':
global RECENT_RELEASES_WEIGHT, RECENT_RELEASES_THRESHOLD
RECENT_RELEASES_WEIGHT = weight
RECENT_RELEASES_THRESHOLD = threshold
elif param_name == 'updated_issues_count':
global UPDATED_ISSUES_WEIGHT, UPDATED_ISSUES_THRESHOLD
UPDATED_ISSUES_WEIGHT = weight
UPDATED_ISSUES_THRESHOLD = threshold
elif param_name == 'closed_issues_count':
global CLOSED_ISSUES_WEIGHT, CLOSED_ISSUES_THRESHOLD
CLOSED_ISSUES_WEIGHT = weight
CLOSED_ISSUES_THRESHOLD = threshold
elif param_name == 'comment_frequency':
global COMMENT_FREQUENCY_WEIGHT, COMMENT_FREQUENCY_THRESHOLD
COMMENT_FREQUENCY_WEIGHT = weight
COMMENT_FREQUENCY_THRESHOLD = threshold
elif param_name == 'dependents_count':
global DEPENDENTS_COUNT_WEIGHT, DEPENDENTS_COUNT_THRESHOLD
DEPENDENTS_COUNT_WEIGHT = weight
DEPENDENTS_COUNT_THRESHOLD = threshold
else:
raise Exception(
'Wrong format argument, unknown parameter: ' + param_name)


def main():
parser = argparse.ArgumentParser(
description='Gives criticality score for an open source project')
Expand All @@ -627,10 +683,18 @@ def main():
default=[],
help='Additional parameters in form <value>:<weight>:<max_threshold>',
required=False)
parser.add_argument(
'--overrides',
calebbrown marked this conversation as resolved.
Show resolved Hide resolved
nargs='+',
default=[],
help='Overriding parameters in form <name>:<weight>:<max_threshold>',
required=False)

initialize_logging_handlers()

args = parser.parse_args()
if args.overrides:
override_params(args.overrides)
repo = get_repository(args.repo)
if not repo:
logger.error(f'Repo is not found: {args.repo}')
Expand Down