Class: Moirai::PullRequestCreator
- Inherits:
-
Object
- Object
- Moirai::PullRequestCreator
- Defined in:
- lib/moirai/pull_request_creator.rb
Constant Summary collapse
- BRANCH_PREFIX =
"moirai-translations-"
Instance Attribute Summary collapse
-
#branch_name ⇒ Object
readonly
Returns the value of attribute branch_name.
-
#github_access_token ⇒ Object
readonly
Returns the value of attribute github_access_token.
-
#github_client ⇒ Object
readonly
Returns the value of attribute github_client.
-
#github_repo_name ⇒ Object
readonly
Returns the value of attribute github_repo_name.
-
#github_repository ⇒ Object
readonly
Returns the value of attribute github_repository.
Class Method Summary collapse
Instance Method Summary collapse
- #cleanup ⇒ Object
- #create_pull_request(changes) ⇒ Object
- #existing_open_pull_request ⇒ Object
-
#initialize ⇒ PullRequestCreator
constructor
A new instance of PullRequestCreator.
- #moirai_branch_exists? ⇒ Boolean
Constructor Details
#initialize ⇒ PullRequestCreator
Returns a new instance of PullRequestCreator.
10 11 12 13 14 15 |
# File 'lib/moirai/pull_request_creator.rb', line 10 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) @github_client = Octokit::Client.new(access_token: github_access_token) @github_repository = github_client.repo(github_repo_name) end |
Instance Attribute Details
#branch_name ⇒ Object (readonly)
Returns the value of attribute branch_name.
8 9 10 |
# File 'lib/moirai/pull_request_creator.rb', line 8 def branch_name @branch_name end |
#github_access_token ⇒ Object (readonly)
Returns the value of attribute github_access_token.
8 9 10 |
# File 'lib/moirai/pull_request_creator.rb', line 8 def github_access_token @github_access_token end |
#github_client ⇒ Object (readonly)
Returns the value of attribute github_client.
8 9 10 |
# File 'lib/moirai/pull_request_creator.rb', line 8 def github_client @github_client end |
#github_repo_name ⇒ Object (readonly)
Returns the value of attribute github_repo_name.
8 9 10 |
# File 'lib/moirai/pull_request_creator.rb', line 8 def github_repo_name @github_repo_name end |
#github_repository ⇒ Object (readonly)
Returns the value of attribute github_repository.
8 9 10 |
# File 'lib/moirai/pull_request_creator.rb', line 8 def github_repository @github_repository end |
Class Method Details
.available? ⇒ Boolean
2 3 4 |
# File 'lib/moirai/pull_request_creator.rb', line 2 def self.available? !!defined?(Octokit) end |
Instance Method Details
#cleanup ⇒ Object
62 63 64 65 66 |
# File 'lib/moirai/pull_request_creator.rb', line 62 def cleanup pr = existing_open_pull_request @github_client.close_pull_request(@github_repo_name, pr.number) @github_client.delete_branch(@github_repo_name, pr.head.ref) end |
#create_pull_request(changes) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/moirai/pull_request_creator.rb', line 18 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 if moirai_branch_exists? Rails.logger.debug { "Branch #{branch_name} already exists - the branch will be updated with the new changes" } else Rails.logger.debug { "Branch #{branch_name} does not exist - creating branch" } default_branch_ref = @github_client.ref(@github_repo_name, "heads/#{default_branch}") latest_commit_sha = default_branch_ref.object.sha @github_client.create_ref(@github_repo_name, "heads/#{branch_name}", latest_commit_sha) end changes.each do |change| update_file(change.file_path, change.content) end unless existing_open_pull_request.present? pull_request = @github_client.create_pull_request( @github_repo_name, default_branch, branch_name, "Adding new content by Moirai", "BODY - This is a pull request created by Moirai" ) puts "Pull request created: #{pull_request.html_url}" end end |
#existing_open_pull_request ⇒ Object
56 57 58 59 60 |
# File 'lib/moirai/pull_request_creator.rb', line 56 def existing_open_pull_request @github_client.pull_requests(@github_repo_name).find do |pull_request| pull_request.head.ref.start_with?(BRANCH_PREFIX) && (pull_request.state == "open") end end |
#moirai_branch_exists? ⇒ Boolean
49 50 51 52 53 54 |
# File 'lib/moirai/pull_request_creator.rb', line 49 def moirai_branch_exists? @github_client.ref(@github_repo_name, "heads/#{branch_name}") true rescue Octokit::NotFound false end |