|
15 | 15 | package librarian |
16 | 16 |
|
17 | 17 | import ( |
| 18 | + "bytes" |
18 | 19 | "context" |
19 | 20 | "fmt" |
| 21 | + "html/template" |
20 | 22 | "log/slog" |
21 | 23 | "path/filepath" |
| 24 | + "strings" |
22 | 25 |
|
23 | 26 | "github.com/googleapis/librarian/internal/config" |
24 | 27 | "github.com/googleapis/librarian/internal/gitrepo" |
@@ -126,18 +129,8 @@ func (r *updateImageRunner) run(ctx context.Context) error { |
126 | 129 | } |
127 | 130 | slog.Info("successful generations", slog.Int("num", len(successfulGenerations))) |
128 | 131 |
|
129 | | - // TODO(#2587): improve PR body content |
130 | 132 | prBodyBuilder := func() (string, error) { |
131 | | - body := fmt.Sprintf("feat: update image to %s\n\n", r.image) |
132 | | - if len(failedGenerations) > 0 { |
133 | | - body += "The following libraries failed to regenerate:\n" |
134 | | - for _, lib := range failedGenerations { |
135 | | - body += fmt.Sprintf("- %s\n", lib.ID) |
136 | | - } |
137 | | - } else { |
138 | | - body += "All libraries regenerated successfully." |
139 | | - } |
140 | | - return body, nil |
| 133 | + return formatUpdateImagePRBody(r.image, failedGenerations) |
141 | 134 | } |
142 | 135 | commitMessage := fmt.Sprintf("feat: update image to %s", r.image) |
143 | 136 | // TODO(#2588): open PR as draft if there are failures |
@@ -185,3 +178,33 @@ func (r *updateImageRunner) regenerateSingleLibrary(ctx context.Context, library |
185 | 178 |
|
186 | 179 | return nil |
187 | 180 | } |
| 181 | + |
| 182 | +var updateImageTemplate = template.Must(template.New("updateImage").Parse(`feat: update image to {{.Image}} |
| 183 | +{{ if .FailedLibraries }} |
| 184 | +## Generation failed for |
| 185 | +{{- range .FailedLibraries }} |
| 186 | +- {{ . }} |
| 187 | +{{- end -}} |
| 188 | +{{- end }} |
| 189 | +`)) |
| 190 | + |
| 191 | +type updateImagePRBody struct { |
| 192 | + Image string |
| 193 | + FailedLibraries []string |
| 194 | +} |
| 195 | + |
| 196 | +func formatUpdateImagePRBody(image string, failedGenerations []*config.LibraryState) (string, error) { |
| 197 | + failedLibraries := make([]string, 0, len(failedGenerations)) |
| 198 | + for _, failedGeneration := range failedGenerations { |
| 199 | + failedLibraries = append(failedLibraries, failedGeneration.ID) |
| 200 | + } |
| 201 | + data := &updateImagePRBody{ |
| 202 | + Image: image, |
| 203 | + FailedLibraries: failedLibraries, |
| 204 | + } |
| 205 | + var out bytes.Buffer |
| 206 | + if err := updateImageTemplate.Execute(&out, data); err != nil { |
| 207 | + return "", fmt.Errorf("error executing template %w", err) |
| 208 | + } |
| 209 | + return strings.TrimSpace(out.String()), nil |
| 210 | +} |
0 commit comments