Skip to content

Commit

Permalink
Merge branch 'release/v0.16.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
obcode committed Aug 10, 2022
2 parents 995669e + 397a828 commit f775b70
Show file tree
Hide file tree
Showing 13 changed files with 302 additions and 105 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.32
version: v1.45.0
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.32.1
rev: v1.45.2
hooks:
- id: golangci-lint
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ Available Commands:
delete Delete repositories.
generate Generate repositories.
help Help about any command
setaccess Set access level for exisiting repositories.
show Show config of an assignment
version Print the version number of Glabs
Expand Down
36 changes: 36 additions & 0 deletions cmd/setaccess.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cmd

import (
"fmt"

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

func init() {
rootCmd.AddCommand(setaccessCmd)
setaccessCmd.Flags().StringVarP(&Level, "level", "l", "", "accesslevel (overrides config file)")
}

var (
setaccessCmd = &cobra.Command{
Use: "setaccess course assignment [groups...|students...]",
Short: "Set access level for exisiting repositories.",
Long: `Set access level for exisiting repositories.`,
Args: cobra.MinimumNArgs(2), //nolint:gomnd
Run: func(cmd *cobra.Command, args []string) {
assignmentConfig := config.GetAssignmentConfig(args[0], args[1], args[2:]...)
if len(Level) > 0 {
assignmentConfig.SetAccessLevel(Level)
}
assignmentConfig.Show()
fmt.Println(aurora.Magenta("Config okay? Press 'Enter' to continue or 'Ctrl-C' to stop ..."))
fmt.Scanln()
c := gitlab.NewClient()
c.Setaccess(assignmentConfig)
},
}
Level string
)
14 changes: 14 additions & 0 deletions config/assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,20 @@ func description(assignmentKey string) string {
return description
}

func (cfg *AssignmentConfig) SetAccessLevel(level string) {
accesslevel := Developer
switch level {
case "guest":
accesslevel = Guest
case "reporter":
accesslevel = Reporter
case "maintainer":
accesslevel = Maintainer
}

cfg.AccessLevel = accesslevel
}

func accessLevel(assignmentKey string) AccessLevel {
accesslevelIdentifier := viper.GetString(assignmentKey + ".accesslevel")

Expand Down
2 changes: 1 addition & 1 deletion git/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"
"time"

"github.com/go-git/go-git/v5"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
"github.com/logrusorgru/aurora"
Expand Down
2 changes: 1 addition & 1 deletion git/starterrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"time"

"github.com/go-git/go-git/v5"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
"github.com/go-git/go-git/v5/storage/memory"
Expand Down
92 changes: 1 addition & 91 deletions gitlab/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package gitlab
import (
"fmt"
"os"
"strings"
"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) Generate(assignmentCfg *config.AssignmentConfig) {
Expand Down Expand Up @@ -155,95 +153,7 @@ func (c *Client) generate(assignmentCfg *config.AssignmentConfig, assignmentGrou
}
}
}

for _, student := range members {
cfg.Suffix = aurora.Sprintf(aurora.Cyan(" ↪ adding member %s to %s as %s"),
aurora.Yellow(student),
aurora.Magenta(projectname),
aurora.Magenta(assignmentCfg.AccessLevel.String()),
)
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")
}

userID, err := c.getUserID(student)
if err != nil {
if strings.Contains(student, "@") {
info, err := c.inviteByEmail(assignmentCfg, project.ID, student)
if err != nil {
spinner.StopFailMessage(fmt.Sprintf("%v", err))

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

spinner.StopMessage(aurora.Sprintf(aurora.Green(info)))
err = spinner.Stop()
if err != nil {
log.Debug().Err(err).Msg("cannot stop spinner")
}
}
continue
} else {
spinner.StopFailMessage(fmt.Sprintf("cannot get user id: %v", err))

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

info, err := c.addMember(assignmentCfg, project.ID, userID)
if err != nil {
log.Debug().Err(err).
Int("projectID", project.ID).
Int("userID", userID).
Str("student", student).
Str("course", assignmentCfg.Course).
Str("assignment", assignmentCfg.Name).
Msg("error while adding member")

spinner.StopFailMessage(fmt.Sprintf("cannot add user %s: %v", student, err))

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

spinner.StopMessage(aurora.Sprintf(aurora.Green(info)))
err = spinner.Stop()
if err != nil {
log.Debug().Err(err).Msg("cannot stop spinner")
}
}

}

func (c *Client) inviteByEmail(cfg *config.AssignmentConfig, projectID int, email string) (string, error) {

m := &gitlab.InvitesOptions{
Email: &email,
AccessLevel: gitlab.AccessLevel(gitlab.AccessLevelValue(cfg.AccessLevel)),
}
resp, _, err := c.Invites.ProjectInvites(projectID, m)
if err != nil {
return "", err
}
if resp.Status != "success" {
return "", fmt.Errorf("inviting user %s failed with %s", email, resp.Message[email])
}
return fmt.Sprintf("successfully invited user %s", email), nil
c.setaccess(assignmentCfg, project, members, &cfg)
}

func (c *Client) generatePerStudent(assignmentCfg *config.AssignmentConfig, assignmentGroupID int,
Expand Down
2 changes: 1 addition & 1 deletion gitlab/seeder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"
"time"

"github.com/go-git/go-git/v5"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
Expand Down
Loading

0 comments on commit f775b70

Please sign in to comment.