From 253d43536139e592926d7e94af2b698beff440df Mon Sep 17 00:00:00 2001 From: codisart Date: Tue, 30 Mar 2021 23:48:44 +0200 Subject: [PATCH] Improve maintainability of labeler class with labels class extract --- bundler/lib/dependabot/bundler.rb | 6 +- cargo/lib/dependabot/cargo.rb | 6 +- .../pull_request_creator/labeler.rb | 55 +++++-------------- .../labelers/package_manager_labels.rb | 31 +++++++++++ common/spec/dummy_package_manager/dummy.rb | 6 +- composer/lib/dependabot/composer.rb | 6 +- dep/lib/dependabot/dep.rb | 6 +- docker/lib/dependabot/docker.rb | 6 +- elm/lib/dependabot/elm.rb | 6 +- .../lib/dependabot/git_submodules.rb | 6 +- .../lib/dependabot/github_actions.rb | 6 +- go_modules/lib/dependabot/go_modules.rb | 6 +- gradle/lib/dependabot/gradle.rb | 6 +- hex/lib/dependabot/hex.rb | 6 +- maven/lib/dependabot/maven.rb | 6 +- npm_and_yarn/lib/dependabot/npm_and_yarn.rb | 6 +- nuget/lib/dependabot/nuget.rb | 6 +- python/lib/dependabot/python.rb | 6 +- terraform/lib/dependabot/terraform.rb | 6 +- 19 files changed, 97 insertions(+), 91 deletions(-) create mode 100644 common/lib/dependabot/pull_request_creator/labelers/package_manager_labels.rb diff --git a/bundler/lib/dependabot/bundler.rb b/bundler/lib/dependabot/bundler.rb index e1762c22cf8..ac47cc21820 100644 --- a/bundler/lib/dependabot/bundler.rb +++ b/bundler/lib/dependabot/bundler.rb @@ -10,9 +10,9 @@ require "dependabot/bundler/requirement" require "dependabot/bundler/version" -require "dependabot/pull_request_creator/labeler" -Dependabot::PullRequestCreator::Labeler. - register_label_details("bundler", name: "ruby", colour: "ce2d2d") +require "dependabot/pull_request_creator/labelers/package_manager_labels" +Dependabot::PullRequestCreator::Labelers::PackageManagerLabels. + register_label("bundler", name: "ruby", colour: "ce2d2d") require "dependabot/dependency" Dependabot::Dependency.register_production_check( diff --git a/cargo/lib/dependabot/cargo.rb b/cargo/lib/dependabot/cargo.rb index 573aaea172c..85e773283a3 100644 --- a/cargo/lib/dependabot/cargo.rb +++ b/cargo/lib/dependabot/cargo.rb @@ -10,9 +10,9 @@ require "dependabot/cargo/requirement" require "dependabot/cargo/version" -require "dependabot/pull_request_creator/labeler" -Dependabot::PullRequestCreator::Labeler. - register_label_details("cargo", name: "rust", colour: "000000") +require "dependabot/pull_request_creator/labelers/package_manager_labels" +Dependabot::PullRequestCreator::Labelers::PackageManagerLabels. + register_label("cargo", name: "rust", colour: "000000") require "dependabot/dependency" Dependabot::Dependency.register_production_check("cargo", ->(_) { true }) diff --git a/common/lib/dependabot/pull_request_creator/labeler.rb b/common/lib/dependabot/pull_request_creator/labeler.rb index b1ab9b8bc56..d170a76c0a6 100644 --- a/common/lib/dependabot/pull_request_creator/labeler.rb +++ b/common/lib/dependabot/pull_request_creator/labeler.rb @@ -9,22 +9,7 @@ class Labeler DEFAULT_DEPENDENCIES_LABEL = "dependencies" DEFAULT_SECURITY_LABEL = "security" - @package_manager_labels = {} - - class << self - attr_reader :package_manager_labels - - def label_details_for_package_manager(package_manager) - label_details = @package_manager_labels[package_manager] - return label_details if label_details - - raise "Unsupported package_manager #{package_manager}" - end - - def register_label_details(package_manager, label_details) - @package_manager_labels[package_manager] = label_details - end - end + Labels = Dependabot::PullRequestCreator::Labelers::PackageManagerLabels def initialize(source:, custom_labels:, credentials:, dependencies:, includes_security_fixes:, label_language:, @@ -226,10 +211,8 @@ def language_label_exists? end def language_label - label_name = - self.class.label_details_for_package_manager(package_manager). - fetch(:name) - labels.find { |l| l.casecmp(label_name).zero? } + label = Labels.details(package_manager) + labels.find { |l| l.casecmp(label.language).zero? } end def labels @@ -269,15 +252,13 @@ def fetch_gitlab_labels end def fetch_azure_labels - langauge_name = - self.class.label_details_for_package_manager(package_manager). - fetch(:name) + label = Labels.details(package_manager) @labels = [ *@labels, DEFAULT_DEPENDENCIES_LABEL, DEFAULT_SECURITY_LABEL, - langauge_name + label.language ].uniq end @@ -351,36 +332,30 @@ def create_gitlab_security_label end def create_github_language_label - langauge_name = - self.class.label_details_for_package_manager(package_manager). - fetch(:name) + label = Labels.details(package_manager) github_client_for_source.add_label( source.repo, - langauge_name, - self.class.label_details_for_package_manager(package_manager). - fetch(:colour), - description: "Pull requests that update #{langauge_name.capitalize} "\ + label.language, + label.colour, + description: "Pull requests that update #{label.language.capitalize} "\ "code", accept: "application/vnd.github.symmetra-preview+json" ) - @labels = [*@labels, langauge_name].uniq + @labels = [*@labels, label.language].uniq rescue Octokit::UnprocessableEntity => e raise unless e.errors.first.fetch(:code) == "already_exists" - @labels = [*@labels, langauge_name].uniq + @labels = [*@labels, label.language].uniq end def create_gitlab_language_label - langauge_name = - self.class.label_details_for_package_manager(package_manager). - fetch(:name) + label = Labels.details(package_manager) gitlab_client_for_source.create_label( source.repo, - langauge_name, - "#" + self.class.label_details_for_package_manager(package_manager). - fetch(:colour) + label.language, + "#" + label.colour ) - @labels = [*@labels, langauge_name].uniq + @labels = [*@labels, label.language].uniq end def github_client_for_source diff --git a/common/lib/dependabot/pull_request_creator/labelers/package_manager_labels.rb b/common/lib/dependabot/pull_request_creator/labelers/package_manager_labels.rb new file mode 100644 index 00000000000..be50c793bd3 --- /dev/null +++ b/common/lib/dependabot/pull_request_creator/labelers/package_manager_labels.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +module Dependabot + class PullRequestCreator + module Labelers + class PackageManagerLabels + attr_reader :language, :colour + + @labels = {} + + def initialize(language, colour) + @language = language + @colour = colour + end + + class << self + def details(package_manager) + label_details = @labels[package_manager] + return label_details if label_details + + raise "Unsupported package_manager #{package_manager}" + end + + def register_label(package_manager, details) + @labels[package_manager] = new(details[:name], details[:colour]) + end + end + end + end + end +end diff --git a/common/spec/dummy_package_manager/dummy.rb b/common/spec/dummy_package_manager/dummy.rb index d6f6ef40495..b061d99ca2e 100644 --- a/common/spec/dummy_package_manager/dummy.rb +++ b/common/spec/dummy_package_manager/dummy.rb @@ -4,9 +4,9 @@ require_relative "requirement" require_relative "version" -require "dependabot/pull_request_creator/labeler" -Dependabot::PullRequestCreator::Labeler. - register_label_details("dummy", name: "ruby", colour: "ce2d2d") +require "dependabot/pull_request_creator/labelers/package_manager_labels" +Dependabot::PullRequestCreator::Labelers::PackageManagerLabels. + register_label("dummy", name: "ruby", colour: "ce2d2d") require "dependabot/dependency" Dependabot::Dependency.register_production_check( diff --git a/composer/lib/dependabot/composer.rb b/composer/lib/dependabot/composer.rb index 542a012d7df..85268adceb8 100644 --- a/composer/lib/dependabot/composer.rb +++ b/composer/lib/dependabot/composer.rb @@ -10,9 +10,9 @@ require "dependabot/composer/requirement" require "dependabot/composer/version" -require "dependabot/pull_request_creator/labeler" -Dependabot::PullRequestCreator::Labeler. - register_label_details("composer", name: "php", colour: "45229e") +require "dependabot/pull_request_creator/labelers/package_manager_labels" +Dependabot::PullRequestCreator::Labelers::PackageManagerLabels. + register_label("composer", name: "php", colour: "45229e") require "dependabot/dependency" Dependabot::Dependency.register_production_check( diff --git a/dep/lib/dependabot/dep.rb b/dep/lib/dependabot/dep.rb index 9d8a59fd917..8852faa1ce3 100644 --- a/dep/lib/dependabot/dep.rb +++ b/dep/lib/dependabot/dep.rb @@ -10,9 +10,9 @@ require "dependabot/dep/requirement" require "dependabot/dep/version" -require "dependabot/pull_request_creator/labeler" -Dependabot::PullRequestCreator::Labeler. - register_label_details("dep", name: "go", colour: "16e2e2") +require "dependabot/pull_request_creator/labelers/package_manager_labels" +Dependabot::PullRequestCreator::Labelers::PackageManagerLabels. + register_label("dep", name: "go", colour: "16e2e2") require "dependabot/dependency" Dependabot::Dependency.register_production_check("dep", ->(_) { true }) diff --git a/docker/lib/dependabot/docker.rb b/docker/lib/dependabot/docker.rb index 430a0ed51d7..0b90d32385b 100644 --- a/docker/lib/dependabot/docker.rb +++ b/docker/lib/dependabot/docker.rb @@ -10,9 +10,9 @@ require "dependabot/docker/requirement" require "dependabot/docker/version" -require "dependabot/pull_request_creator/labeler" -Dependabot::PullRequestCreator::Labeler. - register_label_details("docker", name: "docker", colour: "21ceff") +require "dependabot/pull_request_creator/labelers/package_manager_labels" +Dependabot::PullRequestCreator::Labelers::PackageManagerLabels. + register_label("docker", name: "docker", colour: "21ceff") require "dependabot/dependency" Dependabot::Dependency.register_production_check("docker", ->(_) { true }) diff --git a/elm/lib/dependabot/elm.rb b/elm/lib/dependabot/elm.rb index 4c744fc0a75..4cf14f3c429 100644 --- a/elm/lib/dependabot/elm.rb +++ b/elm/lib/dependabot/elm.rb @@ -10,9 +10,9 @@ require "dependabot/elm/requirement" require "dependabot/elm/version" -require "dependabot/pull_request_creator/labeler" -Dependabot::PullRequestCreator::Labeler. - register_label_details("elm", name: "elm", colour: "76d3f2") +require "dependabot/pull_request_creator/labelers/package_manager_labels" +Dependabot::PullRequestCreator::Labelers::PackageManagerLabels. + register_label("elm", name: "elm", colour: "76d3f2") require "dependabot/dependency" Dependabot::Dependency.register_production_check("elm", ->(_) { true }) diff --git a/git_submodules/lib/dependabot/git_submodules.rb b/git_submodules/lib/dependabot/git_submodules.rb index f2d24bf37fd..125a4c655d0 100644 --- a/git_submodules/lib/dependabot/git_submodules.rb +++ b/git_submodules/lib/dependabot/git_submodules.rb @@ -10,9 +10,9 @@ require "dependabot/git_submodules/requirement" require "dependabot/git_submodules/version" -require "dependabot/pull_request_creator/labeler" -Dependabot::PullRequestCreator::Labeler. - register_label_details("submodules", name: "submodules", colour: "000000") +require "dependabot/pull_request_creator/labelers/package_manager_labels" +Dependabot::PullRequestCreator::Labelers::PackageManagerLabels. + register_label("submodules", name: "submodules", colour: "000000") require "dependabot/dependency" Dependabot::Dependency. diff --git a/github_actions/lib/dependabot/github_actions.rb b/github_actions/lib/dependabot/github_actions.rb index 310b9879826..a2d829c46af 100644 --- a/github_actions/lib/dependabot/github_actions.rb +++ b/github_actions/lib/dependabot/github_actions.rb @@ -10,9 +10,9 @@ require "dependabot/github_actions/requirement" require "dependabot/github_actions/version" -require "dependabot/pull_request_creator/labeler" -Dependabot::PullRequestCreator::Labeler. - register_label_details( +require "dependabot/pull_request_creator/labelers/package_manager_labels" +Dependabot::PullRequestCreator::Labelers::PackageManagerLabels. + register_label( "github_actions", name: "github_actions", colour: "000000" diff --git a/go_modules/lib/dependabot/go_modules.rb b/go_modules/lib/dependabot/go_modules.rb index 5877fd30051..793bdaf2e60 100644 --- a/go_modules/lib/dependabot/go_modules.rb +++ b/go_modules/lib/dependabot/go_modules.rb @@ -10,9 +10,9 @@ require "dependabot/go_modules/requirement" require "dependabot/go_modules/version" -require "dependabot/pull_request_creator/labeler" -Dependabot::PullRequestCreator::Labeler. - register_label_details("go_modules", name: "go", colour: "16e2e2") +require "dependabot/pull_request_creator/labelers/package_manager_labels" +Dependabot::PullRequestCreator::Labelers::PackageManagerLabels. + register_label("go_modules", name: "go", colour: "16e2e2") require "dependabot/dependency" Dependabot::Dependency. diff --git a/gradle/lib/dependabot/gradle.rb b/gradle/lib/dependabot/gradle.rb index 5bf8128ba84..51f432de862 100644 --- a/gradle/lib/dependabot/gradle.rb +++ b/gradle/lib/dependabot/gradle.rb @@ -10,9 +10,9 @@ require "dependabot/gradle/requirement" require "dependabot/gradle/version" -require "dependabot/pull_request_creator/labeler" -Dependabot::PullRequestCreator::Labeler. - register_label_details("gradle", name: "java", colour: "ffa221") +require "dependabot/pull_request_creator/labelers/package_manager_labels" +Dependabot::PullRequestCreator::Labelers::PackageManagerLabels. + register_label("gradle", name: "java", colour: "ffa221") require "dependabot/dependency" Dependabot::Dependency.register_production_check("gradle", ->(_) { true }) diff --git a/hex/lib/dependabot/hex.rb b/hex/lib/dependabot/hex.rb index 8666da31a56..315f7f6c7b6 100644 --- a/hex/lib/dependabot/hex.rb +++ b/hex/lib/dependabot/hex.rb @@ -10,9 +10,9 @@ require "dependabot/hex/requirement" require "dependabot/hex/version" -require "dependabot/pull_request_creator/labeler" -Dependabot::PullRequestCreator::Labeler. - register_label_details("hex", name: "elixir", colour: "9380dd") +require "dependabot/pull_request_creator/labelers/package_manager_labels" +Dependabot::PullRequestCreator::Labelers::PackageManagerLabels. + register_label("hex", name: "elixir", colour: "9380dd") require "dependabot/dependency" Dependabot::Dependency.register_production_check( diff --git a/maven/lib/dependabot/maven.rb b/maven/lib/dependabot/maven.rb index cf9f2a23901..2b00751d840 100644 --- a/maven/lib/dependabot/maven.rb +++ b/maven/lib/dependabot/maven.rb @@ -10,9 +10,9 @@ require "dependabot/maven/requirement" require "dependabot/maven/version" -require "dependabot/pull_request_creator/labeler" -Dependabot::PullRequestCreator::Labeler. - register_label_details("maven", name: "java", colour: "ffa221") +require "dependabot/pull_request_creator/labelers/package_manager_labels" +Dependabot::PullRequestCreator::Labelers::PackageManagerLabels. + register_label("maven", name: "java", colour: "ffa221") require "dependabot/dependency" Dependabot::Dependency. diff --git a/npm_and_yarn/lib/dependabot/npm_and_yarn.rb b/npm_and_yarn/lib/dependabot/npm_and_yarn.rb index 4969c397c0f..b74d83dc5ed 100644 --- a/npm_and_yarn/lib/dependabot/npm_and_yarn.rb +++ b/npm_and_yarn/lib/dependabot/npm_and_yarn.rb @@ -10,9 +10,9 @@ require "dependabot/npm_and_yarn/requirement" require "dependabot/npm_and_yarn/version" -require "dependabot/pull_request_creator/labeler" -Dependabot::PullRequestCreator::Labeler. - register_label_details("npm_and_yarn", name: "javascript", colour: "168700") +require "dependabot/pull_request_creator/labelers/package_manager_labels" +Dependabot::PullRequestCreator::Labelers::PackageManagerLabels. + register_label("npm_and_yarn", name: "javascript", colour: "168700") require "dependabot/dependency" Dependabot::Dependency.register_production_check( diff --git a/nuget/lib/dependabot/nuget.rb b/nuget/lib/dependabot/nuget.rb index bf799895dba..87357de5530 100644 --- a/nuget/lib/dependabot/nuget.rb +++ b/nuget/lib/dependabot/nuget.rb @@ -10,9 +10,9 @@ require "dependabot/nuget/requirement" require "dependabot/nuget/version" -require "dependabot/pull_request_creator/labeler" -Dependabot::PullRequestCreator::Labeler. - register_label_details("nuget", name: ".NET", colour: "7121c6") +require "dependabot/pull_request_creator/labelers/package_manager_labels" +Dependabot::PullRequestCreator::Labelers::PackageManagerLabels. + register_label("nuget", name: ".NET", colour: "7121c6") require "dependabot/dependency" Dependabot::Dependency.register_production_check("nuget", ->(_) { true }) diff --git a/python/lib/dependabot/python.rb b/python/lib/dependabot/python.rb index 0998076ff0f..71ab5d6e39a 100644 --- a/python/lib/dependabot/python.rb +++ b/python/lib/dependabot/python.rb @@ -11,9 +11,9 @@ require "dependabot/python/version" require "dependabot/python/name_normaliser" -require "dependabot/pull_request_creator/labeler" -Dependabot::PullRequestCreator::Labeler. - register_label_details("pip", name: "python", colour: "2b67c6") +require "dependabot/pull_request_creator/labelers/package_manager_labels" +Dependabot::PullRequestCreator::Labelers::PackageManagerLabels. + register_label("pip", name: "python", colour: "2b67c6") require "dependabot/dependency" Dependabot::Dependency.register_production_check( diff --git a/terraform/lib/dependabot/terraform.rb b/terraform/lib/dependabot/terraform.rb index fd013b82502..8b9fbffc4b2 100644 --- a/terraform/lib/dependabot/terraform.rb +++ b/terraform/lib/dependabot/terraform.rb @@ -10,9 +10,9 @@ require "dependabot/terraform/requirement" require "dependabot/terraform/version" -require "dependabot/pull_request_creator/labeler" -Dependabot::PullRequestCreator::Labeler. - register_label_details("terraform", name: "terraform", colour: "5C4EE5") +require "dependabot/pull_request_creator/labelers/package_manager_labels" +Dependabot::PullRequestCreator::Labelers::PackageManagerLabels. + register_label("terraform", name: "terraform", colour: "5C4EE5") require "dependabot/dependency" Dependabot::Dependency.