@@ -6,51 +6,78 @@ import (
66
77 "github.com/obcode/glabs/config"
88 "github.com/rs/zerolog/log"
9+ "github.com/xanzy/go-gitlab"
910)
1011
11- func (c * Client ) getGroupID ( assignmentCfg * config. AssignmentConfig ) (int , error ) {
12- pathParts := strings .Split (assignmentCfg . Path , "/" )
13- groups , _ , err := c . Groups . SearchGroup ( pathParts [len (pathParts )- 1 ])
12+ func (c * Client ) getGroupIDByFullPath ( fullPath string ) (int , error ) {
13+ pathParts := strings .Split (fullPath , "/" )
14+ searchTerm := pathParts [len (pathParts )- 1 ]
1415
16+ groups , _ , err := c .Groups .SearchGroup (searchTerm )
1517 if err != nil {
1618 log .Error ().Err (err ).
17- Str ("course" , assignmentCfg .Course ).
18- Str ("assignmentpath" , assignmentCfg .Path ).
19- Msg ("error while searching id of assignmentPath" )
19+ Str ("grouppath" , fullPath ).
20+ Msg ("error while searching id of group path" )
2021 return 0 , err
2122 }
2223
23- if len (groups ) == 0 {
24- log .Debug ().Str ("group" , assignmentCfg .Course ).
24+ for _ , group := range groups {
25+ if group .FullPath == fullPath {
26+ return group .ID , nil
27+ }
28+ }
29+
30+ return 0 , fmt .Errorf ("no gitlab group found for path %s" , fullPath )
31+ }
32+
33+ func (c * Client ) getGroupID (assignmentCfg * config.AssignmentConfig ) (int , error ) {
34+ assignmentGroupID , err := c .getGroupIDByFullPath (assignmentCfg .Path )
35+ if err != nil {
36+ log .Debug ().Err (err ).
37+ Str ("course" , assignmentCfg .Course ).
2538 Str ("assignmentpath" , assignmentCfg .Path ).
26- Msg ("no group found " )
27- return 0 , fmt . Errorf ( "no gitlab group found for assignmentpath %s" , assignmentCfg . Path )
39+ Msg ("error while searching id of assignment path " )
40+ return 0 , err
2841 }
2942
30- log .Debug ().Str ("assignmentpath" , assignmentCfg .Path ).Msg ("searching id of gitlab group" )
43+ return assignmentGroupID , nil
44+ }
3145
32- // semesterpathID := 0
33- assignmentGroupID := 0
46+ func (c * Client ) createGroup (assignmentCfg * config.AssignmentConfig ) (int , error ) {
47+ pathParts := strings .Split (assignmentCfg .Path , "/" )
48+ path := pathParts [len (pathParts )- 1 ]
49+ name := pathParts [len (pathParts )- 1 ]
3450
35- for _ , group := range groups {
36- // if group.Path == semesterpath {
37- // log.Debug().Str("group.Path", group.Path).Msg("found semester group")
38- // semesterpathID = group.ID
39- // }
40- if group .FullPath == assignmentCfg .Path {
41- log .Debug ().Str ("group.FullPath" , group .FullPath ).Msg ("found assignment group" )
42- assignmentGroupID = group .ID
51+ var parentID * int
52+ if len (pathParts ) > 1 {
53+ parentPath := strings .Join (pathParts [:len (pathParts )- 1 ], "/" )
54+ resolvedParentID , err := c .getGroupIDByFullPath (parentPath )
55+ if err != nil {
56+ log .Error ().Err (err ).
57+ Str ("course" , assignmentCfg .Course ).
58+ Str ("parentpath" , parentPath ).
59+ Msg ("cannot resolve parent group for assignment" )
60+ return 0 , err
4361 }
62+ parentID = & resolvedParentID
4463 }
4564
46- if assignmentGroupID == 0 {
47- log .Info ().Msg ("creating assignment group" )
48- log .Error ().
49- Str ("course" , assignmentCfg .Course ).
50- Str ("assignmentpath" , assignmentCfg .Path ).
51- Msg ("please go to the gitlab website and create the subgroup with the assignment path" )
52- return 0 , fmt .Errorf ("please go to the gitlab website and create the subgroup with the assignment path" )
65+ fmt .Printf ("GitLab group for assignment does not exist, creating group %s at %s\n " , name , assignmentCfg .Path )
66+
67+ visibility := gitlab .InternalVisibility
68+ options := & gitlab.CreateGroupOptions {
69+ Name : & name ,
70+ Path : & path ,
71+ Visibility : & visibility ,
72+ ParentID : parentID ,
5373 }
5474
55- return assignmentGroupID , nil
75+ g , _ , err := c .Groups .CreateGroup (options )
76+ if err != nil {
77+ log .Error ().Err (err ).
78+ Str ("name" , name ).
79+ Str ("path" , path ).
80+ Msg ("cannot create group" )
81+ }
82+ return g .ID , nil
5683}
0 commit comments