package wire import ( "fmt" "regexp" "time" ) // Publishing a website (oficina/equipo discussion 11): after Equipo merges // a task whose candidate carries a static site (release/preview.json), the // creator is asked, on a separate card, whether to publish the merged // commit's site on Carloku. Only their answer starts a publish step; the // creator's primary runs one fixed carlos command for it and confirms the // site by its marker over https. // SpecialistPublish publishes a merged commit's static site. const SpecialistPublish = "publish" // CardPublish asks the task's creator, once per merged commit, whether to // publish the site (Name the task's title, TaskID the task, Site what // would go live). ChoicePublish publishes it; ChoiceNotNow leaves it. const ( CardPublish = "publish" ChoicePublish = "publish" ) // LeasePurposePublish is a LeaseCheck's Purpose right before a publish // step's deploy, with the export's Digest; RefusedPublishNotBuilt refuses // one whose built report the instance has not applied yet. const ( LeasePurposePublish = "site_publish" RefusedPublishNotBuilt = "publish_not_built" ) // PublishDispatch is a publish step's target (DispatchCommand.Publish): // the Carloku app the instance derived from the project, and the default // branch the merged commit (CandidateSHA) must be on. type PublishDispatch struct { App string `json:"app"` Into string `json:"into"` // ProtocolPlaces: the confirmed place it publishes to (console host, // account sqid and name), the place's and the project binding's // revisions the person's tap saw, and the SiteDestination of all of // it. The runner deploys there and nowhere else. Console string `json:"console,omitempty"` Account string `json:"account,omitempty"` AccountName string `json:"account_name,omitempty"` Place string `json:"place,omitempty"` PlaceRevision int64 `json:"place_revision,omitempty"` BindingRevision int64 `json:"binding_revision,omitempty"` Destination string `json:"destination,omitempty"` // Mode is what the person's tap allowed (SiteSetup, SiteMint or // SitePublish); DeployName the key a setup or mint asks for. The // runner never changes mode. Mode string `json:"mode,omitempty"` DeployName string `json:"deploy_name,omitempty"` } // Publish modes. Setup creates the app with the owner's login, then mints // its deploy key; mint only mints the key with that login (the app // exists), then deploys with the key; publish deploys with the held key. const ( SiteSetup = "setup" SiteMint = "mint" SitePublish = "publish" ) // LeasePurposeMint is a LeaseCheck's Purpose right before a setup's or a // mint's services create, with the destination (ProtocolPlaces). const LeasePurposeMint = "site_mint" // PublishResult is a publish step's word: Phase built with the export's // digest, files and bytes; then, done, the app, the site's public URL and // the Outcome (published once the marker answered over https, unconfirmed // when it never did). type PublishResult struct { Phase string `json:"phase"` App string `json:"app,omitempty"` URL string `json:"url,omitempty"` Version string `json:"version,omitempty"` Digest string `json:"digest"` Files int `json:"files"` Bytes int64 `json:"bytes"` Outcome string `json:"outcome,omitempty"` // DeployHeld (ProtocolPlaces): a setup's or a mint's done report says // the runner holds the site's deploy key now. Only a recovery card // reads it; the key's evidence is the runner's reach. DeployHeld bool `json:"deploy_held,omitempty"` } // Publish phases and outcomes. const ( PublishBuilt = "built" PublishDone = "done" PublishPublished = "published" PublishUnconfirmed = "unconfirmed" ) // TaskSite is a merged task's website as the task shows it: the Carloku // app, the merged commit it publishes, where its publishing stands and, // once it answered, its public URL. type TaskSite struct { App string `json:"app"` SHA string `json:"sha"` State string `json:"state"` URL string `json:"url,omitempty"` At *time.Time `json:"at,omitempty"` } // Site states (TaskSite.State). const ( SiteAsked = "asked" SiteDeclined = "declined" SitePublishing = "publishing" SitePublished = "published" SiteUnconfirmed = "unconfirmed" SiteFailed = "failed" ) // CardSite is what a publish card offers: the app and the merged commit; // State and URL follow the publish it started. type CardSite struct { App string `json:"app"` SHA string `json:"sha"` State string `json:"state"` URL string `json:"url,omitempty"` } // MaxSiteApp is Carloku's longest app name. const MaxSiteApp = 32 // siteApp is a publish step's app: site- and a slug of lower-case letters // and digits in hyphen-separated runs, Carloku's app rules within. var siteApp = regexp.MustCompile(`^site-[a-z0-9]+(-[a-z0-9]+)*$`) // ValidSiteApp reports whether app is a publishable site's app name. func ValidSiteApp(app string) bool { return len(app) <= MaxSiteApp && siteApp.MatchString(app) } // downgradePublish clears a body's ProtocolPublish fields and refuses a // publish dispatch or check outright: an older peer has no words for them. func downgradePublish(body any, protocol int) (bool, error) { switch v := body.(type) { case *DispatchCommand: if v.Specialist == SpecialistPublish { return false, fmt.Errorf("%w: a publish step at protocol %d", ErrProtocol, protocol) } set := v.Publish != nil v.Publish = nil return set, nil case *StepReport: set := v.Publish != nil v.Publish = nil return set, nil case *LeaseCheck: if v.Purpose == LeasePurposePublish { return false, fmt.Errorf("%w: a publish check at protocol %d", ErrProtocol, protocol) } case *Hello: return WithoutSites(v), nil } return false, nil } // WithoutSites drops a hello's sites capability, in a copy. The first // hello is sent before the instance says its protocol: an older // instance's strict decoder would refuse it. func WithoutSites(h *Hello) bool { if h.Capabilities == nil || !h.Capabilities.Sites { return false } c := *h.Capabilities c.Sites = false h.Capabilities = &c return true }