package models import ( "fmt" "slices" "github.com/bluesky-social/indigo/atproto/syntax" "tangled.org/core/api/tangled" ) // `sh.tangled.ci.event` type Event struct { SourceRepo syntax.ATURI // repository to find the workflow definition SourceSha string // sha to find the workflow definition TargetSha string // sha to run the workflow // union type of: // 1. PullRequestEvent // 2. PushEvent // 3. ManualEvent } func (e *Event) AsRecord() tangled.CiEvent { // var meta tangled.CiEvent_Meta // return tangled.CiEvent{ // Meta: &meta, // } panic("unimplemented") } // `sh.tangled.ci.pipeline` // // Pipeline is basically a group of workflows triggered by single event. type Pipeline2 struct { Did syntax.DID Rkey syntax.RecordKey Event Event // event that triggered the pipeline WorkflowRuns []WorkflowRun // workflow runs inside this pipeline } func (p *Pipeline2) AtUri() syntax.ATURI { return syntax.ATURI(fmt.Sprintf("at://%s/%s/%s", p.Did, tangled.CiPipelineNSID, p.Rkey)) } func (p *Pipeline2) AsRecord() tangled.CiPipeline { event := p.Event.AsRecord() runs := make([]string, len(p.WorkflowRuns)) for i, run := range p.WorkflowRuns { runs[i] = run.AtUri().String() } return tangled.CiPipeline{ Event: &event, WorkflowRuns: runs, } } // `sh.tangled.ci.workflow.run` type WorkflowRun struct { Did syntax.DID Rkey syntax.RecordKey AdapterId string // adapter id Name string // name of workflow run (not workflow definition name!) Status WorkflowStatus // workflow status // TODO: can add some custom fields like adapter-specific log-id } func (r WorkflowRun) WithStatus(status WorkflowStatus) WorkflowRun { return WorkflowRun{ Did: r.Did, Rkey: r.Rkey, AdapterId: r.AdapterId, Name: r.Name, Status: status, } } func (r *WorkflowRun) AtUri() syntax.ATURI { return syntax.ATURI(fmt.Sprintf("at://%s/%s/%s", r.Did, tangled.CiWorkflowRunNSID, r.Rkey)) } func (r *WorkflowRun) AsRecord() tangled.CiWorkflowRun { statusStr := string(r.Status) return tangled.CiWorkflowRun{ Adapter: r.AdapterId, Name: r.Name, Status: &statusStr, } } // `sh.tangled.ci.workflow.status` type WorkflowStatus string var ( WorkflowStatusPending WorkflowStatus = "pending" WorkflowStatusRunning WorkflowStatus = "running" WorkflowStatusFailed WorkflowStatus = "failed" WorkflowStatusCancelled WorkflowStatus = "cancelled" WorkflowStatusSuccess WorkflowStatus = "success" WorkflowStatusTimeout WorkflowStatus = "timeout" activeStatuses [2]WorkflowStatus = [2]WorkflowStatus{ WorkflowStatusPending, WorkflowStatusRunning, } ) func (s WorkflowStatus) IsActive() bool { return slices.Contains(activeStatuses[:], s) } func (s WorkflowStatus) IsFinish() bool { return !s.IsActive() } // `sh.tangled.ci.workflow.def` // // Brief information of the workflow definition. A workflow can be defined in // any form. This is a common info struct for any workflow definitions type WorkflowDef struct { AdapterId string // adapter id Name string // name or the workflow (usually the yml file name) When any // events the workflow is listening to }