lib/circleci/bundle/update/pr.rb in circleci-bundle-update-pr-1.8.4 vs lib/circleci/bundle/update/pr.rb in circleci-bundle-update-pr-1.9.0

- old
+ new

@@ -5,20 +5,18 @@ module Circleci module Bundle module Update module Pr def self.create_if_needed(git_username: nil, git_email: nil, git_branches: ["master"]) - raise "$CIRCLE_PROJECT_USERNAME isn't set" unless ENV['CIRCLE_PROJECT_USERNAME'] - raise "$CIRCLE_PROJECT_REPONAME isn't set" unless ENV['CIRCLE_PROJECT_REPONAME'] - raise "$GITHUB_ACCESS_TOKEN isn't set" unless ENV['GITHUB_ACCESS_TOKEN'] + raise_if_env_unvalid! return unless need?(git_branches) repo_full_name = "#{ENV['CIRCLE_PROJECT_USERNAME']}/#{ENV['CIRCLE_PROJECT_REPONAME']}" now = Time.now branch = "bundle-update-#{now.strftime('%Y%m%d%H%M%S')}" git_username ||= client.user.login - git_email ||= "#{git_username}@users.noreply.github.com" + git_email ||= "#{git_username}@users.noreply.#{github_host}" create_branch(git_username, git_email, branch, repo_full_name) pull_request = create_pull_request(repo_full_name, branch, now) add_comment_of_compare_linker(repo_full_name, pull_request[:number]) end @@ -31,11 +29,11 @@ `git status -sb 2> /dev/null`.include?("Gemfile.lock") end private_class_method :need? def self.create_branch(git_username, git_email, branch, repo_full_name) - remote = "https://#{ENV["GITHUB_ACCESS_TOKEN"]}@github.com/#{repo_full_name}" + remote = "https://#{github_access_token}@#{github_host}/#{repo_full_name}" system("git remote add github-url-with-token #{remote}") system("git config user.name #{git_username}") system("git config user.email #{git_email}") system("git add Gemfile.lock") system("git commit -m '$ bundle update && bundle update --ruby'") @@ -44,11 +42,12 @@ end private_class_method :create_branch def self.create_pull_request(repo_full_name, branch, now) title = "bundle update at #{now.strftime('%Y-%m-%d %H:%M:%S %Z')}" - body = "auto generated by [CircleCI of #{ENV['CIRCLE_PROJECT_REPONAME']}](https://circleci.com/gh/#{repo_full_name})" + build_url = URI.parse ENV['CIRCLE_BUILD_URL'] + body = "auto generated by [CircleCI of #{ENV['CIRCLE_PROJECT_REPONAME']}](https://#{build_url.host}/gh/#{repo_full_name})" client.create_pull_request(repo_full_name, ENV['CIRCLE_BRANCH'], branch, title, body) end private_class_method :create_pull_request def self.add_comment_of_compare_linker(repo_full_name, pr_number) @@ -61,18 +60,54 @@ #{compare_linker.make_compare_links.to_a.join("\n")} Powered by [compare_linker](https://rubygems.org/gems/compare_linker) EOC - compare_linker.add_comment(repo_full_name, pr_number, comment) end private_class_method :add_comment_of_compare_linker def self.client - Octokit::Client.new(access_token: ENV["GITHUB_ACCESS_TOKEN"]) + if enterprise? + Octokit::Client.new(access_token: ENV['ENTERPRISE_OCTOKIT_ACCESS_TOKEN'], + api_endpoint: ENV['ENTERPRISE_OCTOKIT_API_ENDPOINT']) + else + Octokit::Client.new(access_token: ENV["GITHUB_ACCESS_TOKEN"]) + end end private_class_method :client + + def self.enterprise? + !!ENV['ENTERPRISE_OCTOKIT_ACCESS_TOKEN'] + end + private_class_method :enterprise? + + def self.github_access_token + enterprise? ? ENV['ENTERPRISE_OCTOKIT_ACCESS_TOKEN'] : ENV['GITHUB_ACCESS_TOKEN'] + end + private_class_method :github_access_token + + def self.github_host + # A format like git@github.com:masutaka/compare_linker.git + return $1 if ENV['CIRCLE_REPOSITORY_URL'] =~ %r{([^@]+?):} + # A format like https://github.com/masutaka/circleci-bundle-update-pr.git + return $1 if ENV['CIRCLE_REPOSITORY_URL'] =~ %r{https://(.+?)/} + 'github.com' + end + private_class_method :github_host + + def self.raise_if_env_unvalid! + raise "$CIRCLE_PROJECT_USERNAME isn't set" unless ENV['CIRCLE_PROJECT_USERNAME'] + raise "$CIRCLE_PROJECT_REPONAME isn't set" unless ENV['CIRCLE_PROJECT_REPONAME'] + raise "$GITHUB_ACCESS_TOKEN isn't set" unless ENV['GITHUB_ACCESS_TOKEN'] + if ENV['ENTERPRISE_OCTOKIT_ACCESS_TOKEN'] && !ENV['ENTERPRISE_OCTOKIT_API_ENDPOINT'] + raise "$ENTERPRISE_OCTOKIT_API_ENDPOINT isn't set" + end + if !ENV['ENTERPRISE_OCTOKIT_ACCESS_TOKEN'] && ENV['ENTERPRISE_OCTOKIT_API_ENDPOINT'] + raise "$ENTERPRISE_OCTOKIT_ACCESS_TOKEN isn't set" + end + end + private_class_method :raise_if_env_unvalid! end end end end