Skip to content

Refactor `SyncLicenseScanningRulesService`

We want to identify possible improvements to SyncLicenseScanningRulesService and implement them.


The following discussion from !147561 (merged) should be addressed:

  • @bauerdominic started a discussion: (+1 comment)

    @mcavoj I haven't fully reviewed this yet 🙂 SyncLicenseScanningRulesService has become quite complex and it mixes data structures with processing logic. WDYT about a follow-up to refactor the service? Something along:

    class LicensePolicyData
      attr_reader :scan_result_policy_read, :project
    
      def initialize(scan_result_policy_read, project)
        @scan_result_policy_read = scan_result_policy_read
        @project = project
      end
    
      def license_policies
        # ...
      end
    end
    
    class LicenseReportData
      attr_reader :report, :target_branch_report
    
      def initialize(report, target_branch_report)
        @report = report
        @target_branch_report = target_branch_report
      end
    
      # ...
    end
    
    class LicenseViolationChecker
      def initialize(license_policy_data, license_report_data)
        @license_policy_data = license_policy_data
        @license_report_data = license_report_data
      end
    
      def violates_policy?
        # ...
      end
    end
    
    class SyncLicenseScanningRulesService
      # ...
    
      private
    
      def violates_policy?(merge_request, rule, violations)
        policy_data = LicensePolicyData.new(rule.scan_result_policy_read, project)
        report_data = LicenseReportData.new(report, target_branch_report(merge_request))
        checker = LicenseViolationChecker.new(policy_data, report_data)
        violation = checker.violates_policy?
        save_violation_data(violations, rule, violation.denied_licenses_with_dependencies) if violation
        violation
      end
    
      # ...
    end