Share commit preparation and checkout updates across merge paths
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package agents
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"g.fc.run/ai/byre/internal/api"
|
||||
"g.fc.run/ai/byre/internal/git"
|
||||
)
|
||||
|
||||
func (m *Manager) prepareMainCommit(ctx context.Context, task api.Task, project api.Project, user api.User) (string, string, error) {
|
||||
coAuthors, err := m.CoAuthors(ctx, task.Id, user.Login)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
env := git.IdentityEnv(user.GitName, user.GitEmail)
|
||||
commit, err := git.SquashCommit(project.RepoPath, task.WorktreePath, project.DefaultBranch, squashMessage(task.MergeTitle, coAuthors), env, m.signer(ctx, user))
|
||||
if err != nil || commit == "" {
|
||||
return "", "", err
|
||||
}
|
||||
note, err := m.mergeNote(ctx, task, user.Login)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
notes, err := git.NoteCommit(project.RepoPath, commit, note, env)
|
||||
return commit, notes, err
|
||||
}
|
||||
|
||||
func advanceMainCheckout(project api.Project, commit, notes string) error {
|
||||
pushed := git.Remote + "/" + project.DefaultBranch
|
||||
err := git.AdvanceNotes(project.RepoPath, notes)
|
||||
if err != nil {
|
||||
return fmt.Errorf("pushed %s to %s, but updating %s in the project checkout failed: %w", commit, pushed, git.NotesRef, err)
|
||||
}
|
||||
err = git.FastForward(project.RepoPath, commit)
|
||||
if err != nil {
|
||||
return fmt.Errorf("pushed %s to %s, but fast-forwarding the project checkout failed: %w", commit, pushed, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Manager) CoAuthors(ctx context.Context, taskID, login string) ([]api.Participant, error) {
|
||||
participants, err := m.store.Participants(ctx, taskID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
coAuthors := []api.Participant{}
|
||||
for _, p := range participants {
|
||||
if p.Login != login {
|
||||
coAuthors = append(coAuthors, p)
|
||||
}
|
||||
}
|
||||
return coAuthors, nil
|
||||
}
|
||||
|
||||
func squashMessage(title string, coAuthors []api.Participant) string {
|
||||
trailers := []string{}
|
||||
for _, p := range coAuthors {
|
||||
if p.GitName == "" || p.GitEmail == "" {
|
||||
continue
|
||||
}
|
||||
trailers = append(trailers, fmt.Sprintf("Co-authored-by: %s <%s>", p.GitName, p.GitEmail))
|
||||
}
|
||||
if len(trailers) == 0 {
|
||||
return title
|
||||
}
|
||||
return title + "\n\n" + strings.Join(trailers, "\n")
|
||||
}
|
||||
Reference in New Issue
Block a user