Skip to content

Commit b7931fa

Browse files
krancourfykaa
andauthored
feat: add new gpg signing key option in git client opts (#3893)
Signed-off-by: Kent Rancourt <[email protected]> Co-authored-by: Faeka Ansari <[email protected]>
1 parent 54fd82e commit b7931fa

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

internal/controller/git/base_repo.go

+34-10
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,11 @@ type User struct {
7474
Email string
7575
// SigningKeyType indicates the type of signing key.
7676
SigningKeyType SigningKeyType
77+
// SigningKey is an optional string containing the raw signing key content.
78+
// If provided, it takes precedence over SigningKeyPath.
79+
SigningKey string
7780
// SigningKeyPath is an optional path referencing a signing key for
78-
// signing git objects.
81+
// signing git objects. Ignored if SigningKey is provided.
7982
SigningKeyPath string
8083
}
8184

@@ -107,18 +110,39 @@ func (b *baseRepo) setupAuthor(author *User) error {
107110
return fmt.Errorf("error configuring git user email: %w", err)
108111
}
109112

110-
if author.SigningKeyPath != "" && author.SigningKeyType == SigningKeyTypeGPG {
111-
cmd = b.buildGitCommand("config", "--global", "commit.gpgsign", "true")
112-
cmd.Dir = b.homeDir // Override the cmd.Dir that's set by r.buildGitCommand()
113-
if _, err := libExec.Exec(cmd); err != nil {
114-
return fmt.Errorf("error configuring commit gpg signing: %w", err)
113+
if author.SigningKeyType == SigningKeyTypeGPG {
114+
115+
if author.SigningKey != "" {
116+
author.SigningKeyPath = filepath.Join(b.homeDir, "signing-key.asc")
117+
if err := os.WriteFile(
118+
author.SigningKeyPath,
119+
[]byte(author.SigningKey),
120+
0600,
121+
); err != nil {
122+
return fmt.Errorf("error writing signing key to %q: %w", author.SigningKeyPath, err)
123+
}
115124
}
116125

117-
cmd = b.buildCommand("gpg", "--import", author.SigningKeyPath)
118-
cmd.Dir = b.homeDir // Override the cmd.Dir that's set by r.buildCommand()
119-
if _, err := libExec.Exec(cmd); err != nil {
120-
return fmt.Errorf("error importing gpg key %q: %w", author.SigningKeyPath, err)
126+
if author.SigningKeyPath != "" {
127+
cmd = b.buildGitCommand("config", "--global", "commit.gpgsign", "true")
128+
cmd.Dir = b.homeDir // Override the cmd.Dir that's set by b.buildGitCommand()
129+
if _, err := libExec.Exec(cmd); err != nil {
130+
return fmt.Errorf("error configuring commit gpg signing: %w", err)
131+
}
132+
133+
cmd = b.buildCommand("gpg", "--import", author.SigningKeyPath)
134+
cmd.Dir = b.homeDir // Override the cmd.Dir that's set by b.buildCommand()
135+
if _, err := libExec.Exec(cmd); err != nil {
136+
return fmt.Errorf("error importing gpg key %q: %w", author.SigningKeyPath, err)
137+
}
121138
}
139+
140+
if author.SigningKey != "" {
141+
if err := os.Remove(author.SigningKeyPath); err != nil {
142+
return fmt.Errorf("error removing file %q: %w", author.SigningKeyPath, err)
143+
}
144+
}
145+
122146
}
123147

124148
return nil

0 commit comments

Comments
 (0)