-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1683 from yez/main
Add Support for Storing Repo Labels
Showing
10 changed files
with
230 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class Label < ActiveRecord::Base | ||
has_many :repo_labels | ||
has_many :repos, through: :repo_lables | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class RepoLabel < ActiveRecord::Base | ||
belongs_to :repo | ||
belongs_to :label | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
# PORO | ||
# This class takes in a repo and fetch all labels for that repo. It then | ||
# creates labels and associates them with the passed in repo | ||
# | ||
# Example: | ||
# | ||
# repo = Repo.first | ||
# assigner = RepoLabelAssigner.new(repo: repo) | ||
# assigner.create_and_associate_labels! | ||
# | ||
class RepoLabelAssigner | ||
def initialize(repo:) | ||
@repo = repo | ||
url = ['repos', repo.user_name, repo.name, 'labels'].join('/') | ||
@github_bub_response = GitHubBub.get(url) | ||
end | ||
|
||
def create_and_associate_labels! | ||
return unless github_bub_response.success? | ||
|
||
remote_labels.each do |label_hash| | ||
label_name = label_hash['name'].downcase | ||
label = Label.where(name: label_name).first_or_create! | ||
repo.repo_labels.where(label: label).first_or_create | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_reader :github_bub_response, :repo | ||
|
||
def remote_labels | ||
Array(github_bub_response.json_body) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class CreateLabelsTable < ActiveRecord::Migration[6.1] | ||
def change | ||
create_table :labels do |t| | ||
t.string :name, null: false | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class CreateRepoLabelsTable < ActiveRecord::Migration[6.1] | ||
def change | ||
create_table :repo_labels do |t| | ||
t.references :repo, foreign_key: true, null: false | ||
t.references :label, foreign_key: true, null: false | ||
|
||
t.timestamps | ||
end | ||
|
||
add_index :repo_labels, [:repo_id, :label_id], unique: true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class RepoLabelsAssignerTest < ActiveSupport::TestCase | ||
test '#create_and_associate_labels! creates labels' do | ||
repo = repos(:rails_rails) | ||
VCR.use_cassette('fetch_labels_for_repo', record: :once) do | ||
assigner = RepoLabelAssigner.new(repo: repo) | ||
assert_difference('Label.count', 30) do | ||
assigner.create_and_associate_labels! | ||
end | ||
end | ||
end | ||
|
||
test '#create_and_associate_labels! it does not create existing labels' do | ||
repo = repos(:rails_rails) | ||
Label.create!(name: :autoloading) | ||
VCR.use_cassette('fetch_labels_for_repo', record: :once) do | ||
assigner = RepoLabelAssigner.new(repo: repo) | ||
assigner.create_and_associate_labels! | ||
assert_equal Label.where(name: :autoloading).count, 1 | ||
end | ||
end | ||
|
||
test '#create_and_associate_labels! it does not care about label case' do | ||
repo = repos(:rails_rails) | ||
VCR.use_cassette('fetch_labels_for_repo', record: :once) do | ||
assigner = RepoLabelAssigner.new(repo: repo) | ||
assigner.create_and_associate_labels! | ||
assert_equal Label.where(name: :ActionMailer).count, 0 | ||
end | ||
end | ||
|
||
test '#create_and_associate_labels! associates labels with the repo' do | ||
repo = repos(:rails_rails) | ||
VCR.use_cassette('fetch_labels_for_repo', record: :once) do | ||
assigner = RepoLabelAssigner.new(repo: repo) | ||
assigner.create_and_associate_labels! | ||
end | ||
|
||
assert_equal Label.count, RepoLabel.where(repo: repo).count | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.