From abd576197469e3c9efda4897f86c13fffe3f9953 Mon Sep 17 00:00:00 2001 From: Flipez Date: Sun, 26 Apr 2020 22:01:39 +0200 Subject: [PATCH 1/4] handle labels Create methods to find missing Labels on our Repositories Add a worker which is triggered with every IndexReposWorker run to ensure we have all the Labels from our config in our Repositories on GitHub Signed-off-by: Flipez --- app/models/repository.rb | 61 +++++++++++++++++++++++++++++++ app/workers/index_repos_worker.rb | 1 + app/workers/sync_label_worker.rb | 10 +++++ 3 files changed, 72 insertions(+) create mode 100644 app/workers/sync_label_worker.rb diff --git a/app/models/repository.rb b/app/models/repository.rb index 8d2c4183..2a82e3c9 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -69,6 +69,67 @@ def ensure_label_missing(label) true end + ## + # Fetch the Labels for this Repository from GitHub + # and create it in our database + # + def labels + @labels ||= begin + Github.client.labels("voxpupuli/#{name}").map do |label| + Label.find_or_create_by!(name: label[:name], color: label[:color]) + end + end + end + + ## + # Find all Labels which are in our config but are missing from the + # Repository on GitHub + # + def missing_labels + required_label_names = VOXPUPULI_CONFIG['labels'].map do |label| + label['name'] + end + + label_names = labels.pluck(:name) + + missing_label_names = required_label_names.reject do |name| + label_names.include?(name) + end + + missing_label_names.map do |name| + Label.find_by(name: name) + end + end + + ## + # Create each missing Label for the repository on GitHub + # + def attach_missing_labels + missing_labels.each do |label| + ensure_label_exists(label) + end + end + + ## + # Compare the Labels on GitHub with the ones from our config + # If we have the Label in our config but the color differs + # we update the Label on GitHub to match the config. + # + def sync_label_colors + config_labels = VOXPUPULI_CONFIG['labels'].map do |label| + Label.new(name: label['name'], color: label['color']) + end + + labels.each do |label| + config_label = config_labels.select{|c_label| c_label.name == label.name}.first + + next unless config_label + next if label.color == config_label.color + + Github.client.update_label(github_id, config_label.name, { color: config_label.color }) + end + end + ## # Fetch all open and closed PullRequests and sync our database with them # diff --git a/app/workers/index_repos_worker.rb b/app/workers/index_repos_worker.rb index 84cb64a9..8a06aefd 100644 --- a/app/workers/index_repos_worker.rb +++ b/app/workers/index_repos_worker.rb @@ -23,5 +23,6 @@ def perform end RepoStatusWorker.perform_async + SyncLabelWorker.perform_async end end diff --git a/app/workers/sync_label_worker.rb b/app/workers/sync_label_worker.rb new file mode 100644 index 00000000..c7f52f1e --- /dev/null +++ b/app/workers/sync_label_worker.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +class SyncLabelWorker + include Sidekiq::Worker + + def perform + Repository.each do |repository| + repository.attach_missing_labels + end +end From 8cb9863a25420a8a085c72be662e3e72440b1316 Mon Sep 17 00:00:00 2001 From: Flipez Date: Sun, 26 Apr 2020 22:03:58 +0200 Subject: [PATCH 2/4] sync label colors on every run Signed-off-by: Flipez --- app/workers/sync_label_worker.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/workers/sync_label_worker.rb b/app/workers/sync_label_worker.rb index c7f52f1e..c3fdfd54 100644 --- a/app/workers/sync_label_worker.rb +++ b/app/workers/sync_label_worker.rb @@ -6,5 +6,6 @@ class SyncLabelWorker def perform Repository.each do |repository| repository.attach_missing_labels + repository.sync_label_colors end end From c91bddfa327b02b7e80b2d0b0723466a0f5eee75 Mon Sep 17 00:00:00 2001 From: Flipez Date: Sun, 26 Apr 2020 22:22:03 +0200 Subject: [PATCH 3/4] add handling for descriptions aswell Signed-off-by: Flipez --- app/models/repository.rb | 17 ++++++++++------- app/workers/sync_label_worker.rb | 2 +- .../20200426200729_add_description_to_labels.rb | 5 +++++ db/schema.rb | 3 ++- 4 files changed, 18 insertions(+), 9 deletions(-) create mode 100644 db/migrate/20200426200729_add_description_to_labels.rb diff --git a/app/models/repository.rb b/app/models/repository.rb index 2a82e3c9..d6cae1f2 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -76,7 +76,7 @@ def ensure_label_missing(label) def labels @labels ||= begin Github.client.labels("voxpupuli/#{name}").map do |label| - Label.find_or_create_by!(name: label[:name], color: label[:color]) + Label.find_or_create_by!(name: label[:name], color: label[:color], description: label[:description]) end end end @@ -112,21 +112,24 @@ def attach_missing_labels ## # Compare the Labels on GitHub with the ones from our config - # If we have the Label in our config but the color differs - # we update the Label on GitHub to match the config. + # If we have the Label in our config but the color or + # description differs we update the Label on GitHub to match the config. # - def sync_label_colors + def sync_label_colors_and_descriptions config_labels = VOXPUPULI_CONFIG['labels'].map do |label| - Label.new(name: label['name'], color: label['color']) + Label.new(name: label['name'], color: label['color'], description: label['description']) end labels.each do |label| config_label = config_labels.select{|c_label| c_label.name == label.name}.first + p config_label + + next unless config_label - next if label.color == config_label.color + next if (label.color == config_label.color) && (label.description == config_label.description) - Github.client.update_label(github_id, config_label.name, { color: config_label.color }) + Github.client.update_label(github_id, config_label.name, { color: config_label.color, description: config_label.description }) end end diff --git a/app/workers/sync_label_worker.rb b/app/workers/sync_label_worker.rb index c3fdfd54..0395b3fe 100644 --- a/app/workers/sync_label_worker.rb +++ b/app/workers/sync_label_worker.rb @@ -6,6 +6,6 @@ class SyncLabelWorker def perform Repository.each do |repository| repository.attach_missing_labels - repository.sync_label_colors + repository.sync_label_colors_and_descriptions end end diff --git a/db/migrate/20200426200729_add_description_to_labels.rb b/db/migrate/20200426200729_add_description_to_labels.rb new file mode 100644 index 00000000..b90f8dcd --- /dev/null +++ b/db/migrate/20200426200729_add_description_to_labels.rb @@ -0,0 +1,5 @@ +class AddDescriptionToLabels < ActiveRecord::Migration[6.0] + def change + add_column :labels, :description, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 26ec7456..94fcc692 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_08_24_091954) do +ActiveRecord::Schema.define(version: 2020_04_26_200729) do create_table "labels", force: :cascade do |t| t.string "name" @@ -18,6 +18,7 @@ t.integer "pull_request_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "description" t.index ["pull_request_id"], name: "index_labels_on_pull_request_id" end From 4cfa3314fd607520659671b56d2c7a7c1843533a Mon Sep 17 00:00:00 2001 From: Flipez Date: Sun, 26 Apr 2020 22:26:08 +0200 Subject: [PATCH 4/4] :hammer: Please the linter Signed-off-by: Flipez --- app/models/repository.rb | 20 +++++++++++++------- app/workers/sync_label_worker.rb | 1 + 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/app/models/repository.rb b/app/models/repository.rb index d6cae1f2..e0935706 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -76,7 +76,9 @@ def ensure_label_missing(label) def labels @labels ||= begin Github.client.labels("voxpupuli/#{name}").map do |label| - Label.find_or_create_by!(name: label[:name], color: label[:color], description: label[:description]) + Label.find_or_create_by!(name: label[:name], + color: label[:color], + description: label[:description]) end end end @@ -117,19 +119,23 @@ def attach_missing_labels # def sync_label_colors_and_descriptions config_labels = VOXPUPULI_CONFIG['labels'].map do |label| - Label.new(name: label['name'], color: label['color'], description: label['description']) + Label.new(name: label['name'], + color: label['color'], + description: label['description']) end labels.each do |label| - config_label = config_labels.select{|c_label| c_label.name == label.name}.first - - p config_label - + config_label = config_labels.select { |c_label| c_label.name == label.name }.first next unless config_label next if (label.color == config_label.color) && (label.description == config_label.description) - Github.client.update_label(github_id, config_label.name, { color: config_label.color, description: config_label.description }) + Github.client.update_label(github_id, + config_label.name, + { + color: config_label.color, + description: config_label.description + }) end end diff --git a/app/workers/sync_label_worker.rb b/app/workers/sync_label_worker.rb index 0395b3fe..5ca50a23 100644 --- a/app/workers/sync_label_worker.rb +++ b/app/workers/sync_label_worker.rb @@ -8,4 +8,5 @@ def perform repository.attach_missing_labels repository.sync_label_colors_and_descriptions end + end end