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

Improve maintainability of labeler class with labels class extract #3420

Closed
Closed
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
6 changes: 3 additions & 3 deletions bundler/lib/dependabot/bundler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions cargo/lib/dependabot/cargo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
55 changes: 15 additions & 40 deletions common/lib/dependabot/pull_request_creator/labeler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions common/spec/dummy_package_manager/dummy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions composer/lib/dependabot/composer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions dep/lib/dependabot/dep.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
6 changes: 3 additions & 3 deletions docker/lib/dependabot/docker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
6 changes: 3 additions & 3 deletions elm/lib/dependabot/elm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
6 changes: 3 additions & 3 deletions git_submodules/lib/dependabot/git_submodules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions github_actions/lib/dependabot/github_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions go_modules/lib/dependabot/go_modules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions gradle/lib/dependabot/gradle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
6 changes: 3 additions & 3 deletions hex/lib/dependabot/hex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions maven/lib/dependabot/maven.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions npm_and_yarn/lib/dependabot/npm_and_yarn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions nuget/lib/dependabot/nuget.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
6 changes: 3 additions & 3 deletions python/lib/dependabot/python.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions terraform/lib/dependabot/terraform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down