From a8103617743f5162abd73a3081f4946be0f8e15e Mon Sep 17 00:00:00 2001 From: Benjamin Elfen Date: Wed, 22 May 2024 10:53:14 +0200 Subject: [PATCH 1/4] feat: added skip logic to circumvent Context: Bitbucket is generating ghost commits that have the commit message "Notes added by 'git notes add'" if git-notes is used (e.g. with semantic-release). To prevent Jenkins starting builds that will result in failure because there is nothing to build, the build should be stopped when those commits are coming in. --- jenkins/webhook-proxy/main.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/jenkins/webhook-proxy/main.go b/jenkins/webhook-proxy/main.go index e14e324da..c20e47246 100644 --- a/jenkins/webhook-proxy/main.go +++ b/jenkins/webhook-proxy/main.go @@ -262,7 +262,11 @@ func (s *Server) HandleRoot() http.HandlerFunc { type requestBitbucket struct { EventKey string `json:"eventKey"` Repository repository `json:"repository"` - Changes []struct { + Commit struct { + Id string `json:"id"` + Message string `json:"message"` + } `json:"toCommit"` + Changes []struct { Type string `json:"type"` Ref struct { DisplayID string `json:"displayId"` @@ -392,6 +396,14 @@ func (s *Server) HandleRoot() http.HandlerFunc { return } + // Skip requests with commit messages containing "Notes added by 'git notes add'" + // Reference 1: https://community.atlassian.com/t5/Bitbucket-questions/disable-quot-git-notes-add-quot-behaviour-for-semantic-release/qaq-p/1837322 + // Reference 2: https://github.com/semantic-release/semantic-release/discussions/2017#discussioncomment-995308 + if strings.Contains(req.Commit.Message, "Notes added by 'git notes add'") { + log.Println(requestID, "Skipping request with commit message containing: Notes added by 'git notes add'") + return + } + if req.EventKey == "repo:refs_changed" { repo = req.Repository.Slug if component == "" { From 875f31960c92d8ee44795a63152a0eaed3f02621 Mon Sep 17 00:00:00 2001 From: Benjamin Elfen Date: Wed, 29 May 2024 11:00:48 +0200 Subject: [PATCH 2/4] feat: change to ignore based on build id --- jenkins/webhook-proxy/main.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/jenkins/webhook-proxy/main.go b/jenkins/webhook-proxy/main.go index c20e47246..dd9fc76b7 100644 --- a/jenkins/webhook-proxy/main.go +++ b/jenkins/webhook-proxy/main.go @@ -262,11 +262,7 @@ func (s *Server) HandleRoot() http.HandlerFunc { type requestBitbucket struct { EventKey string `json:"eventKey"` Repository repository `json:"repository"` - Commit struct { - Id string `json:"id"` - Message string `json:"message"` - } `json:"toCommit"` - Changes []struct { + Changes []struct { Type string `json:"type"` Ref struct { DisplayID string `json:"displayId"` @@ -396,11 +392,11 @@ func (s *Server) HandleRoot() http.HandlerFunc { return } - // Skip requests with commit messages containing "Notes added by 'git notes add'" + // Skip requests with where the ref id is starting with "refs/notes/" // Reference 1: https://community.atlassian.com/t5/Bitbucket-questions/disable-quot-git-notes-add-quot-behaviour-for-semantic-release/qaq-p/1837322 // Reference 2: https://github.com/semantic-release/semantic-release/discussions/2017#discussioncomment-995308 - if strings.Contains(req.Commit.Message, "Notes added by 'git notes add'") { - log.Println(requestID, "Skipping request with commit message containing: Notes added by 'git notes add'") + if strings.Contains(req.Changes[0].Ref.DisplayID, "refs/notes/") { + log.Println(requestID, "Skipping request with refs/notes/ prefix in ref id") return } From 202ce48919356be78440a7387605d77bd30bd3b9 Mon Sep 17 00:00:00 2001 From: Benjamin Elfen Date: Wed, 29 May 2024 11:39:13 +0200 Subject: [PATCH 3/4] docs: adapted changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 25358aa50..d6573c01a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +### Added +- Added webhook proxy logic to skip requests with the ref id starting with "refs/notes/" + ### Fixed ### Added From 503789fc8bb1fb407b1cb9a13a6eb0ad167f99c4 Mon Sep 17 00:00:00 2001 From: Benjamin Elfen Date: Mon, 3 Jun 2024 11:09:06 +0200 Subject: [PATCH 4/4] fix: skip check if changes array is empty --- jenkins/webhook-proxy/main.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/jenkins/webhook-proxy/main.go b/jenkins/webhook-proxy/main.go index dd9fc76b7..132fcceab 100644 --- a/jenkins/webhook-proxy/main.go +++ b/jenkins/webhook-proxy/main.go @@ -395,8 +395,10 @@ func (s *Server) HandleRoot() http.HandlerFunc { // Skip requests with where the ref id is starting with "refs/notes/" // Reference 1: https://community.atlassian.com/t5/Bitbucket-questions/disable-quot-git-notes-add-quot-behaviour-for-semantic-release/qaq-p/1837322 // Reference 2: https://github.com/semantic-release/semantic-release/discussions/2017#discussioncomment-995308 - if strings.Contains(req.Changes[0].Ref.DisplayID, "refs/notes/") { + if len(req.Changes) > 0 && strings.Contains(req.Changes[0].Ref.DisplayID, "refs/notes/") { log.Println(requestID, "Skipping request with refs/notes/ prefix in ref id") + // Return 200 OK to Bitbucket to avoid retries + http.Error(w, "OK", http.StatusOK) return }