@@ -93,19 +93,84 @@ func (d DiggerEEController) GitlabWebHookHandler(c *gin.Context) {
93
93
}
94
94
case * gitlab.PushEvent :
95
95
log .Printf ("Got push event for %v %v" , event .Project .URL , event .Ref )
96
- // err := handlePushEvent(gh , event)
97
- // if err != nil {
98
- // log.Printf("handlePushEvent error: %v", err)
99
- // c.String(http.StatusInternalServerError, err.Error())
100
- // return
101
- // }
96
+ err := handlePushEvent (d . GitlabProvider , event , organisationId )
97
+ if err != nil {
98
+ log .Printf ("handlePushEvent error: %v" , err )
99
+ c .String (http .StatusInternalServerError , err .Error ())
100
+ return
101
+ }
102
102
default :
103
103
log .Printf ("Unhandled event, event type %v" , reflect .TypeOf (event ))
104
104
}
105
105
106
106
c .JSON (200 , "ok" )
107
107
}
108
108
109
+ func handlePushEvent (gh utils.GitlabProvider , payload * gitlab.PushEvent , organisationId uint ) error {
110
+ //projectId := payload.Project.ID
111
+ repoFullName := payload .Project .PathWithNamespace
112
+ repoOwner , repoName , _ := strings .Cut (repoFullName , "/" )
113
+ cloneURL := payload .Project .GitHTTPURL
114
+ webURL := payload .Project .WebURL
115
+ ref := payload .Ref
116
+ defaultBranch := payload .Project .DefaultBranch
117
+
118
+ pushBranch := ""
119
+ if strings .HasPrefix (ref , "refs/heads/" ) {
120
+ pushBranch = strings .TrimPrefix (ref , "refs/heads/" )
121
+ } else {
122
+ log .Printf ("push was not to a branch, ignoring %v" , ref )
123
+ return nil
124
+ }
125
+
126
+ diggerRepoName := strings .ReplaceAll (repoFullName , "/" , "-" )
127
+ //repo, err := models.DB.GetRepo(organisationId, diggerRepoName)
128
+ //if err != nil {
129
+ // log.Printf("Error getting Repo: %v", err)
130
+ // return fmt.Errorf("error getting github app link")
131
+ //}
132
+ // create repo if not exists
133
+ org , err := models .DB .GetOrganisationById (organisationId )
134
+ if err != nil {
135
+ log .Printf ("Error: could not get organisation: %v" , err )
136
+ return nil
137
+ }
138
+
139
+ repo , err := models .DB .CreateRepo (diggerRepoName , repoFullName , repoOwner , repoName , webURL , org , "" )
140
+ if err != nil {
141
+ log .Printf ("Error: could not create repo: %v" , err )
142
+ return nil
143
+ }
144
+
145
+ token := os .Getenv ("DIGGER_GITLAB_ACCESS_TOKEN" )
146
+ if token == "" {
147
+ log .Printf ("could not find gitlab token: %v" , err )
148
+ return fmt .Errorf ("could not find gitlab token" )
149
+ }
150
+
151
+ var isMainBranch bool
152
+ if strings .HasSuffix (ref , defaultBranch ) {
153
+ isMainBranch = true
154
+ } else {
155
+ isMainBranch = false
156
+ }
157
+
158
+ err = utils .CloneGitRepoAndDoAction (cloneURL , pushBranch , token , func (dir string ) error {
159
+ config , err := dg_configuration .LoadDiggerConfigYaml (dir , true , nil )
160
+ if err != nil {
161
+ log .Printf ("ERROR load digger.yml: %v" , err )
162
+ return fmt .Errorf ("error loading digger.yml %v" , err )
163
+ }
164
+ models .DB .UpdateRepoDiggerConfig (organisationId , * config , repo , isMainBranch )
165
+ return nil
166
+ })
167
+ if err != nil {
168
+ return fmt .Errorf ("error while cloning repo: %v" , err )
169
+ }
170
+
171
+ return nil
172
+ }
173
+
109
174
func GetGitlabRepoUrl (event interface {}) string {
110
175
var repoUrl = ""
111
176
switch event := event .(type ) {
0 commit comments