Allow the use of another secret defined for the same repo

This commit is contained in:
nemunaire 2022-09-30 12:40:48 +02:00
parent f79c7b43cd
commit 569d44880c

View File

@ -264,7 +264,7 @@ func declareCallbacksRoutes(router *gin.RouterGroup) {
router.POST("/callbacks/trigger.json", func(c *gin.Context) { router.POST("/callbacks/trigger.json", func(c *gin.Context) {
// Check event type // Check event type
if c.Request.Header.Get("X-Gitlab-Event") != "Tag Push Hook" { if c.Request.Header.Get("X-Gitlab-Event") != "Tag Push Hook" {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "This trigger is limited to Tag Push event. Please edit your trigger."}) c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "This trigger is limited to Tag Push event. Please edit your trigger on GitLab."})
return return
} }
@ -284,7 +284,6 @@ func declareCallbacksRoutes(router *gin.RouterGroup) {
var repo *Repository var repo *Repository
for _, r := range repos { for _, r := range repos {
log.Println("Received trigger")
if len(r.Secret) == 0 || base64.StdEncoding.EncodeToString(r.Secret) == c.Request.Header.Get("X-Gitlab-Token") { if len(r.Secret) == 0 || base64.StdEncoding.EncodeToString(r.Secret) == c.Request.Header.Get("X-Gitlab-Token") {
repo = r repo = r
break break
@ -318,10 +317,29 @@ func declareCallbacksRoutes(router *gin.RouterGroup) {
return return
} }
if !strings.HasPrefix(tmp[2], work.Tag) {
// Allow to use a secret for another tag
if len(repos) > 1 {
for _, r := range repos {
w, err := getWork(int(r.IdWork))
if err != nil {
log.Println("Unable to getWork:", err.Error())
continue
}
if strings.HasPrefix(tmp[2], w.Tag) {
repo = r
work = w
break
}
}
}
if !strings.HasPrefix(tmp[2], work.Tag) { if !strings.HasPrefix(tmp[2], work.Tag) {
c.AbortWithStatusJSON(http.StatusOK, gin.H{"errmsg": fmt.Sprintf("Ignore ref %q has it doesn't start with %s. Check submission instructions if this is not expected.", tmp[2], work.Tag)}) c.AbortWithStatusJSON(http.StatusOK, gin.H{"errmsg": fmt.Sprintf("Ignore ref %q has it doesn't start with %s. Check submission instructions if this is not expected.", tmp[2], work.Tag)})
return return
} }
}
TriggerTagUpdate(c, work, repo, user, &tmp[2]) TriggerTagUpdate(c, work, repo, user, &tmp[2])
}) })