Skip to content

Commit e510076

Browse files
author
Scott Schulthess
committed
github app support in git clone
remove run e remove run e fix gofmt fix gofmt fix gofmt
1 parent cb5a0e7 commit e510076

File tree

4 files changed

+39
-22
lines changed

4 files changed

+39
-22
lines changed

git.go

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,24 @@ func NewGitClient(source *Source, dir string, output io.Writer) (*GitClient, err
3636
os.Setenv("GIT_LFS_SKIP_SMUDGE", "true")
3737
}
3838
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,
4245
}, nil
4346
}
4447

4548
// GitClient ...
4649
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
5057
}
5158

5259
func (g *GitClient) command(name string, arg ...string) *exec.Cmd {
@@ -58,6 +65,8 @@ func (g *GitClient) command(name string, arg ...string) *exec.Cmd {
5865
cmd.Env = append(cmd.Env,
5966
"X_OAUTH_BASIC_TOKEN="+g.AccessToken,
6067
"GIT_ASKPASS=/usr/local/bin/askpass.sh")
68+
fmt.Fprint(os.Stderr, fmt.Sprintf("\n%s %v", name, arg))
69+
6170
return cmd
6271
}
6372

@@ -75,9 +84,20 @@ func (g *GitClient) Init(branch string) error {
7584
if err := g.command("git", "config", "--global", "user.email", "concourse@local").Run(); err != nil {
7685
return fmt.Errorf("failed to configure git email: %s", err)
7786
}
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, "\nsds 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+
}
81101
}
82102
if err := g.command("git", "config", "--global", "url.https://.insteadOf", "git://").Run(); err != nil {
83103
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
92112
return err
93113
}
94114

95-
96115
if err := g.command("git", "remote", "add", "origin", endpoint).Run(); err != nil {
97116
return fmt.Errorf("setting 'origin' remote to '%s' failed: %s", endpoint, err)
98117
}

github.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,9 @@ func NewGithubClient(s *Source) (*GithubClient, error) {
6464
var client *http.Client
6565
if s.UseGitHubApp {
6666
var ghAppInstallationTransport *ghinstallation.Transport
67-
if s.PrivateKeyFile != "" {
68-
ghAppInstallationTransport, err = ghinstallation.NewKeyFromFile(transport, s.ApplicationID, s.InstallationID, s.PrivateKeyFile)
69-
if err != nil {
70-
return nil, fmt.Errorf("failed to generate application installation access token using private key file: %s", err)
71-
}
72-
} else {
73-
ghAppInstallationTransport, err = ghinstallation.New(transport, s.ApplicationID, s.InstallationID, []byte(s.PrivateKey))
74-
if err != nil {
75-
return nil, fmt.Errorf("failed to generate application installation access token using private key: %s", err)
76-
}
67+
ghAppInstallationTransport, err = ghinstallation.New(transport, s.ApplicationID, s.InstallationID, []byte(s.PrivateKey))
68+
if err != nil {
69+
return nil, fmt.Errorf("failed to generate application installation access token using private key: %s", err)
7770
}
7871

7972
// Client using ghinstallation transport

models.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ type Source struct {
3030
TrustedTeams []string `json:"trusted_teams"`
3131
TrustedUsers []string `json:"trusted_users"`
3232
UseGitHubApp bool `json:"use_github_app"`
33+
GithubOrganziation string `json:"github_organization"`
3334
PrivateKey string `json:"private_key"`
34-
PrivateKeyFile string `json:"private_key_file"`
3535
ApplicationID int64 `json:"application_id"`
3636
InstallationID int64 `json:"installation_id"`
3737
}
@@ -42,12 +42,15 @@ func (s *Source) Validate() error {
4242
return errors.New("access_token must be set if not using GitHub App authentication")
4343
}
4444
if s.UseGitHubApp {
45-
if s.PrivateKey == "" && s.PrivateKeyFile == "" {
45+
if s.PrivateKey == "" {
4646
return errors.New("Either private_key or private_key_file should be supplied if using GitHub App authentication")
4747
}
4848
if s.ApplicationID == 0 || s.InstallationID == 0 {
4949
return errors.New("application_id and installation_id must be set if using GitHub App authentication")
5050
}
51+
if len(s.GithubOrganziation) == 0 {
52+
return errors.New("github_organization must be set if using GitHub App authentication")
53+
}
5154
if s.AccessToken != "" {
5255
return errors.New("access_token is not required when using GitHub App authentication")
5356
}

models_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func TestSource(t *testing.T) {
3939
description: "should support GitHub App authentication",
4040
source: resource.Source{
4141
Repository: "test/test",
42+
GithubOrganziation: "test",
4243
UseGitHubApp: true,
4344
PrivateKey: "key.pem",
4445
ApplicationID: 123456,
@@ -70,6 +71,7 @@ func TestSource(t *testing.T) {
7071
source: resource.Source{
7172
Repository: "test/test",
7273
UseGitHubApp: true,
74+
GithubOrganziation: "test",
7375
PrivateKey: "key.pem",
7476
ApplicationID: 123456,
7577
InstallationID: 1,

0 commit comments

Comments
 (0)