Skip to content

Commit 972a28e

Browse files
committed
feat: archive and unarchive projects
1 parent 453b41f commit 972a28e

4 files changed

Lines changed: 200 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ Usage:
179179
glabs [command]
180180
181181
Available Commands:
182+
archive Archive or unarchive repositories.
182183
check check course config
183184
clone Clone repositories.
184185
completion Generate the autocompletion script for the specified shell

cmd/archive.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/logrusorgru/aurora/v4"
7+
"github.com/obcode/glabs/config"
8+
"github.com/obcode/glabs/gitlab"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
func init() {
13+
rootCmd.AddCommand(archiveCmd)
14+
archiveCmd.Flags().BoolVarP(&unarchive, "unarchive", "u", false, "unarchive project")
15+
}
16+
17+
var (
18+
archiveCmd = &cobra.Command{
19+
Use: "archive course assignment [groups...|students...]",
20+
Short: "Archive or unarchive repositories.",
21+
Long: `Archive or unarchive repositories.`,
22+
Args: cobra.MinimumNArgs(2), //nolint:gomnd
23+
Run: func(cmd *cobra.Command, args []string) {
24+
assignmentConfig := config.GetAssignmentConfig(args[0], args[1], args[2:]...)
25+
assignmentConfig.Show()
26+
fmt.Println(aurora.Magenta("Config okay? Press 'Enter' to continue or 'Ctrl-C' to stop ..."))
27+
fmt.Scanln() //nolint:errcheck
28+
c := gitlab.NewClient()
29+
c.Archive(assignmentConfig, unarchive)
30+
},
31+
}
32+
unarchive bool
33+
)

