@@ -36,17 +36,24 @@ func NewGitClient(source *Source, dir string, output io.Writer) (*GitClient, err
36
36
os .Setenv ("GIT_LFS_SKIP_SMUDGE" , "true" )
37
37
}
38
38
return & GitClient {
39
- AccessToken : source .AccessToken ,
40
- Directory : dir ,
41
- Output : output ,
39
+ AccessToken : source .AccessToken ,
40
+ PrivateKey : source .PrivateKey ,
41
+ UseGithubApp : source .UseGitHubApp ,
42
+ ApplicationID : source .ApplicationID ,
43
+ Directory : dir ,
44
+ Output : output ,
42
45
}, nil
43
46
}
44
47
45
48
// GitClient ...
46
49
type GitClient struct {
47
- AccessToken string
48
- Directory string
49
- Output io.Writer
50
+ AccessToken string
51
+ UseGithubApp bool
52
+ Directory string
53
+ ApplicationID int64
54
+ GithubOrganziation string
55
+ PrivateKey string
56
+ Output io.Writer
50
57
}
51
58
52
59
func (g * GitClient ) command (name string , arg ... string ) * exec.Cmd {
@@ -58,6 +65,8 @@ func (g *GitClient) command(name string, arg ...string) *exec.Cmd {
58
65
cmd .Env = append (cmd .Env ,
59
66
"X_OAUTH_BASIC_TOKEN=" + g .AccessToken ,
60
67
"GIT_ASKPASS=/usr/local/bin/askpass.sh" )
68
+ fmt .Fprint (os .Stderr , fmt .Sprintf ("\n %s %v" , name , arg ))
69
+
61
70
return cmd
62
71
}
63
72
@@ -75,9 +84,20 @@ func (g *GitClient) Init(branch string) error {
75
84
if err := g .command ("git" , "config" , "--global" , "user.email" , "concourse@local" ).Run (); err != nil {
76
85
return fmt .Errorf ("failed to configure git email: %s" , err )
77
86
}
78
- fmt .Println ("SDS" )
79
- if err := g .command ("git" , "config" , "credential.https://github.com.helper" ,"'!git-credential-github-app --appId ((github/concourse-app-id)) -organization ((github/concourse-app-organization-name)) -username x-access-token'" ).Run (); err != nil {
80
- return fmt .Errorf ("failed to configure github url: %s" , err )
87
+
88
+ if g .UseGithubApp {
89
+ filePath := "/tmp/git-resource-private-key"
90
+ err := ioutil .WriteFile (filePath , []byte (g .PrivateKey ), 0600 )
91
+ if err != nil {
92
+ fmt .Println ("Error writing private key:" , err )
93
+ os .Exit (1 )
94
+ }
95
+
96
+ helperStr := fmt .Sprintf ("!git-credential-github-app --appId %d -organization %s -username x-access-token -privateKeyFile /tmp/git-resource-private-key" , g .ApplicationID , g .GithubOrganziation )
97
+ fmt .Fprint (os .Stderr , "\n sds helperStr" , helperStr )
98
+ if err := g .command ("git" , "config" , "credential.https://github.com.helper" , helperStr ).Run (); err != nil {
99
+ return fmt .Errorf ("failed to configure github url: %s" , err )
100
+ }
81
101
}
82
102
if err := g .command ("git" , "config" , "--global" , "url.https://.insteadOf" , "git://" ).Run (); err != nil {
83
103
return fmt .Errorf ("failed to configure github url: %s" , err )
@@ -92,7 +112,6 @@ func (g *GitClient) Pull(uri, branch string, depth int, submodules bool, fetchTa
92
112
return err
93
113
}
94
114
95
-
96
115
if err := g .command ("git" , "remote" , "add" , "origin" , endpoint ).Run (); err != nil {
97
116
return fmt .Errorf ("setting 'origin' remote to '%s' failed: %s" , endpoint , err )
98
117
}
0 commit comments