Skip to content
34 changes: 19 additions & 15 deletions services/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"errors"
"fmt"
"net/http"
"strings"

"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
Expand Down Expand Up @@ -46,21 +45,25 @@
// hookQueue is a global queue of web hooks
var hookQueue *queue.WorkerPoolQueue[int64]

// getPayloadBranch returns branch for hook event, if applicable.
func getPayloadBranch(p api.Payloader) string {
// getPayloadRef returns the full ref name for hook event, if applicable.
func getPayloadRef(p api.Payloader) git.RefName {
switch pp := p.(type) {
case *api.CreatePayload:
if pp.RefType == "branch" {
return pp.Ref
switch pp.RefType {
case "branch":
return git.RefNameFromBranch(pp.Ref)
case "tag":
return git.RefNameFromTag(pp.Ref)
}
case *api.DeletePayload:
if pp.RefType == "branch" {
return pp.Ref
switch pp.RefType {
case "branch":
return git.RefNameFromBranch(pp.Ref)
case "tag":
return git.RefNameFromTag(pp.Ref)
}
case *api.PushPayload:
if strings.HasPrefix(pp.Ref, git.BranchPrefix) {
return pp.Ref[len(git.BranchPrefix):]
}
return git.RefName(pp.Ref)
}
return ""
}
Expand Down Expand Up @@ -144,11 +147,12 @@
return nil
}

// If payload has no associated branch (e.g. it's a new tag, issue, etc.),
// branch filter has no effect.
if branch := getPayloadBranch(p); branch != "" {
if !checkBranch(w, branch) {
log.Info("Branch %q doesn't match branch filter %q, skipping", branch, w.BranchFilter)
// Apply the filter directly to the ref name
if ref := getPayloadRef(p); ref != "" {
// FIXME: here comes the problem, "ref" is the full ref name, but the filter
// But, "checkBranch" check it against "w.BranchFilter", does it make sense?
if !checkBranch(w, ref) {

Check failure on line 154 in services/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

cannot use ref (variable of string type "code.gitea.io/gitea/modules/git".RefName) as string value in argument to checkBranch (typecheck)

Check failure on line 154 in services/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / lint-backend

cannot use ref (variable of string type "code.gitea.io/gitea/modules/git".RefName) as string value in argument to checkBranch (typecheck)

Check failure on line 154 in services/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / backend

cannot use ref (variable of string type "code.gitea.io/gitea/modules/git".RefName) as string value in argument to checkBranch

Check failure on line 154 in services/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / checks-backend

cannot use ref (variable of string type "code.gitea.io/gitea/modules/git".RefName) as string value in argument to checkBranch

Check failure on line 154 in services/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / checks-backend

cannot use ref (variable of string type "code.gitea.io/gitea/modules/git".RefName) as string value in argument to checkBranch

Check failure on line 154 in services/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / checks-backend

cannot use ref (variable of string type "code.gitea.io/gitea/modules/git".RefName) as string value in argument to checkBranch

Check failure on line 154 in services/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / checks-backend

cannot use ref (variable of string type "code.gitea.io/gitea/modules/git".RefName) as string value in argument to checkBranch

Check failure on line 154 in services/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / checks-backend

cannot use ref (variable of string type "code.gitea.io/gitea/modules/git".RefName) as string value in argument to checkBranch

Check failure on line 154 in services/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

cannot use ref (variable of string type "code.gitea.io/gitea/modules/git".RefName) as string value in argument to checkBranch (typecheck)

Check failure on line 154 in services/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / test-mysql

cannot use ref (variable of string type "code.gitea.io/gitea/modules/git".RefName) as string value in argument to checkBranch

Check failure on line 154 in services/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / test-pgsql

cannot use ref (variable of string type "code.gitea.io/gitea/modules/git".RefName) as string value in argument to checkBranch

Check failure on line 154 in services/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / test-mssql

cannot use ref (variable of string type "code.gitea.io/gitea/modules/git".RefName) as string value in argument to checkBranch

Check failure on line 154 in services/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / test-sqlite

cannot use ref (variable of string type "code.gitea.io/gitea/modules/git".RefName) as string value in argument to checkBranch

Check failure on line 154 in services/webhook/webhook.go

View workflow job for this annotation

GitHub Actions / test-unit

cannot use ref (variable of string type "code.gitea.io/gitea/modules/git".RefName) as string value in argument to checkBranch
log.Info("Ref %q doesn't match branch filter %q, skipping", ref, w.BranchFilter)
return nil
}
}
Expand Down
Loading