|
| 1 | +{-# LANGUAGE DeriveDataTypeable #-} |
| 2 | +{-# LANGUAGE DeriveGeneric #-} |
| 3 | +{-# LANGUAGE NoImplicitPrelude #-} |
| 4 | +{-# LANGUAGE OverloadedStrings #-} |
| 5 | +module GitHub.Data.Statuses where |
| 6 | + |
| 7 | +import GitHub.Data.Definitions |
| 8 | +import GitHub.Data.Name (Name) |
| 9 | +import GitHub.Data.Id (Id) |
| 10 | +import GitHub.Data.URL (URL) |
| 11 | +import GitHub.Internal.Prelude |
| 12 | +import Prelude () |
| 13 | + |
| 14 | +import GitHub.Data.GitData (Commit) |
| 15 | +import GitHub.Data.Repos (RepoRef) |
| 16 | + |
| 17 | + |
| 18 | +data StatusState |
| 19 | + = StatusPending |
| 20 | + | StatusSuccess |
| 21 | + | StatusError |
| 22 | + | StatusFailure |
| 23 | + deriving (Show, Data, Enum, Bounded, Typeable, Eq, Ord, Generic) |
| 24 | + |
| 25 | +instance NFData StatusState where rnf = genericRnf |
| 26 | +instance Binary StatusState |
| 27 | + |
| 28 | +instance FromJSON StatusState where |
| 29 | + parseJSON (String "pending") = pure StatusPending |
| 30 | + parseJSON (String "success") = pure StatusSuccess |
| 31 | + parseJSON (String "error") = pure StatusError |
| 32 | + parseJSON (String "failure") = pure StatusFailure |
| 33 | + parseJSON _ = fail "Could not build a StatusState" |
| 34 | + |
| 35 | +instance ToJSON StatusState where |
| 36 | + toJSON StatusPending = String "pending" |
| 37 | + toJSON StatusSuccess = String "success" |
| 38 | + toJSON StatusError = String "error" |
| 39 | + toJSON StatusFailure = String "failure" |
| 40 | + |
| 41 | + |
| 42 | +data Status = Status |
| 43 | + { statusCreatedAt :: !UTCTime |
| 44 | + , statusUpdatedAt :: !UTCTime |
| 45 | + , statusState :: !StatusState |
| 46 | + , statusTargetUrl :: !(Maybe URL) |
| 47 | + , statusDescription :: !(Maybe Text) |
| 48 | + , statusId :: !(Id Status) |
| 49 | + , statusUrl :: !URL |
| 50 | + , statusContext :: !(Maybe Text) |
| 51 | + , statusCreator :: !(Maybe SimpleUser) |
| 52 | + } |
| 53 | + deriving (Show, Data, Typeable, Eq, Ord, Generic) |
| 54 | + |
| 55 | +instance FromJSON Status where |
| 56 | + parseJSON = withObject "Status" $ \o -> Status |
| 57 | + <$> o .: "created_at" |
| 58 | + <*> o .: "updated_at" |
| 59 | + <*> o .: "state" |
| 60 | + <*> o .:? "target_url" |
| 61 | + <*> o .:? "description" |
| 62 | + <*> o .: "id" |
| 63 | + <*> o .: "url" |
| 64 | + <*> o .:? "context" |
| 65 | + <*> o .:? "creator" |
| 66 | + |
| 67 | + |
| 68 | +data NewStatus = NewStatus |
| 69 | + { newStatusState :: !StatusState |
| 70 | + , newStatusTargetUrl :: !(Maybe URL) |
| 71 | + , newStatusDescription :: !(Maybe Text) |
| 72 | + , newStatusContext :: !(Maybe Text) |
| 73 | + } |
| 74 | + deriving (Show, Data, Typeable, Eq, Ord, Generic) |
| 75 | + |
| 76 | +instance NFData NewStatus where rnf = genericRnf |
| 77 | +instance Binary NewStatus |
| 78 | + |
| 79 | +instance ToJSON NewStatus where |
| 80 | + toJSON (NewStatus s t d c) = object $ filter notNull $ |
| 81 | + [ "state" .= s |
| 82 | + , "target_url" .= t |
| 83 | + , "description" .= d |
| 84 | + , "context" .= c |
| 85 | + ] |
| 86 | + where |
| 87 | + notNull (_, Null) = False |
| 88 | + notNull (_, _) = True |
| 89 | + |
| 90 | + |
| 91 | +data CombinedStatus = CombinedStatus |
| 92 | + { combinedStatusState :: !StatusState |
| 93 | + , combinedStatusSha :: !(Name Commit) |
| 94 | + , combinedStatusTotalCount :: !Int |
| 95 | + , combinedStatusStatuses :: !(Vector Status) |
| 96 | + , combinedStatusRepository :: !RepoRef |
| 97 | + , combinedStatusCommitUrl :: !URL |
| 98 | + , combinedStatusUrl :: !URL |
| 99 | + } |
| 100 | + deriving (Show, Data, Typeable, Eq, Ord, Generic) |
| 101 | + |
| 102 | +instance FromJSON CombinedStatus where |
| 103 | + parseJSON = withObject "CombinedStatus" $ \o -> CombinedStatus |
| 104 | + <$> o .: "state" |
| 105 | + <*> o .: "sha" |
| 106 | + <*> o .: "total_count" |
| 107 | + <*> o .: "statuses" |
| 108 | + <*> o .: "repository" |
| 109 | + <*> o .: "commit_url" |
| 110 | + <*> o .: "url" |
0 commit comments