@@ -10,20 +10,14 @@ def initialize(syncer, pr_title, pr_message, &commit_block)
10
10
end
11
11
12
12
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
-
19
13
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 )
21
15
22
16
commits_created = commit_block . ( new_branch , token )
23
17
return unless commits_created
24
18
25
19
client . create_pull_request (
26
- repo ,
20
+ repo_full_name ,
27
21
base_branch ,
28
22
new_branch ,
29
23
pr_title ,
@@ -34,9 +28,42 @@ def call
34
28
private
35
29
attr_reader :syncer , :pr_title , :pr_message , :commit_block
36
30
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
+
37
52
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
40
59
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 )
41
68
end
42
69
end
0 commit comments