gitlab/archive.go

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
package gitlab
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"time"
7+
8+
"github.com/logrusorgru/aurora"
9+
"github.com/obcode/glabs/config"
10+
"github.com/rs/zerolog/log"
11+
"github.com/theckman/yacspin"
12+
"github.com/xanzy/go-gitlab"
13+
)
14+
15+
func (c *Client) Archive(assignmentCfg *config.AssignmentConfig, unarchive bool) {
16+
_, err := c.getGroupID(assignmentCfg)
17+
if err != nil {
18+
fmt.Printf("error: GitLab group for assignment does not exist, please create the group %s\n", assignmentCfg.URL)
19+
os.Exit(1)
20+
}
21+
22+
switch per := assignmentCfg.Per; per {
23+
case config.PerGroup:
24+
c.archivePerGroup(assignmentCfg, unarchive)
25+
case config.PerStudent:
26+
c.archivePerStudent(assignmentCfg, unarchive)
27+
default:
28+
fmt.Printf("it is only possible to set access levels for students oder groups, not for %v", per)
29+
os.Exit(1)
30+
}
31+
}
32+
33+
func (c *Client) archivePerStudent(assignmentCfg *config.AssignmentConfig, unarchive bool) {
34+
if len(assignmentCfg.Students) == 0 {
35+
fmt.Println("no students in config for assignment found")
36+
return
37+
}
38+
39+
for _, student := range assignmentCfg.Students {
40+
name := assignmentCfg.Name + "-" + assignmentCfg.RepoSuffix(student)
41+
projectname := fmt.Sprintf("%s/%s", assignmentCfg.Path, name)
42+
project, _, err := c.Projects.GetProject(
43+
projectname,
44+
&gitlab.GetProjectOptions{},
45+
)
46+
if err != nil {
47+
fmt.Printf("cannot archive project %s failed with %s", projectname, err)
48+
return
49+
}
50+
if err := c.archive(assignmentCfg, project, true, unarchive); err != nil {
51+
log.Error().Err(err).Str("group", assignmentCfg.Course).Msg("cannot archive project")
52+
}
53+
}
54+
}
55+
56+
func (c *Client) archivePerGroup(assignmentCfg *config.AssignmentConfig, unarchive bool) {
57+
if len(assignmentCfg.Groups) == 0 {
58+
log.Info().Str("group", assignmentCfg.Course).Msg("no groups found")
59+
return
60+
}
61+
62+
for _, grp := range assignmentCfg.Groups {
63+
projectname := fmt.Sprintf("%s/%s-%s", assignmentCfg.Path, assignmentCfg.Name, grp.Name)
64+
project, _, err := c.Projects.GetProject(
65+
projectname,
66+
&gitlab.GetProjectOptions{},
67+
)
68+
if err != nil {
69+
fmt.Printf("cannot archive project %s failed with %s", projectname, err)
70+
return
71+
}
72+
if err := c.archive(assignmentCfg, project, true, unarchive); err != nil {
73+
log.Error().Err(err).Str("group", assignmentCfg.Course).Msg("cannot archive project")
74+
}
75+
}
76+
}
77+
78+
func (c *Client) archive(assignmentCfg *config.AssignmentConfig, project *gitlab.Project, spin bool, unarchive bool) error {
79+
// var cfg yacspin.Config
80+
var spinner *yacspin.Spinner
81+
if spin {
82+
un := ""
83+
if unarchive {
84+
un = "un"
85+
}
86+
cfg := yacspin.Config{
87+
Frequency: 100 * time.Millisecond,
88+
CharSet: yacspin.CharSets[69],
89+
Suffix: aurora.Sprintf(aurora.Cyan(" %sarchiving project %s at %s"),
90+
un,
91+
aurora.Yellow(project.Name),
92+
aurora.Magenta(assignmentCfg.URL+"/"+project.Name),
93+
),
94+
SuffixAutoColon: true,
95+
StopCharacter: "✓",
96+
StopColors: []string{"fgGreen"},
97+
StopFailMessage: "error",
98+
StopFailCharacter: "✗",
99+
StopFailColors: []string{"fgRed"},
100+
}
101+
var err error
102+
spinner, err = yacspin.New(cfg)
103+
if err != nil {
104+
log.Debug().Err(err).Msg("cannot create spinner")
105+
}
106+
err = spinner.Start()
107+
if err != nil {
108+
log.Debug().Err(err).Msg("cannot start spinner")
109+
}
110+
}
111+
112+
log.Debug().
113+
Str("name", project.Name).
114+
Str("toURL", project.SSHURLToRepo).
115+
Str("branch", assignmentCfg.Startercode.ToBranch).
116+
Msg("protecting branch")
117+
118+
var err error
119+
if unarchive {
120+
_, _, err = c.Projects.UnarchiveProject(project.ID)
121+
if err != nil {
122+
log.Debug().Err(err).
123+
Str("name", project.Name).
124+
Str("toURL", project.SSHURLToRepo).
125+
Msg("cannot unarchive project")
126+
127+
if spin {
128+
err := spinner.StopFail()
129+
if err != nil {
130+
log.Debug().Err(err).Msg("cannot stop spinner")
131+
}
132+
}
133+
return fmt.Errorf("error while trying to unarchive project: %w", err)
134+
}
135+
} else {
136+
_, _, err = c.Projects.ArchiveProject(project.ID)
137+
if err != nil {
138+
log.Debug().Err(err).
139+
Str("name", project.Name).
140+
Str("toURL", project.SSHURLToRepo).
141+
Msg("cannot archive project")
142+
143+
if spin {
144+
err := spinner.StopFail()
145+
if err != nil {
146+
log.Debug().Err(err).Msg("cannot stop spinner")
147+
}
148+
}
149+
return fmt.Errorf("error while trying to archive project: %w", err)
150+
}
151+
}
152+
153+
if spin {
154+
spinner.StopMessage(aurora.Sprintf(aurora.Green("ok")))
155+
err = spinner.Stop()
156+
if err != nil {
157+
log.Debug().Err(err).Msg("cannot stop spinner")
158+
}
159+
}
160+
161+
return nil
162+
}

gitlab/report.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ func (c *Client) Report(assignmentCfg *config.AssignmentConfig, templateFile *st
2929
}
3030

3131
if output != nil {
32-
os.Remove(*output)
32+
os.Remove(*output) //nolint
3333
f, err := os.Create(*output)
3434
if err != nil {
3535
panic(err)
3636
}
37-
defer f.Close()
37+
defer f.Close() //nolint
3838
err = tmpl.Execute(f, report)
3939
if err != nil {
4040
panic(err)
@@ -65,12 +65,12 @@ func (c *Client) ReportHTML(assignmentCfg *config.AssignmentConfig, templateFile
6565
}
6666

6767
if output != nil {
68-
os.Remove(*output)
68+
os.Remove(*output) //nolint
6969
f, err := os.Create(*output)
7070
if err != nil {
7171
panic(err)
7272
}
73-
defer f.Close()
73+
defer f.Close() //nolint
7474
err = tmpl.Execute(f, report)
7575
if err != nil {
7676
panic(err)

0 commit comments

Comments
 (0)