|
| 1 | +module UnisonShare.Check exposing (..) |
| 2 | + |
| 3 | +import Code.BranchRef as BranchRef exposing (BranchRef) |
| 4 | +import Code.Hash as Hash exposing (Hash) |
| 5 | +import Json.Decode as Decode |
| 6 | +import Json.Decode.Pipeline exposing (required) |
| 7 | +import Lib.Decode.Helpers exposing (url, whenTagIs) |
| 8 | +import Lib.UserHandle as UserHandle exposing (UserHandle) |
| 9 | +import Parser exposing (Parser) |
| 10 | +import UI.DateTime as DateTime exposing (DateTime) |
| 11 | +import UnisonShare.Project.ProjectRef as ProjectRef exposing (ProjectRef) |
| 12 | +import Url exposing (Url) |
| 13 | + |
| 14 | + |
| 15 | +type CheckStatus |
| 16 | + = NotStarted |
| 17 | + | Waiting { startedAt : DateTime } |
| 18 | + | TimeOut |
| 19 | + { startedAt : DateTime |
| 20 | + , endedAt : DateTime |
| 21 | + } |
| 22 | + | Failure |
| 23 | + { startedAt : DateTime |
| 24 | + , endedAt : DateTime |
| 25 | + , errorTitle : String |
| 26 | + , errorDetails : String |
| 27 | + , output : String |
| 28 | + } |
| 29 | + | Success |
| 30 | + { startedAt : DateTime |
| 31 | + , endedAt : DateTime |
| 32 | + , output : String |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | +type Runner |
| 37 | + = LocalUcm UserHandle |
| 38 | + | Webhook Url |
| 39 | + |
| 40 | + |
| 41 | +type alias Check = |
| 42 | + { id : CheckId |
| 43 | + , runner : Runner |
| 44 | + , projectRef : ProjectRef |
| 45 | + , branchRef : BranchRef |
| 46 | + , causalHash : Hash |
| 47 | + , createdAt : DateTime |
| 48 | + , updatedAt : DateTime |
| 49 | + , status : CheckStatus |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +-- CheckId |
| 55 | + |
| 56 | + |
| 57 | +type CheckId |
| 58 | + = CheckId String |
| 59 | + |
| 60 | + |
| 61 | +checkIdToString : CheckId -> String |
| 62 | +checkIdToString (CheckId raw) = |
| 63 | + raw |
| 64 | + |
| 65 | + |
| 66 | +{-| TODO |
| 67 | +-} |
| 68 | +checkIdFromString : String -> Maybe CheckId |
| 69 | +checkIdFromString raw = |
| 70 | + Just (CheckId raw) |
| 71 | + |
| 72 | + |
| 73 | +checkIdFromUrl : Parser CheckId |
| 74 | +checkIdFromUrl = |
| 75 | + let |
| 76 | + parseMaybe checkId = |
| 77 | + case checkId of |
| 78 | + Just checkId_ -> |
| 79 | + Parser.succeed checkId_ |
| 80 | + |
| 81 | + Nothing -> |
| 82 | + Parser.problem "Invalid CheckId" |
| 83 | + in |
| 84 | + Parser.chompUntilEndOr "/" |
| 85 | + |> Parser.getChompedString |
| 86 | + |> Parser.map checkIdFromString |
| 87 | + |> Parser.andThen parseMaybe |
| 88 | + |
| 89 | + |
| 90 | +title : Check -> String |
| 91 | +title check = |
| 92 | + "" |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +-- DECODE |
| 97 | + |
| 98 | + |
| 99 | +decodeRunner : Decode.Decoder Runner |
| 100 | +decodeRunner = |
| 101 | + Decode.oneOf |
| 102 | + [ whenTagIs "Local" (Decode.map LocalUcm (Decode.field "handle" UserHandle.decode)) |
| 103 | + , whenTagIs "Webhook" (Decode.map Webhook (Decode.field "url" url)) |
| 104 | + ] |
| 105 | + |
| 106 | + |
| 107 | +decodeStatus : Decode.Decoder CheckStatus |
| 108 | +decodeStatus = |
| 109 | + let |
| 110 | + decodeWaiting = |
| 111 | + Decode.succeed (\s -> Waiting { startedAt = s }) |
| 112 | + |> required "startedAt" DateTime.decode |
| 113 | + |
| 114 | + decodeTimeOut = |
| 115 | + Decode.succeed (\s e -> TimeOut { startedAt = s, endedAt = e }) |
| 116 | + |> required "startedAt" DateTime.decode |
| 117 | + |> required "endedAt" DateTime.decode |
| 118 | + |
| 119 | + decodeFailure = |
| 120 | + let |
| 121 | + mkFailure started ended error errorDetails output = |
| 122 | + Failure |
| 123 | + { startedAt = started |
| 124 | + , endedAt = ended |
| 125 | + , errorTitle = error |
| 126 | + , errorDetails = errorDetails |
| 127 | + , output = output |
| 128 | + } |
| 129 | + in |
| 130 | + Decode.succeed mkFailure |
| 131 | + |> required "startedAt" DateTime.decode |
| 132 | + |> required "endedAt" DateTime.decode |
| 133 | + |> required "errorTitle" Decode.string |
| 134 | + |> required "errorDetails" Decode.string |
| 135 | + |> required "output" Decode.string |
| 136 | + |
| 137 | + decodeSuccess = |
| 138 | + Decode.succeed (\s e o -> Success { startedAt = s, endedAt = e, output = o }) |
| 139 | + |> required "startedAt" DateTime.decode |
| 140 | + |> required "endedAt" DateTime.decode |
| 141 | + |> required "output" Decode.string |
| 142 | + in |
| 143 | + Decode.oneOf |
| 144 | + [ whenTagIs "NotStarted" (Decode.succeed NotStarted) |
| 145 | + , whenTagIs "Waiting" decodeWaiting |
| 146 | + , whenTagIs "TimeOut" decodeTimeOut |
| 147 | + , whenTagIs "Failure" decodeFailure |
| 148 | + , whenTagIs "Success" decodeSuccess |
| 149 | + ] |
| 150 | + |
| 151 | + |
| 152 | +decodeCheckId : Decode.Decoder CheckId |
| 153 | +decodeCheckId = |
| 154 | + Decode.map CheckId Decode.string |
| 155 | + |
| 156 | + |
| 157 | +decode : Decode.Decoder Check |
| 158 | +decode = |
| 159 | + let |
| 160 | + make id runner projectRef branchRef causalHash createdAt updatedAt status = |
| 161 | + { id = id |
| 162 | + , runner = runner |
| 163 | + , projectRef = projectRef |
| 164 | + , branchRef = branchRef |
| 165 | + , causalHash = causalHash |
| 166 | + , createdAt = createdAt |
| 167 | + , updatedAt = updatedAt |
| 168 | + , status = status |
| 169 | + } |
| 170 | + in |
| 171 | + Decode.succeed make |
| 172 | + |> required "id" decodeCheckId |
| 173 | + |> required "runner" decodeRunner |
| 174 | + |> required "projectRef" ProjectRef.decode |
| 175 | + |> required "branchRef" BranchRef.decode |
| 176 | + |> required "causalHash" Hash.decode |
| 177 | + |> required "createdAt" DateTime.decode |
| 178 | + |> required "updatedAt" DateTime.decode |
| 179 | + |> required "status" decodeStatus |
0 commit comments