@@ -44,7 +44,7 @@ import Server.Schema
44import Squeal.PostgreSQL
44import Squeal.PostgreSQL
45import System.IO
45import System.IO
46import Text.Blaze.Html
46import Text.Blaze.Html
47−import UnliftIO.Exception (try)
47+import UnliftIO.Exception (catch, throwIO, try)
50 "github-webhook"
50 "github-webhook"
@@ -52,8 +52,8 @@ type API =
52 :> GitHubSignedReqBody '[JSON] InstallationEvent
52 :> GitHubSignedReqBody '[JSON] InstallationEvent
53 :> Post '[JSON] ()
53 :> Post '[JSON] ()
54 :<|> GitHubEvent '[ 'WebhookPushEvent]
54 :<|> GitHubEvent '[ 'WebhookPushEvent]
55− :> GitHubSignedReqBody '[JSON] PushEvent
55+ :> GitHubSignedReqBody '[JSON] PushEvent
56− :> Post '[JSON] ()
56+ :> Post '[JSON] ()
58 :<|> "submission"
58 :<|> "submission"
59 :> Capture "user name" String
59 :> Capture "user name" String
@@ -78,7 +78,7 @@ pendingStatus :: NewStatus
78pendingStatus = NewStatus StatusPending Nothing Nothing Nothing
78pendingStatus = NewStatus StatusPending Nothing Nothing Nothing
80webhookPushEvent ::
80webhookPushEvent ::
81− (StaticPQ m, WithLog env Message m, MonadUnliftIO m, HasTasks m) =>
81+ (StaticPQ m, WithLog env Message m, MonadUnliftIO m, HasTasks m, MonadError ServerError m) =>
82 RepoWebhookEvent ->
82 RepoWebhookEvent ->
83 ((), PushEvent) ->
83 ((), PushEvent) ->
@@ -86,8 +86,10 @@ webhookPushEvent _ ((), ev) = do
86 let repo = evPushRepository ev
86 let repo = evPushRepository ev
87 user = T.unpack . whUserLogin . evPushSender $ ev
87 user = T.unpack . whUserLogin . evPushSender $ ev
88 fullRepoName = whRepoFullName repo
88 fullRepoName = whRepoFullName repo
89− [owner, repoName] = T.splitOn "/" fullRepoName
89+ (owner, repoName) <- case T.splitOn "/" fullRepoName of
90+ [a, b] -> return (a, b)
91+ _ -> throwError err422
90− Just sha = evPushHeadSha ev
92+ sha <- maybe (throwError err422) return $ evPushHeadSha ev
91 tasks <- getTasks
93 tasks <- getTasks
92 let task =
94 let task =
93 M.lookupMax $
95 M.lookupMax $
@@ -102,7 +104,7 @@ webhookPushEvent _ ((), ev) = do
102 (T.unpack name)
104 (T.unpack name)
103 (T.unpack sha)
105 (T.unpack sha)
104 (Jsonb BuildScheduled)
106 (Jsonb BuildScheduled)
105− scheduleTask $ Build prebuild dockerfile (N owner) (N repoName) (N sha)
107+ scheduleTask $ Build prebuild dockerfile (N owner) (N repoName) (N sha) timeoutMinutes
106 liftIO $ do
108 liftIO $ do
108 hFlush stdout
110 hFlush stdout
@@ -130,7 +132,7 @@ retestSubmission owner repoName sha = do
130 Just (name, TaskConfig {..}) | T.take (T.length name) repoName' == name -> do
132 Just (name, TaskConfig {..}) | T.take (T.length name) repoName' == name -> do
131 scheduleTask $ StatusUpdate (N owner') (N repoName') (N sha') pendingStatus
133 scheduleTask $ StatusUpdate (N owner') (N repoName') (N sha') pendingStatus
132 updateSubmissionStatus fullRepoName sha BuildScheduled
134 updateSubmissionStatus fullRepoName sha BuildScheduled
133− scheduleTask $ Build prebuild dockerfile (N owner') (N repoName') (N sha')
135+ scheduleTask $ Build prebuild dockerfile (N owner') (N repoName') (N sha') timeoutMinutes
134 _ -> return ()
136 _ -> return ()
135 redirectToSubmission owner repoName sha
137 redirectToSubmission owner repoName sha
@@ -148,6 +150,10 @@ newtype ServerM (schema :: SchemasType) a
148 MonadUnliftIO
150 MonadUnliftIO
153+instance MonadError ServerError (ServerM schema) where
154+ throwError = throwIO
151server :: ServerT API (ServerM Schema)
157server :: ServerT API (ServerM Schema)
152server = (webhookInstallation :<|> webhookPushEvent) :<|> getSubmissionR :<|> retestSubmission :<|> getResults
158server = (webhookInstallation :<|> webhookPushEvent) :<|> getSubmissionR :<|> retestSubmission :<|> getResults
@@ -164,15 +170,16 @@ runServer = do
164 let context =
170 let context =
165 gitPolyHubKey (return webhookSecret)
171 gitPolyHubKey (return webhookSecret)
166 :. EmptyContext
172 :. EmptyContext
167− serverData = ServerData
173+ serverData =
168− { githubAppAuth = auth,
175+ { githubAppAuth = auth,
169− githubUserName = GithubUserName githubUsername,
176+ githubUserName = GithubUserName githubUsername,
170− githubAccessToken = GithubAccessToken personalAccessToken,
177+ githubAccessToken = GithubAccessToken personalAccessToken,
171− tasks = ts,
178+ tasks = ts,
172− logger = cfilter ((logSeverity <=) . msgSeverity) richMessageAction,
179+ logger = cfilter ((logSeverity <=) . msgSeverity) richMessageAction,
173− baseUrl = baseSiteUrl,
180+ baseUrl = baseSiteUrl,
174− dockerUrl = T.pack cfgDockerUrl
181+ dockerUrl = T.pack cfgDockerUrl
176 repeatIfNotEmpty n f = f >>= \m -> do
183 repeatIfNotEmpty n f = f >>= \m -> do
177 when (m == 0) $ liftIO $ threadDelay n
184 when (m == 0) $ liftIO $ threadDelay n
178 repeatIfNotEmpty n f
185 repeatIfNotEmpty n f
@@ -221,7 +228,6 @@ instance HasContextEntry '[PolyGitHubKey] (GitHubKey result) where
221 getContextEntry (PolyGitHubKey x :. _) = x
228 getContextEntry (PolyGitHubKey x :. _) = x
223instance MonadReader (ServerData n) m => GithubCloner m where
230instance MonadReader (ServerData n) m => GithubCloner m where
225 getGithubAccessToken = asks githubAccessToken
231 getGithubAccessToken = asks githubAccessToken
227 getGithubUserName = asks githubUserName
233 getGithubUserName = asks githubUserName
@@ -245,8 +251,8 @@ type TasksConfig = Map T.Text TaskConfig
245data TaskConfig
251data TaskConfig
246 = TaskConfig
252 = TaskConfig
247 { prebuild :: String,
253 { prebuild :: String,
248− dockerfile :: String
254+ dockerfile :: String,
249− -- build :: T.Text
255+ timeoutMinutes :: Int
251 deriving stock (Show, Eq, Generic)
257 deriving stock (Show, Eq, Generic)
252 deriving anyclass (FromJSON, ToJSON)
258 deriving anyclass (FromJSON, ToJSON)
@@ -255,7 +261,6 @@ class HasTasks m where
255 getTasks :: m TasksConfig
261 getTasks :: m TasksConfig
257instance HasLog (ServerData m) Message m where
263instance HasLog (ServerData m) Message m where
259 getLogAction :: ServerData m -> LogAction m Message
264 getLogAction :: ServerData m -> LogAction m Message
260 getLogAction = logger
265 getLogAction = logger
261 {-# INLINE getLogAction #-}
266 {-# INLINE getLogAction #-}