-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline_step_git_commit.go
More file actions
183 lines (162 loc) · 5.41 KB
/
pipeline_step_git_commit.go
File metadata and controls
183 lines (162 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package module
import (
"bytes"
"context"
"fmt"
"os/exec"
"strconv"
"strings"
"github.com/CrisisTextLine/modular"
)
// GitCommitStep creates a git commit in a local repository.
type GitCommitStep struct {
name string
directory string
message string
authorName string
authorEmail string
addAll bool
addFiles []string
tmpl *TemplateEngine
execCommand func(ctx context.Context, name string, args ...string) *exec.Cmd
}
// NewGitCommitStepFactory returns a StepFactory that creates GitCommitStep instances.
func NewGitCommitStepFactory() StepFactory {
return func(name string, config map[string]any, _ modular.Application) (PipelineStep, error) {
directory, _ := config["directory"].(string)
if directory == "" {
return nil, fmt.Errorf("git_commit step %q: 'directory' is required", name)
}
message, _ := config["message"].(string)
if message == "" {
return nil, fmt.Errorf("git_commit step %q: 'message' is required", name)
}
authorName, _ := config["author_name"].(string)
authorEmail, _ := config["author_email"].(string)
addAll, _ := config["add_all"].(bool)
var addFiles []string
if filesRaw, ok := config["add_files"].([]any); ok {
for i, f := range filesRaw {
s, ok := f.(string)
if !ok {
return nil, fmt.Errorf("git_commit step %q: add_files[%d] must be a string", name, i)
}
addFiles = append(addFiles, s)
}
}
return &GitCommitStep{
name: name,
directory: directory,
message: message,
authorName: authorName,
authorEmail: authorEmail,
addAll: addAll,
addFiles: addFiles,
tmpl: NewTemplateEngine(),
execCommand: exec.CommandContext,
}, nil
}
}
// Name returns the step name.
func (s *GitCommitStep) Name() string { return s.name }
// Execute stages files and creates a commit.
func (s *GitCommitStep) Execute(ctx context.Context, pc *PipelineContext) (*StepResult, error) {
directory, err := s.tmpl.Resolve(s.directory, pc)
if err != nil {
return nil, fmt.Errorf("git_commit step %q: failed to resolve directory: %w", s.name, err)
}
message, err := s.tmpl.Resolve(s.message, pc)
if err != nil {
return nil, fmt.Errorf("git_commit step %q: failed to resolve message: %w", s.name, err)
}
// Stage files.
if s.addAll {
if err := s.runGit(ctx, directory, "add", "-A"); err != nil {
return nil, fmt.Errorf("git_commit step %q: git add -A failed: %w", s.name, err)
}
} else if len(s.addFiles) > 0 {
addArgs := append([]string{"add", "--"}, s.addFiles...)
if err := s.runGit(ctx, directory, addArgs...); err != nil {
return nil, fmt.Errorf("git_commit step %q: git add failed: %w", s.name, err)
}
}
// Build commit args.
commitArgs := []string{"commit", "-m", message}
if s.authorName != "" && s.authorEmail != "" {
commitArgs = append(commitArgs, "--author", fmt.Sprintf("%s <%s>", s.authorName, s.authorEmail))
}
// Run commit.
var stdout, stderr bytes.Buffer
cmd := s.execCommand(ctx, "git", append([]string{"-C", directory}, commitArgs...)...) //nolint:gosec // G204: args from trusted pipeline config
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
// Check if "nothing to commit" — not an error.
combined := stdout.String() + stderr.String()
if strings.Contains(combined, "nothing to commit") ||
strings.Contains(combined, "nothing added to commit") {
return &StepResult{
Output: map[string]any{
"commit_sha": "",
"message": message,
"files_changed": 0,
"success": true,
},
}, nil
}
return nil, fmt.Errorf("git_commit step %q: git commit failed: %w\nstdout: %s\nstderr: %s",
s.name, err, stdout.String(), stderr.String())
}
// Parse commit SHA.
commitSHA, err := s.getCommitSHA(ctx, directory)
if err != nil {
commitSHA = ""
}
// Count files changed from commit output.
filesChanged := parseFilesChanged(stdout.String())
return &StepResult{
Output: map[string]any{
"commit_sha": commitSHA,
"message": message,
"files_changed": filesChanged,
"success": true,
},
}, nil
}
// runGit runs a git subcommand in the given directory.
func (s *GitCommitStep) runGit(ctx context.Context, dir string, args ...string) error {
fullArgs := append([]string{"-C", dir}, args...)
var stdout, stderr bytes.Buffer
cmd := s.execCommand(ctx, "git", fullArgs...) //nolint:gosec // G204: args from trusted pipeline config
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("%w\nstdout: %s\nstderr: %s", err, stdout.String(), stderr.String())
}
return nil
}
// getCommitSHA returns the HEAD commit SHA for the given directory.
func (s *GitCommitStep) getCommitSHA(ctx context.Context, dir string) (string, error) {
var stdout bytes.Buffer
cmd := s.execCommand(ctx, "git", "-C", dir, "rev-parse", "HEAD") //nolint:gosec // G204: args from trusted pipeline config
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
return "", err
}
return strings.TrimSpace(stdout.String()), nil
}
// parseFilesChanged extracts the number of files changed from git commit output.
// e.g. " 3 files changed, 10 insertions(+)"
func parseFilesChanged(output string) int {
for _, line := range strings.Split(output, "\n") {
if strings.Contains(line, "file") && strings.Contains(line, "changed") {
fields := strings.Fields(line)
if len(fields) > 0 {
if n, err := strconv.Atoi(fields[0]); err == nil {
return n
}
}
}
}
return 0
}