Skip to content

Commit

Permalink
chore: Rework git auth for compatibility with github actions environm…
Browse files Browse the repository at this point in the history
  • Loading branch information
dazuma authored Feb 9, 2022
1 parent 2075a15 commit 7dbdbf7
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions .toys/.lib/pull_request_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

require "json"
require "base64"

module PullRequestGenerator
def ensure_pull_request_generation_dependencies
Expand All @@ -37,6 +38,7 @@ def generate_pull_request git_remote:,
end

@dependencies_checked = false
@created_fork = false

class << self
def generate context:,
Expand All @@ -46,10 +48,14 @@ def generate context:,
pr_body: nil
if git_remote
ensure_dependencies context: context
created_fork_name = environment_fork_name context: context if @created_fork
github_token = environment_github_token context: context
impl = Impl.new context: context,
git_remote: git_remote,
branch_name: branch_name,
commit_message: commit_message,
created_fork_name: created_fork_name,
github_token: github_token,
pr_body: pr_body
impl.start
yield
Expand All @@ -71,16 +77,14 @@ def ensure_dependencies context:
end

def ensure_fork context:, git_remote:
ensure_dependencies context: context
git_remote ||= "pull-request-fork"
fork_name = environment_fork_name context: context
context.exec ["gh", "repo", "fork", "--remote=false"]
repo = ::JSON.parse(context.capture(["gh", "repo", "view", "--json=name"]))["name"]
owner = ::JSON.parse(context.capture(["gh", "api", "/user"]))["login"]
context.exec ["gh", "repo", "sync", "#{owner}/#{repo}"]
context.exec ["gh", "repo", "sync", fork_name]
unless context.exec(["git", "remote", "get-url", git_remote], e: false, out: :capture, err: :capture).success?
token = environment_github_token context: context
cmd = ["git", "remote", "add", git_remote, "https://#{owner}:#{token}@github.com/#{owner}/#{repo}.git"]
log = ["git", "remote", "add", git_remote, "https://#{owner}:xxxxxxxx@github.com/#{owner}/#{repo}.git"]
context.exec cmd, log_cmd: log.inspect
context.exec ["git", "remote", "add", git_remote, "https://github.com/#{fork_name}.git"]
@created_fork = true
end
git_remote
end
Expand Down Expand Up @@ -128,8 +132,16 @@ def ensure_git_identity context:
context.exec ["git", "config", "--global", "user.name", "Yoshi Automation Bot"]
end

def environment_fork_name context:
@fork_name ||= begin
repo = JSON.parse(context.capture(["gh", "repo", "view", "--json=name"]))["name"]
owner = JSON.parse(context.capture(["gh", "api", "/user"]))["login"]
"#{owner}/#{repo}"
end
end

def environment_github_token context:
@environment_github_token ||= ENV["GITHUB_TOKEN"] || begin
@github_token ||= ENV["GITHUB_TOKEN"] || begin
result = context.exec ["gh", "auth", "status", "-t"], e: false, out: :capture, err: [:child, :out]
raise "Failed to get github token" unless result.success? && result.captured_out =~ /Token: (\w+)/
Regexp.last_match[1]
Expand All @@ -142,12 +154,16 @@ def initialize git_remote:,
context:,
branch_name: nil,
commit_message: nil,
created_fork_name: nil,
github_token: nil,
pr_body: nil
@git_remote = git_remote
@context = context
@branch_name = branch_name || generate_default_branch_name
@commit_message = commit_message || generate_default_commit_message
@pr_body = pr_body || generate_default_pr_body
@created_fork_name = created_fork_name
@github_token = github_token
end

def start
Expand All @@ -167,7 +183,16 @@ def finish
if result
@context.exec ["git", "add", "."]
@context.exec ["git", "commit", "-m", @commit_message]
@context.exec ["git", "push", "-u", @git_remote, @branch_name]
log = cmd = ["git", "push", "-u", @git_remote, @branch_name]
if @github_token && @created_fork_name
username = @created_fork_name.split("/").first
credential = Base64.encode64 "#{username}:#{@github_token}"
cmd += [
"-c", "http.https://github.com/.extraheader=",
"-c", "http.https://github.com/#{@created_fork_name}.extraheader=AUTHORIZATION: basic #{credential}"
]
end
@context.exec cmd, log_cmd: "exec: #{log.inspect}"
@context.exec ["gh", "pr", "create",
"--title", @commit_message,
"--body", @pr_body]
Expand Down

0 comments on commit 7dbdbf7

Please sign in to comment.