|
| 1 | +# |
| 2 | +# Copyright 2023 ABSA Group Limited |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | +"""This module contains the Filter classes which are responsible for filtering records based on various criteria.""" |
| 18 | + |
| 19 | +import logging |
| 20 | +from copy import deepcopy |
| 21 | +from typing import Optional |
| 22 | +from release_notes_generator.model.mined_data import MinedData |
| 23 | + |
| 24 | +logger = logging.getLogger(__name__) |
| 25 | + |
| 26 | + |
| 27 | +class Filter: |
| 28 | + """ |
| 29 | + Base class for filtering records. |
| 30 | + """ |
| 31 | + |
| 32 | + def filter(self, data: MinedData) -> MinedData: |
| 33 | + """ |
| 34 | + Filter the mined data based on specific criteria. |
| 35 | +
|
| 36 | + @param data: The mined data to filter. |
| 37 | + @return: The filtered mined data. |
| 38 | + """ |
| 39 | + raise NotImplementedError("Subclasses should implement this method.") |
| 40 | + |
| 41 | + |
| 42 | +class FilterByRelease(Filter): |
| 43 | + """ |
| 44 | + Filter records based on the release version. |
| 45 | + """ |
| 46 | + |
| 47 | + def __init__(self, release_version: Optional[str] = None): |
| 48 | + self.release_version = release_version |
| 49 | + |
| 50 | + def filter(self, data: MinedData) -> MinedData: |
| 51 | + """ |
| 52 | + Filters issues, pull requests, and commits based on the latest release date. |
| 53 | + If the release is not None, it filters out closed issues, merged pull requests, and commits |
| 54 | + that occurred before the release date. |
| 55 | +
|
| 56 | + @Parameters: |
| 57 | + - data (MinedData): The mined data containing issues, pull requests, commits, and release information. |
| 58 | +
|
| 59 | + @Returns: |
| 60 | + - MinedData: The filtered mined data with issues, pull requests, and commits reduced based on the release date. |
| 61 | + """ |
| 62 | + md = MinedData() |
| 63 | + md.repository = data.repository |
| 64 | + md.release = data.release |
| 65 | + md.since = data.since |
| 66 | + |
| 67 | + if data.release is not None: |
| 68 | + logger.info("Starting issue, prs and commit reduction by the latest release since time.") |
| 69 | + |
| 70 | + # filter out closed Issues before the date |
| 71 | + issues_list = list( |
| 72 | + filter(lambda issue: issue.closed_at is not None and issue.closed_at >= data.since, data.issues) |
| 73 | + ) |
| 74 | + logger.debug("Count of issues reduced from %d to %d", len(data.issues), len(issues_list)) |
| 75 | + |
| 76 | + # filter out merged PRs and commits before the date |
| 77 | + pulls_list = list( |
| 78 | + filter(lambda pull: pull.merged_at is not None and pull.merged_at >= data.since, data.pull_requests) |
| 79 | + ) |
| 80 | + logger.debug("Count of pulls reduced from %d to %d", len(data.pull_requests), len(pulls_list)) |
| 81 | + |
| 82 | + commits_list = list(filter(lambda commit: commit.commit.author.date > data.since, data.commits)) |
| 83 | + logger.debug("Count of commits reduced from %d to %d", len(data.commits), len(commits_list)) |
| 84 | + |
| 85 | + md.issues = issues_list |
| 86 | + md.pull_requests = pulls_list |
| 87 | + md.commits = commits_list |
| 88 | + |
| 89 | + logger.debug( |
| 90 | + "Input data. Issues: %d, Pull Requests: %d, Commits: %d", |
| 91 | + len(data.issues), |
| 92 | + len(data.pull_requests), |
| 93 | + len(data.commits), |
| 94 | + ) |
| 95 | + logger.debug( |
| 96 | + "Filtered data. Issues: %d, Pull Requests: %d, Commits: %d", |
| 97 | + len(md.issues), |
| 98 | + len(md.pull_requests), |
| 99 | + len(md.commits), |
| 100 | + ) |
| 101 | + else: |
| 102 | + md.issues = deepcopy(data.issues) |
| 103 | + md.pull_requests = deepcopy(data.pull_requests) |
| 104 | + md.commits = deepcopy(data.commits) |
| 105 | + |
| 106 | + return md |
0 commit comments