// Code generated by cmd/lexgen (see Makefile's lexgen); DO NOT EDIT.

package tangled

// schema: sh.tangled.ci.pipeline

import (
	"encoding/json"
	"fmt"

	"github.com/bluesky-social/indigo/lex/util"
)

const (
	CiPipelineNSID = "sh.tangled.ci.pipeline"
)

// CiPipeline is a "main" in the sh.tangled.ci.pipeline schema.
//
// A CI pipeline. Record-like, but owned by the spindle rather than a PDS.
type CiPipeline struct {
	// commit: Commit Id this pipeline is running on
	Commit    string  `json:"commit" cborgen:"commit"`
	CreatedAt *string `json:"createdAt,omitempty" cborgen:"createdAt,omitempty"`
	// id: Spindle-local pipeline id
	Id string `json:"id" cborgen:"id"`
	// repo: Repository DID
	Repo *string `json:"repo,omitempty" cborgen:"repo,omitempty"`
	// sourceRepo: Repository DID that the commit was checked out from, if different from repo (e.g. a fork for a fork-based pull request)
	SourceRepo *string `json:"sourceRepo,omitempty" cborgen:"sourceRepo,omitempty"`
	// trigger: Trigger event metadata
	Trigger *CiPipeline_Trigger `json:"trigger" cborgen:"trigger"`
	// workflows: Triggered workflows
	Workflows []*CiPipeline_Workflow `json:"workflows" cborgen:"workflows"`
}

// Trigger event metadata
type CiPipeline_Trigger struct {
	CiTrigger_Push        *CiTrigger_Push
	CiTrigger_PullRequest *CiTrigger_PullRequest
	CiTrigger_Manual      *CiTrigger_Manual
}

func (t *CiPipeline_Trigger) MarshalJSON() ([]byte, error) {
	if t.CiTrigger_Push != nil {
		t.CiTrigger_Push.LexiconTypeID = "sh.tangled.ci.trigger#push"
		return json.Marshal(t.CiTrigger_Push)
	}
	if t.CiTrigger_PullRequest != nil {
		t.CiTrigger_PullRequest.LexiconTypeID = "sh.tangled.ci.trigger#pullRequest"
		return json.Marshal(t.CiTrigger_PullRequest)
	}
	if t.CiTrigger_Manual != nil {
		t.CiTrigger_Manual.LexiconTypeID = "sh.tangled.ci.trigger#manual"
		return json.Marshal(t.CiTrigger_Manual)
	}
	return nil, fmt.Errorf("cannot marshal empty enum")
}
func (t *CiPipeline_Trigger) UnmarshalJSON(b []byte) error {
	typ, err := util.TypeExtract(b)
	if err != nil {
		return err
	}

	switch typ {
	case "sh.tangled.ci.trigger#push":
		t.CiTrigger_Push = new(CiTrigger_Push)
		return json.Unmarshal(b, t.CiTrigger_Push)
	case "sh.tangled.ci.trigger#pullRequest":
		t.CiTrigger_PullRequest = new(CiTrigger_PullRequest)
		return json.Unmarshal(b, t.CiTrigger_PullRequest)
	case "sh.tangled.ci.trigger#manual":
		t.CiTrigger_Manual = new(CiTrigger_Manual)
		return json.Unmarshal(b, t.CiTrigger_Manual)

	default:
		return nil
	}
}

// CiPipeline_Workflow is a "workflow" in the sh.tangled.ci.pipeline schema.
type CiPipeline_Workflow struct {
	Error      *string `json:"error,omitempty" cborgen:"error,omitempty"`
	FinishedAt *string `json:"finishedAt,omitempty" cborgen:"finishedAt,omitempty"`
	// id: Spindle-local workflow id. Unique per pipeline, usually same as name.
	Id string `json:"id" cborgen:"id"`
	// name: Name of the workflow
	Name      string  `json:"name" cborgen:"name"`
	StartedAt *string `json:"startedAt,omitempty" cborgen:"startedAt,omitempty"`
	// status: Workflow status
	Status string `json:"status" cborgen:"status"`
}
