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

handle labels #135

Merged
merged 4 commits into from
Apr 26, 2020
Merged
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
70 changes: 70 additions & 0 deletions app/models/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,76 @@ 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],
description: label[:description])
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 or
# description differs we update the Label on GitHub to match the config.
#
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'])
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) && (label.description == config_label.description)

Github.client.update_label(github_id,
config_label.name,
{
color: config_label.color,
description: config_label.description
})
end
end

##
# Fetch all open and closed PullRequests and sync our database with them
#
Expand Down
1 change: 1 addition & 0 deletions app/workers/index_repos_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ def perform
end

RepoStatusWorker.perform_async
SyncLabelWorker.perform_async
end
end
12 changes: 12 additions & 0 deletions app/workers/sync_label_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class SyncLabelWorker
include Sidekiq::Worker

def perform
Repository.each do |repository|
repository.attach_missing_labels
repository.sync_label_colors_and_descriptions
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20200426200729_add_description_to_labels.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDescriptionToLabels < ActiveRecord::Migration[6.0]
def change
add_column :labels, :description, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
#
# 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"
t.string "color"
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

Expand Down