Skip to content

Commit

Permalink
✨ push additional branches, closes #29
Browse files Browse the repository at this point in the history
  • Loading branch information
obcode committed Mar 8, 2023
1 parent 7aa110e commit c87de5b
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
repos:
- repo: https://github.com/golangci/golangci-lint.git
rev: v1.45.2
rev: v1.51.2
hooks:
- id: golangci-lint
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ Contents:
name: <name of the author used for commit>
email: <email of the author used for commit>
toBranch: <branch to commit to> # default main
additionalBranches: # push more than one branch to new repos
<array of branch names>
signKey: <plaintext private key for signing commits> # Optional key for signing the commit. If the key is encrypted the password will be requested on running the tool.
protectToBranch: <false|true> # whether only maintainer can push, default false
clone:
Expand Down Expand Up @@ -127,6 +129,10 @@ algdati:
url: [email protected]:algdati/startercode/startercodeBlatt1.git
fromBranch: ws20
protectToBranch: true
additionalBranches:
- ss20
- 22ss
- main
# accesslevel: developer # default
clone:
localpath: /tmp
Expand Down
16 changes: 11 additions & 5 deletions config/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,18 @@ func startercode(assignmentKey string) *Startercode {
devBranch = dB
}

additionalBranches := []string{}
if addB := viper.GetStringSlice(assignmentKey + ".startercode.additionalBranches"); len(addB) > 0 {
additionalBranches = addB
}

return &Startercode{
URL: url,
FromBranch: fromBranch,
ToBranch: toBranch,
DevBranch: devBranch,
ProtectToBranch: viper.GetBool(assignmentKey + ".startercode.protectToBranch"),
URL: url,
FromBranch: fromBranch,
ToBranch: toBranch,
DevBranch: devBranch,
AdditionalBranches: additionalBranches,
ProtectToBranch: viper.GetBool(assignmentKey + ".startercode.protectToBranch"),
}
}

