130 lines
3.4 KiB
Go
130 lines
3.4 KiB
Go
package tui
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"git.a-wan.net/alan/pipelines/internal/client"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type editorPublicationMsg struct {
|
|
edit editedMsg
|
|
generation uint64
|
|
pipelines []protocol.PipelineSummary
|
|
detail *protocol.PipelineDetail
|
|
published bool
|
|
err error
|
|
}
|
|
|
|
func checkEditorPublication(ctx context.Context, endpoint string, edit editedMsg, generation uint64) tea.Cmd {
|
|
return func() tea.Msg {
|
|
result := editorPublicationMsg{edit: edit, generation: generation}
|
|
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
defer cancel()
|
|
c, err := client.Open(ctx, endpoint)
|
|
if err != nil {
|
|
result.err = err
|
|
return result
|
|
}
|
|
defer c.Close()
|
|
list, err := c.Call(ctx, protocol.Message{Type: protocol.List})
|
|
if err != nil {
|
|
result.err = err
|
|
return result
|
|
}
|
|
result.pipelines = list.Pipelines
|
|
for _, p := range list.Pipelines {
|
|
if p.Name != strings.TrimSpace(edit.name) {
|
|
continue
|
|
}
|
|
response, err := c.Call(ctx, protocol.Message{Type: protocol.GetPipeline, PipelineID: p.ID})
|
|
if err != nil {
|
|
result.err = err
|
|
return result
|
|
}
|
|
result.detail = response.Detail
|
|
if result.detail == nil {
|
|
result.err = fmt.Errorf("server returned no pipeline detail")
|
|
return result
|
|
}
|
|
if result.detail.Source == edit.source {
|
|
result.published = true
|
|
return result
|
|
}
|
|
// If another version was published afterward, the editor's submitted
|
|
// source still must not be offered for duplicate submission. Only
|
|
// inspect versions newer than this draft's immutable starting point.
|
|
for _, v := range result.detail.Versions {
|
|
if v.Version <= edit.baseVersion || v.Version == result.detail.Version {
|
|
continue
|
|
}
|
|
response, err := c.Call(ctx, protocol.Message{Type: protocol.GetPipeline, PipelineID: p.ID, Version: v.Version})
|
|
if err != nil {
|
|
result.err = err
|
|
return result
|
|
}
|
|
if response.Detail != nil && response.Detail.Source == edit.source {
|
|
result.published = true
|
|
return result
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
return result
|
|
}
|
|
}
|
|
|
|
func (m Model) finishEditorPublication(msg editorPublicationMsg) (tea.Model, tea.Cmd) {
|
|
if msg.generation != m.editorCheckGeneration {
|
|
return m, nil
|
|
}
|
|
m.editorChecking = false
|
|
if msg.err == nil {
|
|
m.pipelines = msg.pipelines
|
|
m.loaded = true
|
|
if msg.detail != nil {
|
|
m.detail = msg.detail
|
|
m.keys.detail = true
|
|
m.detailLoading = false
|
|
m.detailError = ""
|
|
m.detailRequest = ""
|
|
m.requestedPipelineID = msg.detail.ID
|
|
m.requestedVersion = msg.detail.Version
|
|
m.resetPipelineView()
|
|
}
|
|
}
|
|
if msg.published {
|
|
m.pendingDraft = nil
|
|
m.confirmOpen = false
|
|
m.keys.confirm = false
|
|
m.draftOpen = false
|
|
m.keys.drafts = false
|
|
m.templateOpen = false
|
|
m.keys.templates = false
|
|
m.submitNotice = "Already published; reloaded latest version."
|
|
m.appendLine(m.submitNotice)
|
|
return m, nil
|
|
}
|
|
if msg.edit.unchanged {
|
|
m.confirmOpen = false
|
|
m.keys.confirm = false
|
|
m.submitNotice = "No local changes; page refreshed."
|
|
if msg.err != nil {
|
|
m.submitNotice = "No local changes; refresh failed: " + msg.err.Error()
|
|
}
|
|
m.appendLine(m.submitNotice)
|
|
return m, nil
|
|
}
|
|
m.pendingDraft = &msg.edit
|
|
m.confirmOpen = true
|
|
m.keys.confirm = true
|
|
m.submitNotice = ""
|
|
if msg.err != nil {
|
|
m.submitNotice = "Could not check publication; draft retained: " + msg.err.Error()
|
|
}
|
|
return m, nil
|
|
}
|