Skip to content

Commit

Permalink
add update command
Browse files Browse the repository at this point in the history
  • Loading branch information
obcode committed Sep 11, 2023
1 parent 9259459 commit 6925a7a
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 1 deletion.
33 changes: 33 additions & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cmd

import (
"fmt"

"github.com/logrusorgru/aurora/v4"
"github.com/obcode/glabs/config"
"github.com/obcode/glabs/gitlab"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(updateCmd)
}

var updateCmd = &cobra.Command{
Use: "update course assignment [groups...|students...]",
Short: "Update repositories with code.",
Long: `Update repositories with code from the startercode repo.
USE WITH CARE!
This can result in merge conflicts, which cannot be handled
Use only with fresh, i.e. untouched, repositories.`,
Args: cobra.MinimumNArgs(2), //nolint:gomnd
Run: func(cmd *cobra.Command, args []string) {
assignmentConfig := config.GetAssignmentConfig(args[0], args[1], args[2:]...)
assignmentConfig.Show()
fmt.Println(aurora.Red("Heads up! Use only with untouched projects."))
fmt.Println(aurora.Magenta("Config okay? Press 'Enter' to continue or 'Ctrl-C' to stop ..."))
fmt.Scanln()
c := gitlab.NewClient()
c.Update(assignmentConfig)
},
}
2 changes: 1 addition & 1 deletion gitlab/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (c *Client) generate(assignmentCfg *config.AssignmentConfig, assignmentGrou
func (c *Client) generatePerStudent(assignmentCfg *config.AssignmentConfig, assignmentGroupID int,
starterrepo *git.Starterrepo) {
if len(assignmentCfg.Students) == 0 {
fmt.Println("no students in config for assignment found")
log.Info().Str("group", assignmentCfg.Course).Msg("no students found")
return
}

Expand Down
133 changes: 133 additions & 0 deletions gitlab/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package gitlab

import (
"fmt"
"os"
"time"

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

func (c *Client) Update(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)
os.Exit(1)
}

var starterrepo *git.Starterrepo

if assignmentCfg.Startercode != nil {
starterrepo, err = git.PrepareStartercodeRepo(assignmentCfg)

if err != nil {
fmt.Println(err)
os.Exit(1)
}
}

switch per := assignmentCfg.Per; per {
case config.PerGroup:
c.updatePerGroup(assignmentCfg, assignmentGitLabGroupID, starterrepo)
case config.PerStudent:
c.updatePerStudent(assignmentCfg, assignmentGitLabGroupID, starterrepo)
default:
fmt.Printf("it is only possible to update for students oder groups, not for %v", per)
os.Exit(1)
}
}

func (c *Client) update(assignmentCfg *config.AssignmentConfig, project *gitlab.Project, starterrepo *git.Starterrepo) {

cfg := yacspin.Config{
Frequency: 100 * time.Millisecond,
CharSet: yacspin.CharSets[69],
Suffix: aurora.Sprintf(aurora.Cyan(" updating project %s at %s"),
aurora.Yellow(project.Name),
aurora.Magenta(assignmentCfg.URL+"/"+project.Name),
),
SuffixAutoColon: true,
StopCharacter: "✓",
StopColors: []string{"fgGreen"},
StopFailMessage: "error",
StopFailCharacter: "✗",
StopFailColors: []string{"fgRed"},
}

spinner, err := yacspin.New(cfg)
if err != nil {
log.Debug().Err(err).Msg("cannot create spinner")
}
err = spinner.Start()
if err != nil {
log.Debug().Err(err).Msg("cannot start spinner")
}

if starterrepo != nil {
cfg.Suffix = aurora.Sprintf(aurora.Cyan(" ↪ pushing updates from startercode"))

err = c.pushStartercode(assignmentCfg, starterrepo, project)
if err != nil {
spinner.StopFailMessage(fmt.Sprintf("problem: %v", err))

err := spinner.StopFail()
if err != nil {
log.Debug().Err(err).Msg("cannot stop spinner")
}
return
}

err = spinner.Stop()
if err != nil {
log.Debug().Err(err).Msg("cannot stop spinner")
}
}
}

func (c *Client) updatePerStudent(assignmentCfg *config.AssignmentConfig, assignmentGroupID int,
starterrepo *git.Starterrepo) {
if len(assignmentCfg.Students) == 0 {
log.Info().Str("group", assignmentCfg.Course).Msg("no students found")
return
}

for _, student := range assignmentCfg.Students {
name := assignmentCfg.Name + "-" + assignmentCfg.RepoSuffix(student)
projectname := fmt.Sprintf("%s/%s", assignmentCfg.Path, name)
project, _, err := c.Projects.GetProject(
projectname,
&gitlab.GetProjectOptions{},
)
if err != nil {
fmt.Printf("cannot set access for project %s failed with %s", projectname, err)
return
}
c.update(assignmentCfg, project, starterrepo)
}
}

func (c *Client) updatePerGroup(assignmentCfg *config.AssignmentConfig, assignmentGroupID int,
starterrepo *git.Starterrepo) {
if len(assignmentCfg.Groups) == 0 {
log.Info().Str("group", assignmentCfg.Course).Msg("no groups found")
return
}

for _, grp := range assignmentCfg.Groups {
projectname := fmt.Sprintf("%s/%s-%s", assignmentCfg.Path, assignmentCfg.Name, grp.Name)
project, _, err := c.Projects.GetProject(
projectname,
&gitlab.GetProjectOptions{},
)
if err != nil {
fmt.Printf("cannot set access for project %s failed with %s", projectname, err)
return
}
c.update(assignmentCfg, project, starterrepo)
}
}

0 comments on commit 6925a7a

Please sign in to comment.