Skip to content

Commit 5f0c870

Browse files
authored
Tweaks to deal with empty repos (exercism#7894)
* Tweaks to deal with empty repos * Fix test
1 parent bb4b4d2 commit 5f0c870

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

app/commands/user/github_solution_syncer/create_pull_request.rb

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,14 @@ def initialize(syncer, pr_title, pr_message, &commit_block)
1010
end
1111

1212
def call
13-
repo = syncer.repo_full_name
14-
client = Octokit::Client.new(access_token: token)
15-
16-
base_branch = client.repository(repo).default_branch
17-
base_sha = client.branch(repo, base_branch).commit.sha
18-
1913
new_branch = "exercism-sync/#{SecureRandom.hex(8)}"
20-
client.create_ref(repo, "heads/#{new_branch}", base_sha)
14+
client.create_ref(repo_full_name, "heads/#{new_branch}", base_sha)
2115

2216
commits_created = commit_block.(new_branch, token)
2317
return unless commits_created
2418

2519
client.create_pull_request(
26-
repo,
20+
repo_full_name,
2721
base_branch,
2822
new_branch,
2923
pr_title,
@@ -34,9 +28,42 @@ def call
3428
private
3529
attr_reader :syncer, :pr_title, :pr_message, :commit_block
3630

31+
delegate :repo_full_name, to: :syncer
32+
33+
memoize
34+
def base_sha
35+
# This will raise if the branch doesn't exit
36+
client.branch(repo_full_name, base_branch).commit.sha
37+
rescue Octokit::NotFound
38+
# If it doesn't, then this is a naked repo, so create it.
39+
Dir.mktmpdir do |dir|
40+
@path = dir
41+
42+
# No existing branch so create it
43+
git "init", "-b", base_branch
44+
git "commit", "--allow-empty", "-m", "Initial empty commit"
45+
git "remote", "add", "origin", repo_url
46+
git "push", "origin", base_branch
47+
end
48+
49+
client.branch(repo_full_name, base_branch).commit.sha
50+
end
51+
3752
memoize
38-
def token
39-
GithubApp.generate_installation_token!(syncer.installation_id)
53+
def base_branch = client.repository(repo_full_name).default_branch
54+
55+
def git(*args)
56+
Dir.chdir(@path) do
57+
system("git", *args, exception: true)
58+
end
4059
end
60+
61+
def repo_url = "https://x-access-token:#{token}@github.com/#{repo_full_name}.git"
62+
63+
memoize
64+
def token = GithubApp.generate_installation_token!(syncer.installation_id)
65+
66+
memoize
67+
def client = Octokit::Client.new(access_token: token)
4168
end
4269
end

app/controllers/settings/github_syncer_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class Settings::GithubSyncerController < ApplicationController
22
skip_before_action :verify_authenticity_token, only: %i[update sync_everything sync_track sync_solution sync_iteration]
33

44
def show
5-
current_user.github_solution_syncer
5+
@just_connected = !!params[:connected]
66
end
77

88
def update
@@ -68,7 +68,7 @@ def callback
6868
User::GithubSolutionSyncer::Create.(current_user, installation_id)
6969
redirect_to settings_github_syncer_path, notice: "GitHub connected successfully"
7070
rescue GithubSolutionSyncerCreationError => e
71-
redirect_to settings_github_syncer_path, alert: e.message
71+
redirect_to settings_github_syncer_path(connected: true), alert: e.message
7272
end
7373
end
7474

0 commit comments

Comments
 (0)