Expand Down
12 changes: 7 additions & 5 deletions config/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ func (cfg *AssignmentConfig) Show() {
startercode := aurora.Sprintf(aurora.Red("not defined"))
if cfg.Startercode != nil {
startercode = aurora.Sprintf(aurora.Cyan(`
URL: %s
FromBranch: %s
ToBranch: %s
DevBranch: %s
ProtectToBranch: %t`),
URL: %s
FromBranch: %s
ToBranch: %s
DevBranch: %s
AdditionalBranches: %s
ProtectToBranch: %t`),
aurora.Yellow(cfg.Startercode.URL),
aurora.Yellow(cfg.Startercode.FromBranch),
aurora.Yellow(cfg.Startercode.ToBranch),
aurora.Yellow(cfg.Startercode.DevBranch),
aurora.Yellow(cfg.Startercode.AdditionalBranches),
aurora.Yellow(cfg.Startercode.ProtectToBranch),
)
}
Expand Down
11 changes: 6 additions & 5 deletions config/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ type Seeder struct {
}

type Startercode struct {
URL string
FromBranch string
ToBranch string
DevBranch string
ProtectToBranch bool
URL string
FromBranch string
ToBranch string
DevBranch string
AdditionalBranches []string
ProtectToBranch bool
}

type Clone struct {
Expand Down
19 changes: 10 additions & 9 deletions gitlab/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ import (
func (c *Client) generateProject(assignmentCfg *config.AssignmentConfig, name string, inID int) (*gitlab.Project, bool, error) {
generated := false
p := &gitlab.CreateProjectOptions{
Name: gitlab.String(name),
Description: gitlab.String(assignmentCfg.Description),
NamespaceID: gitlab.Int(inID),
MergeRequestsAccessLevel: gitlab.AccessControl("enabled"),
IssuesAccessLevel: gitlab.AccessControl("enabled"),
BuildsAccessLevel: gitlab.AccessControl("enabled"),
JobsEnabled: gitlab.Bool(true),
Visibility: gitlab.Visibility(gitlab.PrivateVisibility),
ContainerRegistryEnabled: gitlab.Bool(assignmentCfg.ContainerRegistry),
Name: gitlab.String(name),
Description: gitlab.String(assignmentCfg.Description),
NamespaceID: gitlab.Int(inID),
MergeRequestsAccessLevel: gitlab.AccessControl("enabled"),
IssuesAccessLevel: gitlab.AccessControl("enabled"),
BuildsAccessLevel: gitlab.AccessControl("enabled"),
JobsEnabled: gitlab.Bool(true),
Visibility: gitlab.Visibility(gitlab.PrivateVisibility),
ContainerRegistryEnabled: gitlab.Bool(assignmentCfg.ContainerRegistry),
OnlyAllowMergeIfAllStatusChecksPassed: gitlab.Bool(false),
}

project, _, err := c.Projects.CreateProject(p)
Expand Down
5 changes: 2 additions & 3 deletions gitlab/protect.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import (

"github.com/logrusorgru/aurora"
"github.com/obcode/glabs/config"
cfg "github.com/obcode/glabs/config"
"github.com/rs/zerolog/log"
"github.com/theckman/yacspin"
"github.com/xanzy/go-gitlab"
)

func (c *Client) ProtectToBranch(assignmentCfg *cfg.AssignmentConfig) {
func (c *Client) ProtectToBranch(assignmentCfg *config.AssignmentConfig) {
assignmentGitLabGroupID, err := c.getGroupID(assignmentCfg)
if err != nil {
fmt.Printf("error: GitLab group for assignment does not exist, please create the group %s\n", assignmentCfg.URL)
Expand All @@ -31,7 +30,7 @@ func (c *Client) ProtectToBranch(assignmentCfg *cfg.AssignmentConfig) {
}
}

func (c *Client) protectBranch(assignmentCfg *cfg.AssignmentConfig, project *gitlab.Project, spin bool) error {
func (c *Client) protectBranch(assignmentCfg *config.AssignmentConfig, project *gitlab.Project, spin bool) error {
if assignmentCfg.Startercode.ProtectToBranch {
// var cfg yacspin.Config
var spinner *yacspin.Spinner
Expand Down
42 changes: 40 additions & 2 deletions gitlab/starterrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ func (c *Client) pushStartercode(assignmentCfg *cfg.AssignmentConfig, from *g.St
return fmt.Errorf("cannot create remote: %w", err)
}

refSpec := config.RefSpec("refs/heads/" + assignmentCfg.Startercode.FromBranch +
":refs/heads/" + assignmentCfg.Startercode.ToBranch)
refSpec := config.RefSpec(
fmt.Sprintf("+refs/heads/%s:refs/heads/%s",
assignmentCfg.Startercode.FromBranch,
assignmentCfg.Startercode.ToBranch),
)

log.Debug().
Str("refSpec", string(refSpec)).
Expand Down Expand Up @@ -66,6 +69,41 @@ func (c *Client) pushStartercode(assignmentCfg *cfg.AssignmentConfig, from *g.St
}
}

for _, additionalBranch := range assignmentCfg.Startercode.AdditionalBranches {
log.Debug().Str("branch", additionalBranch).Msg("pushing additional branch")

// worktree, err := from.Repo.Worktree()
// if err != nil {
// log.Debug().Err(err).
// Str("branch", additionalBranch).
// Str("name", project.Name).Str("url", project.SSHURLToRepo).
// Msg("cannot get worktree")
// return fmt.Errorf("cannot get worktree: %w", err)
// }

// worktree.Checkout(&git.CheckoutOptions{
// Branch: plumbing.ReferenceName(additionalBranch),
// })

refSpec := config.RefSpec(fmt.Sprintf("+refs/remotes/origin/%s:refs/heads/%s", additionalBranch, additionalBranch))

pushOpts := &git.PushOptions{
RemoteName: remote.Config().Name,
RefSpecs: []config.RefSpec{refSpec},
Auth: from.Auth,
}
err = from.Repo.Push(pushOpts)
if err != nil {
log.Debug().Err(err).
Str("branch", additionalBranch).
Str("refspec", refSpec.String()).
Str("name", project.Name).Str("url", project.SSHURLToRepo).
Msg("cannot push to remote")
return fmt.Errorf("cannot push to remote: %w", err)
}

}

return nil
}

Expand Down

0 comments on commit c87de5b

Please sign in to comment.