|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + "unicode/utf8" |
| 9 | + |
| 10 | + "github.com/flashcatcloud/go-flashduty" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +const maxPostMortemContentIdempotencyKeyRunes = 128 |
| 15 | + |
| 16 | +func newIncidentPostMortemContentResetCmd() *cobra.Command { |
| 17 | + var ( |
| 18 | + markdownFile string |
| 19 | + expectedRevision int64 |
| 20 | + idempotencyKey string |
| 21 | + ) |
| 22 | + |
| 23 | + cmd := &cobra.Command{ |
| 24 | + Use: "post-mortem-content-reset <post-mortem-id>", |
| 25 | + Short: "Reset post-mortem Markdown content", |
| 26 | + Long: curatedLong( |
| 27 | + "Replace the collaborative Markdown body of a drafting post-mortem report in one shot.\n\n"+ |
| 28 | + "Read Markdown from --markdown-file (or \"-\" for stdin). The entire file is sent as-is; leading and trailing content is preserved. Empty Markdown is rejected.\n\n"+ |
| 29 | + "--expected-revision guards against overwriting a concurrent edit: the reset only succeeds when the document's current revision equals it, and 0 is valid (first write / empty document). Negative values are rejected. When omitted, the CLI first fetches the report's current revision via the post-mortem info endpoint and uses that; pass it explicitly for strict concurrency control, when the caller already holds a revision and must fail on any intervening write.\n\n"+ |
| 30 | + "--idempotency-key is required, must be non-empty, and at most 128 Unicode characters.", |
| 31 | + "Incidents", |
| 32 | + "PostMortemWriteResetContent", |
| 33 | + ), |
| 34 | + Args: requireExactArg("post-mortem-id"), |
| 35 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 36 | + return runCommand(cmd, args, func(ctx *RunContext) error { |
| 37 | + markdown, err := readPostMortemMarkdownFile(markdownFile) |
| 38 | + if err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + if err := validatePostMortemContentResetFlags(idempotencyKey); err != nil { |
| 42 | + return err |
| 43 | + } |
| 44 | + |
| 45 | + revision := expectedRevision |
| 46 | + if cmd.Flags().Changed("expected-revision") { |
| 47 | + if revision < 0 { |
| 48 | + return fmt.Errorf("--expected-revision must be >= 0") |
| 49 | + } |
| 50 | + } else { |
| 51 | + revision, err = currentPostMortemRevisionFn(ctx, ctx.Args[0]) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + out, _, err := ctx.Client.Incidents.PostMortemWriteResetContent(cmdContext(ctx.Cmd), &flashduty.ResetPostMortemContentRequest{ |
| 58 | + PostMortemID: ctx.Args[0], |
| 59 | + Markdown: markdown, |
| 60 | + ExpectedRevision: flashduty.Int64(revision), |
| 61 | + IdempotencyKey: idempotencyKey, |
| 62 | + }) |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + |
| 67 | + human := fmt.Sprintf( |
| 68 | + "Reset post-mortem content for %s: generation %d→%d, revision %d→%d", |
| 69 | + out.PostMortemID, |
| 70 | + out.PreviousGeneration, |
| 71 | + out.Generation, |
| 72 | + out.PreviousRevision, |
| 73 | + out.Revision, |
| 74 | + ) |
| 75 | + return ctx.WriteResultJSON(out, human) |
| 76 | + }) |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + cmd.Flags().StringVar(&markdownFile, "markdown-file", "", "Path to Markdown content, or \"-\" to read stdin (required)") |
| 81 | + cmd.Flags().Int64Var(&expectedRevision, "expected-revision", 0, "Expected document revision; 0 is valid (optional: when omitted, the current revision is fetched via post-mortem info first)") |
| 82 | + cmd.Flags().StringVar(&idempotencyKey, "idempotency-key", "", "Idempotency key for safe retries; max 128 Unicode characters (required)") |
| 83 | + _ = cmd.MarkFlagRequired("markdown-file") |
| 84 | + _ = cmd.MarkFlagRequired("idempotency-key") |
| 85 | + |
| 86 | + return cmd |
| 87 | +} |
| 88 | + |
| 89 | +func validatePostMortemContentResetFlags(idempotencyKey string) error { |
| 90 | + if idempotencyKey == "" { |
| 91 | + return fmt.Errorf("--idempotency-key must not be empty") |
| 92 | + } |
| 93 | + if utf8.RuneCountInString(idempotencyKey) > maxPostMortemContentIdempotencyKeyRunes { |
| 94 | + return fmt.Errorf("--idempotency-key must be at most %d Unicode characters", maxPostMortemContentIdempotencyKeyRunes) |
| 95 | + } |
| 96 | + return nil |
| 97 | +} |
| 98 | + |
| 99 | +// currentPostMortemRevisionFn resolves the current collaboration revision of a |
| 100 | +// post-mortem report. It is a package variable so tests can stub it. |
| 101 | +var currentPostMortemRevisionFn = fetchCurrentPostMortemRevision |
| 102 | + |
| 103 | +// fetchCurrentPostMortemRevision reads the report's current collaboration |
| 104 | +// revision via the typed SDK (data.meta.revision on PostMortemInfo, exposed |
| 105 | +// since go-flashduty v0.5.12). Revision 0 is a legitimate value — a document |
| 106 | +// that has never been saved — so no presence check is needed. |
| 107 | +func fetchCurrentPostMortemRevision(ctx *RunContext, postMortemID string) (int64, error) { |
| 108 | + info, _, err := ctx.Client.Incidents.PostMortemInfo(cmdContext(ctx.Cmd), &flashduty.IncidentsPostMortemInfoRequest{ |
| 109 | + PostMortemID: postMortemID, |
| 110 | + }) |
| 111 | + if err != nil { |
| 112 | + return 0, fmt.Errorf("failed to fetch post-mortem info for %s: %w", postMortemID, err) |
| 113 | + } |
| 114 | + return info.Meta.Revision, nil |
| 115 | +} |
| 116 | + |
| 117 | +// readPostMortemMarkdownFile loads Markdown bytes without trimming. Path "-" |
| 118 | +// reads the injectable stdinReader so tests never touch the real stdin and |
| 119 | +// absent flags never block on an empty pipe. |
| 120 | +func readPostMortemMarkdownFile(path string) (string, error) { |
| 121 | + path = strings.TrimSpace(path) |
| 122 | + if path == "" { |
| 123 | + return "", fmt.Errorf("--markdown-file is required") |
| 124 | + } |
| 125 | + |
| 126 | + var ( |
| 127 | + b []byte |
| 128 | + err error |
| 129 | + ) |
| 130 | + if path == "-" { |
| 131 | + b, err = io.ReadAll(stdinReader) |
| 132 | + if err != nil { |
| 133 | + return "", fmt.Errorf("failed to read markdown from stdin: %w", err) |
| 134 | + } |
| 135 | + } else { |
| 136 | + b, err = os.ReadFile(path) |
| 137 | + if err != nil { |
| 138 | + return "", fmt.Errorf("failed to read markdown file: %w", err) |
| 139 | + } |
| 140 | + } |
| 141 | + if len(b) == 0 { |
| 142 | + return "", fmt.Errorf("markdown content must not be empty") |
| 143 | + } |
| 144 | + return string(b), nil |
| 145 | +} |
0 commit comments