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

Introduce change model #44

Merged
merged 1 commit into from
Nov 1, 2024
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
15 changes: 15 additions & 0 deletions app/models/moirai/change.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

# Prepares a certain file to be changefd in a Pull Request.
# It takes care of adjusting the file_path and content
module Moirai
class Change
attr_reader :file_path, :content

def initialize(file_path, content)
@file_path = file_path
@file_path = file_path.to_s.start_with?("./") ? file_path : "./#{file_path}"
@content = content.to_s.end_with?("\n") ? content : "#{content}\n"
end
end
end
7 changes: 2 additions & 5 deletions app/models/moirai/translation_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ def initialize
@key_finder = KeyFinder.new
end

# @return Array[Hash]
# @return Array[Moirai::Change]
def call
translations_by_file_path = group_translations_by_file_path
changes = []
translations_by_file_path.each do |file_path, translations|
relative_file_path = Pathname.new(file_path).relative_path_from(Rails.root)
changes << {
file_path: relative_file_path,
content: get_updated_file_contents(file_path, translations)
}
changes << Change.new(relative_file_path, get_updated_file_contents(file_path, translations))
end
changes
end
Expand Down
12 changes: 4 additions & 8 deletions lib/moirai/pull_request_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ def initialize
@github_repository = github_client.repo(github_repo_name)
end

def create_pull_request(translations_array)
# @param changes Array[Moirai::Change]
def create_pull_request(changes)
@branch_name = "#{BRANCH_PREFIX}#{Time.current.strftime("%F-%H-%M-%S")}-#{rand(1000..9999)}"
default_branch = github_repository.default_branch

Expand All @@ -28,13 +29,8 @@ def create_pull_request(translations_array)
@github_client.create_ref(@github_repo_name, "heads/#{branch_name}", latest_commit_sha)
end

translations_array.each do |translation_hash|
converted_file_path = if translation_hash[:file_path].to_s.start_with?("./")
translation_hash[:file_path]
else
"./#{translation_hash[:file_path]}"
end
update_file(converted_file_path, translation_hash[:content])
changes.each do |change|
update_file(change.file_path, change.content)
end

unless existing_open_pull_request.present?
Expand Down
8 changes: 2 additions & 6 deletions test/models/moirai/pull_request_creator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ class PullRequestCreatorTest < ActiveSupport::TestCase
end

test "it creates a new pull request with the required changes" do
changes = [
{
file_path: "./README.md",
content: "New content for README.md"
}
]
changes = [Change.new("README.md", "New content for README.md"),
Change.new("config/locales/moirai.de.yml", "New file added")]

@pull_request_creator.create_pull_request(changes)

Expand Down
40 changes: 16 additions & 24 deletions test/models/moirai/translation_dumper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ module Moirai
class TranslationDumperTest < ActiveSupport::TestCase
setup do
@translation_dumper = TranslationDumper.new
@de_relative_file_path = Pathname.new("config/locales/de.yml")
@de_relative_file_path = Pathname.new("./config/locales/de.yml")
@de_file_path = Rails.root.join(@de_relative_file_path).to_s
@it_relative_file_path = Pathname.new("config/locales/it.yml")
@it_relative_file_path = Pathname.new("./config/locales/it.yml")
@it_file_path = Rails.root.join(@it_relative_file_path).to_s
end

Expand All @@ -23,43 +23,35 @@ class TranslationDumperTest < ActiveSupport::TestCase

changes = @translation_dumper.call
assert_equal 1, changes.length
assert_equal @de_relative_file_path, changes[0][:file_path]
content = YAML.load(changes[0][:content], symbolize_names: true)
assert_equal @de_relative_file_path.to_s, changes[0].file_path
content = YAML.load(changes[0].content, symbolize_names: true)
assert_equal "Italianese", content.dig(:de, :locales, :italian)
end

test "it merges two changes on the same file" do
Moirai::Translation.create!(locale: "de",
key: "locales.italian",
value: "Italianese")
Moirai::Translation.create!(locale: "de",
key: "locales.german",
value: "Germanese")
Moirai::Translation.create!(locale: "de", key: "locales.italian", value: "Italianese")
Moirai::Translation.create!(locale: "de", key: "locales.german", value: "Germanese")

changes = @translation_dumper.call
assert_equal 1, changes.length
assert_equal @de_relative_file_path, changes[0][:file_path]
content = YAML.load(changes[0][:content], symbolize_names: true)
assert_equal @de_relative_file_path.to_s, changes[0].file_path
content = YAML.load(changes[0].content, symbolize_names: true)
assert_equal "Italianese", content.dig(:de, :locales, :italian)
assert_equal "Germanese", content.dig(:de, :locales, :german)
end

test "it returns two changes for separate files" do
Moirai::Translation.create!(locale: "de",
key: "locales.italian",
value: "Italianese")
Moirai::Translation.create!(locale: "it",
key: "locales.german",
value: "Germanese")
Moirai::Translation.create!(locale: "de", key: "locales.italian", value: "Italianese")
Moirai::Translation.create!(locale: "it", key: "locales.german", value: "Germanese")

changes = @translation_dumper.call
assert_equal 2, changes.length
assert_equal @de_relative_file_path, changes[0][:file_path]
de_content = YAML.load(changes[0][:content], symbolize_names: true)
assert_equal @de_relative_file_path.to_s, changes[0].file_path
de_content = YAML.load(changes[0].content, symbolize_names: true)
assert_equal "Italianese", de_content.dig(:de, :locales, :italian)

assert_equal @it_relative_file_path, changes[1][:file_path]
it_content = YAML.load(changes[1][:content], symbolize_names: true)
assert_equal @it_relative_file_path.to_s, changes[1].file_path
it_content = YAML.load(changes[1].content, symbolize_names: true)
assert_equal "Germanese", it_content.dig(:it, :locales, :german)
end

Expand All @@ -74,8 +66,8 @@ class TranslationDumperTest < ActiveSupport::TestCase

changes = @translation_dumper.call
assert_equal 1, changes.length
assert_equal @de_relative_file_path, changes[0][:file_path]
content = YAML.load(changes[0][:content], symbolize_names: true)
assert_equal @de_relative_file_path.to_s, changes[0].file_path
content = YAML.load(changes[0].content, symbolize_names: true)
assert_equal "Italianese Recent", content.dig(:de, :locales, :italian)
end
end
Expand Down
Loading