From a1fecf3a87cabd56e99f4c0b3bb55e845307a1a4 Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Thu, 12 Mar 2026 17:39:24 +0100 Subject: [PATCH 01/10] ci(ff): support feature freeze automation phase 1 --- go.mod | 18 ++ go.sum | 93 +++++++++ magefile.go | 540 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 651 insertions(+) diff --git a/go.mod b/go.mod index 3f6020c326..fb6c961b57 100644 --- a/go.mod +++ b/go.mod @@ -48,11 +48,15 @@ require ( ) require ( + dario.cat/mergo v1.0.0 // indirect github.com/Microsoft/go-winio v0.6.1 // indirect + github.com/ProtonMail/go-crypto v1.0.0 // indirect github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect github.com/armon/go-radix v1.0.0 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/cloudflare/circl v1.3.7 // indirect + github.com/cyphar/filepath-securejoin v0.2.5 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/elastic/elastic-transport-go/v8 v8.8.0 // indirect @@ -60,31 +64,44 @@ require ( github.com/elastic/go-sysinfo v1.15.1 // indirect github.com/elastic/go-windows v1.0.2 // indirect github.com/elastic/gosigar v0.14.3 // indirect + github.com/emirpasic/gods v1.18.1 // indirect github.com/fatih/color v1.15.0 // indirect + github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect + github.com/go-git/go-billy/v5 v5.5.0 // indirect + github.com/go-git/go-git/v5 v5.12.0 // indirect github.com/go-logr/logr v1.4.3 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/go-ole/go-ole v1.3.0 // indirect + github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect + github.com/google/go-github/v68 v68.0.0 // indirect + github.com/google/go-querystring v1.1.0 // indirect github.com/google/uuid v1.6.0 // indirect github.com/gorilla/websocket v1.5.3 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect github.com/josharian/intern v1.0.0 // indirect + github.com/kevinburke/ssh_config v1.2.0 // indirect github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect + github.com/pjbgf/sha1cd v0.3.0 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect github.com/prometheus/client_model v0.6.2 // indirect github.com/prometheus/common v0.66.1 // indirect github.com/prometheus/procfs v0.16.1 // indirect + github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect github.com/shirou/gopsutil/v4 v4.24.7 // indirect github.com/shoenig/go-m1cpu v0.1.6 // indirect + github.com/skeema/knownhosts v1.2.2 // indirect github.com/spf13/pflag v1.0.9 // indirect github.com/stretchr/objx v0.5.2 // indirect github.com/tklauser/go-sysconf v0.3.12 // indirect github.com/tklauser/numcpus v0.6.1 // indirect github.com/x448/float16 v0.8.4 // indirect + github.com/xanzy/ssh-agent v0.3.3 // indirect github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect github.com/yusufpapurcu/wmi v1.2.4 // indirect go.elastic.co/ecszap v1.0.3 // indirect @@ -100,6 +117,7 @@ require ( golang.org/x/text v0.34.0 // indirect golang.org/x/tools v0.41.0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20251202230838-ff82c1b0f217 // indirect + gopkg.in/warnings.v0 v0.1.2 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect howett.net/plist v1.0.1 // indirect ) diff --git a/go.sum b/go.sum index 5400b45c66..7920f5c528 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,12 @@ +dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk= +dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk= +github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY= github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow= github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= github.com/Pallinder/go-randomdata v1.2.0 h1:DZ41wBchNRb/0GfsePLiSwb0PHZmT67XY00lCDlaYPg= github.com/Pallinder/go-randomdata v1.2.0/go.mod h1:yHmJgulpD2Nfrm0cR9tI/+oAgRqCQQixsA8HyRZfV9Y= +github.com/ProtonMail/go-crypto v1.0.0 h1:LRuvITjQWX+WIfr930YHG2HNfjR1uOfyf5vE0kC2U78= +github.com/ProtonMail/go-crypto v1.0.0/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0= github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk= github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ= github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP2+08jFMw88y4klk= @@ -10,12 +15,18 @@ github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgI github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w= +github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8= github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA= +github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU= +github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= +github.com/cyphar/filepath-securejoin v0.2.5 h1:6iR5tXJ/e6tJZzzdMc1km3Sa7RRIVBKAK32O2s7AYfo= +github.com/cyphar/filepath-securejoin v0.2.5/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -53,6 +64,8 @@ github.com/elastic/go-windows v1.0.2 h1:yoLLsAsV5cfg9FLhZ9EXZ2n2sQFKeDYrHenkcivY github.com/elastic/go-windows v1.0.2/go.mod h1:bGcDpBzXgYSqM0Gx3DM4+UxFj300SZLixie9u9ixLM8= github.com/elastic/gosigar v0.14.3 h1:xwkKwPia+hSfg9GqrCUKYdId102m9qTJIIr7egmK/uo= github.com/elastic/gosigar v0.14.3/go.mod h1:iXRIGg2tLnu7LBdpqzyQfGDEidKCfWcCMS0WKyPWoMs= +github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= +github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg= @@ -61,6 +74,12 @@ github.com/fxamacker/cbor/v2 v2.9.0 h1:NpKPmjDBgUfBms6tr6JZkTHtfFGcMKsw3eGcmD/sa github.com/fxamacker/cbor/v2 v2.9.0/go.mod h1:vM4b+DJCtHn+zz7h3FFp/hDAI9WNWCsZj23V5ytsSxQ= github.com/go-chi/chi/v5 v5.2.5 h1:Eg4myHZBjyvJmAFjFvWgrqDTXFyOzjj7YIm3L3mu6Ug= github.com/go-chi/chi/v5 v5.2.5/go.mod h1:X7Gx4mteadT3eDOMTsXzmI4/rwUpOwBHLpAfupzFJP0= +github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI= +github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic= +github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+mTU= +github.com/go-git/go-billy/v5 v5.5.0/go.mod h1:hmexnoNsr2SJU1Ju67OaNz5ASJY3+sHgFRpCtpDCKow= +github.com/go-git/go-git/v5 v5.12.0 h1:7Md+ndsjrzZxbddRDZjF14qK+NN56sy6wkqaVrjZtys= +github.com/go-git/go-git/v5 v5.12.0/go.mod h1:FTM9VKtnI2m65hNI/TenDDDnUf2Q9FHnXYjuz9i5OEY= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI= github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= @@ -74,11 +93,18 @@ github.com/gofrs/uuid/v5 v5.4.0 h1:EfbpCTjqMuGyq5ZJwxqzn3Cbr2d0rUZU7v5ycAk/e/0= github.com/gofrs/uuid/v5 v5.4.0/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= +github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= +github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= +github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= +github.com/google/go-github/v68 v68.0.0 h1:ZW57zeNZiXTdQ16qrDiZ0k6XucrxZ2CGmoTvcCyQG6s= +github.com/google/go-github/v68 v68.0.0/go.mod h1:K9HAUBovM2sLwM408A18h+wd9vqdLOEqTUCbnRIcx68= +github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= +github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/google/pprof v0.0.0-20230426061923-93006964c1fc h1:AGDHt781oIcL4EFk7cPnvBUYTwU8BEU6GDTO3ZMn1sE= github.com/google/pprof v0.0.0-20230426061923-93006964c1fc/go.mod h1:79YE0hCXdHag9sBkw2o+N/YnZtTkXi0UT9Nnixa5eYk= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= @@ -93,14 +119,21 @@ github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= +github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE= +github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4= +github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo= github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= @@ -135,6 +168,8 @@ github.com/opencontainers/image-spec v1.0.2 h1:9yCKha/T5XdGtO0q9Q9a6T5NUCsTn/DrB github.com/opencontainers/image-spec v1.0.2/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0= github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 h1:onHthvaw9LFnH4t2DcNVpwGmV9E1BkGknEliJkfwQj0= github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58/go.mod h1:DXv8WO4yhMYhSNPKjeNKa5WY9YCIEBRbNzFFPJbWO6Y= +github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4= +github.com/pjbgf/sha1cd v0.3.0/go.mod h1:nZ1rrWOcGJ5uZgEEVL1VUM9iRQiZvWdbZjkKyFzPPsI= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -158,12 +193,17 @@ github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWR github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY= github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8= +github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4= github.com/shirou/gopsutil/v4 v4.24.7 h1:V9UGTK4gQ8HvcnPKf6Zt3XHyQq/peaekfxpJ2HSocJk= github.com/shirou/gopsutil/v4 v4.24.7/go.mod h1:0uW/073rP7FYLOkvxolUQM5rMOLTNmRXnFKafpb71rw= github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM= github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ= github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU= github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnjqq0k= +github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/skeema/knownhosts v1.2.2 h1:Iug2P4fLmDw9f41PB6thxUkNUkJzB5i+1/exaj40L3A= +github.com/skeema/knownhosts v1.2.2/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo= github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU= github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4= github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY= @@ -172,6 +212,7 @@ github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKk github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= @@ -183,8 +224,11 @@ github.com/tklauser/numcpus v0.6.1 h1:ng9scYS7az0Bk4OZLvrNXNSAO2Pxr1XXRAPyjhIx+F github.com/tklauser/numcpus v0.6.1/go.mod h1:1XfjsgE2zo8GVw7POkMbHENHzVg3GzmoZ9fESEdAacY= github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= +github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM= +github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw= github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM= github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= go.elastic.co/apm/module/apmchiv5/v2 v2.7.4 h1:X46uD2aE3cY4PU9Z7jV3id+4oHKhGz3Ej/bmbfD0JOU= @@ -228,19 +272,48 @@ go.uber.org/zap v1.27.1/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E= go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI= go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU= go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= +golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= golang.org/x/crypto v0.48.0 h1:/VRzVqiRSggnhY7gNRxPauEQ5Drw9haKdM0jqfcCFts= golang.org/x/crypto v0.48.0/go.mod h1:r0kV5h3qnFPlQnBSrULhlsRfryS2pmewsg+XfMgkVos= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.32.0 h1:9F4d3PHLljb6x//jOyokMv3eX+YDeepZSEo3mFJy93c= golang.org/x/mod v0.32.0/go.mod h1:SgipZ/3h2Ci89DlEtEXWUk/HteuRin+HHhN+WbNhguU= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= golang.org/x/net v0.51.0 h1:94R/GTO7mt3/4wIKpcR5gkGmRLOuE/2hNGeWq/GBIFo= golang.org/x/net v0.51.0/go.mod h1:aamm+2QF5ogm02fjy5Bb7CQ0WMt1/WVM7FtyaTLlA9Y= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4= golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0= golang.org/x/sys v0.0.0-20180810173357-98c5dad5d1a0/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -248,12 +321,29 @@ golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k= golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/text v0.34.0 h1:oL/Qq0Kdaqxa1KbNeMKwQq0reLCCaFtqu2eNuSeNHbk= golang.org/x/text v0.34.0/go.mod h1:homfLqTYRFyVYemLBFl5GgL/DWEiH5wcsQ5gSh1yziA= golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U= golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/tools v0.41.0 h1:a9b8iMweWG+S0OBnlU36rzLp20z1Rp10w+IY2czHTQc= golang.org/x/tools v0.41.0/go.mod h1:XSY6eDqxVNiYgezAVqqCeihT4j1U2CCsqvH3WhQpnlg= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gonum.org/v1/gonum v0.16.0 h1:5+ul4Swaf3ESvrOnidPp4GZbzf0mxVQpDCYUQE7OJfk= gonum.org/v1/gonum v0.16.0/go.mod h1:fef3am4MQ93R2HHpKnLk4/Tbh/s0+wqD5nfa6Pnwy4E= @@ -264,10 +354,13 @@ google.golang.org/grpc v1.79.2/go.mod h1:KmT0Kjez+0dde/v2j9vzwoAScgEPx/Bw1CYChhH google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE= google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/mcuadros/go-syslog.v2 v2.3.0 h1:kcsiS+WsTKyIEPABJBJtoG0KkOS6yzvJ+/eZlhD79kk= gopkg.in/mcuadros/go-syslog.v2 v2.3.0/go.mod h1:l5LPIyOOyIdQquNg+oU6Z3524YwrcqEm0aKH+5zpt2U= +gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME= +gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0/go.mod h1:WDnlLJ4WF5VGsH/HVa3CI79GS0ol3YnhVnKP89i0kNg= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= diff --git a/magefile.go b/magefile.go index 06aecf9878..6b92365d3e 100644 --- a/magefile.go +++ b/magefile.go @@ -40,6 +40,11 @@ import ( "sync" "time" + "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/config" + "github.com/go-git/go-git/v5/plumbing" + "github.com/go-git/go-git/v5/plumbing/object" + "github.com/google/go-github/v68/github" "github.com/magefile/mage/mg" "github.com/magefile/mage/sh" "gopkg.in/yaml.v3" @@ -364,6 +369,9 @@ type Test mg.Namespace // Docker is the namespace for docker related tasks. type Docker mg.Namespace +// Release is the namespace for release automation. +type Release mg.Namespace + // envToBool reads the env var string s and parses it as a bool. func envToBool(s string) bool { v, ok := os.LookupEnv(s) @@ -2255,3 +2263,535 @@ func (Test) CloudE2ERun() error { err = errors.Join(err, os.WriteFile(filepath.Join("build", "test-cloude2e.out"), b.Bytes(), 0o644)) return err } + +// ============================================================================ +// Release Automation Functions +// ============================================================================ + +// ReleaseConfig holds configuration for release operations +type ReleaseConfig struct { + Version string + BaseBranch string + ReleaseBranch string + Owner string + Repo string + AuthorName string + AuthorEmail string +} + +// loadReleaseConfigFromEnv loads release configuration from environment variables +func loadReleaseConfigFromEnv() (*ReleaseConfig, error) { + version := os.Getenv("CURRENT_RELEASE") + if version == "" { + return nil, fmt.Errorf("CURRENT_RELEASE environment variable not set") + } + + baseBranch := os.Getenv("BASE_BRANCH") + if baseBranch == "" { + baseBranch = "main" + } + + // Extract major.minor for release branch (e.g., "9.4.0" -> "9.4") + parts := strings.Split(version, ".") + if len(parts) < 2 { + return nil, fmt.Errorf("invalid version format: %s (expected X.Y.Z)", version) + } + releaseBranch := fmt.Sprintf("%s.%s", parts[0], parts[1]) + + owner := os.Getenv("PROJECT_OWNER") + if owner == "" { + owner = "elastic" + } + + repo := os.Getenv("PROJECT_REPO") + if repo == "" { + repo = "fleet-server" + } + + authorName := os.Getenv("GITHUB_USERNAME") + if authorName == "" { + authorName = "elasticmachine" + } + + authorEmail := os.Getenv("GITHUB_EMAIL") + if authorEmail == "" { + authorEmail = "infra-root+elasticmachine@elastic.co" + } + + return &ReleaseConfig{ + Version: version, + BaseBranch: baseBranch, + ReleaseBranch: releaseBranch, + Owner: owner, + Repo: repo, + AuthorName: authorName, + AuthorEmail: authorEmail, + }, nil +} + +// UpdateVersion updates the version in version/version.go +func (Release) UpdateVersion(newVersion string) error { + versionFile := "version/version.go" + + content, err := os.ReadFile(versionFile) + if err != nil { + return fmt.Errorf("failed to read %s: %w", versionFile, err) + } + + // Replace the version string + // Pattern: const DefaultVersion = "X.Y.Z" + re := regexp.MustCompile(`(const\s+DefaultVersion\s*=\s*)"[^"]+"`) + newContent := re.ReplaceAllString(string(content), `${1}"`+newVersion+`"`) + + if newContent == string(content) { + return fmt.Errorf("version pattern not found in %s", versionFile) + } + + err = os.WriteFile(versionFile, []byte(newContent), 0644) + if err != nil { + return fmt.Errorf("failed to write %s: %w", versionFile, err) + } + + fmt.Printf("✓ Updated version to %s in %s\n", newVersion, versionFile) + return nil +} + +// UpdateMergify adds a new backport rule to .mergify.yml +func (Release) UpdateMergify(version string) error { + mergifyFile := ".mergify.yml" + + // Read the YAML file + content, err := os.ReadFile(mergifyFile) + if err != nil { + return fmt.Errorf("failed to read %s: %w", mergifyFile, err) + } + + // Extract major.minor from version (e.g., "9.4.0" -> "9.4") + parts := strings.Split(version, ".") + if len(parts) < 2 { + return fmt.Errorf("invalid version format: %s (expected X.Y.Z)", version) + } + branchVersion := fmt.Sprintf("%s.%s", parts[0], parts[1]) + + // Check if rule already exists + searchPattern := fmt.Sprintf("backport patches to %s branch", branchVersion) + if strings.Contains(string(content), searchPattern) { + fmt.Printf(" Backport rule for %s already exists\n", branchVersion) + return nil + } + + // Create new backport rule following the existing format + newRule := fmt.Sprintf(` - name: backport patches to %s branch + conditions: + - merged + - label=backport-%s + actions: + backport: + branches: + - "%s" +`, branchVersion, branchVersion, branchVersion) + + // Append the new rule to the end of the file + output := string(content) + newRule + + // Write back to file + err = os.WriteFile(mergifyFile, []byte(output), 0644) + if err != nil { + return fmt.Errorf("failed to write %s: %w", mergifyFile, err) + } + + fmt.Printf("✓ Added backport rule for %s to %s\n", branchVersion, mergifyFile) + return nil +} + +// GitRepo represents a Git repository with common operations +type GitRepo struct { + repo *git.Repository +} + +// openRepo opens the Git repository at the given path +func openRepo(path string) (*GitRepo, error) { + repo, err := git.PlainOpen(path) + if err != nil { + return nil, fmt.Errorf("failed to open git repo: %w", err) + } + return &GitRepo{repo: repo}, nil +} + +// createBranch creates and checks out a new branch +func (g *GitRepo) createBranch(branchName string) error { + w, err := g.repo.Worktree() + if err != nil { + return fmt.Errorf("failed to get worktree: %w", err) + } + + // Get current HEAD reference + headRef, err := g.repo.Head() + if err != nil { + return fmt.Errorf("failed to get HEAD: %w", err) + } + + // Create new branch reference + refName := plumbing.NewBranchReferenceName(branchName) + ref := plumbing.NewHashReference(refName, headRef.Hash()) + + err = g.repo.Storer.SetReference(ref) + if err != nil { + return fmt.Errorf("failed to create branch: %w", err) + } + + // Checkout the new branch + err = w.Checkout(&git.CheckoutOptions{ + Branch: refName, + }) + if err != nil { + return fmt.Errorf("failed to checkout branch: %w", err) + } + + fmt.Printf("✓ Created and checked out branch: %s\n", branchName) + return nil +} + +// commitAll commits all changes with the given message +func (g *GitRepo) commitAll(message, authorName, authorEmail string) error { + w, err := g.repo.Worktree() + if err != nil { + return fmt.Errorf("failed to get worktree: %w", err) + } + + // Add all changes + err = w.AddWithOptions(&git.AddOptions{ + All: true, + }) + if err != nil { + return fmt.Errorf("failed to add changes: %w", err) + } + + // Create commit + commit, err := w.Commit(message, &git.CommitOptions{ + Author: &object.Signature{ + Name: authorName, + Email: authorEmail, + When: time.Now(), + }, + }) + if err != nil { + return fmt.Errorf("failed to commit: %w", err) + } + + fmt.Printf("✓ Committed changes: %s\n", commit.String()[:7]) + return nil +} + +// push pushes the current branch to the remote +func (g *GitRepo) push(remoteName string, dryRun bool) error { + if dryRun { + fmt.Printf(" [DRY RUN] Would push to remote: %s\n", remoteName) + return nil + } + + err := g.repo.Push(&git.PushOptions{ + RemoteName: remoteName, + }) + if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) { + return fmt.Errorf("failed to push: %w", err) + } + + if errors.Is(err, git.NoErrAlreadyUpToDate) { + fmt.Println(" Branch already up to date on remote") + } else { + fmt.Printf("✓ Pushed to remote: %s\n", remoteName) + } + return nil +} + +// getCurrentBranch returns the name of the current branch +func (g *GitRepo) getCurrentBranch() (string, error) { + ref, err := g.repo.Head() + if err != nil { + return "", fmt.Errorf("failed to get HEAD: %w", err) + } + + if !ref.Name().IsBranch() { + return "", fmt.Errorf("HEAD is not a branch") + } + + return ref.Name().Short(), nil +} + +// setRemoteURL sets the URL for a remote +func (g *GitRepo) setRemoteURL(remoteName, url string) error { + _, err := g.repo.Remote(remoteName) + if errors.Is(err, git.ErrRemoteNotFound) { + // Remote doesn't exist, create it + _, err = g.repo.CreateRemote(&config.RemoteConfig{ + Name: remoteName, + URLs: []string{url}, + }) + if err != nil { + return fmt.Errorf("failed to create remote: %w", err) + } + fmt.Printf("✓ Created remote %s: %s\n", remoteName, url) + return nil + } else if err != nil { + return fmt.Errorf("failed to get remote: %w", err) + } + + // Update remote URL + err = g.repo.DeleteRemote(remoteName) + if err != nil { + return fmt.Errorf("failed to delete remote: %w", err) + } + + _, err = g.repo.CreateRemote(&config.RemoteConfig{ + Name: remoteName, + URLs: []string{url}, + }) + if err != nil { + return fmt.Errorf("failed to recreate remote: %w", err) + } + + fmt.Printf("✓ Updated remote %s: %s\n", remoteName, url) + return nil +} + +// newGitHubClient creates a new GitHub client from environment +func newGitHubClient() (*github.Client, error) { + token := os.Getenv("GITHUB_TOKEN") + if token == "" { + return nil, fmt.Errorf("GITHUB_TOKEN environment variable not set") + } + + client := github.NewClient(nil).WithAuthToken(token) + return client, nil +} + +// createPR creates a pull request on GitHub +func createPR(client *github.Client, cfg *ReleaseConfig, title, body, head, base string, dryRun bool) (*github.PullRequest, error) { + if dryRun { + fmt.Printf(" [DRY RUN] Would create PR:\n") + fmt.Printf(" Title: %s\n", title) + fmt.Printf(" Head: %s\n", head) + fmt.Printf(" Base: %s\n", base) + return nil, nil + } + + pr, _, err := client.PullRequests.Create(context.Background(), cfg.Owner, cfg.Repo, &github.NewPullRequest{ + Title: github.String(title), + Head: github.String(head), + Base: github.String(base), + Body: github.String(body), + }) + if err != nil { + return nil, fmt.Errorf("failed to create PR: %w", err) + } + + fmt.Printf("✓ Created PR #%d: %s\n", *pr.Number, *pr.HTMLURL) + return pr, nil +} + +// PrepareMajorMinor prepares files for a major/minor release +func (Release) PrepareMajorMinor() error { + cfg, err := loadReleaseConfigFromEnv() + if err != nil { + return err + } + + fmt.Printf("Preparing major/minor release for version %s\n", cfg.Version) + + // Update version + r := Release{} + if err := r.UpdateVersion(cfg.Version); err != nil { + return err + } + + // Update mergify + if err := r.UpdateMergify(cfg.Version); err != nil { + return err + } + + fmt.Println("✓ Major/minor release preparation complete") + fmt.Printf(" Next steps:\n") + fmt.Printf(" 1. Review changes: git diff\n") + fmt.Printf(" 2. Create branch: mage release:createBranch\n") + fmt.Printf(" 3. Create PR: mage release:createPR\n") + + return nil +} + +// CreateBranch creates a release branch with all changes committed +func (Release) CreateBranch() error { + cfg, err := loadReleaseConfigFromEnv() + if err != nil { + return err + } + + dryRun := os.Getenv("DRY_RUN") == "true" + + repo, err := openRepo(".") + if err != nil { + return err + } + + branchName := cfg.ReleaseBranch + fmt.Printf("Creating release branch: %s\n", branchName) + + if !dryRun { + // Create and checkout branch + if err := repo.createBranch(branchName); err != nil { + return err + } + + // Commit all changes + commitMsg := fmt.Sprintf("[Release] Prepare %s release", cfg.Version) + if err := repo.commitAll(commitMsg, cfg.AuthorName, cfg.AuthorEmail); err != nil { + return err + } + } else { + fmt.Printf(" [DRY RUN] Would create branch %s and commit changes\n", branchName) + } + + // Push to remote + if err := repo.push("origin", dryRun); err != nil { + return err + } + + return nil +} + +// CreatePR creates a pull request for the release +func (Release) CreatePR() error { + cfg, err := loadReleaseConfigFromEnv() + if err != nil { + return err + } + + dryRun := os.Getenv("DRY_RUN") == "true" + + client, err := newGitHubClient() + if err != nil { + return err + } + + title := fmt.Sprintf("[Release] %s", cfg.Version) + body := fmt.Sprintf("## Release %s\n\nThis PR prepares the %s release.\n\n### Changes\n- Updated version to %s\n- Added mergify backport rule for %s\n", + cfg.Version, cfg.Version, cfg.Version, cfg.ReleaseBranch) + + _, err = createPR(client, cfg, title, body, cfg.ReleaseBranch, cfg.BaseBranch, dryRun) + return err +} + +// RunMajorMinor orchestrates the complete major/minor release workflow +func (Release) RunMajorMinor() error { + cfg, err := loadReleaseConfigFromEnv() + if err != nil { + return err + } + + dryRun := os.Getenv("DRY_RUN") == "true" + if dryRun { + fmt.Println("🔍 DRY RUN MODE - No changes will be pushed") + } + + fmt.Printf("🚀 Starting major/minor release workflow for %s\n\n", cfg.Version) + + // Step 1: Prepare files + fmt.Println("Step 1: Preparing release files...") + r := Release{} + if err := r.PrepareMajorMinor(); err != nil { + return fmt.Errorf("failed to prepare release: %w", err) + } + + // Step 2: Create branch + fmt.Println("\nStep 2: Creating release branch...") + if err := r.CreateBranch(); err != nil { + return fmt.Errorf("failed to create branch: %w", err) + } + + // Step 3: Create PR + fmt.Println("\nStep 3: Creating pull request...") + if err := r.CreatePR(); err != nil { + return fmt.Errorf("failed to create PR: %w", err) + } + + fmt.Printf("\n✅ Major/minor release workflow complete for %s\n", cfg.Version) + return nil +} + +// PreparePatch prepares files for a patch release +func (Release) PreparePatch() error { + cfg, err := loadReleaseConfigFromEnv() + if err != nil { + return err + } + + fmt.Printf("Preparing patch release for version %s\n", cfg.Version) + + // Update version + r := Release{} + if err := r.UpdateVersion(cfg.Version); err != nil { + return err + } + + fmt.Println("✓ Patch release preparation complete") + return nil +} + +// RunPatch orchestrates the complete patch release workflow +func (Release) RunPatch() error { + cfg, err := loadReleaseConfigFromEnv() + if err != nil { + return err + } + + dryRun := os.Getenv("DRY_RUN") == "true" + if dryRun { + fmt.Println("🔍 DRY RUN MODE - No changes will be pushed") + } + + fmt.Printf("🚀 Starting patch release workflow for %s\n\n", cfg.Version) + + // Prepare files + fmt.Println("Preparing patch release files...") + r := Release{} + if err := r.PreparePatch(); err != nil { + return fmt.Errorf("failed to prepare patch release: %w", err) + } + + fmt.Printf("\n✅ Patch release workflow complete for %s\n", cfg.Version) + fmt.Println(" Review changes and commit to the release branch") + return nil +} + +// PrepareNext prepares files for the next development cycle +func (Release) PrepareNext() error { + // Get current version + currentVersion := os.Getenv("CURRENT_RELEASE") + if currentVersion == "" { + return fmt.Errorf("CURRENT_RELEASE environment variable not set") + } + + // Calculate next version (increment minor) + parts := strings.Split(currentVersion, ".") + if len(parts) < 3 { + return fmt.Errorf("invalid version format: %s", currentVersion) + } + + minor, err := strconv.Atoi(parts[1]) + if err != nil { + return fmt.Errorf("invalid minor version: %s", parts[1]) + } + + nextVersion := fmt.Sprintf("%s.%d.0", parts[0], minor+1) + + fmt.Printf("Preparing next development cycle: %s -> %s\n", currentVersion, nextVersion) + + // Update version + r := Release{} + if err := r.UpdateVersion(nextVersion); err != nil { + return err + } + + fmt.Printf("✓ Next release preparation complete: %s\n", nextVersion) + return nil +} From 23422b96cf39cff68aa72809842cddf10959e519 Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Thu, 12 Mar 2026 17:47:55 +0100 Subject: [PATCH 02/10] ci(ff): support feature freeze automation phase 2 --- RELEASE.md | 464 +++++++++++++++++++++++++++++++++++++++++++++++ magefile_test.go | 378 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 842 insertions(+) create mode 100644 RELEASE.md create mode 100644 magefile_test.go diff --git a/RELEASE.md b/RELEASE.md new file mode 100644 index 0000000000..c21df14b31 --- /dev/null +++ b/RELEASE.md @@ -0,0 +1,464 @@ +# Fleet-Server Release Automation + +This document describes the fleet-server release automation implemented using Mage, following the architecture patterns established in elastic-agent. + +## Quick Start + +### Prerequisites + +- Go 1.25+ +- Mage installed (`go install github.com/magefile/mage@latest`) +- GitHub token with repo permissions +- Clean git working directory + +### Major/Minor Release (e.g., 9.5.0) + +```bash +export CURRENT_RELEASE="9.5.0" +export GITHUB_TOKEN="ghp_your_token" +export DRY_RUN=true + +# Test first (updates files locally, no push/PR) +mage release:runMajorMinor + +# Review changes +git status +git diff + +# Run for real +export DRY_RUN=false +mage release:runMajorMinor +``` + +### Patch Release (e.g., 9.4.1) + +```bash +# Checkout base branch +git checkout 9.4 +git pull + +export CURRENT_RELEASE="9.4.1" +export BASE_BRANCH="9.4" +export GITHUB_TOKEN="ghp_your_token" + +mage release:runPatch +``` + +## Available Commands + +### Orchestration Commands (Recommended) + +These commands run the full release workflow: + +- `mage release:runMajorMinor` - Complete major/minor release workflow +- `mage release:runPatch` - Complete patch release workflow + +### Individual Commands + +For manual control or debugging: + +- `mage release:updateVersion ` - Update version in version/version.go +- `mage release:updateMergify ` - Add backport rule to .mergify.yml +- `mage release:prepareMajorMinor` - Prepare all files for major/minor release +- `mage release:preparePatch` - Prepare files for patch release +- `mage release:prepareNext` - Prepare next development cycle +- `mage release:createBranch` - Create and push release branch +- `mage release:createPR` - Create GitHub pull request + +## Environment Variables + +### Required + +| Variable | Description | Example | +|----------|-------------|---------| +| `CURRENT_RELEASE` | Version to release | `9.5.0` | +| `GITHUB_TOKEN` | GitHub API token | `ghp_xxx` | + +### Optional + +| Variable | Description | Default | +|----------|-------------|---------| +| `DRY_RUN` | Test mode (no push/PR) | `false` | +| `BASE_BRANCH` | Base branch for PR | `main` | +| `PROJECT_OWNER` | GitHub repository owner | `elastic` | +| `PROJECT_REPO` | GitHub repository name | `fleet-server` | +| `GITHUB_USERNAME` | Git commit author name | `elasticmachine` | +| `GITHUB_EMAIL` | Git commit author email | `infra-root+elasticmachine@elastic.co` | + +## Workflows + +### Major/Minor Release Workflow + +The `runMajorMinor` command performs these steps: + +1. **Prepare Files** + - Updates `version/version.go` with new version + - Adds backport rule to `.mergify.yml` + +2. **Create Branch** + - Creates release branch (e.g., `9.5`) + - Commits all changes + - Pushes to remote (unless DRY_RUN=true) + +3. **Create PR** + - Creates pull request from release branch to main + - Includes version update details + - (Skipped if DRY_RUN=true) + +### Patch Release Workflow + +The `runPatch` command performs these steps: + +1. **Prepare Files** + - Updates `version/version.go` with patch version + +2. **Commit & Review** + - Changes are prepared for manual review + - No automatic branch creation + - Commit to the current release branch manually + +### Next Development Cycle + +The `prepareNext` command: + +- Increments minor version (e.g., 9.4.0 → 9.5.0) +- Updates `version/version.go` +- Used after a release to bump version on main + +## DRY_RUN Mode + +DRY_RUN mode allows you to test the release process safely: + +```bash +export DRY_RUN=true +mage release:runMajorMinor +``` + +**What DRY_RUN does:** +- ✅ Updates local files (version/version.go, .mergify.yml) +- ✅ Shows what would be committed +- ❌ Does NOT create branches +- ❌ Does NOT push to remote +- ❌ Does NOT create pull requests + +**After DRY_RUN:** +- Review changes with `git diff` +- Discard changes with `git checkout .` +- Or proceed with `DRY_RUN=false` + +## Testing on a Fork + +Before using the release automation in production, it's **highly recommended** to test on your personal fork first. This allows you to verify the workflow end-to-end without affecting the official repository. + +### Step 1: Fork and Clone + +1. Fork the fleet-server repository on GitHub to your account +2. Clone your fork locally: + ```bash + git clone https://github.com/YOUR-USERNAME/fleet-server.git + cd fleet-server + ``` + +3. Ensure you're on a clean main branch: + ```bash + git checkout main + git pull origin main + ``` + +### Step 2: Configure Environment + +Set environment variables to point to your fork: + +```bash +# Point to your fork +export PROJECT_OWNER="your-username" +export PROJECT_REPO="fleet-server" + +# Use a test version to avoid confusion +export CURRENT_RELEASE="9.5.0-test" + +# Your GitHub personal access token +export GITHUB_TOKEN="ghp_your_token" + +# Important: Set to false to actually create branches and PRs +export DRY_RUN=false +``` + +### Step 3: Run the Workflow + +Execute the major/minor release workflow: + +```bash +mage release:runMajorMinor +``` + +This will: +- Update `version/version.go` to `9.5.0-test` +- Add backport rule for `9.5` to `.mergify.yml` +- Create branch `9.5` on your fork +- Push the branch to your fork +- Create a PR from `9.5` → `main` on your fork + +### Step 4: Verify the Results + +Check what was created: + +```bash +# View the created branch +git branch -a | grep 9.5 + +# View the created PR +gh pr list --repo YOUR-USERNAME/fleet-server +``` + +Or visit GitHub: `https://github.com/YOUR-USERNAME/fleet-server/pulls` + +### Step 5: Review and Clean Up + +1. Review the PR on your fork to ensure: + - Version was updated correctly + - Mergify rule was added properly + - Commit message is appropriate + +2. If satisfied, close the PR and delete the test branch: + ```bash + gh pr close BRANCH-NUMBER --repo YOUR-USERNAME/fleet-server --delete-branch + ``` + +3. Reset your fork's main branch: + ```bash + git checkout main + git reset --hard origin/main + ``` + +### Testing Different Scenarios + +#### Test Patch Release on Fork + +```bash +export PROJECT_OWNER="your-username" +export CURRENT_RELEASE="9.4.1-test" +export BASE_BRANCH="9.4" +export GITHUB_TOKEN="ghp_your_token" + +# Create the release branch first if it doesn't exist +git checkout -b 9.4 +git push -u origin 9.4 + +# Run patch workflow +mage release:runPatch +``` + +#### Test with DRY_RUN First + +For extra safety, test with DRY_RUN before creating real branches: + +```bash +export PROJECT_OWNER="your-username" +export DRY_RUN=true +mage release:runMajorMinor + +# Review local changes +git diff + +# If satisfied, run for real +export DRY_RUN=false +mage release:runMajorMinor +``` + +### Troubleshooting Fork Testing + +**Issue: "failed to push: authentication required"** +- Ensure your GITHUB_TOKEN has write access to your fork +- Verify: `gh auth status` + +**Issue: "branch already exists"** +- Delete the existing branch first: + ```bash + git branch -D 9.5 + git push origin --delete 9.5 + ``` + +**Issue: "PR already exists"** +- Close or merge the existing PR first +- Or use a different version number (e.g., `9.5.0-test2`) + +### Best Practices + +1. **Always test on fork first** before running in production +2. **Use test version numbers** (e.g., `X.Y.Z-test`) to distinguish from real releases +3. **Clean up test branches and PRs** after testing to keep your fork tidy +4. **Test both workflows**: major/minor AND patch releases +5. **Verify DRY_RUN mode** works correctly before relying on it + +## Troubleshooting + +### "CURRENT_RELEASE environment variable not set" + +Set the required environment variable: +```bash +export CURRENT_RELEASE="9.5.0" +``` + +### "GITHUB_TOKEN environment variable not set" + +Set your GitHub token: +```bash +export GITHUB_TOKEN=$(gh auth token) +``` + +Or create a personal access token at: https://github.com/settings/tokens + +### "failed to open git repo" + +Ensure you're in the fleet-server repository root: +```bash +cd /path/to/fleet-server +``` + +### "version pattern not found" + +The version file format has changed. Check that `version/version.go` contains: +```go +const DefaultVersion = "X.Y.Z" +``` + +### Uncommitted changes warning + +The workflow warns about uncommitted files but continues. To avoid confusion: +```bash +git status +git stash # or commit your changes +``` + +## Architecture + +Fleet-server follows the same architecture as elastic-agent but is simpler: + +**Key Differences from elastic-agent:** +- Fewer files to update (only 1: `version/version.go`) +- No docs/K8s manifest updates needed +- All code in `magefile.go` (no separate package) +- Simpler implementation (~540 lines vs ~2000+ in elastic-agent) + +**Libraries used:** +- `github.com/go-git/go-git/v5` - Git operations +- `github.com/google/go-github/v68` - GitHub API +- `gopkg.in/yaml.v3` - YAML parsing (already present) + +**No external tools:** +- ❌ No `hub` CLI +- ❌ No `gh` CLI +- ❌ No `sed` / `awk` +- ❌ No `yq` +- ✅ Pure Go implementation + +## Examples + +### Complete Major/Minor Release + +```bash +# 1. Start on main branch +git checkout main +git pull + +# 2. Set up environment +export CURRENT_RELEASE="9.5.0" +export GITHUB_TOKEN=$(gh auth token) + +# 3. Test with dry run +export DRY_RUN=true +mage release:runMajorMinor + +# 4. Review changes +git diff version/version.go +git diff .mergify.yml + +# 5. Run for real +export DRY_RUN=false +mage release:runMajorMinor + +# 6. Check the PR that was created +gh pr list +``` + +### Patch Release on Existing Branch + +```bash +# 1. Checkout release branch +git checkout 9.4 +git pull + +# 2. Set up environment +export CURRENT_RELEASE="9.4.1" +export BASE_BRANCH="9.4" +export GITHUB_TOKEN=$(gh auth token) + +# 3. Run patch workflow +mage release:runPatch + +# 4. Review and commit +git diff +git add version/version.go +git commit -m "Bump version to 9.4.1" +git push +``` + +### Bump Version After Release + +```bash +# 1. After 9.5.0 release, bump main to 9.6.0 +git checkout main + +# 2. Run next version prep +export CURRENT_RELEASE="9.5.0" +mage release:prepareNext + +# 3. Commit the change +git add version/version.go +git commit -m "Bump version to 9.6.0" +git push +``` + +## Testing + +Run unit tests for release functions: + +```bash +go test -v -tags=mage -run "TestUpdate|TestLoad|TestPrepare" . +``` + +Check test coverage: + +```bash +go test -v -tags=mage -coverprofile=coverage.out . +go tool cover -func=coverage.out | grep -E "(UpdateVersion|UpdateMergify)" +``` + +## Migration from Makefile + +This replaces the old Makefile-based release process: + +| Old (Makefile) | New (Mage) | +|----------------|------------| +| `make prepare-major-minor-release` | `mage release:runMajorMinor` | +| `make prepare-patch-release` | `mage release:runPatch` | +| `make update-version VERSION=9.5.0` | `mage release:updateVersion 9.5.0` | +| `make prepare-next-release` | `mage release:prepareNext` | + +**Benefits of Mage:** +- Pure Go (no sed, hub, Python dependencies) +- Type-safe with compile-time checks +- Built-in DRY_RUN mode +- Better error messages +- Easier to test and maintain + +## Support + +- **Issues**: https://github.com/elastic/fleet-server/issues +- **Documentation**: This file and migration plan +- **Code**: `magefile.go` (search for "Release Automation") + +--- + +**Last Updated**: 2026-03-12 diff --git a/magefile_test.go b/magefile_test.go new file mode 100644 index 0000000000..a468713400 --- /dev/null +++ b/magefile_test.go @@ -0,0 +1,378 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + +//go:build mage + +package main + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +func TestUpdateVersion(t *testing.T) { + tests := []struct { + name string + initial string + newVersion string + wantErr bool + wantContent string + }{ + { + name: "update version successfully", + initial: `const DefaultVersion = "9.4.0"`, + newVersion: "9.5.0", + wantErr: false, + wantContent: `const DefaultVersion = "9.5.0"`, + }, + { + name: "update to snapshot version", + initial: `const DefaultVersion = "9.4.0"`, + newVersion: "9.5.0-SNAPSHOT", + wantErr: false, + wantContent: `const DefaultVersion = "9.5.0-SNAPSHOT"`, + }, + { + name: "update with different spacing", + initial: `const DefaultVersion = "9.4.0"`, + newVersion: "9.5.0", + wantErr: false, + wantContent: `const DefaultVersion = "9.5.0"`, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Create temp directory and file + tmpDir := t.TempDir() + versionDir := filepath.Join(tmpDir, "version") + err := os.Mkdir(versionDir, 0755) + if err != nil { + t.Fatalf("failed to create version dir: %v", err) + } + + versionFile := filepath.Join(versionDir, "version.go") + initialContent := `// Copyright header + +package version + +` + tt.initial + ` +` + err = os.WriteFile(versionFile, []byte(initialContent), 0644) + if err != nil { + t.Fatalf("failed to write initial file: %v", err) + } + + // Save current dir and change to temp dir + origDir, _ := os.Getwd() + defer os.Chdir(origDir) + os.Chdir(tmpDir) + + // Run UpdateVersion + r := Release{} + err = r.UpdateVersion(tt.newVersion) + + if (err != nil) != tt.wantErr { + t.Errorf("UpdateVersion() error = %v, wantErr %v", err, tt.wantErr) + return + } + + if !tt.wantErr { + // Verify file content + content, err := os.ReadFile(versionFile) + if err != nil { + t.Fatalf("failed to read updated file: %v", err) + } + + if !strings.Contains(string(content), tt.wantContent) { + t.Errorf("UpdateVersion() content = %s, want to contain %s", string(content), tt.wantContent) + } + } + }) + } +} + +func TestUpdateMergify(t *testing.T) { + tests := []struct { + name string + version string + wantErr bool + wantBranch string + wantLabel string + shouldAppend bool + }{ + { + name: "add new backport rule", + version: "9.5.0", + wantErr: false, + wantBranch: "9.5", + wantLabel: "backport-9.5", + shouldAppend: true, + }, + { + name: "version with patch", + version: "10.0.1", + wantErr: false, + wantBranch: "10.0", + wantLabel: "backport-10.0", + shouldAppend: true, + }, + { + name: "invalid version format", + version: "9", + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Create temp directory and file + tmpDir := t.TempDir() + mergifyFile := filepath.Join(tmpDir, ".mergify.yml") + + initialContent := `pull_request_rules: + - name: backport patches to 9.3 branch + conditions: + - merged + - label=backport-9.3 + actions: + backport: + branches: + - "9.3" +` + err := os.WriteFile(mergifyFile, []byte(initialContent), 0644) + if err != nil { + t.Fatalf("failed to write initial file: %v", err) + } + + // Save current dir and change to temp dir + origDir, _ := os.Getwd() + defer os.Chdir(origDir) + os.Chdir(tmpDir) + + // Run UpdateMergify + r := Release{} + err = r.UpdateMergify(tt.version) + + if (err != nil) != tt.wantErr { + t.Errorf("UpdateMergify() error = %v, wantErr %v", err, tt.wantErr) + return + } + + if !tt.wantErr && tt.shouldAppend { + // Verify file content + content, err := os.ReadFile(mergifyFile) + if err != nil { + t.Fatalf("failed to read updated file: %v", err) + } + + contentStr := string(content) + + // Check that the new rule was added + expectedName := "backport patches to " + tt.wantBranch + " branch" + if !strings.Contains(contentStr, expectedName) { + t.Errorf("UpdateMergify() missing rule name: %s", expectedName) + } + + if !strings.Contains(contentStr, tt.wantLabel) { + t.Errorf("UpdateMergify() missing label: %s", tt.wantLabel) + } + + if !strings.Contains(contentStr, `"`+tt.wantBranch+`"`) { + t.Errorf("UpdateMergify() missing branch: %s", tt.wantBranch) + } + + // Verify old rule is still there + if !strings.Contains(contentStr, "9.3") { + t.Error("UpdateMergify() removed existing rules") + } + } + }) + } +} + +func TestUpdateMergify_Idempotent(t *testing.T) { + // Create temp directory and file + tmpDir := t.TempDir() + mergifyFile := filepath.Join(tmpDir, ".mergify.yml") + + initialContent := `pull_request_rules: + - name: backport patches to 9.5 branch + conditions: + - merged + - label=backport-9.5 + actions: + backport: + branches: + - "9.5" +` + err := os.WriteFile(mergifyFile, []byte(initialContent), 0644) + if err != nil { + t.Fatalf("failed to write initial file: %v", err) + } + + // Save current dir and change to temp dir + origDir, _ := os.Getwd() + defer os.Chdir(origDir) + os.Chdir(tmpDir) + + // Run UpdateMergify twice with same version + r := Release{} + err = r.UpdateMergify("9.5.0") + if err != nil { + t.Fatalf("first UpdateMergify() failed: %v", err) + } + + // Get content after first run + content1, _ := os.ReadFile(mergifyFile) + + err = r.UpdateMergify("9.5.0") + if err != nil { + t.Fatalf("second UpdateMergify() failed: %v", err) + } + + // Get content after second run + content2, _ := os.ReadFile(mergifyFile) + + // Content should be identical (idempotent) + if string(content1) != string(content2) { + t.Error("UpdateMergify() is not idempotent - content changed on second run") + } + + // Should only have one rule for 9.5 + count := strings.Count(string(content2), "backport patches to 9.5 branch") + if count != 1 { + t.Errorf("UpdateMergify() created %d rules for 9.5, want 1", count) + } +} + +func TestLoadReleaseConfigFromEnv(t *testing.T) { + tests := []struct { + name string + envVars map[string]string + wantErr bool + wantCheck func(*testing.T, *ReleaseConfig) + }{ + { + name: "all env vars set", + envVars: map[string]string{ + "CURRENT_RELEASE": "9.5.0", + "BASE_BRANCH": "main", + "PROJECT_OWNER": "elastic", + "PROJECT_REPO": "fleet-server", + }, + wantErr: false, + wantCheck: func(t *testing.T, cfg *ReleaseConfig) { + if cfg.Version != "9.5.0" { + t.Errorf("Version = %s, want 9.5.0", cfg.Version) + } + if cfg.ReleaseBranch != "9.5" { + t.Errorf("ReleaseBranch = %s, want 9.5", cfg.ReleaseBranch) + } + if cfg.BaseBranch != "main" { + t.Errorf("BaseBranch = %s, want main", cfg.BaseBranch) + } + }, + }, + { + name: "defaults applied", + envVars: map[string]string{ + "CURRENT_RELEASE": "10.0.0", + }, + wantErr: false, + wantCheck: func(t *testing.T, cfg *ReleaseConfig) { + if cfg.BaseBranch != "main" { + t.Errorf("BaseBranch = %s, want main (default)", cfg.BaseBranch) + } + if cfg.Owner != "elastic" { + t.Errorf("Owner = %s, want elastic (default)", cfg.Owner) + } + if cfg.Repo != "fleet-server" { + t.Errorf("Repo = %s, want fleet-server (default)", cfg.Repo) + } + }, + }, + { + name: "missing CURRENT_RELEASE", + envVars: map[string]string{}, + wantErr: true, + }, + { + name: "invalid version format", + envVars: map[string]string{ + "CURRENT_RELEASE": "9", + }, + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Clear env and set test values + os.Clearenv() + for k, v := range tt.envVars { + os.Setenv(k, v) + } + + cfg, err := loadReleaseConfigFromEnv() + + if (err != nil) != tt.wantErr { + t.Errorf("loadReleaseConfigFromEnv() error = %v, wantErr %v", err, tt.wantErr) + return + } + + if !tt.wantErr && tt.wantCheck != nil { + tt.wantCheck(t, cfg) + } + }) + } +} + +func TestPrepareNext(t *testing.T) { + // Create temp directory and file + tmpDir := t.TempDir() + versionDir := filepath.Join(tmpDir, "version") + err := os.Mkdir(versionDir, 0755) + if err != nil { + t.Fatalf("failed to create version dir: %v", err) + } + + versionFile := filepath.Join(versionDir, "version.go") + initialContent := `package version + +const DefaultVersion = "9.4.0" +` + err = os.WriteFile(versionFile, []byte(initialContent), 0644) + if err != nil { + t.Fatalf("failed to write initial file: %v", err) + } + + // Save current dir and change to temp dir + origDir, _ := os.Getwd() + defer os.Chdir(origDir) + os.Chdir(tmpDir) + + // Set env var + os.Setenv("CURRENT_RELEASE", "9.4.0") + + // Run PrepareNext + r := Release{} + err = r.PrepareNext() + if err != nil { + t.Fatalf("PrepareNext() failed: %v", err) + } + + // Verify version was incremented + content, err := os.ReadFile(versionFile) + if err != nil { + t.Fatalf("failed to read updated file: %v", err) + } + + expectedVersion := "9.5.0" + if !strings.Contains(string(content), expectedVersion) { + t.Errorf("PrepareNext() = %s, want %s", string(content), expectedVersion) + } +} From a666488077ce4f04fdca8ed17ae73e8037a7181f Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Thu, 12 Mar 2026 18:21:30 +0100 Subject: [PATCH 03/10] update --- go.mod | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index fb6c961b57..14a0608c62 100644 --- a/go.mod +++ b/go.mod @@ -14,8 +14,10 @@ require ( github.com/elastic/go-ucfg v0.9.1 github.com/fxamacker/cbor/v2 v2.9.0 github.com/go-chi/chi/v5 v5.2.5 + github.com/go-git/go-git/v5 v5.12.0 github.com/gofrs/uuid/v5 v5.4.0 github.com/google/go-cmp v0.7.0 + github.com/google/go-github/v68 v68.0.0 github.com/hashicorp/go-cleanhttp v0.5.2 github.com/hashicorp/go-version v1.8.0 github.com/hashicorp/golang-lru/v2 v2.0.7 @@ -68,12 +70,10 @@ require ( github.com/fatih/color v1.15.0 // indirect github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect github.com/go-git/go-billy/v5 v5.5.0 // indirect - github.com/go-git/go-git/v5 v5.12.0 // indirect github.com/go-logr/logr v1.4.3 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/go-ole/go-ole v1.3.0 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect - github.com/google/go-github/v68 v68.0.0 // indirect github.com/google/go-querystring v1.1.0 // indirect github.com/google/uuid v1.6.0 // indirect github.com/gorilla/websocket v1.5.3 // indirect From eff7847ee8d4fec539d99a10ba1b26d9993a6645 Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Thu, 12 Mar 2026 18:59:25 +0100 Subject: [PATCH 04/10] fix linting --- go.sum | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/go.sum b/go.sum index 7920f5c528..b83ba7e17d 100644 --- a/go.sum +++ b/go.sum @@ -8,10 +8,14 @@ github.com/Pallinder/go-randomdata v1.2.0/go.mod h1:yHmJgulpD2Nfrm0cR9tI/+oAgRqC github.com/ProtonMail/go-crypto v1.0.0 h1:LRuvITjQWX+WIfr930YHG2HNfjR1uOfyf5vE0kC2U78= github.com/ProtonMail/go-crypto v1.0.0/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0= github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk= +github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8= +github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4= github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ= github.com/apapsch/go-jsonmerge/v2 v2.0.0/go.mod h1:lvDnEdqiQrp0O42VQGgmlKpxL1AP2+08jFMw88y4klk= github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI= github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= +github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= +github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w= @@ -64,6 +68,8 @@ github.com/elastic/go-windows v1.0.2 h1:yoLLsAsV5cfg9FLhZ9EXZ2n2sQFKeDYrHenkcivY github.com/elastic/go-windows v1.0.2/go.mod h1:bGcDpBzXgYSqM0Gx3DM4+UxFj300SZLixie9u9ixLM8= github.com/elastic/gosigar v0.14.3 h1:xwkKwPia+hSfg9GqrCUKYdId102m9qTJIIr7egmK/uo= github.com/elastic/gosigar v0.14.3/go.mod h1:iXRIGg2tLnu7LBdpqzyQfGDEidKCfWcCMS0WKyPWoMs= +github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a h1:mATvB/9r/3gvcejNsXKSkQ6lcIaNec2nyfOdlTBR2lU= +github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM= github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= @@ -72,12 +78,16 @@ github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2 github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/fxamacker/cbor/v2 v2.9.0 h1:NpKPmjDBgUfBms6tr6JZkTHtfFGcMKsw3eGcmD/sapM= github.com/fxamacker/cbor/v2 v2.9.0/go.mod h1:vM4b+DJCtHn+zz7h3FFp/hDAI9WNWCsZj23V5ytsSxQ= +github.com/gliderlabs/ssh v0.3.7 h1:iV3Bqi942d9huXnzEF2Mt+CY9gLu8DNM4Obd+8bODRE= +github.com/gliderlabs/ssh v0.3.7/go.mod h1:zpHEXBstFnQYtGnB8k8kQLol82umzn/2/snG7alWVD8= github.com/go-chi/chi/v5 v5.2.5 h1:Eg4myHZBjyvJmAFjFvWgrqDTXFyOzjj7YIm3L3mu6Ug= github.com/go-chi/chi/v5 v5.2.5/go.mod h1:X7Gx4mteadT3eDOMTsXzmI4/rwUpOwBHLpAfupzFJP0= github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI= github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic= github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+mTU= github.com/go-git/go-billy/v5 v5.5.0/go.mod h1:hmexnoNsr2SJU1Ju67OaNz5ASJY3+sHgFRpCtpDCKow= +github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMje31YglSBqCdIqdhKBW8lokaMrL3uTkpGYlE2OOT4= +github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399/go.mod h1:1OCfN199q1Jm3HZlxleg+Dw/mwps2Wbk9frAWm+4FII= github.com/go-git/go-git/v5 v5.12.0 h1:7Md+ndsjrzZxbddRDZjF14qK+NN56sy6wkqaVrjZtys= github.com/go-git/go-git/v5 v5.12.0/go.mod h1:FTM9VKtnI2m65hNI/TenDDDnUf2Q9FHnXYjuz9i5OEY= github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= @@ -160,6 +170,8 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/oapi-codegen/runtime v1.2.0 h1:RvKc1CVS1QeKSNzO97FBQbSMZyQ8s6rZd+LpmzwHMP4= github.com/oapi-codegen/runtime v1.2.0/go.mod h1:Y7ZhmmlE8ikZOmuHRRndiIm7nf3xcVv+YMweKgG1DT0= +github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI= +github.com/onsi/gomega v1.27.10/go.mod h1:RsS8tutOdbdgzbPtzzATp12yT7kM5I5aElG3evPbQ0M= github.com/open-telemetry/opamp-go v0.23.0 h1:k7h7w/muprut9/DAhUC4anX4v7hIdgO02gIsSjV4uq0= github.com/open-telemetry/opamp-go v0.23.0/go.mod h1:DIIVdkLefdqPW5L+4I2twmAicVrTB0Bp5XJAfedZzAM= github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= @@ -326,6 +338,8 @@ golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuX golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= +golang.org/x/term v0.40.0 h1:36e4zGLqU4yhjlmxEaagx2KuYbJq3EwY8K943ZsHcvg= +golang.org/x/term v0.40.0/go.mod h1:w2P8uVp06p2iyKKuvXIm7N/y0UCRt3UfJTfZ7oOpglM= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= From de623a90155005e55155c0b9d2168d924f0caa4b Mon Sep 17 00:00:00 2001 From: Victor Martinez Date: Mon, 23 Mar 2026 16:08:18 +0100 Subject: [PATCH 05/10] go mod tidy --- go.sum | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/go.sum b/go.sum index af084b6efd..cf68cf87d3 100644 --- a/go.sum +++ b/go.sum @@ -168,8 +168,6 @@ github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3N github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= -github.com/oapi-codegen/runtime v1.2.0 h1:RvKc1CVS1QeKSNzO97FBQbSMZyQ8s6rZd+LpmzwHMP4= -github.com/oapi-codegen/runtime v1.2.0/go.mod h1:Y7ZhmmlE8ikZOmuHRRndiIm7nf3xcVv+YMweKgG1DT0= github.com/oapi-codegen/runtime v1.3.0 h1:vyK1zc0gDWWXgk2xoQa4+X4RNNc5SL2RbTpJS/4vMYA= github.com/oapi-codegen/runtime v1.3.0/go.mod h1:kOdeacKy7t40Rclb1je37ZLFboFxh+YLy0zaPCMibPY= github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI= @@ -291,14 +289,10 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= -golang.org/x/crypto v0.48.0 h1:/VRzVqiRSggnhY7gNRxPauEQ5Drw9haKdM0jqfcCFts= -golang.org/x/crypto v0.48.0/go.mod h1:r0kV5h3qnFPlQnBSrULhlsRfryS2pmewsg+XfMgkVos= golang.org/x/crypto v0.49.0 h1:+Ng2ULVvLHnJ/ZFEq4KdcDd/cfjrrjjNSXNzxg0Y4U4= golang.org/x/crypto v0.49.0/go.mod h1:ErX4dUh2UM+CFYiXZRTcMpEcN8b/1gxEuv3nODoYtCA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/mod v0.32.0 h1:9F4d3PHLljb6x//jOyokMv3eX+YDeepZSEo3mFJy93c= -golang.org/x/mod v0.32.0/go.mod h1:SgipZ/3h2Ci89DlEtEXWUk/HteuRin+HHhN+WbNhguU= golang.org/x/mod v0.33.0 h1:tHFzIWbBifEmbwtGz65eaWyGiGZatSrT9prnU8DbVL8= golang.org/x/mod v0.33.0/go.mod h1:swjeQEj+6r7fODbD2cqrnje9PnziFuw4bmLbBZFrQ5w= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= @@ -308,8 +302,6 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= -golang.org/x/net v0.51.0 h1:94R/GTO7mt3/4wIKpcR5gkGmRLOuE/2hNGeWq/GBIFo= -golang.org/x/net v0.51.0/go.mod h1:aamm+2QF5ogm02fjy5Bb7CQ0WMt1/WVM7FtyaTLlA9Y= golang.org/x/net v0.52.0 h1:He/TN1l0e4mmR3QqHMT2Xab3Aj3L9qjbhRm78/6jrW0= golang.org/x/net v0.52.0/go.mod h1:R1MAz7uMZxVMualyPXb+VaqGSa3LIaUqk0eEt3w36Sw= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -339,8 +331,6 @@ golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k= -golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo= golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= @@ -348,8 +338,8 @@ golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuX golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= -golang.org/x/term v0.40.0 h1:36e4zGLqU4yhjlmxEaagx2KuYbJq3EwY8K943ZsHcvg= -golang.org/x/term v0.40.0/go.mod h1:w2P8uVp06p2iyKKuvXIm7N/y0UCRt3UfJTfZ7oOpglM= +golang.org/x/term v0.41.0 h1:QCgPso/Q3RTJx2Th4bDLqML4W6iJiaXFq2/ftQF13YU= +golang.org/x/term v0.41.0/go.mod h1:3pfBgksrReYfZ5lvYM0kSO0LIkAl4Yl2bXOkKP7Ec2A= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= @@ -357,8 +347,6 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= -golang.org/x/text v0.34.0 h1:oL/Qq0Kdaqxa1KbNeMKwQq0reLCCaFtqu2eNuSeNHbk= -golang.org/x/text v0.34.0/go.mod h1:homfLqTYRFyVYemLBFl5GgL/DWEiH5wcsQ5gSh1yziA= golang.org/x/text v0.35.0 h1:JOVx6vVDFokkpaq1AEptVzLTpDe9KGpj5tR4/X+ybL8= golang.org/x/text v0.35.0/go.mod h1:khi/HExzZJ2pGnjenulevKNX1W67CUy0AsXcNubPGCA= golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U= @@ -367,8 +355,6 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= -golang.org/x/tools v0.41.0 h1:a9b8iMweWG+S0OBnlU36rzLp20z1Rp10w+IY2czHTQc= -golang.org/x/tools v0.41.0/go.mod h1:XSY6eDqxVNiYgezAVqqCeihT4j1U2CCsqvH3WhQpnlg= golang.org/x/tools v0.42.0 h1:uNgphsn75Tdz5Ji2q36v/nsFSfR/9BRFvqhGBaJGd5k= golang.org/x/tools v0.42.0/go.mod h1:Ma6lCIwGZvHK6XtgbswSoWroEkhugApmsXyrUmBhfr0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= From bf17d6ffe9cf467142d90cbc44dbb1004cb3c079 Mon Sep 17 00:00:00 2001 From: "fr4nc1sc0.r4m0n" Date: Thu, 9 Jul 2026 11:53:05 +0200 Subject: [PATCH 06/10] fix(ci): update magefile_test.go license header to Elastic License 2.0 --- magefile_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/magefile_test.go b/magefile_test.go index a468713400..87c6e72a5c 100644 --- a/magefile_test.go +++ b/magefile_test.go @@ -1,6 +1,6 @@ // Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one -// or more contributor license agreements. Licensed under the Elastic License; -// you may not use this file except in compliance with the Elastic License. +// or more contributor license agreements. Licensed under the Elastic License 2.0; +// you may not use this file except in compliance with the Elastic License 2.0. //go:build mage From c67df45be48133d3e1da6dd2cdcf3ffd5695cdc0 Mon Sep 17 00:00:00 2001 From: "fr4nc1sc0.r4m0n" Date: Thu, 9 Jul 2026 12:42:06 +0200 Subject: [PATCH 07/10] Make release automation idempotent on re-run Allow release workflows to be safely retriggered by no-oping when the target version is already applied, reusing existing branches and open PRs, and skipping empty commits. --- RELEASE.md | 21 +++++- magefile.go | 83 +++++++++++++++++++-- magefile_test.go | 188 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 285 insertions(+), 7 deletions(-) diff --git a/RELEASE.md b/RELEASE.md index c21df14b31..38a781d6c2 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -146,6 +146,21 @@ mage release:runMajorMinor - Discard changes with `git checkout .` - Or proceed with `DRY_RUN=false` +## Idempotency + +All release automation steps are designed to be **idempotent** — safe to re-run after a partial failure or CI retry: + +| Step | Re-run behavior | +|------|-----------------| +| `updateVersion` | No-op if `DefaultVersion` already matches | +| `updateMergify` | No-op if backport rule already exists | +| `createBranch` | Checks out existing branch instead of failing | +| `commitAll` | Skips commit when there are no changes | +| `push` | Succeeds when remote is already up to date | +| `createPR` | Returns existing open PR for the same head/base | + +This means `mage release:runMajorMinor` and `mage release:runPatch` can be safely retried without manual cleanup when a step has already completed. + ## Testing on a Fork Before using the release automation in production, it's **highly recommended** to test on your personal fork first. This allows you to verify the workflow end-to-end without affecting the official repository. @@ -273,14 +288,16 @@ mage release:runMajorMinor - Verify: `gh auth status` **Issue: "branch already exists"** -- Delete the existing branch first: +- The automation checks out the existing branch automatically on re-run +- To start fresh, delete the branch manually: ```bash git branch -D 9.5 git push origin --delete 9.5 ``` **Issue: "PR already exists"** -- Close or merge the existing PR first +- The automation returns the existing open PR on re-run +- To create a new PR, close the existing one first - Or use a different version number (e.g., `9.5.0-test2`) ### Best Practices diff --git a/magefile.go b/magefile.go index 3047df6906..4f487d65c9 100644 --- a/magefile.go +++ b/magefile.go @@ -2370,9 +2370,16 @@ func (Release) UpdateVersion(newVersion string) error { // Replace the version string // Pattern: const DefaultVersion = "X.Y.Z" re := regexp.MustCompile(`(const\s+DefaultVersion\s*=\s*)"[^"]+"`) - newContent := re.ReplaceAllString(string(content), `${1}"`+newVersion+`"`) + contentStr := string(content) + alreadyAtVersion := regexp.MustCompile(`const\s+DefaultVersion\s*=\s*"` + regexp.QuoteMeta(newVersion) + `"`) + if alreadyAtVersion.MatchString(contentStr) { + fmt.Printf(" Version already set to %s in %s\n", newVersion, versionFile) + return nil + } + + newContent := re.ReplaceAllString(contentStr, `${1}"`+newVersion+`"`) - if newContent == string(content) { + if newContent == contentStr { return fmt.Errorf("version pattern not found in %s", versionFile) } @@ -2454,6 +2461,22 @@ func (g *GitRepo) createBranch(branchName string) error { return fmt.Errorf("failed to get worktree: %w", err) } + refName := plumbing.NewBranchReferenceName(branchName) + _, err = g.repo.Reference(refName, true) + if err == nil { + err = w.Checkout(&git.CheckoutOptions{ + Branch: refName, + }) + if err != nil { + return fmt.Errorf("failed to checkout existing branch: %w", err) + } + fmt.Printf("✓ Checked out existing branch: %s\n", branchName) + return nil + } + if !errors.Is(err, plumbing.ErrReferenceNotFound) { + return fmt.Errorf("failed to check branch: %w", err) + } + // Get current HEAD reference headRef, err := g.repo.Head() if err != nil { @@ -2461,7 +2484,6 @@ func (g *GitRepo) createBranch(branchName string) error { } // Create new branch reference - refName := plumbing.NewBranchReferenceName(branchName) ref := plumbing.NewHashReference(refName, headRef.Hash()) err = g.repo.Storer.SetReference(ref) @@ -2488,6 +2510,15 @@ func (g *GitRepo) commitAll(message, authorName, authorEmail string) error { return fmt.Errorf("failed to get worktree: %w", err) } + status, err := w.Status() + if err != nil { + return fmt.Errorf("failed to get worktree status: %w", err) + } + if status.IsClean() { + fmt.Println(" No changes to commit") + return nil + } + // Add all changes err = w.AddWithOptions(&git.AddOptions{ All: true, @@ -2550,7 +2581,7 @@ func (g *GitRepo) getCurrentBranch() (string, error) { // setRemoteURL sets the URL for a remote func (g *GitRepo) setRemoteURL(remoteName, url string) error { - _, err := g.repo.Remote(remoteName) + remote, err := g.repo.Remote(remoteName) if errors.Is(err, git.ErrRemoteNotFound) { // Remote doesn't exist, create it _, err = g.repo.CreateRemote(&config.RemoteConfig{ @@ -2566,6 +2597,10 @@ func (g *GitRepo) setRemoteURL(remoteName, url string) error { return fmt.Errorf("failed to get remote: %w", err) } + if len(remote.Config().URLs) > 0 && remote.Config().URLs[0] == url { + return nil + } + // Update remote URL err = g.repo.DeleteRemote(remoteName) if err != nil { @@ -2595,6 +2630,23 @@ func newGitHubClient() (*github.Client, error) { return client, nil } +// findExistingPR returns an open pull request for the given head and base branches, if one exists. +func findExistingPR(ctx context.Context, client *github.Client, owner, repo, head, base string) (*github.PullRequest, error) { + opts := &github.PullRequestListOptions{ + State: "open", + Head: fmt.Sprintf("%s:%s", owner, head), + Base: base, + } + prs, _, err := client.PullRequests.List(ctx, owner, repo, opts) + if err != nil { + return nil, fmt.Errorf("failed to list pull requests: %w", err) + } + if len(prs) > 0 { + return prs[0], nil + } + return nil, nil +} + // createPR creates a pull request on GitHub func createPR(client *github.Client, cfg *ReleaseConfig, title, body, head, base string, dryRun bool) (*github.PullRequest, error) { if dryRun { @@ -2605,13 +2657,34 @@ func createPR(client *github.Client, cfg *ReleaseConfig, title, body, head, base return nil, nil } - pr, _, err := client.PullRequests.Create(context.Background(), cfg.Owner, cfg.Repo, &github.NewPullRequest{ + ctx := context.Background() + existingPR, err := findExistingPR(ctx, client, cfg.Owner, cfg.Repo, head, base) + if err != nil { + return nil, err + } + if existingPR != nil { + fmt.Printf(" Pull request already exists: #%d %s\n", existingPR.GetNumber(), existingPR.GetHTMLURL()) + return existingPR, nil + } + + pr, _, err := client.PullRequests.Create(ctx, cfg.Owner, cfg.Repo, &github.NewPullRequest{ Title: github.String(title), Head: github.String(head), Base: github.String(base), Body: github.String(body), }) if err != nil { + var ghErr *github.ErrorResponse + if errors.As(err, &ghErr) && ghErr.Response.StatusCode == 422 { + existingPR, listErr := findExistingPR(ctx, client, cfg.Owner, cfg.Repo, head, base) + if listErr != nil { + return nil, fmt.Errorf("failed to create PR: %w", err) + } + if existingPR != nil { + fmt.Printf(" Pull request already exists: #%d %s\n", existingPR.GetNumber(), existingPR.GetHTMLURL()) + return existingPR, nil + } + } return nil, fmt.Errorf("failed to create PR: %w", err) } diff --git a/magefile_test.go b/magefile_test.go index 87c6e72a5c..92fd6e2175 100644 --- a/magefile_test.go +++ b/magefile_test.go @@ -11,6 +11,9 @@ import ( "path/filepath" "strings" "testing" + + "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing/object" ) func TestUpdateVersion(t *testing.T) { @@ -95,6 +98,191 @@ package version } } +func TestUpdateVersion_Idempotent(t *testing.T) { + tmpDir := t.TempDir() + versionDir := filepath.Join(tmpDir, "version") + err := os.Mkdir(versionDir, 0755) + if err != nil { + t.Fatalf("failed to create version dir: %v", err) + } + + versionFile := filepath.Join(versionDir, "version.go") + initialContent := `package version + +const DefaultVersion = "9.4.0" +` + err = os.WriteFile(versionFile, []byte(initialContent), 0644) + if err != nil { + t.Fatalf("failed to write initial file: %v", err) + } + + origDir, _ := os.Getwd() + defer os.Chdir(origDir) + os.Chdir(tmpDir) + + r := Release{} + err = r.UpdateVersion("9.5.0") + if err != nil { + t.Fatalf("first UpdateVersion() failed: %v", err) + } + + content1, err := os.ReadFile(versionFile) + if err != nil { + t.Fatalf("failed to read file after first update: %v", err) + } + + err = r.UpdateVersion("9.5.0") + if err != nil { + t.Fatalf("second UpdateVersion() failed: %v", err) + } + + content2, err := os.ReadFile(versionFile) + if err != nil { + t.Fatalf("failed to read file after second update: %v", err) + } + + if string(content1) != string(content2) { + t.Error("UpdateVersion() is not idempotent - content changed on second run") + } +} + +func TestUpdateVersion_AlreadyAtVersion(t *testing.T) { + tmpDir := t.TempDir() + versionDir := filepath.Join(tmpDir, "version") + err := os.Mkdir(versionDir, 0755) + if err != nil { + t.Fatalf("failed to create version dir: %v", err) + } + + versionFile := filepath.Join(versionDir, "version.go") + initialContent := `package version + +const DefaultVersion = "9.5.0" +` + err = os.WriteFile(versionFile, []byte(initialContent), 0644) + if err != nil { + t.Fatalf("failed to write initial file: %v", err) + } + + origDir, _ := os.Getwd() + defer os.Chdir(origDir) + os.Chdir(tmpDir) + + r := Release{} + err = r.UpdateVersion("9.5.0") + if err != nil { + t.Fatalf("UpdateVersion() failed when version already set: %v", err) + } + + content, err := os.ReadFile(versionFile) + if err != nil { + t.Fatalf("failed to read file: %v", err) + } + if string(content) != initialContent { + t.Errorf("UpdateVersion() modified file when already at target version") + } +} + +func createTestGitRepo(t *testing.T) (*GitRepo, string) { + t.Helper() + + tmpDir := t.TempDir() + repo, err := git.PlainInit(tmpDir, false) + if err != nil { + t.Fatalf("failed to init repo: %v", err) + } + + w, err := repo.Worktree() + if err != nil { + t.Fatalf("failed to get worktree: %v", err) + } + + testFile := filepath.Join(tmpDir, "README.md") + err = os.WriteFile(testFile, []byte("# Test Repo"), 0644) + if err != nil { + t.Fatalf("failed to write test file: %v", err) + } + + _, err = w.Add("README.md") + if err != nil { + t.Fatalf("failed to add file: %v", err) + } + + _, err = w.Commit("Initial commit", &git.CommitOptions{ + Author: &object.Signature{ + Name: "Test User", + Email: "test@example.com", + }, + }) + if err != nil { + t.Fatalf("failed to create initial commit: %v", err) + } + + return &GitRepo{repo: repo}, tmpDir +} + +func TestCreateBranch_Idempotent(t *testing.T) { + gitRepo, _ := createTestGitRepo(t) + + err := gitRepo.createBranch("9.5") + if err != nil { + t.Fatalf("first createBranch() failed: %v", err) + } + + branch, err := gitRepo.getCurrentBranch() + if err != nil { + t.Fatalf("getCurrentBranch() failed: %v", err) + } + if branch != "9.5" { + t.Fatalf("expected branch 9.5, got %s", branch) + } + + err = gitRepo.createBranch("9.5") + if err != nil { + t.Fatalf("second createBranch() failed: %v", err) + } + + branch, err = gitRepo.getCurrentBranch() + if err != nil { + t.Fatalf("getCurrentBranch() after second call failed: %v", err) + } + if branch != "9.5" { + t.Errorf("createBranch() is not idempotent - on branch %s, want 9.5", branch) + } +} + +func TestCommitAll_NoChanges(t *testing.T) { + gitRepo, _ := createTestGitRepo(t) + + err := gitRepo.commitAll("Empty commit", "Test Author", "test@example.com") + if err != nil { + t.Errorf("commitAll() with no changes error = %v", err) + } +} + +func TestSetRemoteURL_Idempotent(t *testing.T) { + gitRepo, _ := createTestGitRepo(t) + remoteURL := "https://github.com/test/repo.git" + + err := gitRepo.setRemoteURL("origin", remoteURL) + if err != nil { + t.Fatalf("first setRemoteURL() failed: %v", err) + } + + err = gitRepo.setRemoteURL("origin", remoteURL) + if err != nil { + t.Fatalf("second setRemoteURL() failed: %v", err) + } + + remote, err := gitRepo.repo.Remote("origin") + if err != nil { + t.Fatalf("failed to get remote: %v", err) + } + if len(remote.Config().URLs) == 0 || remote.Config().URLs[0] != remoteURL { + t.Errorf("setRemoteURL() URL = %v, want %s", remote.Config().URLs, remoteURL) + } +} + func TestUpdateMergify(t *testing.T) { tests := []struct { name string From 64eae34b55cbf3e6fe366fe02b70c87e56453658 Mon Sep 17 00:00:00 2001 From: "fr4nc1sc0.r4m0n" Date: Thu, 9 Jul 2026 12:50:44 +0200 Subject: [PATCH 08/10] refactor(release): extract FF automation to dev-tools/mage/release Move release workflow logic into a dedicated package aligned with elastic-agent and beats, leaving thin mage release:* wrappers in magefile.go. --- RELEASE.md | 26 +- dev-tools/go.mod | 27 +- dev-tools/go.sum | 98 ++- dev-tools/mage/release/git.go | 188 ++++++ dev-tools/mage/release/git_test.go | 115 ++++ dev-tools/mage/release/github.go | 106 ++++ dev-tools/mage/release/release.go | 296 +++++++++ .../mage/release/release_test.go | 238 ++----- go.mod | 7 +- magefile.go | 586 +----------------- 10 files changed, 937 insertions(+), 750 deletions(-) create mode 100644 dev-tools/mage/release/git.go create mode 100644 dev-tools/mage/release/git_test.go create mode 100644 dev-tools/mage/release/github.go create mode 100644 dev-tools/mage/release/release.go rename magefile_test.go => dev-tools/mage/release/release_test.go (64%) diff --git a/RELEASE.md b/RELEASE.md index 38a781d6c2..7e70288da2 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -350,13 +350,23 @@ git stash # or commit your changes ## Architecture -Fleet-server follows the same architecture as elastic-agent but is simpler: +Fleet-server follows the same architecture as elastic-agent and beats: **Key Differences from elastic-agent:** -- Fewer files to update (only 1: `version/version.go`) +- Fewer files to update (only `version/version.go` and `.mergify.yml`) - No docs/K8s manifest updates needed -- All code in `magefile.go` (no separate package) -- Simpler implementation (~540 lines vs ~2000+ in elastic-agent) + +**File structure:** +``` +dev-tools/mage/release/ +├── release.go # Version/mergify updates and workflow orchestration +├── git.go # Git operations (branch, commit, push) +├── github.go # GitHub API integration (PR creation) +├── release_test.go +└── git_test.go +``` + +`magefile.go` exposes thin `mage release:*` wrappers that delegate to this package. **Libraries used:** - `github.com/go-git/go-git/v5` - Git operations @@ -442,14 +452,14 @@ git push Run unit tests for release functions: ```bash -go test -v -tags=mage -run "TestUpdate|TestLoad|TestPrepare" . +cd dev-tools && go test ./mage/release/... -count=1 ``` Check test coverage: ```bash -go test -v -tags=mage -coverprofile=coverage.out . -go tool cover -func=coverage.out | grep -E "(UpdateVersion|UpdateMergify)" +cd dev-tools && go test ./mage/release/... -coverprofile=coverage.out +go tool cover -func=coverage.out ``` ## Migration from Makefile @@ -474,7 +484,7 @@ This replaces the old Makefile-based release process: - **Issues**: https://github.com/elastic/fleet-server/issues - **Documentation**: This file and migration plan -- **Code**: `magefile.go` (search for "Release Automation") +- **Code**: `dev-tools/mage/release/` and `magefile.go` release wrappers --- diff --git a/dev-tools/go.mod b/dev-tools/go.mod index 2f31faa140..63e91b1c12 100644 --- a/dev-tools/go.mod +++ b/dev-tools/go.mod @@ -13,33 +13,56 @@ tool ( ) require ( + github.com/go-git/go-git/v5 v5.12.0 + github.com/google/go-github/v68 v68.0.0 +) + +require ( + dario.cat/mergo v1.0.0 // indirect + github.com/Microsoft/go-winio v0.6.1 // indirect + github.com/ProtonMail/go-crypto v1.0.0 // indirect github.com/aclements/go-moremath v0.0.0-20210112150236-f10218a38794 // indirect + github.com/cloudflare/circl v1.3.7 // indirect github.com/cyphar/filepath-securejoin v0.2.5 // indirect github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect github.com/elastic/go-json-schema-generate v0.0.0-20220323152209-ec19b88f6b5e // indirect github.com/elastic/go-licenser v0.4.2 // indirect + github.com/emirpasic/gods v1.18.1 // indirect github.com/getkin/kin-openapi v0.132.0 // indirect + github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect + github.com/go-git/go-billy/v5 v5.5.0 // indirect github.com/go-openapi/jsonpointer v0.21.0 // indirect github.com/go-openapi/swag v0.23.0 // indirect + github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect + github.com/google/go-querystring v1.1.0 // indirect github.com/google/licenseclassifier v0.0.0-20200402202327-879cb1424de0 // indirect + github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/jstemmer/go-junit-report v1.0.0 // indirect + github.com/kevinburke/ssh_config v1.2.0 // indirect github.com/mailru/easyjson v0.7.7 // indirect github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect github.com/oapi-codegen/oapi-codegen/v2 v2.5.0 // indirect github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037 // indirect github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90 // indirect github.com/perimeterx/marshmallow v1.1.5 // indirect - github.com/sergi/go-diff v1.1.0 // indirect + github.com/pjbgf/sha1cd v0.3.0 // indirect + github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect + github.com/skeema/knownhosts v1.2.2 // indirect github.com/speakeasy-api/jsonpath v0.6.0 // indirect github.com/speakeasy-api/openapi-overlay v0.10.2 // indirect github.com/vmware-labs/yaml-jsonpath v0.3.2 // indirect + github.com/xanzy/ssh-agent v0.3.3 // indirect go.elastic.co/go-licence-detector v0.7.0 // indirect + golang.org/x/crypto v0.38.0 // indirect golang.org/x/mod v0.24.0 // indirect + golang.org/x/net v0.40.0 // indirect golang.org/x/perf v0.0.0-20250305200902-02a15fd477ba // indirect golang.org/x/sync v0.14.0 // indirect - golang.org/x/text v0.23.0 // indirect + golang.org/x/sys v0.33.0 // indirect + golang.org/x/text v0.25.0 // indirect golang.org/x/tools v0.33.0 // indirect + gopkg.in/warnings.v0 v0.1.2 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/dev-tools/go.sum b/dev-tools/go.sum index 8355f27cc1..7a40c2b16a 100644 --- a/dev-tools/go.sum +++ b/dev-tools/go.sum @@ -1,8 +1,23 @@ +dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk= +dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk= +github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY= +github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow= +github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= +github.com/ProtonMail/go-crypto v1.0.0 h1:LRuvITjQWX+WIfr930YHG2HNfjR1uOfyf5vE0kC2U78= +github.com/ProtonMail/go-crypto v1.0.0/go.mod h1:EjAoLdwvbIOoOQr3ihjnSoLZRtE8azugULFRteWMNc0= github.com/aclements/go-moremath v0.0.0-20210112150236-f10218a38794 h1:xlwdaKcTNVW4PtpQb8aKA4Pjy0CdJHEqvFbAnvR5m2g= github.com/aclements/go-moremath v0.0.0-20210112150236-f10218a38794/go.mod h1:7e+I0LQFUI9AXWxOfsQROs9xPhoJtbsyWcjJqDd4KPY= +github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8= +github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4= +github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= +github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= +github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= +github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA= +github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU= +github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA= github.com/cyphar/filepath-securejoin v0.2.5 h1:6iR5tXJ/e6tJZzzdMc1km3Sa7RRIVBKAK32O2s7AYfo= github.com/cyphar/filepath-securejoin v0.2.5/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -15,11 +30,25 @@ github.com/elastic/go-json-schema-generate v0.0.0-20220323152209-ec19b88f6b5e h1 github.com/elastic/go-json-schema-generate v0.0.0-20220323152209-ec19b88f6b5e/go.mod h1:w6t176CDaF2cZXwuQtFA5T+trYjvo5OYxLbBwAE7gxU= github.com/elastic/go-licenser v0.4.2 h1:bPbGm8bUd8rxzSswFOqvQh1dAkKGkgAmrPxbUi+Y9+A= github.com/elastic/go-licenser v0.4.2/go.mod h1:W8eH6FaZDR8fQGm+7FnVa7MxI1b/6dAqxz+zPB8nm5c= +github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a h1:mATvB/9r/3gvcejNsXKSkQ6lcIaNec2nyfOdlTBR2lU= +github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM= +github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= +github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/getkin/kin-openapi v0.132.0 h1:3ISeLMsQzcb5v26yeJrBcdTCEQTag36ZjaGk7MIRUwk= github.com/getkin/kin-openapi v0.132.0/go.mod h1:3OlG51PCYNsPByuiMB0t4fjnNlIDnaEDsjiKUV8nL58= +github.com/gliderlabs/ssh v0.3.7 h1:iV3Bqi942d9huXnzEF2Mt+CY9gLu8DNM4Obd+8bODRE= +github.com/gliderlabs/ssh v0.3.7/go.mod h1:zpHEXBstFnQYtGnB8k8kQLol82umzn/2/snG7alWVD8= +github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI= +github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmSxCcxctByoQdvwPiA7DTK7jaaFDBTtu0ic= +github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+mTU= +github.com/go-git/go-billy/v5 v5.5.0/go.mod h1:hmexnoNsr2SJU1Ju67OaNz5ASJY3+sHgFRpCtpDCKow= +github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMje31YglSBqCdIqdhKBW8lokaMrL3uTkpGYlE2OOT4= +github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399/go.mod h1:1OCfN199q1Jm3HZlxleg+Dw/mwps2Wbk9frAWm+4FII= +github.com/go-git/go-git/v5 v5.12.0 h1:7Md+ndsjrzZxbddRDZjF14qK+NN56sy6wkqaVrjZtys= +github.com/go-git/go-git/v5 v5.12.0/go.mod h1:FTM9VKtnI2m65hNI/TenDDDnUf2Q9FHnXYjuz9i5OEY= github.com/go-openapi/jsonpointer v0.21.0 h1:YgdVicSA9vH5RiHs9TZW5oyafXZFc6+2Vc1rr/O9oNQ= github.com/go-openapi/jsonpointer v0.21.0/go.mod h1:IUyH9l/+uyhIYQ/PXVA41Rexl+kOkAPDdXEYns6fzUY= github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+GrE= @@ -27,6 +56,8 @@ github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM= github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE= +github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE= +github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= @@ -40,18 +71,27 @@ github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5a github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-github/v68 v68.0.0 h1:ZW57zeNZiXTdQ16qrDiZ0k6XucrxZ2CGmoTvcCyQG6s= +github.com/google/go-github/v68 v68.0.0/go.mod h1:K9HAUBovM2sLwM408A18h+wd9vqdLOEqTUCbnRIcx68= +github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= +github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/google/licenseclassifier v0.0.0-20200402202327-879cb1424de0 h1:OggOMmdI0JLwg1FkOKH9S7fVHF0oEm8PX6S8kAdpOps= github.com/google/licenseclassifier v0.0.0-20200402202327-879cb1424de0/go.mod h1:qsqn2hxC+vURpyBRygGUuinTO42MFRLcsmQ/P8v94+M= github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= +github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= +github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/jstemmer/go-junit-report v1.0.0 h1:8X1gzZpR+nVQLAht+L/foqOeX2l9DTZoaIPbEQHxsds= github.com/jstemmer/go-junit-report v1.0.0/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= +github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4= +github.com/kevinburke/ssh_config v1.2.0/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= @@ -82,22 +122,32 @@ github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1Cpa github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY= -github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw= github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro= +github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI= +github.com/onsi/gomega v1.27.10/go.mod h1:RsS8tutOdbdgzbPtzzATp12yT7kM5I5aElG3evPbQ0M= github.com/perimeterx/marshmallow v1.1.5 h1:a2LALqQ1BlHM8PZblsDdidgv1mWi1DgC2UmX50IvK2s= github.com/perimeterx/marshmallow v1.1.5/go.mod h1:dsXbUu8CRzfYP5a87xpp0xq9S3u0Vchtcl8we9tYaXw= +github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4= +github.com/pjbgf/sha1cd v0.3.0/go.mod h1:nZ1rrWOcGJ5uZgEEVL1VUM9iRQiZvWdbZjkKyFzPPsI= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= -github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= +github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8= +github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4= +github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/skeema/knownhosts v1.2.2 h1:Iug2P4fLmDw9f41PB6thxUkNUkJzB5i+1/exaj40L3A= +github.com/skeema/knownhosts v1.2.2/go.mod h1:xYbVRSPxqBZFrdmDyMmsOs+uX1UZC3nTN3ThzgDxUwo= github.com/speakeasy-api/jsonpath v0.6.0 h1:IhtFOV9EbXplhyRqsVhHoBmmYjblIRh5D1/g8DHMXJ8= github.com/speakeasy-api/jsonpath v0.6.0/go.mod h1:ymb2iSkyOycmzKwbEAYPJV/yi2rSmvBCLZJcyD+VVWw= github.com/speakeasy-api/openapi-overlay v0.10.2 h1:VOdQ03eGKeiHnpb1boZCGm7x8Haj6gST0P3SGTX95GU= github.com/speakeasy-api/openapi-overlay v0.10.2/go.mod h1:n0iOU7AqKpNFfEt6tq7qYITC4f0yzVVdFw0S7hukemg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= @@ -107,13 +157,24 @@ github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4d github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= github.com/vmware-labs/yaml-jsonpath v0.3.2 h1:/5QKeCBGdsInyDCyVNLbXyilb61MXGi9NP674f9Hobk= github.com/vmware-labs/yaml-jsonpath v0.3.2/go.mod h1:U6whw1z03QyqgWdgXxvVnQ90zN1BWz5V+51Ewf8k+rQ= +github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM= +github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= go.elastic.co/go-licence-detector v0.7.0 h1:qC31sfyfNcNx/zMYcLABU0ac3MbGHZgksCAb5lMDUMg= go.elastic.co/go-licence-detector v0.7.0/go.mod h1:f5ty8pjynzQD8BcS+s0qtlOGKc35/HKQxCVi8SHhV5k= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= +golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= +golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8= +golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU= golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -121,8 +182,14 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210428140749-89ef3d95e781/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= +golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= +golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc= golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY= golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds= golang.org/x/perf v0.0.0-20250305200902-02a15fd477ba h1:0iM7SkkyXtZKGBWfn+ka1Rj6VOCRyFMkEHaCWsaR7no= @@ -130,6 +197,8 @@ golang.org/x/perf v0.0.0-20250305200902-02a15fd477ba/go.mod h1:YEhzh+7vn0fUjydWE golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.14.0 h1:woo0S4Yywslg6hp4eUFjTVOyKt0RookbpAHG4c1HmhQ= golang.org/x/sync v0.14.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -137,28 +206,47 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210112080510-489259a85091/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw= golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= +golang.org/x/term v0.32.0 h1:DR4lr0TjUs3epypdhTOkMmuF5CDFJ/8pOnbzMZPQ7bg= +golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY= -golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4= +golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= +golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4= +golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/tools v0.33.0 h1:4qz2S3zmRxbGIhDIAgjxvFutSvH5EfnsYrRBj0UI0bc= golang.org/x/tools v0.33.0/go.mod h1:CIJMaWEY88juyUfo7UbgPqbC8rU2OqfAV1h2Qp0oMYI= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -180,6 +268,8 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EV gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME= +gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/dev-tools/mage/release/git.go b/dev-tools/mage/release/git.go new file mode 100644 index 0000000000..cd3e35e80b --- /dev/null +++ b/dev-tools/mage/release/git.go @@ -0,0 +1,188 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License 2.0; +// you may not use this file except in compliance with the Elastic License 2.0. + +package release + +import ( + "errors" + "fmt" + "time" + + "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/config" + "github.com/go-git/go-git/v5/plumbing" + "github.com/go-git/go-git/v5/plumbing/object" +) + +// GitRepo represents a Git repository with common operations. +type GitRepo struct { + repo *git.Repository +} + +// OpenRepo opens the Git repository at the given path. +func OpenRepo(path string) (*GitRepo, error) { + repo, err := git.PlainOpen(path) + if err != nil { + return nil, fmt.Errorf("failed to open git repo: %w", err) + } + return &GitRepo{repo: repo}, nil +} + +// CreateBranch creates and checks out a new branch, or checks out an existing one. +func (g *GitRepo) CreateBranch(branchName string) error { + w, err := g.repo.Worktree() + if err != nil { + return fmt.Errorf("failed to get worktree: %w", err) + } + + refName := plumbing.NewBranchReferenceName(branchName) + _, err = g.repo.Reference(refName, true) + if err == nil { + err = w.Checkout(&git.CheckoutOptions{ + Branch: refName, + }) + if err != nil { + return fmt.Errorf("failed to checkout existing branch: %w", err) + } + fmt.Printf("✓ Checked out existing branch: %s\n", branchName) + return nil + } + if !errors.Is(err, plumbing.ErrReferenceNotFound) { + return fmt.Errorf("failed to check branch: %w", err) + } + + headRef, err := g.repo.Head() + if err != nil { + return fmt.Errorf("failed to get HEAD: %w", err) + } + + ref := plumbing.NewHashReference(refName, headRef.Hash()) + + err = g.repo.Storer.SetReference(ref) + if err != nil { + return fmt.Errorf("failed to create branch: %w", err) + } + + err = w.Checkout(&git.CheckoutOptions{ + Branch: refName, + }) + if err != nil { + return fmt.Errorf("failed to checkout branch: %w", err) + } + + fmt.Printf("✓ Created and checked out branch: %s\n", branchName) + return nil +} + +// CommitAll commits all changes with the given message. +func (g *GitRepo) CommitAll(message, authorName, authorEmail string) error { + w, err := g.repo.Worktree() + if err != nil { + return fmt.Errorf("failed to get worktree: %w", err) + } + + status, err := w.Status() + if err != nil { + return fmt.Errorf("failed to get worktree status: %w", err) + } + if status.IsClean() { + fmt.Println(" No changes to commit") + return nil + } + + err = w.AddWithOptions(&git.AddOptions{ + All: true, + }) + if err != nil { + return fmt.Errorf("failed to add changes: %w", err) + } + + commit, err := w.Commit(message, &git.CommitOptions{ + Author: &object.Signature{ + Name: authorName, + Email: authorEmail, + When: time.Now(), + }, + }) + if err != nil { + return fmt.Errorf("failed to commit: %w", err) + } + + fmt.Printf("✓ Committed changes: %s\n", commit.String()[:7]) + return nil +} + +// Push pushes the current branch to the remote. +func (g *GitRepo) Push(remoteName string, dryRun bool) error { + if dryRun { + fmt.Printf(" [DRY RUN] Would push to remote: %s\n", remoteName) + return nil + } + + err := g.repo.Push(&git.PushOptions{ + RemoteName: remoteName, + }) + if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) { + return fmt.Errorf("failed to push: %w", err) + } + + if errors.Is(err, git.NoErrAlreadyUpToDate) { + fmt.Println(" Branch already up to date on remote") + } else { + fmt.Printf("✓ Pushed to remote: %s\n", remoteName) + } + return nil +} + +// GetCurrentBranch returns the name of the current branch. +func (g *GitRepo) GetCurrentBranch() (string, error) { + ref, err := g.repo.Head() + if err != nil { + return "", fmt.Errorf("failed to get HEAD: %w", err) + } + + if !ref.Name().IsBranch() { + return "", fmt.Errorf("HEAD is not a branch") + } + + return ref.Name().Short(), nil +} + +// SetRemoteURL sets the URL for a remote. +func (g *GitRepo) SetRemoteURL(remoteName, url string) error { + remote, err := g.repo.Remote(remoteName) + if errors.Is(err, git.ErrRemoteNotFound) { + _, err = g.repo.CreateRemote(&config.RemoteConfig{ + Name: remoteName, + URLs: []string{url}, + }) + if err != nil { + return fmt.Errorf("failed to create remote: %w", err) + } + fmt.Printf("✓ Created remote %s: %s\n", remoteName, url) + return nil + } else if err != nil { + return fmt.Errorf("failed to get remote: %w", err) + } + + if len(remote.Config().URLs) > 0 && remote.Config().URLs[0] == url { + return nil + } + + err = g.repo.DeleteRemote(remoteName) + if err != nil { + return fmt.Errorf("failed to delete remote: %w", err) + } + + _, err = g.repo.CreateRemote(&config.RemoteConfig{ + Name: remoteName, + URLs: []string{url}, + }) + if err != nil { + return fmt.Errorf("failed to recreate remote: %w", err) + } + + fmt.Printf("✓ Updated remote %s: %s\n", remoteName, url) + return nil +} diff --git a/dev-tools/mage/release/git_test.go b/dev-tools/mage/release/git_test.go new file mode 100644 index 0000000000..2f8d3398ed --- /dev/null +++ b/dev-tools/mage/release/git_test.go @@ -0,0 +1,115 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License 2.0; +// you may not use this file except in compliance with the Elastic License 2.0. + +package release + +import ( + "os" + "path/filepath" + "testing" + + "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing/object" +) + +func createTestGitRepo(t *testing.T) (*GitRepo, string) { + t.Helper() + + tmpDir := t.TempDir() + + repo, err := git.PlainInit(tmpDir, false) + if err != nil { + t.Fatalf("failed to init repo: %v", err) + } + + w, err := repo.Worktree() + if err != nil { + t.Fatalf("failed to get worktree: %v", err) + } + + testFile := filepath.Join(tmpDir, "README.md") + err = os.WriteFile(testFile, []byte("# Test Repo"), 0o644) + if err != nil { + t.Fatalf("failed to write test file: %v", err) + } + + _, err = w.Add("README.md") + if err != nil { + t.Fatalf("failed to add file: %v", err) + } + + _, err = w.Commit("Initial commit", &git.CommitOptions{ + Author: &object.Signature{ + Name: "Test User", + Email: "test@example.com", + }, + }) + if err != nil { + t.Fatalf("failed to create initial commit: %v", err) + } + + return &GitRepo{repo: repo}, tmpDir +} + +func TestCreateBranchIdempotent(t *testing.T) { + gitRepo, _ := createTestGitRepo(t) + + err := gitRepo.CreateBranch("9.5") + if err != nil { + t.Fatalf("first CreateBranch() failed: %v", err) + } + + branch, err := gitRepo.GetCurrentBranch() + if err != nil { + t.Fatalf("GetCurrentBranch() failed: %v", err) + } + if branch != "9.5" { + t.Fatalf("expected branch 9.5, got %s", branch) + } + + err = gitRepo.CreateBranch("9.5") + if err != nil { + t.Fatalf("second CreateBranch() failed: %v", err) + } + + branch, err = gitRepo.GetCurrentBranch() + if err != nil { + t.Fatalf("GetCurrentBranch() after second call failed: %v", err) + } + if branch != "9.5" { + t.Errorf("CreateBranch() is not idempotent - on branch %s, want 9.5", branch) + } +} + +func TestCommitAllNoChanges(t *testing.T) { + gitRepo, _ := createTestGitRepo(t) + + err := gitRepo.CommitAll("Empty commit", "Test Author", "test@example.com") + if err != nil { + t.Errorf("CommitAll() with no changes error = %v", err) + } +} + +func TestSetRemoteURLIdempotent(t *testing.T) { + gitRepo, _ := createTestGitRepo(t) + remoteURL := "https://github.com/test/repo.git" + + err := gitRepo.SetRemoteURL("origin", remoteURL) + if err != nil { + t.Fatalf("first SetRemoteURL() failed: %v", err) + } + + err = gitRepo.SetRemoteURL("origin", remoteURL) + if err != nil { + t.Fatalf("second SetRemoteURL() failed: %v", err) + } + + remote, err := gitRepo.repo.Remote("origin") + if err != nil { + t.Fatalf("failed to get remote: %v", err) + } + if len(remote.Config().URLs) == 0 || remote.Config().URLs[0] != remoteURL { + t.Errorf("SetRemoteURL() URL = %v, want %s", remote.Config().URLs, remoteURL) + } +} diff --git a/dev-tools/mage/release/github.go b/dev-tools/mage/release/github.go new file mode 100644 index 0000000000..40b28b2061 --- /dev/null +++ b/dev-tools/mage/release/github.go @@ -0,0 +1,106 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License 2.0; +// you may not use this file except in compliance with the Elastic License 2.0. + +package release + +import ( + "context" + "errors" + "fmt" + "os" + + "github.com/google/go-github/v68/github" +) + +// GitHubClient wraps the GitHub API client. +type GitHubClient struct { + client *github.Client + ctx context.Context +} + +// NewGitHubClient creates a new GitHub client with authentication. +func NewGitHubClient(token string) *GitHubClient { + return &GitHubClient{ + client: github.NewClient(nil).WithAuthToken(token), + ctx: context.Background(), + } +} + +// NewGitHubClientFromEnv creates a GitHub client using GITHUB_TOKEN env var. +func NewGitHubClientFromEnv() (*GitHubClient, error) { + token := os.Getenv("GITHUB_TOKEN") + if token == "" { + return nil, fmt.Errorf("GITHUB_TOKEN environment variable not set") + } + return NewGitHubClient(token), nil +} + +// PROptions contains options for creating a pull request. +type PROptions struct { + Owner string + Repo string + Title string + Head string + Base string + Body string +} + +// FindOpenPR returns an open pull request matching head and base, or nil if none exists. +func (gh *GitHubClient) FindOpenPR(owner, repo, head, base string) (*github.PullRequest, error) { + headRef := fmt.Sprintf("%s:%s", owner, head) + prs, _, err := gh.client.PullRequests.List(gh.ctx, owner, repo, &github.PullRequestListOptions{ + State: "open", + Head: headRef, + Base: base, + }) + if err != nil { + return nil, fmt.Errorf("failed to list pull requests: %w", err) + } + if len(prs) == 0 { + return nil, nil + } + return prs[0], nil +} + +// CreatePR creates a new pull request. +// If an open pull request already exists for the same head and base, it is returned instead. +func (gh *GitHubClient) CreatePR(opts PROptions, dryRun bool) (*github.PullRequest, error) { + if dryRun { + fmt.Printf(" [DRY RUN] Would create PR:\n") + fmt.Printf(" Title: %s\n", opts.Title) + fmt.Printf(" Head: %s\n", opts.Head) + fmt.Printf(" Base: %s\n", opts.Base) + return nil, nil + } + + existing, err := gh.FindOpenPR(opts.Owner, opts.Repo, opts.Head, opts.Base) + if err != nil { + return nil, err + } + if existing != nil { + fmt.Printf(" Pull request already exists: #%d %s\n", existing.GetNumber(), existing.GetHTMLURL()) + return existing, nil + } + + pr, _, err := gh.client.PullRequests.Create(gh.ctx, opts.Owner, opts.Repo, &github.NewPullRequest{ + Title: github.String(opts.Title), + Head: github.String(opts.Head), + Base: github.String(opts.Base), + Body: github.String(opts.Body), + }) + if err != nil { + var ghErr *github.ErrorResponse + if errors.As(err, &ghErr) && ghErr.Response != nil && ghErr.Response.StatusCode == 422 { + existing, findErr := gh.FindOpenPR(opts.Owner, opts.Repo, opts.Head, opts.Base) + if findErr == nil && existing != nil { + fmt.Printf(" Pull request already exists: #%d %s\n", existing.GetNumber(), existing.GetHTMLURL()) + return existing, nil + } + } + return nil, fmt.Errorf("failed to create PR: %w", err) + } + + fmt.Printf("✓ Created PR #%d: %s\n", pr.GetNumber(), pr.GetHTMLURL()) + return pr, nil +} diff --git a/dev-tools/mage/release/release.go b/dev-tools/mage/release/release.go new file mode 100644 index 0000000000..414823b887 --- /dev/null +++ b/dev-tools/mage/release/release.go @@ -0,0 +1,296 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License 2.0; +// you may not use this file except in compliance with the Elastic License 2.0. + +package release + +import ( + "fmt" + "os" + "regexp" + "strconv" + "strings" +) + +// ReleaseConfig holds configuration for release operations. +type ReleaseConfig struct { + Version string + BaseBranch string + ReleaseBranch string + Owner string + Repo string + AuthorName string + AuthorEmail string +} + +// LoadReleaseConfigFromEnv loads release configuration from environment variables. +func LoadReleaseConfigFromEnv() (*ReleaseConfig, error) { + version := os.Getenv("CURRENT_RELEASE") + if version == "" { + return nil, fmt.Errorf("CURRENT_RELEASE environment variable not set") + } + + baseBranch := os.Getenv("BASE_BRANCH") + if baseBranch == "" { + baseBranch = "main" + } + + parts := strings.Split(version, ".") + if len(parts) < 2 { + return nil, fmt.Errorf("invalid version format: %s (expected X.Y.Z)", version) + } + releaseBranch := fmt.Sprintf("%s.%s", parts[0], parts[1]) + + owner := os.Getenv("PROJECT_OWNER") + if owner == "" { + owner = "elastic" + } + + repo := os.Getenv("PROJECT_REPO") + if repo == "" { + repo = "fleet-server" + } + + authorName := os.Getenv("GITHUB_USERNAME") + if authorName == "" { + authorName = "elasticmachine" + } + + authorEmail := os.Getenv("GITHUB_EMAIL") + if authorEmail == "" { + authorEmail = "infra-root+elasticmachine@elastic.co" + } + + return &ReleaseConfig{ + Version: version, + BaseBranch: baseBranch, + ReleaseBranch: releaseBranch, + Owner: owner, + Repo: repo, + AuthorName: authorName, + AuthorEmail: authorEmail, + }, nil +} + +// UpdateVersion updates the version in version/version.go. +func UpdateVersion(newVersion string) error { + versionFile := "version/version.go" + + content, err := os.ReadFile(versionFile) + if err != nil { + return fmt.Errorf("failed to read %s: %w", versionFile, err) + } + + re := regexp.MustCompile(`(const\s+DefaultVersion\s*=\s*)"[^"]+"`) + contentStr := string(content) + alreadyAtVersion := regexp.MustCompile(`const\s+DefaultVersion\s*=\s*"` + regexp.QuoteMeta(newVersion) + `"`) + if alreadyAtVersion.MatchString(contentStr) { + fmt.Printf(" Version already set to %s in %s\n", newVersion, versionFile) + return nil + } + + newContent := re.ReplaceAllString(contentStr, `${1}"`+newVersion+`"`) + + if newContent == contentStr { + return fmt.Errorf("version pattern not found in %s", versionFile) + } + + err = os.WriteFile(versionFile, []byte(newContent), 0o644) + if err != nil { + return fmt.Errorf("failed to write %s: %w", versionFile, err) + } + + fmt.Printf("✓ Updated version to %s in %s\n", newVersion, versionFile) + return nil +} + +// UpdateMergify adds a new backport rule to .mergify.yml. +func UpdateMergify(version string) error { + mergifyFile := ".mergify.yml" + + content, err := os.ReadFile(mergifyFile) + if err != nil { + return fmt.Errorf("failed to read %s: %w", mergifyFile, err) + } + + parts := strings.Split(version, ".") + if len(parts) < 2 { + return fmt.Errorf("invalid version format: %s (expected X.Y.Z)", version) + } + branchVersion := fmt.Sprintf("%s.%s", parts[0], parts[1]) + + searchPattern := fmt.Sprintf("backport patches to %s branch", branchVersion) + if strings.Contains(string(content), searchPattern) { + fmt.Printf(" Backport rule for %s already exists\n", branchVersion) + return nil + } + + newRule := fmt.Sprintf(` - name: backport patches to %s branch + conditions: + - merged + - label=backport-%s + actions: + backport: + branches: + - "%s" +`, branchVersion, branchVersion, branchVersion) + + output := string(content) + newRule + + err = os.WriteFile(mergifyFile, []byte(output), 0o644) + if err != nil { + return fmt.Errorf("failed to write %s: %w", mergifyFile, err) + } + + fmt.Printf("✓ Added backport rule for %s to %s\n", branchVersion, mergifyFile) + return nil +} + +// PrepareMajorMinorRelease prepares files for a major/minor release. +func PrepareMajorMinorRelease(cfg *ReleaseConfig) error { + fmt.Printf("Preparing major/minor release for version %s\n", cfg.Version) + + if err := UpdateVersion(cfg.Version); err != nil { + return err + } + + if err := UpdateMergify(cfg.Version); err != nil { + return err + } + + fmt.Println("✓ Major/minor release preparation complete") + fmt.Printf(" Next steps:\n") + fmt.Printf(" 1. Review changes: git diff\n") + fmt.Printf(" 2. Create branch: mage release:createBranch\n") + fmt.Printf(" 3. Create PR: mage release:createPR\n") + + return nil +} + +// CreateReleaseBranch creates a release branch with all changes committed. +func CreateReleaseBranch(cfg *ReleaseConfig, repoPath string, dryRun bool) error { + repo, err := OpenRepo(repoPath) + if err != nil { + return err + } + + branchName := cfg.ReleaseBranch + fmt.Printf("Creating release branch: %s\n", branchName) + + if !dryRun { + if err := repo.CreateBranch(branchName); err != nil { + return err + } + + commitMsg := fmt.Sprintf("[Release] Prepare %s release", cfg.Version) + if err := repo.CommitAll(commitMsg, cfg.AuthorName, cfg.AuthorEmail); err != nil { + return err + } + } else { + fmt.Printf(" [DRY RUN] Would create branch %s and commit changes\n", branchName) + } + + return repo.Push("origin", dryRun) +} + +// CreateReleasePR creates a pull request for the release. +func CreateReleasePR(cfg *ReleaseConfig, ghClient *GitHubClient, dryRun bool) error { + title := fmt.Sprintf("[Release] %s", cfg.Version) + body := fmt.Sprintf("## Release %s\n\nThis PR prepares the %s release.\n\n### Changes\n- Updated version to %s\n- Added mergify backport rule for %s\n", + cfg.Version, cfg.Version, cfg.Version, cfg.ReleaseBranch) + + _, err := ghClient.CreatePR(PROptions{ + Owner: cfg.Owner, + Repo: cfg.Repo, + Title: title, + Head: cfg.ReleaseBranch, + Base: cfg.BaseBranch, + Body: body, + }, dryRun) + return err +} + +// RunMajorMinorRelease orchestrates the complete major/minor release workflow. +func RunMajorMinorRelease(cfg *ReleaseConfig, dryRun bool) error { + if dryRun { + fmt.Println("🔍 DRY RUN MODE - No changes will be pushed") + } + + fmt.Printf("🚀 Starting major/minor release workflow for %s\n\n", cfg.Version) + + fmt.Println("Step 1: Preparing release files...") + if err := PrepareMajorMinorRelease(cfg); err != nil { + return fmt.Errorf("failed to prepare release: %w", err) + } + + fmt.Println("\nStep 2: Creating release branch...") + if err := CreateReleaseBranch(cfg, ".", dryRun); err != nil { + return fmt.Errorf("failed to create branch: %w", err) + } + + fmt.Println("\nStep 3: Creating pull request...") + ghClient, err := NewGitHubClientFromEnv() + if err != nil { + return err + } + if err := CreateReleasePR(cfg, ghClient, dryRun); err != nil { + return fmt.Errorf("failed to create PR: %w", err) + } + + fmt.Printf("\n✅ Major/minor release workflow complete for %s\n", cfg.Version) + return nil +} + +// PreparePatchRelease prepares files for a patch release. +func PreparePatchRelease(cfg *ReleaseConfig) error { + fmt.Printf("Preparing patch release for version %s\n", cfg.Version) + + if err := UpdateVersion(cfg.Version); err != nil { + return err + } + + fmt.Println("✓ Patch release preparation complete") + return nil +} + +// RunPatchRelease orchestrates the complete patch release workflow. +func RunPatchRelease(cfg *ReleaseConfig, dryRun bool) error { + if dryRun { + fmt.Println("🔍 DRY RUN MODE - No changes will be pushed") + } + + fmt.Printf("🚀 Starting patch release workflow for %s\n\n", cfg.Version) + + fmt.Println("Preparing patch release files...") + if err := PreparePatchRelease(cfg); err != nil { + return fmt.Errorf("failed to prepare patch release: %w", err) + } + + fmt.Printf("\n✅ Patch release workflow complete for %s\n", cfg.Version) + fmt.Println(" Review changes and commit to the release branch") + return nil +} + +// PrepareNextRelease prepares files for the next development cycle. +func PrepareNextRelease(currentVersion string) error { + parts := strings.Split(currentVersion, ".") + if len(parts) < 3 { + return fmt.Errorf("invalid version format: %s", currentVersion) + } + + minor, err := strconv.Atoi(parts[1]) + if err != nil { + return fmt.Errorf("invalid minor version: %s", parts[1]) + } + + nextVersion := fmt.Sprintf("%s.%d.0", parts[0], minor+1) + + fmt.Printf("Preparing next development cycle: %s -> %s\n", currentVersion, nextVersion) + + if err := UpdateVersion(nextVersion); err != nil { + return err + } + + fmt.Printf("✓ Next release preparation complete: %s\n", nextVersion) + return nil +} diff --git a/magefile_test.go b/dev-tools/mage/release/release_test.go similarity index 64% rename from magefile_test.go rename to dev-tools/mage/release/release_test.go index 92fd6e2175..d92d53e2fe 100644 --- a/magefile_test.go +++ b/dev-tools/mage/release/release_test.go @@ -2,18 +2,13 @@ // or more contributor license agreements. Licensed under the Elastic License 2.0; // you may not use this file except in compliance with the Elastic License 2.0. -//go:build mage - -package main +package release import ( "os" "path/filepath" "strings" "testing" - - "github.com/go-git/go-git/v5" - "github.com/go-git/go-git/v5/plumbing/object" ) func TestUpdateVersion(t *testing.T) { @@ -49,10 +44,9 @@ func TestUpdateVersion(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - // Create temp directory and file tmpDir := t.TempDir() versionDir := filepath.Join(tmpDir, "version") - err := os.Mkdir(versionDir, 0755) + err := os.Mkdir(versionDir, 0o755) if err != nil { t.Fatalf("failed to create version dir: %v", err) } @@ -64,19 +58,20 @@ package version ` + tt.initial + ` ` - err = os.WriteFile(versionFile, []byte(initialContent), 0644) + err = os.WriteFile(versionFile, []byte(initialContent), 0o644) if err != nil { t.Fatalf("failed to write initial file: %v", err) } - // Save current dir and change to temp dir origDir, _ := os.Getwd() - defer os.Chdir(origDir) - os.Chdir(tmpDir) + defer func() { + _ = os.Chdir(origDir) + }() + if err := os.Chdir(tmpDir); err != nil { + t.Fatalf("failed to change to temp directory: %v", err) + } - // Run UpdateVersion - r := Release{} - err = r.UpdateVersion(tt.newVersion) + err = UpdateVersion(tt.newVersion) if (err != nil) != tt.wantErr { t.Errorf("UpdateVersion() error = %v, wantErr %v", err, tt.wantErr) @@ -84,7 +79,6 @@ package version } if !tt.wantErr { - // Verify file content content, err := os.ReadFile(versionFile) if err != nil { t.Fatalf("failed to read updated file: %v", err) @@ -98,10 +92,10 @@ package version } } -func TestUpdateVersion_Idempotent(t *testing.T) { +func TestUpdateVersionIdempotent(t *testing.T) { tmpDir := t.TempDir() versionDir := filepath.Join(tmpDir, "version") - err := os.Mkdir(versionDir, 0755) + err := os.Mkdir(versionDir, 0o755) if err != nil { t.Fatalf("failed to create version dir: %v", err) } @@ -111,17 +105,20 @@ func TestUpdateVersion_Idempotent(t *testing.T) { const DefaultVersion = "9.4.0" ` - err = os.WriteFile(versionFile, []byte(initialContent), 0644) + err = os.WriteFile(versionFile, []byte(initialContent), 0o644) if err != nil { t.Fatalf("failed to write initial file: %v", err) } origDir, _ := os.Getwd() - defer os.Chdir(origDir) - os.Chdir(tmpDir) + defer func() { + _ = os.Chdir(origDir) + }() + if err := os.Chdir(tmpDir); err != nil { + t.Fatalf("failed to change to temp directory: %v", err) + } - r := Release{} - err = r.UpdateVersion("9.5.0") + err = UpdateVersion("9.5.0") if err != nil { t.Fatalf("first UpdateVersion() failed: %v", err) } @@ -131,7 +128,7 @@ const DefaultVersion = "9.4.0" t.Fatalf("failed to read file after first update: %v", err) } - err = r.UpdateVersion("9.5.0") + err = UpdateVersion("9.5.0") if err != nil { t.Fatalf("second UpdateVersion() failed: %v", err) } @@ -146,10 +143,10 @@ const DefaultVersion = "9.4.0" } } -func TestUpdateVersion_AlreadyAtVersion(t *testing.T) { +func TestUpdateVersionAlreadyAtVersion(t *testing.T) { tmpDir := t.TempDir() versionDir := filepath.Join(tmpDir, "version") - err := os.Mkdir(versionDir, 0755) + err := os.Mkdir(versionDir, 0o755) if err != nil { t.Fatalf("failed to create version dir: %v", err) } @@ -159,17 +156,20 @@ func TestUpdateVersion_AlreadyAtVersion(t *testing.T) { const DefaultVersion = "9.5.0" ` - err = os.WriteFile(versionFile, []byte(initialContent), 0644) + err = os.WriteFile(versionFile, []byte(initialContent), 0o644) if err != nil { t.Fatalf("failed to write initial file: %v", err) } origDir, _ := os.Getwd() - defer os.Chdir(origDir) - os.Chdir(tmpDir) + defer func() { + _ = os.Chdir(origDir) + }() + if err := os.Chdir(tmpDir); err != nil { + t.Fatalf("failed to change to temp directory: %v", err) + } - r := Release{} - err = r.UpdateVersion("9.5.0") + err = UpdateVersion("9.5.0") if err != nil { t.Fatalf("UpdateVersion() failed when version already set: %v", err) } @@ -183,106 +183,6 @@ const DefaultVersion = "9.5.0" } } -func createTestGitRepo(t *testing.T) (*GitRepo, string) { - t.Helper() - - tmpDir := t.TempDir() - repo, err := git.PlainInit(tmpDir, false) - if err != nil { - t.Fatalf("failed to init repo: %v", err) - } - - w, err := repo.Worktree() - if err != nil { - t.Fatalf("failed to get worktree: %v", err) - } - - testFile := filepath.Join(tmpDir, "README.md") - err = os.WriteFile(testFile, []byte("# Test Repo"), 0644) - if err != nil { - t.Fatalf("failed to write test file: %v", err) - } - - _, err = w.Add("README.md") - if err != nil { - t.Fatalf("failed to add file: %v", err) - } - - _, err = w.Commit("Initial commit", &git.CommitOptions{ - Author: &object.Signature{ - Name: "Test User", - Email: "test@example.com", - }, - }) - if err != nil { - t.Fatalf("failed to create initial commit: %v", err) - } - - return &GitRepo{repo: repo}, tmpDir -} - -func TestCreateBranch_Idempotent(t *testing.T) { - gitRepo, _ := createTestGitRepo(t) - - err := gitRepo.createBranch("9.5") - if err != nil { - t.Fatalf("first createBranch() failed: %v", err) - } - - branch, err := gitRepo.getCurrentBranch() - if err != nil { - t.Fatalf("getCurrentBranch() failed: %v", err) - } - if branch != "9.5" { - t.Fatalf("expected branch 9.5, got %s", branch) - } - - err = gitRepo.createBranch("9.5") - if err != nil { - t.Fatalf("second createBranch() failed: %v", err) - } - - branch, err = gitRepo.getCurrentBranch() - if err != nil { - t.Fatalf("getCurrentBranch() after second call failed: %v", err) - } - if branch != "9.5" { - t.Errorf("createBranch() is not idempotent - on branch %s, want 9.5", branch) - } -} - -func TestCommitAll_NoChanges(t *testing.T) { - gitRepo, _ := createTestGitRepo(t) - - err := gitRepo.commitAll("Empty commit", "Test Author", "test@example.com") - if err != nil { - t.Errorf("commitAll() with no changes error = %v", err) - } -} - -func TestSetRemoteURL_Idempotent(t *testing.T) { - gitRepo, _ := createTestGitRepo(t) - remoteURL := "https://github.com/test/repo.git" - - err := gitRepo.setRemoteURL("origin", remoteURL) - if err != nil { - t.Fatalf("first setRemoteURL() failed: %v", err) - } - - err = gitRepo.setRemoteURL("origin", remoteURL) - if err != nil { - t.Fatalf("second setRemoteURL() failed: %v", err) - } - - remote, err := gitRepo.repo.Remote("origin") - if err != nil { - t.Fatalf("failed to get remote: %v", err) - } - if len(remote.Config().URLs) == 0 || remote.Config().URLs[0] != remoteURL { - t.Errorf("setRemoteURL() URL = %v, want %s", remote.Config().URLs, remoteURL) - } -} - func TestUpdateMergify(t *testing.T) { tests := []struct { name string @@ -317,7 +217,6 @@ func TestUpdateMergify(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - // Create temp directory and file tmpDir := t.TempDir() mergifyFile := filepath.Join(tmpDir, ".mergify.yml") @@ -331,19 +230,20 @@ func TestUpdateMergify(t *testing.T) { branches: - "9.3" ` - err := os.WriteFile(mergifyFile, []byte(initialContent), 0644) + err := os.WriteFile(mergifyFile, []byte(initialContent), 0o644) if err != nil { t.Fatalf("failed to write initial file: %v", err) } - // Save current dir and change to temp dir origDir, _ := os.Getwd() - defer os.Chdir(origDir) - os.Chdir(tmpDir) + defer func() { + _ = os.Chdir(origDir) + }() + if err := os.Chdir(tmpDir); err != nil { + t.Fatalf("failed to change to temp directory: %v", err) + } - // Run UpdateMergify - r := Release{} - err = r.UpdateMergify(tt.version) + err = UpdateMergify(tt.version) if (err != nil) != tt.wantErr { t.Errorf("UpdateMergify() error = %v, wantErr %v", err, tt.wantErr) @@ -351,7 +251,6 @@ func TestUpdateMergify(t *testing.T) { } if !tt.wantErr && tt.shouldAppend { - // Verify file content content, err := os.ReadFile(mergifyFile) if err != nil { t.Fatalf("failed to read updated file: %v", err) @@ -359,7 +258,6 @@ func TestUpdateMergify(t *testing.T) { contentStr := string(content) - // Check that the new rule was added expectedName := "backport patches to " + tt.wantBranch + " branch" if !strings.Contains(contentStr, expectedName) { t.Errorf("UpdateMergify() missing rule name: %s", expectedName) @@ -373,7 +271,6 @@ func TestUpdateMergify(t *testing.T) { t.Errorf("UpdateMergify() missing branch: %s", tt.wantBranch) } - // Verify old rule is still there if !strings.Contains(contentStr, "9.3") { t.Error("UpdateMergify() removed existing rules") } @@ -382,8 +279,7 @@ func TestUpdateMergify(t *testing.T) { } } -func TestUpdateMergify_Idempotent(t *testing.T) { - // Create temp directory and file +func TestUpdateMergifyIdempotent(t *testing.T) { tmpDir := t.TempDir() mergifyFile := filepath.Join(tmpDir, ".mergify.yml") @@ -397,40 +293,37 @@ func TestUpdateMergify_Idempotent(t *testing.T) { branches: - "9.5" ` - err := os.WriteFile(mergifyFile, []byte(initialContent), 0644) + err := os.WriteFile(mergifyFile, []byte(initialContent), 0o644) if err != nil { t.Fatalf("failed to write initial file: %v", err) } - // Save current dir and change to temp dir origDir, _ := os.Getwd() - defer os.Chdir(origDir) - os.Chdir(tmpDir) + defer func() { + _ = os.Chdir(origDir) + }() + if err := os.Chdir(tmpDir); err != nil { + t.Fatalf("failed to change to temp directory: %v", err) + } - // Run UpdateMergify twice with same version - r := Release{} - err = r.UpdateMergify("9.5.0") + err = UpdateMergify("9.5.0") if err != nil { t.Fatalf("first UpdateMergify() failed: %v", err) } - // Get content after first run content1, _ := os.ReadFile(mergifyFile) - err = r.UpdateMergify("9.5.0") + err = UpdateMergify("9.5.0") if err != nil { t.Fatalf("second UpdateMergify() failed: %v", err) } - // Get content after second run content2, _ := os.ReadFile(mergifyFile) - // Content should be identical (idempotent) if string(content1) != string(content2) { t.Error("UpdateMergify() is not idempotent - content changed on second run") } - // Should only have one rule for 9.5 count := strings.Count(string(content2), "backport patches to 9.5 branch") if count != 1 { t.Errorf("UpdateMergify() created %d rules for 9.5, want 1", count) @@ -499,16 +392,15 @@ func TestLoadReleaseConfigFromEnv(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - // Clear env and set test values os.Clearenv() for k, v := range tt.envVars { os.Setenv(k, v) } - cfg, err := loadReleaseConfigFromEnv() + cfg, err := LoadReleaseConfigFromEnv() if (err != nil) != tt.wantErr { - t.Errorf("loadReleaseConfigFromEnv() error = %v, wantErr %v", err, tt.wantErr) + t.Errorf("LoadReleaseConfigFromEnv() error = %v, wantErr %v", err, tt.wantErr) return } @@ -519,11 +411,10 @@ func TestLoadReleaseConfigFromEnv(t *testing.T) { } } -func TestPrepareNext(t *testing.T) { - // Create temp directory and file +func TestPrepareNextRelease(t *testing.T) { tmpDir := t.TempDir() versionDir := filepath.Join(tmpDir, "version") - err := os.Mkdir(versionDir, 0755) + err := os.Mkdir(versionDir, 0o755) if err != nil { t.Fatalf("failed to create version dir: %v", err) } @@ -533,27 +424,24 @@ func TestPrepareNext(t *testing.T) { const DefaultVersion = "9.4.0" ` - err = os.WriteFile(versionFile, []byte(initialContent), 0644) + err = os.WriteFile(versionFile, []byte(initialContent), 0o644) if err != nil { t.Fatalf("failed to write initial file: %v", err) } - // Save current dir and change to temp dir origDir, _ := os.Getwd() - defer os.Chdir(origDir) - os.Chdir(tmpDir) - - // Set env var - os.Setenv("CURRENT_RELEASE", "9.4.0") + defer func() { + _ = os.Chdir(origDir) + }() + if err := os.Chdir(tmpDir); err != nil { + t.Fatalf("failed to change to temp directory: %v", err) + } - // Run PrepareNext - r := Release{} - err = r.PrepareNext() + err = PrepareNextRelease("9.4.0") if err != nil { - t.Fatalf("PrepareNext() failed: %v", err) + t.Fatalf("PrepareNextRelease() failed: %v", err) } - // Verify version was incremented content, err := os.ReadFile(versionFile) if err != nil { t.Fatalf("failed to read updated file: %v", err) @@ -561,6 +449,6 @@ const DefaultVersion = "9.4.0" expectedVersion := "9.5.0" if !strings.Contains(string(content), expectedVersion) { - t.Errorf("PrepareNext() = %s, want %s", string(content), expectedVersion) + t.Errorf("PrepareNextRelease() = %s, want %s", string(content), expectedVersion) } } diff --git a/go.mod b/go.mod index 708a51dcde..950ad1539b 100644 --- a/go.mod +++ b/go.mod @@ -10,14 +10,13 @@ require ( github.com/elastic/elastic-agent-client/v7 v7.18.1 github.com/elastic/elastic-agent-libs v0.44.0 github.com/elastic/elastic-agent-system-metrics v0.14.4 + github.com/elastic/fleet-server/dev-tools v0.0.0 github.com/elastic/go-elasticsearch/v8 v8.19.6 github.com/elastic/go-ucfg v0.9.1 github.com/fxamacker/cbor/v2 v2.9.2 github.com/go-chi/chi/v5 v5.3.0 - github.com/go-git/go-git/v5 v5.12.0 github.com/gofrs/uuid/v5 v5.4.0 github.com/google/go-cmp v0.7.0 - github.com/google/go-github/v68 v68.0.0 github.com/hashicorp/go-cleanhttp v0.5.2 github.com/hashicorp/go-version v1.9.0 github.com/hashicorp/golang-lru/v2 v2.0.7 @@ -70,10 +69,12 @@ require ( github.com/fatih/color v1.15.0 // indirect github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect github.com/go-git/go-billy/v5 v5.5.0 // indirect + github.com/go-git/go-git/v5 v5.12.0 // indirect github.com/go-logr/logr v1.4.3 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/go-ole/go-ole v1.3.0 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect + github.com/google/go-github/v68 v68.0.0 // indirect github.com/google/go-querystring v1.1.0 // indirect github.com/google/uuid v1.6.0 // indirect github.com/gorilla/websocket v1.5.3 // indirect @@ -119,3 +120,5 @@ require ( gopkg.in/yaml.v2 v2.4.0 // indirect howett.net/plist v1.0.1 // indirect ) + +replace github.com/elastic/fleet-server/dev-tools => ./dev-tools diff --git a/magefile.go b/magefile.go index 4f487d65c9..3784891d16 100644 --- a/magefile.go +++ b/magefile.go @@ -40,17 +40,13 @@ import ( "sync" "time" - "github.com/go-git/go-git/v5" - "github.com/go-git/go-git/v5/config" - "github.com/go-git/go-git/v5/plumbing" - "github.com/go-git/go-git/v5/plumbing/object" - "github.com/google/go-github/v68/github" "github.com/magefile/mage/mg" "github.com/magefile/mage/sh" "gopkg.in/yaml.v3" "github.com/elastic/elastic-agent-libs/logp" "github.com/elastic/elastic-agent-libs/transport/tlscommon" + "github.com/elastic/fleet-server/dev-tools/mage/release" "github.com/elastic/fleet-server/v7/version" ) @@ -2293,607 +2289,79 @@ func (Test) CloudE2ERun() error { return err } -// ============================================================================ -// Release Automation Functions -// ============================================================================ - -// ReleaseConfig holds configuration for release operations -type ReleaseConfig struct { - Version string - BaseBranch string - ReleaseBranch string - Owner string - Repo string - AuthorName string - AuthorEmail string +// UpdateVersion updates the version in version/version.go. +func (Release) UpdateVersion(version string) error { + return release.UpdateVersion(version) } -// loadReleaseConfigFromEnv loads release configuration from environment variables -func loadReleaseConfigFromEnv() (*ReleaseConfig, error) { - version := os.Getenv("CURRENT_RELEASE") - if version == "" { - return nil, fmt.Errorf("CURRENT_RELEASE environment variable not set") - } - - baseBranch := os.Getenv("BASE_BRANCH") - if baseBranch == "" { - baseBranch = "main" - } - - // Extract major.minor for release branch (e.g., "9.4.0" -> "9.4") - parts := strings.Split(version, ".") - if len(parts) < 2 { - return nil, fmt.Errorf("invalid version format: %s (expected X.Y.Z)", version) - } - releaseBranch := fmt.Sprintf("%s.%s", parts[0], parts[1]) - - owner := os.Getenv("PROJECT_OWNER") - if owner == "" { - owner = "elastic" - } - - repo := os.Getenv("PROJECT_REPO") - if repo == "" { - repo = "fleet-server" - } - - authorName := os.Getenv("GITHUB_USERNAME") - if authorName == "" { - authorName = "elasticmachine" - } - - authorEmail := os.Getenv("GITHUB_EMAIL") - if authorEmail == "" { - authorEmail = "infra-root+elasticmachine@elastic.co" - } - - return &ReleaseConfig{ - Version: version, - BaseBranch: baseBranch, - ReleaseBranch: releaseBranch, - Owner: owner, - Repo: repo, - AuthorName: authorName, - AuthorEmail: authorEmail, - }, nil -} - -// UpdateVersion updates the version in version/version.go -func (Release) UpdateVersion(newVersion string) error { - versionFile := "version/version.go" - - content, err := os.ReadFile(versionFile) - if err != nil { - return fmt.Errorf("failed to read %s: %w", versionFile, err) - } - - // Replace the version string - // Pattern: const DefaultVersion = "X.Y.Z" - re := regexp.MustCompile(`(const\s+DefaultVersion\s*=\s*)"[^"]+"`) - contentStr := string(content) - alreadyAtVersion := regexp.MustCompile(`const\s+DefaultVersion\s*=\s*"` + regexp.QuoteMeta(newVersion) + `"`) - if alreadyAtVersion.MatchString(contentStr) { - fmt.Printf(" Version already set to %s in %s\n", newVersion, versionFile) - return nil - } - - newContent := re.ReplaceAllString(contentStr, `${1}"`+newVersion+`"`) - - if newContent == contentStr { - return fmt.Errorf("version pattern not found in %s", versionFile) - } - - err = os.WriteFile(versionFile, []byte(newContent), 0644) - if err != nil { - return fmt.Errorf("failed to write %s: %w", versionFile, err) - } - - fmt.Printf("✓ Updated version to %s in %s\n", newVersion, versionFile) - return nil -} - -// UpdateMergify adds a new backport rule to .mergify.yml +// UpdateMergify adds a new backport rule to .mergify.yml. func (Release) UpdateMergify(version string) error { - mergifyFile := ".mergify.yml" - - // Read the YAML file - content, err := os.ReadFile(mergifyFile) - if err != nil { - return fmt.Errorf("failed to read %s: %w", mergifyFile, err) - } - - // Extract major.minor from version (e.g., "9.4.0" -> "9.4") - parts := strings.Split(version, ".") - if len(parts) < 2 { - return fmt.Errorf("invalid version format: %s (expected X.Y.Z)", version) - } - branchVersion := fmt.Sprintf("%s.%s", parts[0], parts[1]) - - // Check if rule already exists - searchPattern := fmt.Sprintf("backport patches to %s branch", branchVersion) - if strings.Contains(string(content), searchPattern) { - fmt.Printf(" Backport rule for %s already exists\n", branchVersion) - return nil - } - - // Create new backport rule following the existing format - newRule := fmt.Sprintf(` - name: backport patches to %s branch - conditions: - - merged - - label=backport-%s - actions: - backport: - branches: - - "%s" -`, branchVersion, branchVersion, branchVersion) - - // Append the new rule to the end of the file - output := string(content) + newRule - - // Write back to file - err = os.WriteFile(mergifyFile, []byte(output), 0644) - if err != nil { - return fmt.Errorf("failed to write %s: %w", mergifyFile, err) - } - - fmt.Printf("✓ Added backport rule for %s to %s\n", branchVersion, mergifyFile) - return nil -} - -// GitRepo represents a Git repository with common operations -type GitRepo struct { - repo *git.Repository -} - -// openRepo opens the Git repository at the given path -func openRepo(path string) (*GitRepo, error) { - repo, err := git.PlainOpen(path) - if err != nil { - return nil, fmt.Errorf("failed to open git repo: %w", err) - } - return &GitRepo{repo: repo}, nil -} - -// createBranch creates and checks out a new branch -func (g *GitRepo) createBranch(branchName string) error { - w, err := g.repo.Worktree() - if err != nil { - return fmt.Errorf("failed to get worktree: %w", err) - } - - refName := plumbing.NewBranchReferenceName(branchName) - _, err = g.repo.Reference(refName, true) - if err == nil { - err = w.Checkout(&git.CheckoutOptions{ - Branch: refName, - }) - if err != nil { - return fmt.Errorf("failed to checkout existing branch: %w", err) - } - fmt.Printf("✓ Checked out existing branch: %s\n", branchName) - return nil - } - if !errors.Is(err, plumbing.ErrReferenceNotFound) { - return fmt.Errorf("failed to check branch: %w", err) - } - - // Get current HEAD reference - headRef, err := g.repo.Head() - if err != nil { - return fmt.Errorf("failed to get HEAD: %w", err) - } - - // Create new branch reference - ref := plumbing.NewHashReference(refName, headRef.Hash()) - - err = g.repo.Storer.SetReference(ref) - if err != nil { - return fmt.Errorf("failed to create branch: %w", err) - } - - // Checkout the new branch - err = w.Checkout(&git.CheckoutOptions{ - Branch: refName, - }) - if err != nil { - return fmt.Errorf("failed to checkout branch: %w", err) - } - - fmt.Printf("✓ Created and checked out branch: %s\n", branchName) - return nil -} - -// commitAll commits all changes with the given message -func (g *GitRepo) commitAll(message, authorName, authorEmail string) error { - w, err := g.repo.Worktree() - if err != nil { - return fmt.Errorf("failed to get worktree: %w", err) - } - - status, err := w.Status() - if err != nil { - return fmt.Errorf("failed to get worktree status: %w", err) - } - if status.IsClean() { - fmt.Println(" No changes to commit") - return nil - } - - // Add all changes - err = w.AddWithOptions(&git.AddOptions{ - All: true, - }) - if err != nil { - return fmt.Errorf("failed to add changes: %w", err) - } - - // Create commit - commit, err := w.Commit(message, &git.CommitOptions{ - Author: &object.Signature{ - Name: authorName, - Email: authorEmail, - When: time.Now(), - }, - }) - if err != nil { - return fmt.Errorf("failed to commit: %w", err) - } - - fmt.Printf("✓ Committed changes: %s\n", commit.String()[:7]) - return nil -} - -// push pushes the current branch to the remote -func (g *GitRepo) push(remoteName string, dryRun bool) error { - if dryRun { - fmt.Printf(" [DRY RUN] Would push to remote: %s\n", remoteName) - return nil - } - - err := g.repo.Push(&git.PushOptions{ - RemoteName: remoteName, - }) - if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) { - return fmt.Errorf("failed to push: %w", err) - } - - if errors.Is(err, git.NoErrAlreadyUpToDate) { - fmt.Println(" Branch already up to date on remote") - } else { - fmt.Printf("✓ Pushed to remote: %s\n", remoteName) - } - return nil -} - -// getCurrentBranch returns the name of the current branch -func (g *GitRepo) getCurrentBranch() (string, error) { - ref, err := g.repo.Head() - if err != nil { - return "", fmt.Errorf("failed to get HEAD: %w", err) - } - - if !ref.Name().IsBranch() { - return "", fmt.Errorf("HEAD is not a branch") - } - - return ref.Name().Short(), nil -} - -// setRemoteURL sets the URL for a remote -func (g *GitRepo) setRemoteURL(remoteName, url string) error { - remote, err := g.repo.Remote(remoteName) - if errors.Is(err, git.ErrRemoteNotFound) { - // Remote doesn't exist, create it - _, err = g.repo.CreateRemote(&config.RemoteConfig{ - Name: remoteName, - URLs: []string{url}, - }) - if err != nil { - return fmt.Errorf("failed to create remote: %w", err) - } - fmt.Printf("✓ Created remote %s: %s\n", remoteName, url) - return nil - } else if err != nil { - return fmt.Errorf("failed to get remote: %w", err) - } - - if len(remote.Config().URLs) > 0 && remote.Config().URLs[0] == url { - return nil - } - - // Update remote URL - err = g.repo.DeleteRemote(remoteName) - if err != nil { - return fmt.Errorf("failed to delete remote: %w", err) - } - - _, err = g.repo.CreateRemote(&config.RemoteConfig{ - Name: remoteName, - URLs: []string{url}, - }) - if err != nil { - return fmt.Errorf("failed to recreate remote: %w", err) - } - - fmt.Printf("✓ Updated remote %s: %s\n", remoteName, url) - return nil -} - -// newGitHubClient creates a new GitHub client from environment -func newGitHubClient() (*github.Client, error) { - token := os.Getenv("GITHUB_TOKEN") - if token == "" { - return nil, fmt.Errorf("GITHUB_TOKEN environment variable not set") - } - - client := github.NewClient(nil).WithAuthToken(token) - return client, nil -} - -// findExistingPR returns an open pull request for the given head and base branches, if one exists. -func findExistingPR(ctx context.Context, client *github.Client, owner, repo, head, base string) (*github.PullRequest, error) { - opts := &github.PullRequestListOptions{ - State: "open", - Head: fmt.Sprintf("%s:%s", owner, head), - Base: base, - } - prs, _, err := client.PullRequests.List(ctx, owner, repo, opts) - if err != nil { - return nil, fmt.Errorf("failed to list pull requests: %w", err) - } - if len(prs) > 0 { - return prs[0], nil - } - return nil, nil + return release.UpdateMergify(version) } -// createPR creates a pull request on GitHub -func createPR(client *github.Client, cfg *ReleaseConfig, title, body, head, base string, dryRun bool) (*github.PullRequest, error) { - if dryRun { - fmt.Printf(" [DRY RUN] Would create PR:\n") - fmt.Printf(" Title: %s\n", title) - fmt.Printf(" Head: %s\n", head) - fmt.Printf(" Base: %s\n", base) - return nil, nil - } - - ctx := context.Background() - existingPR, err := findExistingPR(ctx, client, cfg.Owner, cfg.Repo, head, base) - if err != nil { - return nil, err - } - if existingPR != nil { - fmt.Printf(" Pull request already exists: #%d %s\n", existingPR.GetNumber(), existingPR.GetHTMLURL()) - return existingPR, nil - } - - pr, _, err := client.PullRequests.Create(ctx, cfg.Owner, cfg.Repo, &github.NewPullRequest{ - Title: github.String(title), - Head: github.String(head), - Base: github.String(base), - Body: github.String(body), - }) - if err != nil { - var ghErr *github.ErrorResponse - if errors.As(err, &ghErr) && ghErr.Response.StatusCode == 422 { - existingPR, listErr := findExistingPR(ctx, client, cfg.Owner, cfg.Repo, head, base) - if listErr != nil { - return nil, fmt.Errorf("failed to create PR: %w", err) - } - if existingPR != nil { - fmt.Printf(" Pull request already exists: #%d %s\n", existingPR.GetNumber(), existingPR.GetHTMLURL()) - return existingPR, nil - } - } - return nil, fmt.Errorf("failed to create PR: %w", err) - } - - fmt.Printf("✓ Created PR #%d: %s\n", *pr.Number, *pr.HTMLURL) - return pr, nil -} - -// PrepareMajorMinor prepares files for a major/minor release +// PrepareMajorMinor prepares files for a major/minor release. func (Release) PrepareMajorMinor() error { - cfg, err := loadReleaseConfigFromEnv() + cfg, err := release.LoadReleaseConfigFromEnv() if err != nil { return err } - - fmt.Printf("Preparing major/minor release for version %s\n", cfg.Version) - - // Update version - r := Release{} - if err := r.UpdateVersion(cfg.Version); err != nil { - return err - } - - // Update mergify - if err := r.UpdateMergify(cfg.Version); err != nil { - return err - } - - fmt.Println("✓ Major/minor release preparation complete") - fmt.Printf(" Next steps:\n") - fmt.Printf(" 1. Review changes: git diff\n") - fmt.Printf(" 2. Create branch: mage release:createBranch\n") - fmt.Printf(" 3. Create PR: mage release:createPR\n") - - return nil + return release.PrepareMajorMinorRelease(cfg) } -// CreateBranch creates a release branch with all changes committed +// CreateBranch creates a release branch with all changes committed. func (Release) CreateBranch() error { - cfg, err := loadReleaseConfigFromEnv() - if err != nil { - return err - } - - dryRun := os.Getenv("DRY_RUN") == "true" - - repo, err := openRepo(".") + cfg, err := release.LoadReleaseConfigFromEnv() if err != nil { return err } - - branchName := cfg.ReleaseBranch - fmt.Printf("Creating release branch: %s\n", branchName) - - if !dryRun { - // Create and checkout branch - if err := repo.createBranch(branchName); err != nil { - return err - } - - // Commit all changes - commitMsg := fmt.Sprintf("[Release] Prepare %s release", cfg.Version) - if err := repo.commitAll(commitMsg, cfg.AuthorName, cfg.AuthorEmail); err != nil { - return err - } - } else { - fmt.Printf(" [DRY RUN] Would create branch %s and commit changes\n", branchName) - } - - // Push to remote - if err := repo.push("origin", dryRun); err != nil { - return err - } - - return nil + return release.CreateReleaseBranch(cfg, ".", os.Getenv("DRY_RUN") == "true") } -// CreatePR creates a pull request for the release +// CreatePR creates a pull request for the release. func (Release) CreatePR() error { - cfg, err := loadReleaseConfigFromEnv() + cfg, err := release.LoadReleaseConfigFromEnv() if err != nil { return err } - - dryRun := os.Getenv("DRY_RUN") == "true" - - client, err := newGitHubClient() + ghClient, err := release.NewGitHubClientFromEnv() if err != nil { return err } - - title := fmt.Sprintf("[Release] %s", cfg.Version) - body := fmt.Sprintf("## Release %s\n\nThis PR prepares the %s release.\n\n### Changes\n- Updated version to %s\n- Added mergify backport rule for %s\n", - cfg.Version, cfg.Version, cfg.Version, cfg.ReleaseBranch) - - _, err = createPR(client, cfg, title, body, cfg.ReleaseBranch, cfg.BaseBranch, dryRun) - return err + return release.CreateReleasePR(cfg, ghClient, os.Getenv("DRY_RUN") == "true") } -// RunMajorMinor orchestrates the complete major/minor release workflow +// RunMajorMinor orchestrates the complete major/minor release workflow. func (Release) RunMajorMinor() error { - cfg, err := loadReleaseConfigFromEnv() + cfg, err := release.LoadReleaseConfigFromEnv() if err != nil { return err } - - dryRun := os.Getenv("DRY_RUN") == "true" - if dryRun { - fmt.Println("🔍 DRY RUN MODE - No changes will be pushed") - } - - fmt.Printf("🚀 Starting major/minor release workflow for %s\n\n", cfg.Version) - - // Step 1: Prepare files - fmt.Println("Step 1: Preparing release files...") - r := Release{} - if err := r.PrepareMajorMinor(); err != nil { - return fmt.Errorf("failed to prepare release: %w", err) - } - - // Step 2: Create branch - fmt.Println("\nStep 2: Creating release branch...") - if err := r.CreateBranch(); err != nil { - return fmt.Errorf("failed to create branch: %w", err) - } - - // Step 3: Create PR - fmt.Println("\nStep 3: Creating pull request...") - if err := r.CreatePR(); err != nil { - return fmt.Errorf("failed to create PR: %w", err) - } - - fmt.Printf("\n✅ Major/minor release workflow complete for %s\n", cfg.Version) - return nil + return release.RunMajorMinorRelease(cfg, os.Getenv("DRY_RUN") == "true") } -// PreparePatch prepares files for a patch release +// PreparePatch prepares files for a patch release. func (Release) PreparePatch() error { - cfg, err := loadReleaseConfigFromEnv() + cfg, err := release.LoadReleaseConfigFromEnv() if err != nil { return err } - - fmt.Printf("Preparing patch release for version %s\n", cfg.Version) - - // Update version - r := Release{} - if err := r.UpdateVersion(cfg.Version); err != nil { - return err - } - - fmt.Println("✓ Patch release preparation complete") - return nil + return release.PreparePatchRelease(cfg) } -// RunPatch orchestrates the complete patch release workflow +// RunPatch orchestrates the complete patch release workflow. func (Release) RunPatch() error { - cfg, err := loadReleaseConfigFromEnv() + cfg, err := release.LoadReleaseConfigFromEnv() if err != nil { return err } - - dryRun := os.Getenv("DRY_RUN") == "true" - if dryRun { - fmt.Println("🔍 DRY RUN MODE - No changes will be pushed") - } - - fmt.Printf("🚀 Starting patch release workflow for %s\n\n", cfg.Version) - - // Prepare files - fmt.Println("Preparing patch release files...") - r := Release{} - if err := r.PreparePatch(); err != nil { - return fmt.Errorf("failed to prepare patch release: %w", err) - } - - fmt.Printf("\n✅ Patch release workflow complete for %s\n", cfg.Version) - fmt.Println(" Review changes and commit to the release branch") - return nil + return release.RunPatchRelease(cfg, os.Getenv("DRY_RUN") == "true") } -// PrepareNext prepares files for the next development cycle +// PrepareNext prepares files for the next development cycle. func (Release) PrepareNext() error { - // Get current version currentVersion := os.Getenv("CURRENT_RELEASE") if currentVersion == "" { return fmt.Errorf("CURRENT_RELEASE environment variable not set") } - - // Calculate next version (increment minor) - parts := strings.Split(currentVersion, ".") - if len(parts) < 3 { - return fmt.Errorf("invalid version format: %s", currentVersion) - } - - minor, err := strconv.Atoi(parts[1]) - if err != nil { - return fmt.Errorf("invalid minor version: %s", parts[1]) - } - - nextVersion := fmt.Sprintf("%s.%d.0", parts[0], minor+1) - - fmt.Printf("Preparing next development cycle: %s -> %s\n", currentVersion, nextVersion) - - // Update version - r := Release{} - if err := r.UpdateVersion(nextVersion); err != nil { - return err - } - - fmt.Printf("✓ Next release preparation complete: %s\n", nextVersion) - return nil + return release.PrepareNextRelease(currentVersion) } From 815c688a7404ee9a01a36c2f4a2cbe27c18c95a5 Mon Sep 17 00:00:00 2001 From: "fr4nc1sc0.r4m0n" Date: Thu, 9 Jul 2026 13:15:31 +0200 Subject: [PATCH 09/10] fix(ci): allow local go.mod replace for dev-tools module Enable gomoddirectives replace-local so the dev-tools submodule replace required by mage release imports passes golangci-lint. --- .golangci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 104cf923ce..ce610eb8be 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -91,8 +91,8 @@ linters: min-occurrences: 2 gomoddirectives: - # Allow local `replace` directives. Default is false. - replace-local: false + # Allow local `replace` directives (e.g. dev-tools submodule in go.mod). + replace-local: true gomodguard: blocked: # List of blocked modules. From 457a0a78ea1a6dcb00ce467ae96739a7e30ee91f Mon Sep 17 00:00:00 2001 From: "fr4nc1sc0.r4m0n" Date: Thu, 9 Jul 2026 13:29:39 +0200 Subject: [PATCH 10/10] fix(docker): copy dev-tools go.mod before go mod download The root go.mod replaces dev-tools with a local path. Docker layer caching must include dev-tools/go.mod and go.sum so go mod download can resolve the replaced module. --- Dockerfile | 1 + Dockerfile.build | 1 + Dockerfile.fips | 2 ++ 3 files changed, 4 insertions(+) diff --git a/Dockerfile b/Dockerfile index 04ef6931f8..ea683ba431 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,6 +5,7 @@ WORKDIR /fleet-server # pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change COPY go.mod go.sum ./ +COPY dev-tools/go.mod dev-tools/go.sum ./dev-tools/ RUN go mod download && go mod verify RUN go install github.com/magefile/mage # Uses version from go.mod implicitly ENV PATH="$PATH:/go/bin" diff --git a/Dockerfile.build b/Dockerfile.build index 4cb027053d..00ac2a8935 100644 --- a/Dockerfile.build +++ b/Dockerfile.build @@ -13,6 +13,7 @@ USER fleet-server WORKDIR /fleet-server/ # pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change COPY go.mod go.sum ./ +COPY dev-tools/go.mod dev-tools/go.sum ./dev-tools/ RUN go mod download && go mod verify RUN go install github.com/magefile/mage # uses version in go.mod diff --git a/Dockerfile.fips b/Dockerfile.fips index 8adae79231..a773061d6e 100644 --- a/Dockerfile.fips +++ b/Dockerfile.fips @@ -10,6 +10,7 @@ WORKDIR /fleet-server/ # pre-copy/cache go.mod for pre-downloading dependencies and only redownloading them in subsequent builds if they change COPY go.mod go.sum ./ +COPY dev-tools/go.mod dev-tools/go.sum ./dev-tools/ # GOFIPS140 must be set before go mod download so that the golang.org/fips140 module is cached during image build. # Without this, the module would be fetched at build time by the host user who lacks write access to the module cache. ENV GOFIPS140=certified @@ -32,6 +33,7 @@ FROM docker.elastic.co/beats-dev/golang-crossbuild:${GO_VERSION}-${SUFFIX} AS bu WORKDIR /fleet-server ENV PATH="$PATH:/go/bin" COPY go.mod go.sum ./ +COPY dev-tools/go.mod dev-tools/go.sum ./dev-tools/ RUN go mod download && go mod verify RUN go install github.com/magefile/mage # uses version in go.mod