git-roast/internal/roast/request.go

91 lines
2.1 KiB
Go
Raw Normal View History

2025-01-05 06:30:54 +01:00
package roast
import (
"errors"
"fmt"
"git-roast/internal/log"
2025-01-05 07:35:12 +01:00
"git-roast/internal/util"
"net/url"
"os"
"os/exec"
"path/filepath"
"time"
2025-01-05 06:30:54 +01:00
)
func (r *Roast) Request(from string, into string) error {
log.Debugf("requesting merge from %s into %s\n", from, into)
sourceBranch, err := r.repo.Branch(from)
if err != nil {
return errors.Join(fmt.Errorf("failed to get source branch %s", from), err)
}
2025-01-05 07:55:11 +01:00
log.Debugf("branch %s found\n", sourceBranch.Name)
2025-01-05 06:30:54 +01:00
targetBranch, err := r.repo.Branch(into)
if err != nil {
return errors.Join(fmt.Errorf("failed to get target branch %s", from), err)
}
2025-01-05 07:55:11 +01:00
log.Debugf("branch %s found\n", targetBranch.Name)
2025-01-05 06:30:54 +01:00
2025-01-05 07:35:12 +01:00
id := url.PathEscape(fmt.Sprintf(
"%016x_from_%s_into_%s",
time.Now().Unix(),
2025-01-05 06:30:54 +01:00
sourceBranch.Name,
targetBranch.Name,
2025-01-05 07:35:12 +01:00
))
initialMessage, err := util.Textedit(fmt.Sprintf(
"%s is requesting merge into `%s` from `%s`\n",
GitUserName(),
into,
from,
))
if err != nil {
return err
}
p := filepath.Join(r.dir, id)
if err := os.MkdirAll(p, 0700); err != nil {
return errors.Join(errors.New("failed to create directory for request"), err)
}
successful := false
defer func() {
if !successful {
2025-01-05 07:55:11 +01:00
clean := exec.Command("git", "clean", "-xfd")
clean.Path = r.dir
_ = clean.Run()
2025-01-05 07:35:12 +01:00
log.Printf("action not successful, rollback...\n")
reset := exec.Command("git", "reset", "--hard")
reset.Path = r.dir
_ = reset.Run()
}
}()
2025-01-05 06:30:54 +01:00
2025-01-05 07:35:12 +01:00
f, err := os.Create(filepath.Join(p, fmt.Sprintf(
"%016x_%s.md",
time.Now().Unix(),
GitUserName(),
)))
if err != nil {
return errors.Join(errors.New("failed to create file for request"), err)
}
if _, err := f.Write([]byte(initialMessage)); err != nil {
return errors.Join(errors.New("failed to write to file for request"), err)
}
2025-01-05 07:55:11 +01:00
if err := r.AddAll(); err != nil {
return errors.Join(errors.New("failed to add changes before commit"), err)
}
if err := r.Commit(id); err != nil {
return errors.Join(errors.New("failed to commit changes before push request"), err)
}
if err := r.Push(); err != nil {
2025-01-05 07:35:12 +01:00
return errors.Join(errors.New("failed to push request"), err)
}
2025-01-05 06:30:54 +01:00
2025-01-05 07:35:12 +01:00
successful = true
2025-01-05 06:30:54 +01:00
return nil
}