okami.mom/albedo
git clone https://git.nkp.pet/okami.mom/albedo
82b9457
main
1// Gallery states of the Webhooks screen; TestShotGallery/webhooks draws them. 2// Without ALBEDO_SHOT_DIR they are only built, which keeps each one buildable. 3package tui 4 5import ( 6"errors" 7 8 tea"charm.land/bubbletea/v2" 9) 10 11// galleryHooks are the fixtures every webhooks state starts from: one queued 12// hook, one quiet hook under the same session, and one off hook under another. 13var galleryHooks = []webhookEntry { 14 {ID :"wh1" ,Session :"s" ,Name :"deploy" ,Enabled :true ,URL :"https://albedo.example/extensions/webhooks/hooks/wh1/deliveries" ,Header :"x-hub-signature-256" ,Prefix :"sha256=" ,Queued :2 ,Deferred :"session busy" }, 15 {ID :"wh3" ,Session :"s" ,Name :"alerts" ,Enabled :true ,URL :"/extensions/webhooks/hooks/wh3/deliveries" ,Header :"x-albedo-signature" ,Prefix :"sha256=" }, 16 {ID :"wh2" ,Session :"t" ,Name :"grafana" ,URL :"/extensions/webhooks/hooks/wh2/deliveries" ,Header :"x-albedo-signature" ,Prefix :"sha256=" }, 17} 18 19func init () { 20enter := tea.KeyPressMsg {Code :tea .KeyEnter } 21down := tea.KeyPressMsg {Code :tea .KeyDown } 22shiftTab := tea.KeyPressMsg {Code :tea .KeyTab ,Mod :tea .ModShift } 23ctrl := func (r rune ) tea.Msg {return tea.KeyPressMsg {Code :r ,Mod :tea .ModCtrl } } 24typed := func (text string ) []tea.Msg { 25var keys []tea.Msg 26for _ ,r := range text { 27keys = append (keys , tea.KeyPressMsg {Code :r ,Text :string (r )}) 28 } 29return keys 30 } 31registerShots ("webhooks" , 32shotState {"list" ,webhooksShot (nil )}, 33shotState {"list-agent-on" ,webhooksShot (func (m * WebhooksPageModel ) {m .AgentManagement = true })}, 34shotState {"cursor-off" ,webhooksShot (nil ,down ,down )}, 35shotState {"filtering" ,webhooksShot (nil ,typed ("grafana" )... )}, 36shotState {"filter-session" ,webhooksShot (nil ,typed ("release" )... )}, 37shotState {"no-match" ,webhooksShot (nil ,typed ("zzz" )... )}, 38shotState {"confirm-delete" ,webhooksShot (nil ,ctrl ('d' ))}, 39shotState {"confirm-new-secret" ,webhooksShot (nil ,ctrl ('g' ))}, 40shotState {"saving" ,webhooksShot (nil ,enter )}, 41shotState {"save-failed" ,func (w ,h int )string { 42m := webhooksModel (w ,h ,nil ) 43m ,_ = m .Update (enter ) 44m ,_ = m .Update (webhooksSavedMsg {Gen :m .Generation ,Err :errors .New ("daemon refused the change: webhook etag is stale" )}) 45return m .View () 46 }}, 47shotState {"notice" ,webhooksShot (func (m * WebhooksPageModel ) { 48m .Notice = "deploy disabled. New deliveries will receive a 404 response." 49 })}, 50shotState {"copied-url" ,webhooksShot (nil ,ctrl ('l' ))}, 51shotState {"not-listening" ,webhooksShot (func (m * WebhooksPageModel ) {m .Mounted = false })}, 52shotState {"loading" ,func (w ,h int )string { 53m := NewWebhooksPageModel (nil ,"s" ) 54m .SetSize (w ,h ) 55return m .View () 56 }}, 57shotState {"empty" ,func (w ,h int )string { 58m := NewWebhooksPageModel (nil ,"s" ) 59m .SetSize (w ,h ) 60m ,_ = m .Update (webhooksLoadedMsg {Gen :m .Generation ,Mounted :true ,Sessions :webhookSessions }) 61return m .View () 62 }}, 63shotState {"load-failed" ,func (w ,h int )string { 64m := NewWebhooksPageModel (nil ,"s" ) 65m .SetSize (w ,h ) 66m ,_ = m .Update (webhooksLoadedMsg {Gen :m .Generation ,Err :errors .New ("daemon connection unavailable" )}) 67return m .View () 68 }}, 69shotState {"form-add" ,webhooksShot (nil ,ctrl ('o' ))}, 70shotState {"form-add-session" ,webhooksShot (nil ,ctrl ('o' ),shiftTab )}, 71shotState {"form-typed" ,webhooksShot (nil ,append ([]tea.Msg {ctrl ('o' )},typed ("ci-deploys" )... )... )}, 72shotState {"form-invalid" ,webhooksShot (nil ,ctrl ('o' ),ctrl ('s' ))}, 73shotState {"form-edit" ,webhooksShot (nil ,ctrl ('e' ))}, 74shotState {"reveal" ,func (w ,h int )string { 75m := webhooksModel (w ,h ,nil ) 76m .Reveal = & webhookSecret {Hook :"deploy" ,Session :"s" ,Secret :"whsec_9fQ2xLr7Tq1mZ0vBcW4yHs8dNe3KpA6u" } 77return m .View () 78 }}, 79shotState {"reveal-copied" ,func (w ,h int )string { 80m := webhooksModel (w ,h ,nil ) 81m .Reveal = & webhookSecret {Hook :"deploy" ,Session :"s" ,Secret :"whsec_9fQ2xLr7Tq1mZ0vBcW4yHs8dNe3KpA6u" } 82m ,_ = m .Update (ctrl ('l' )) 83return m .View () 84 }}, 85 ) 86} 87 88// webhooksModel is the screen loaded with the gallery's hooks. 89func webhooksModel (width ,height int ,setup func (* WebhooksPageModel ))WebhooksPageModel { 90m := NewWebhooksPageModel (nil ,"s" ) 91m .SetSize (width ,height ) 92m ,_ = m .Update (webhooksLoadedMsg {Gen :m .Generation ,Mounted :true ,Sessions :webhookSessions ,Hooks :galleryHooks }) 93if setup != nil { 94setup (& m ) 95 } 96return m 97} 98 99func webhooksShot (setup func (* WebhooksPageModel ),keys ... tea.Msg )func (width ,height int )string { 100return func (width ,height int )string { 101m := webhooksModel (width ,height ,setup ) 102for _ ,key := range keys { 103m ,_ = m .Update (key ) 104 } 105return m .View () 106 } 107}