Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
f74721d
feat: introduce forge-agnostic MergeMethod type in config
harshs-dyte Feb 16, 2026
49e8285
feat: add common types for all forges
harshs-dyte Feb 16, 2026
1cbec7d
refactor: rename GitHub-specific config fields to forge-generic names
harshs-dyte Feb 16, 2026
0d2c427
refactor: migrate all packages from github types to forge types
harshs-dyte Feb 16, 2026
2f22014
refactor: remove obsolete github-specific type definitions
harshs-dyte Feb 16, 2026
e0c2efa
feat: add GitLab client implementing forge interface
harshs-dyte Feb 16, 2026
77c01f0
refactor: rename YAML config keys to forge-agnostic names with migration
harshs-dyte Feb 16, 2026
a114197
refactor: move PR template system from github/template/ to forge/temp…
harshs-dyte Feb 16, 2026
13a7372
feat: add forge auto-detection in main based on host name
harshs-dyte Feb 16, 2026
cd99d43
fix: align `commit-id` check in reword helper with main executable
harshs-dyte Feb 16, 2026
b91f4e9
fix: use the correct autolinking prefix for each forge
harshs-dyte Feb 16, 2026
c19dbe8
fix: persist user selection on unknown forge type
harshs-dyte Feb 16, 2026
939bc7b
refactor: make `PullRequestURL` a `ForgeInterface` method
harshs-dyte Feb 17, 2026
058607c
fix: support nested GitLab subgroups in remote URL parsing
harshs-dyte Feb 20, 2026
e8213e9
fix: use forge-agnostic label in status header
harshs-dyte Feb 20, 2026
d599e70
fix: prevent empty commit body from producing YAML frontmatter in MR …
harshs-dyte Feb 20, 2026
69f549f
fix: retry GitLab merge on 405 after retarget or rebase
harshs-dyte Feb 23, 2026
964fd30
fix: check actual approvers instead of vacuously-true approval rules
harshs-dyte Feb 24, 2026
0bf2ed8
fix: use GitLab auto-merge when CI pipeline blocks immediate merge
harshs-dyte Feb 24, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions .spr.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
githubRepoOwner: ejoffe
githubRepoName: spr
githubHost: github.com
githubRemote: origin
githubBranch: master
repoOwner: ejoffe
repoName: spr
forgeHost: github.com
forgeType: github
remote: origin
branch: master
requireChecks: true
requireApproval: false
defaultReviewers: []
mergeMethod: rebase
mergeQueue: false
prTemplateType: stack
forceFetchTags: false
showPrTitlesInStack: false
branchPushIndividually: false
7 changes: 5 additions & 2 deletions cmd/reword/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ import (
"bufio"
"fmt"
"os"
"regexp"
"strings"

"github.com/ejoffe/spr/config"
"github.com/ejoffe/spr/git/realgit"
"github.com/google/uuid"
)

var commitIDRegex = regexp.MustCompile(`commit-id\:\s*([a-f0-9]{8})`)

