Skip to content

Commit

Permalink
Add a test to the PR creator
Browse files Browse the repository at this point in the history
  • Loading branch information
coorasse committed Oct 30, 2024
1 parent e330cbe commit 186a4f7
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 24 deletions.
54 changes: 30 additions & 24 deletions lib/moirai/pull_request_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,42 @@ class Moirai::PullRequestCreator
BRANCH_NAME = "moirai-translations"

def self.available?
defined?(Octokit)
!!defined?(Octokit)
end

attr_reader :github_repo_name, :github_access_token, :github_client, :github_repository

def initialize
@github_repo_name = ENV["MOIRAI_GITHUB_REPO_NAME"] || Rails.application.credentials.dig(:moirai, :github_repo_name)
@github_access_token = ENV["MOIRAI_GITHUB_ACCESS_TOKEN"] || Rails.application.credentials.dig(:moirai, :github_access_token)
@client = Octokit::Client.new(
access_token: @github_access_token
)
@github_client = Octokit::Client.new(access_token: github_access_token)
@github_repository = github_client.repo(github_repo_name)
end

def create_pull_request(translations_array)
repo = @client.repo(@github_repo_name)
default_branch = repo.default_branch
default_branch = github_repository.default_branch

if branch_exists?
if moirai_branch_exists?
puts "Branch #{BRANCH_NAME} already exists - the branch will be updated with the new changes"
else
puts "Branch #{BRANCH_NAME} does not exist - creating branch"
default_branch_ref = @client.ref(@github_repo_name, "heads/#{default_branch}")
default_branch_ref = @github_client.ref(@github_repo_name, "heads/#{default_branch}")
latest_commit_sha = default_branch_ref.object.sha

@client.create_ref(@github_repo_name, "heads/#{BRANCH_NAME}", latest_commit_sha)
@github_client.create_ref(@github_repo_name, "heads/#{BRANCH_NAME}", latest_commit_sha)
end

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

unless pull_request_exists?
pull_request = @client.create_pull_request(
unless open_pull_request.present?
pull_request = @github_client.create_pull_request(
@github_repo_name,
default_branch,
BRANCH_NAME,
Expand All @@ -44,20 +49,27 @@ def create_pull_request(translations_array)
end
end

def branch_exists?
@client.ref(@github_repo_name, "heads/#{BRANCH_NAME}")
def moirai_branch_exists?
@github_client.ref(@github_repo_name, "heads/#{BRANCH_NAME}")
true
rescue Octokit::NotFound
false
end

def open_pull_request
@github_client.pull_requests(@github_repo_name).find do |pull_request|
(pull_request.head.ref == BRANCH_NAME) && (pull_request.state == "open")
end
end

private

def update_file(path, content)
# TODO: check what happens if branch exists

file = @client.contents(@github_repo_name, path: path, ref: BRANCH_NAME)
file = @github_client.contents(@github_repo_name, path: path, ref: BRANCH_NAME)
file_sha = file.sha

@client.update_contents(
@github_client.update_contents(
@github_repo_name,
path,
"Updating translations for #{path} by Moirai",
Expand All @@ -66,18 +78,12 @@ def update_file(path, content)
branch: BRANCH_NAME
)
rescue Octokit::NotFound
@client.create_contents(
@github_client.create_contents(
@github_repo_name,
path,
"Creating translations for #{path} by Moirai",
content,
branch: BRANCH_NAME
)
end

def pull_request_exists?
@client.pull_requests(@github_repo_name).any? do |pull_request|
pull_request.head.ref == BRANCH_NAME
end
end
end
50 changes: 50 additions & 0 deletions test/models/moirai/pull_request_creator_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

require "test_helper"

module Moirai
class PullRequestCreatorTest < ActiveSupport::TestCase
setup do
@pull_request_creator = PullRequestCreator.new
end

test "it's available when Octokit is also available" do
assert PullRequestCreator.available?
end

test "it has a github_repo_name" do
assert @pull_request_creator.github_repo_name
end

test "it has a github_access_token" do
assert @pull_request_creator.github_access_token
end

test "it has a octokit_client" do
assert @pull_request_creator.octokit_client
end

test "it creates a new pull request with the required changes" do
changes = [
{
file_path: "./README.md",
content: "New content for README.md"
}
]

@pull_request_creator.create_pull_request(changes)

repository = @pull_request_creator.github_repository
default_branch = repository.default_branch
assert_equal "main", default_branch
assert @pull_request_creator.moirai_branch_exists?
pr = @pull_request_creator.open_pull_request
assert pr

@pull_request_creator.github_client.update_pull_request(@pull_request_creator.github_repo_name, pr.number, state: "closed")

pr = @pull_request_creator.open_pull_request
refute pr
end
end
end

0 comments on commit 186a4f7

Please sign in to comment.