|
| 1 | +{-# LANGUAGE LambdaCase #-} |
| 2 | + |
| 3 | +module GitHub.Data.Deployments |
| 4 | + ( DeploymentQueryOption (..) |
| 5 | + , renderDeploymentQueryOption |
| 6 | + |
| 7 | + , Deployment (..) |
| 8 | + , CreateDeployment (..) |
| 9 | + |
| 10 | + , DeploymentStatus (..) |
| 11 | + , DeploymentStatusState (..) |
| 12 | + , CreateDeploymentStatus (..) |
| 13 | + ) where |
| 14 | + |
| 15 | +import Control.Arrow (second) |
| 16 | + |
| 17 | +import Data.ByteString (ByteString) |
| 18 | +import Data.Maybe (catMaybes) |
| 19 | +import Data.Text (Text) |
| 20 | +import Data.Time.Clock (UTCTime) |
| 21 | +import Data.Vector (Vector) |
| 22 | + |
| 23 | +import GitHub.Data.Definitions (SimpleUser) |
| 24 | +import GitHub.Data.Id (Id) |
| 25 | +import GitHub.Data.Name (Name) |
| 26 | +import GitHub.Data.URL (URL) |
| 27 | +import GitHub.Internal.Prelude |
| 28 | + |
| 29 | +import qualified Data.Aeson as JSON |
| 30 | +import qualified Data.Text as Text |
| 31 | +import qualified Data.Text.Encoding as Text |
| 32 | + |
| 33 | +data DeploymentQueryOption |
| 34 | + = DeploymentQuerySha !Text |
| 35 | + | DeploymentQueryRef !Text |
| 36 | + | DeploymentQueryTask !Text |
| 37 | + | DeploymentQueryEnvironment !Text |
| 38 | + deriving (Show, Data, Typeable, Eq, Ord, Generic) |
| 39 | + |
| 40 | +instance NFData DeploymentQueryOption where rnf = genericRnf |
| 41 | +instance Binary DeploymentQueryOption |
| 42 | + |
| 43 | +renderDeploymentQueryOption :: DeploymentQueryOption -> (ByteString, ByteString) |
| 44 | +renderDeploymentQueryOption = |
| 45 | + second Text.encodeUtf8 . \case |
| 46 | + DeploymentQuerySha sha -> ("sha", sha) |
| 47 | + DeploymentQueryRef ref -> ("ref", ref) |
| 48 | + DeploymentQueryTask task -> ("task", task) |
| 49 | + DeploymentQueryEnvironment env -> ("environment", env) |
| 50 | + |
| 51 | +data Deployment a = Deployment |
| 52 | + { deploymentUrl :: !URL |
| 53 | + , deploymentId :: !(Id (Deployment a)) |
| 54 | + , deploymentSha :: !(Name (Deployment a)) |
| 55 | + , deploymentRef :: !Text |
| 56 | + , deploymentTask :: !Text |
| 57 | + , deploymentPayload :: !(Maybe a) |
| 58 | + , deploymentEnvironment :: !Text |
| 59 | + , deploymentDescription :: !Text |
| 60 | + , deploymentCreator :: !SimpleUser |
| 61 | + , deploymentCreatedAt :: !UTCTime |
| 62 | + , deploymentUpdatedAt :: !UTCTime |
| 63 | + , deploymentStatusesUrl :: !URL |
| 64 | + , deploymentRepositoryUrl :: !URL |
| 65 | + } deriving (Show, Data, Typeable, Eq, Ord, Generic) |
| 66 | + |
| 67 | +instance NFData a => NFData (Deployment a) where rnf = genericRnf |
| 68 | +instance Binary a => Binary (Deployment a) |
| 69 | + |
| 70 | +instance FromJSON a => FromJSON (Deployment a) where |
| 71 | + parseJSON = withObject "GitHub Deployment" $ \o -> |
| 72 | + Deployment |
| 73 | + <$> o .: "url" |
| 74 | + <*> o .: "id" |
| 75 | + <*> o .: "sha" |
| 76 | + <*> o .: "ref" |
| 77 | + <*> o .: "task" |
| 78 | + <*> o .:? "payload" |
| 79 | + <*> o .: "environment" |
| 80 | + <*> o .: "description" |
| 81 | + <*> o .: "creator" |
| 82 | + <*> o .: "created_at" |
| 83 | + <*> o .: "updated_at" |
| 84 | + <*> o .: "statuses_url" |
| 85 | + <*> o .: "repository_url" |
| 86 | + |
| 87 | +data CreateDeployment a = CreateDeployment |
| 88 | + { createDeploymentRef :: !Text |
| 89 | + -- ^ Required. The ref to deploy. This can be a branch, tag, or SHA. |
| 90 | + , createDeploymentTask :: !(Maybe Text) |
| 91 | + -- ^ Specifies a task to execute (e.g., deploy or deploy:migrations). |
| 92 | + -- Default: deploy |
| 93 | + , createDeploymentAutoMerge :: !(Maybe Bool) |
| 94 | + -- ^ Attempts to automatically merge the default branch into the requested |
| 95 | + -- ref, if it is behind the default branch. Default: true |
| 96 | + , createDeploymentRequiredContexts :: !(Maybe (Vector Text)) |
| 97 | + -- ^ The status contexts to verify against commit status checks. If this |
| 98 | + -- parameter is omitted, then all unique contexts will be verified before a |
| 99 | + -- deployment is created. To bypass checking entirely pass an empty array. |
| 100 | + -- Defaults to all unique contexts. |
| 101 | + , createDeploymentPayload :: !(Maybe a) |
| 102 | + -- ^ JSON payload with extra information about the deployment. Default: "" |
| 103 | + , createDeploymentEnvironment :: !(Maybe Text) |
| 104 | + -- ^ Name for the target deployment environment (e.g., production, staging, |
| 105 | + -- qa). Default: production |
| 106 | + , createDeploymentDescription :: !(Maybe Text) |
| 107 | + -- ^ Short description of the deployment. Default: "" |
| 108 | + } deriving (Show, Data, Typeable, Eq, Ord, Generic) |
| 109 | + |
| 110 | +instance NFData a => NFData (CreateDeployment a) where rnf = genericRnf |
| 111 | +instance Binary a => Binary (CreateDeployment a) |
| 112 | + |
| 113 | +instance ToJSON a => ToJSON (CreateDeployment a) where |
| 114 | + toJSON x = |
| 115 | + JSON.object $ catMaybes |
| 116 | + [ Just ("ref" .= createDeploymentRef x) |
| 117 | + , ("task" .=) <$> createDeploymentTask x |
| 118 | + , ("auto_merge" .=) <$> createDeploymentAutoMerge x |
| 119 | + , ("required_contexts" .=) <$> createDeploymentRequiredContexts x |
| 120 | + , ("payload" .=) <$> createDeploymentPayload x |
| 121 | + , ("environment" .=) <$> createDeploymentEnvironment x |
| 122 | + , ("description" .=) <$> createDeploymentDescription x |
| 123 | + ] |
| 124 | + |
| 125 | +data DeploymentStatus = DeploymentStatus |
| 126 | + { deploymentStatusUrl :: !URL |
| 127 | + , deploymentStatusId :: !(Id DeploymentStatus) |
| 128 | + , deploymentStatusState :: !DeploymentStatusState |
| 129 | + , deploymentStatusCreator :: !SimpleUser |
| 130 | + , deploymentStatusDescription :: !Text |
| 131 | + , deploymentStatusTargetUrl :: !URL |
| 132 | + , deploymentStatusCreatedAt :: !UTCTime |
| 133 | + , deploymentStatusUpdatedAt :: !UTCTime |
| 134 | + , deploymentStatusDeploymentUrl :: !URL |
| 135 | + , deploymentStatusRepositoryUrl :: !URL |
| 136 | + } deriving (Show, Data, Typeable, Eq, Ord, Generic) |
| 137 | + |
| 138 | +instance NFData DeploymentStatus where rnf = genericRnf |
| 139 | +instance Binary DeploymentStatus |
| 140 | + |
| 141 | +instance FromJSON DeploymentStatus where |
| 142 | + parseJSON = withObject "GitHub DeploymentStatus" $ \o -> |
| 143 | + DeploymentStatus |
| 144 | + <$> o .: "url" |
| 145 | + <*> o .: "id" |
| 146 | + <*> o .: "state" |
| 147 | + <*> o .: "creator" |
| 148 | + <*> o .: "description" |
| 149 | + <*> o .: "target_url" |
| 150 | + <*> o .: "created_at" |
| 151 | + <*> o .: "updated_at" |
| 152 | + <*> o .: "deployment_url" |
| 153 | + <*> o .: "repository_url" |
| 154 | + |
| 155 | +data DeploymentStatusState |
| 156 | + = DeploymentStatusError |
| 157 | + | DeploymentStatusFailure |
| 158 | + | DeploymentStatusPending |
| 159 | + | DeploymentStatusSuccess |
| 160 | + | DeploymentStatusInactive |
| 161 | + deriving (Show, Data, Typeable, Eq, Ord, Generic) |
| 162 | + |
| 163 | +instance NFData DeploymentStatusState where rnf = genericRnf |
| 164 | +instance Binary DeploymentStatusState |
| 165 | + |
| 166 | +instance ToJSON DeploymentStatusState where |
| 167 | + toJSON = \case |
| 168 | + DeploymentStatusError -> "error" |
| 169 | + DeploymentStatusFailure -> "failure" |
| 170 | + DeploymentStatusPending -> "pending" |
| 171 | + DeploymentStatusSuccess -> "success" |
| 172 | + DeploymentStatusInactive -> "inactive" |
| 173 | + |
| 174 | +instance FromJSON DeploymentStatusState where |
| 175 | + parseJSON = withText "GitHub DeploymentStatusState" $ \case |
| 176 | + "error" -> pure DeploymentStatusError |
| 177 | + "failure" -> pure DeploymentStatusFailure |
| 178 | + "pending" -> pure DeploymentStatusPending |
| 179 | + "success" -> pure DeploymentStatusSuccess |
| 180 | + "inactive" -> pure DeploymentStatusInactive |
| 181 | + x -> fail $ "Unknown deployment status: " ++ Text.unpack x |
| 182 | + |
| 183 | +data CreateDeploymentStatus = CreateDeploymentStatus |
| 184 | + { createDeploymentStatusState :: !DeploymentStatusState |
| 185 | + -- ^ Required. The state of the status. Can be one of error, failure, |
| 186 | + -- pending, or success. |
| 187 | + , createDeploymentStatusTargetUrl :: !(Maybe Text) -- TODO: should this be URL? |
| 188 | + -- ^ The target URL to associate with this status. This URL should contain |
| 189 | + -- output to keep the user updated while the task is running or serve as |
| 190 | + -- historical information for what happened in the deployment. Default: "" |
| 191 | + , createDeploymentStatusDescription :: !(Maybe Text) |
| 192 | + -- ^ A short description of the status. Maximum length of 140 characters. |
| 193 | + -- Default: "" |
| 194 | + } deriving (Show, Data, Typeable, Eq, Ord, Generic) |
| 195 | + |
| 196 | +instance NFData CreateDeploymentStatus where rnf = genericRnf |
| 197 | +instance Binary CreateDeploymentStatus |
| 198 | + |
| 199 | +instance ToJSON CreateDeploymentStatus where |
| 200 | + toJSON x = |
| 201 | + JSON.object $ catMaybes |
| 202 | + [ Just ("state" .= createDeploymentStatusState x) |
| 203 | + , ("target_url" .=) <$> createDeploymentStatusTargetUrl x |
| 204 | + , ("description" .=) <$> createDeploymentStatusDescription x |
| 205 | + ] |
0 commit comments