|  | 
| 8 | 8 | 	"errors" | 
| 9 | 9 | 	"fmt" | 
| 10 | 10 | 	"net/http" | 
| 11 |  | -	"strings" | 
| 12 | 11 | 
 | 
| 13 | 12 | 	"code.gitea.io/gitea/models/db" | 
| 14 | 13 | 	repo_model "code.gitea.io/gitea/models/repo" | 
| @@ -46,21 +45,25 @@ func IsValidHookTaskType(name string) bool { | 
| 46 | 45 | // hookQueue is a global queue of web hooks | 
| 47 | 46 | var hookQueue *queue.WorkerPoolQueue[int64] | 
| 48 | 47 | 
 | 
| 49 |  | -// getPayloadBranch returns branch for hook event, if applicable. | 
| 50 |  | -func getPayloadBranch(p api.Payloader) string { | 
|  | 48 | +// getPayloadRef returns the full ref name for hook event, if applicable. | 
|  | 49 | +func getPayloadRef(p api.Payloader) string { | 
| 51 | 50 | 	switch pp := p.(type) { | 
| 52 | 51 | 	case *api.CreatePayload: | 
| 53 |  | -		if pp.RefType == "branch" { | 
| 54 |  | -			return pp.Ref | 
|  | 52 | +		switch pp.RefType { | 
|  | 53 | +		case "branch": | 
|  | 54 | +			return git.BranchPrefix + pp.Ref | 
|  | 55 | +		case "tag": | 
|  | 56 | +			return git.TagPrefix + pp.Ref | 
| 55 | 57 | 		} | 
| 56 | 58 | 	case *api.DeletePayload: | 
| 57 |  | -		if pp.RefType == "branch" { | 
| 58 |  | -			return pp.Ref | 
|  | 59 | +		switch pp.RefType { | 
|  | 60 | +		case "branch": | 
|  | 61 | +			return git.BranchPrefix + pp.Ref | 
|  | 62 | +		case "tag": | 
|  | 63 | +			return git.TagPrefix + pp.Ref | 
| 59 | 64 | 		} | 
| 60 | 65 | 	case *api.PushPayload: | 
| 61 |  | -		if strings.HasPrefix(pp.Ref, git.BranchPrefix) { | 
| 62 |  | -			return pp.Ref[len(git.BranchPrefix):] | 
| 63 |  | -		} | 
|  | 66 | +		return pp.Ref | 
| 64 | 67 | 	} | 
| 65 | 68 | 	return "" | 
| 66 | 69 | } | 
| @@ -144,11 +147,10 @@ func PrepareWebhook(ctx context.Context, w *webhook_model.Webhook, event webhook | 
| 144 | 147 | 		return nil | 
| 145 | 148 | 	} | 
| 146 | 149 | 
 | 
| 147 |  | -	// If payload has no associated branch (e.g. it's a new tag, issue, etc.), | 
| 148 |  | -	// branch filter has no effect. | 
| 149 |  | -	if branch := getPayloadBranch(p); branch != "" { | 
| 150 |  | -		if !checkBranch(w, branch) { | 
| 151 |  | -			log.Info("Branch %q doesn't match branch filter %q, skipping", branch, w.BranchFilter) | 
|  | 150 | +	// Apply the filter directly to the ref name | 
|  | 151 | +	if ref := getPayloadRef(p); ref != "" { | 
|  | 152 | +		if !checkBranch(w, ref) { | 
|  | 153 | +			log.Info("Ref %q doesn't match branch filter %q, skipping", ref, w.BranchFilter) | 
| 152 | 154 | 			return nil | 
| 153 | 155 | 		} | 
| 154 | 156 | 	} | 
|  | 
0 commit comments