func main() {
filename := os.Args[1]
gitcmd := realgit.NewGitCmd(config.DefaultConfig())
Expand All @@ -35,7 +38,7 @@ func main() {
res := strings.Split(line, " ")
var out string
gitcmd.Git("log --format=%B -n 1 "+res[1], &out)
if !strings.Contains(out, "commit-id") {
if !commitIDRegex.MatchString(out) {
line = strings.Replace(line, "pick ", "reword ", 1)
}
}
Expand Down Expand Up @@ -69,7 +72,7 @@ func shouldAppendCommitID(filename string) (missingCommitID bool, missingNewLine
if !strings.HasPrefix(line, "#") {
lineCount += 1
}
if strings.HasPrefix(line, "commit-id:") {
if commitIDRegex.MatchString(line) {
missingCommitID = false
return
}
Expand Down
55 changes: 53 additions & 2 deletions cmd/spr/main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package main

import (
"bufio"
"context"
"fmt"
"os"
"strings"

"github.com/ejoffe/rake"
"github.com/ejoffe/spr/config"
"github.com/ejoffe/spr/config/config_parser"
"github.com/ejoffe/spr/forge"
"github.com/ejoffe/spr/git/realgit"
"github.com/ejoffe/spr/github/githubclient"
"github.com/ejoffe/spr/gitlab/gitlabclient"
"github.com/ejoffe/spr/spr"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -49,7 +53,49 @@ func main() {
gitcmd = realgit.NewGitCmd(cfg)

ctx := context.Background()
client := githubclient.NewGitHubClient(ctx, cfg)

forgeType := strings.ToLower(cfg.Repo.ForgeType)
if forgeType == "" {
host := strings.ToLower(cfg.Repo.ForgeHost)
switch {
case strings.Contains(host, "github"):
forgeType = "github"
case strings.Contains(host, "gitlab"):
forgeType = "gitlab"
default:
fmt.Printf("Unable to detect forge type from host %q.\n", cfg.Repo.ForgeHost)
fmt.Println("Please select your forge:")
fmt.Println(" 1. GitHub")
fmt.Println(" 2. GitLab")
fmt.Print("Choice [1/2]: ")
reader := bufio.NewReader(os.Stdin)
line, _ := reader.ReadString('\n')
line = strings.TrimSpace(line)
switch line {
case "1":
forgeType = "github"
case "2":
forgeType = "gitlab"
default:
fmt.Println("Invalid choice.")
os.Exit(2)
}
}
cfg.Repo.ForgeType = forgeType
rake.LoadSources(cfg.Repo,
rake.YamlFileWriter(config_parser.RepoConfigFilePath(gitcmd)))
}

var client forge.ForgeInterface
switch forgeType {
case "github":
client = githubclient.NewGitHubClient(ctx, cfg)
case "gitlab":
client = gitlabclient.NewGitLabClient(ctx, cfg)
default:
fmt.Printf("Unknown forge type %q. Valid values: github, gitlab.\n", forgeType)
os.Exit(2)
}
stackedpr := spr.NewStackedPR(cfg, client, gitcmd)

detailFlag := &cli.BoolFlag{
Expand Down Expand Up @@ -122,7 +168,12 @@ VERSION: fork of {{.Version}}
cfg.User.LogGitCommands = true
cfg.User.LogGitHubCalls = true
}
client.MaybeStar(ctx, cfg)
type stargazer interface {
MaybeStar(ctx context.Context, cfg *config.Config)
}
if s, ok := client.(stargazer); ok {
s.MaybeStar(ctx, cfg)
}
return nil
},
Commands: []*cli.Command{
Expand Down
33 changes: 19 additions & 14 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"strings"

"github.com/ejoffe/rake"
"github.com/ejoffe/spr/github/githubclient/gen/genclient"
)

type Config struct {
Expand All @@ -16,12 +15,13 @@ type Config struct {

// Config object to hold spr configuration
type RepoConfig struct {
GitHubRepoOwner string `yaml:"githubRepoOwner"`
GitHubRepoName string `yaml:"githubRepoName"`
GitHubHost string `default:"github.com" yaml:"githubHost"`
RepoOwner string `yaml:"repoOwner"`
RepoName string `yaml:"repoName"`
ForgeHost string `yaml:"forgeHost"`
ForgeType string `yaml:"forgeType,omitempty"`

GitHubRemote string `default:"origin" yaml:"githubRemote"`
GitHubBranch string `default:"main" yaml:"githubBranch"`
Remote string `default:"origin" yaml:"remote"`
Branch string `default:"main" yaml:"branch"`

RequireChecks bool `default:"true" yaml:"requireChecks"`
RequireApproval bool `default:"true" yaml:"requireApproval"`
Expand Down Expand Up @@ -99,21 +99,26 @@ func (c *Config) Normalize() {
}
}

func (c Config) MergeMethod() (genclient.PullRequestMergeMethod, error) {
var mergeMethod genclient.PullRequestMergeMethod
var err error
type MergeMethod string

const (
MergeMethodMerge MergeMethod = "merge"
MergeMethodSquash MergeMethod = "squash"
MergeMethodRebase MergeMethod = "rebase"
)

func (c Config) ParseMergeMethod() (MergeMethod, error) {
switch strings.ToLower(c.Repo.MergeMethod) {
case "merge":
mergeMethod = genclient.PullRequestMergeMethod_MERGE
return MergeMethodMerge, nil
case "squash":
mergeMethod = genclient.PullRequestMergeMethod_SQUASH
return MergeMethodSquash, nil
case "rebase", "":
mergeMethod = genclient.PullRequestMergeMethod_REBASE
return MergeMethodRebase, nil
default:
err = fmt.Errorf(
return "", fmt.Errorf(
`unknown merge method %q, choose from "merge", "squash", or "rebase"`,
c.Repo.MergeMethod,
)
}
return mergeMethod, err
}
69 changes: 64 additions & 5 deletions config/config_parser/config_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,86 @@ import (
"github.com/ejoffe/rake"
"github.com/ejoffe/spr/config"
"github.com/ejoffe/spr/git"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v3"
)

// migrateRepoConfigKeys migrates old GitHub-specific YAML keys to
// forge-agnostic names in the given .spr.yml file. If the file contains
// any legacy keys they are renamed in place and the file is rewritten.
func migrateRepoConfigKeys(cfgPath string) {
data, err := os.ReadFile(cfgPath)
if err != nil {
return // file doesn't exist yet or is unreadable; nothing to migrate
}

var raw yaml.Node
if err := yaml.Unmarshal(data, &raw); err != nil {
return
}

renames := map[string]string{
"githubRepoOwner": "repoOwner",
"githubRepoName": "repoName",
"githubHost": "forgeHost",
"githubRemote": "remote",
"githubBranch": "branch",
}

// The top-level node is a document; its first child is the mapping.
if raw.Kind != yaml.DocumentNode || len(raw.Content) == 0 {
return
}
mapping := raw.Content[0]
if mapping.Kind != yaml.MappingNode {
return
}

migrated := false
for i := 0; i < len(mapping.Content)-1; i += 2 {
keyNode := mapping.Content[i]
if newKey, ok := renames[keyNode.Value]; ok {
keyNode.Value = newKey
migrated = true
}
}

if !migrated {
return
}

out, err := yaml.Marshal(&raw)
if err != nil {
log.Warn().Err(err).Msg("failed to marshal migrated config")
return
}
if err := os.WriteFile(cfgPath, out, 0644); err != nil {
log.Warn().Err(err).Msg("failed to write migrated config")
}
}

func ParseConfig(gitcmd git.GitInterface) *config.Config {
cfg := config.EmptyConfig()

// Migrate legacy GitHub-specific config keys before loading.
migrateRepoConfigKeys(RepoConfigFilePath(gitcmd))

rake.LoadSources(cfg.Repo,
rake.DefaultSource(),
NewGitHubRemoteSource(cfg, gitcmd),
NewRemoteSource(cfg, gitcmd),
rake.YamlFileSource(RepoConfigFilePath(gitcmd)),
NewRemoteBranchSource(gitcmd),
)
if cfg.Repo.GitHubHost == "" {
if cfg.Repo.ForgeHost == "" {
fmt.Println("unable to auto configure repository host - must be set manually in .spr.yml")
os.Exit(2)
}
if cfg.Repo.GitHubRepoOwner == "" {
if cfg.Repo.RepoOwner == "" {
fmt.Println("unable to auto configure repository owner - must be set manually in .spr.yml")
os.Exit(3)
}

if cfg.Repo.GitHubRepoName == "" {
if cfg.Repo.RepoName == "" {
fmt.Println("unable to auto configure repository name - must be set manually in .spr.yml")
os.Exit(4)
}
Expand Down Expand Up @@ -69,7 +128,7 @@ func ParseConfig(gitcmd git.GitInterface) *config.Config {
}

func CheckConfig(cfg *config.Config) error {
if strings.Contains(cfg.Repo.GitHubBranch, "/") {
if strings.Contains(cfg.Repo.Branch, "/") {
return errors.New("Remote branch name must not contain backslashes '/'")
}
return nil
Expand Down
32 changes: 19 additions & 13 deletions config/config_parser/config_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (

func TestGetRepoDetailsFromRemote(t *testing.T) {
type testCase struct {
remote string
githubHost string
repoOwner string
repoName string
match bool
remote string
forgeHost string
repoOwner string
repoName string
match bool
}
testCases := []testCase{
{"origin https://github.com/r2/d2.git (push)", "github.com", "r2", "d2", true},
Expand Down Expand Up @@ -44,12 +44,18 @@ func TestGetRepoDetailsFromRemote(t *testing.T) {

// GitHub names are case-sensitive
{"origin https://github.com/R2/D2.git (push)", "github.com", "R2", "D2", true},

// GitLab nested subgroups (multi-level paths)
{"origin https://gitlab.cfdata.org/cloudflare/rt/mobile/core.git (push)", "gitlab.cfdata.org", "cloudflare/rt/mobile", "core", true},
{"origin git@gitlab.cfdata.org:cloudflare/rt/mobile/core.git (push)", "gitlab.cfdata.org", "cloudflare/rt/mobile", "core", true},
{"origin ssh://git@gitlab.cfdata.org/cloudflare/rt/mobile/core.git (push)", "gitlab.cfdata.org", "cloudflare/rt/mobile", "core", true},
{"origin https://gitlab.cfdata.org/cloudflare/rt/mobile/core (push)", "gitlab.cfdata.org", "cloudflare/rt/mobile", "core", true},
}
for i, testCase := range testCases {
t.Logf("Testing %v %q", i, testCase.remote)
githubHost, repoOwner, repoName, match := getRepoDetailsFromRemote(testCase.remote)
if githubHost != testCase.githubHost {
t.Fatalf("Wrong \"githubHost\" returned for test case %v, expected %q, got %q", i, testCase.githubHost, githubHost)
forgeHost, repoOwner, repoName, match := getRepoDetailsFromRemote(testCase.remote)
if forgeHost != testCase.forgeHost {
t.Fatalf("Wrong \"forgeHost\" returned for test case %v, expected %q, got %q", i, testCase.forgeHost, forgeHost)
}
if repoOwner != testCase.repoOwner {
t.Fatalf("Wrong \"repoOwner\" returned for test case %v, expected %q, got %q", i, testCase.repoOwner, repoOwner)
Expand All @@ -63,15 +69,15 @@ func TestGetRepoDetailsFromRemote(t *testing.T) {
}
}

func TestGitHubRemoteSource(t *testing.T) {
func TestRemoteSource(t *testing.T) {
mock := mockgit.NewMockGit(t)
mock.ExpectRemote("https://github.com/r2/d2.git")

expect := config.Config{
Repo: &config.RepoConfig{
GitHubRepoOwner: "r2",
GitHubRepoName: "d2",
GitHubHost: "github.com",
RepoOwner: "r2",
RepoName: "d2",
ForgeHost: "github.com",
RequireChecks: false,
RequireApproval: false,
MergeMethod: "",
Expand All @@ -88,7 +94,7 @@ func TestGitHubRemoteSource(t *testing.T) {
Repo: &config.RepoConfig{},
User: &config.UserConfig{},
}
source := NewGitHubRemoteSource(&actual, mock)
source := NewRemoteSource(&actual, mock)
source.Load(nil)
assert.Equal(t, expect, actual)
mock.ExpectationsMet()
Expand Down
4 changes: 2 additions & 2 deletions config/config_parser/remote_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ func (s *remoteBranch) Load(cfg interface{}) {

repoCfg := cfg.(*config.RepoConfig)

repoCfg.GitHubRemote = matches[2]
repoCfg.GitHubBranch = matches[3]
repoCfg.Remote = matches[2]
repoCfg.Branch = matches[3]
}
Loading