darcsden :: chris -> darcsden -> blob

how meta. (fork of alex's darcsden) http://darcsden.com/

root / src / DarcsDen / Handler / Repository.hs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
{-# LANGUAGE OverloadedStrings #-}
module DarcsDen.Handler.Repository where

import Control.Monad (when)
import Control.Monad.Trans
import Data.Char (toLower)
import Data.List (groupBy, inits, isPrefixOf, nub, partition, sortBy)
import Data.List.Split (wordsBy)
import Data.Map ((!))
import Data.Maybe (fromJust, isJust, isNothing)
import Data.Ord (comparing)
import Data.Time (getCurrentTime)
import Database.CouchDB (doc)
import Snap.Types

import DarcsDen.Handler.Repository.Util
import DarcsDen.Handler.Repository.Browse
import DarcsDen.Handler.Repository.Changes
import DarcsDen.Handler.Repository.Forks
import DarcsDen.State.Comment
import DarcsDen.State.Issue
import DarcsDen.State.Repository
import DarcsDen.State.Session
import DarcsDen.State.User
import DarcsDen.State.Util
import DarcsDen.Util
import DarcsDen.Validate
import DarcsDen.WebUtils
import qualified DarcsDen.Pages.Repository as Page


initialize :: Page
initialize s@(Session { sUser = Nothing }) = do
    warn "You must be logged in to create a repository." s
    redirectTo "/login"
initialize s = doPage (Page.init []) s

doInitialize :: Page
doInitialize s@(Session { sUser = Nothing }) = do
    warn "You must be logged in to create a repository." s
    redirectTo "/login"
doInitialize s@(Session { sUser = Just n }) = validate
    [ iff
        (And (nonEmpty "name") $
            predicate "name" isSane
                "contain only alphanumeric characters, -, or _")
        (\(OK i) ->
            io "destination repository already exists" $
                fmap isNothing (getRepository (n, i ! "name")))

    , Or (isEmpty "bootstrap")
        (Predicate "bootstrap"
                   ((`elem` ["http", "https"]) . takeWhile (/= ':'))
                   "start with http:// or https://")

    , io "user is not valid" (fmap (/= Nothing) (getUser n))
    ]
    (\(OK r) -> do
        now <- liftIO getCurrentTime
        desc <- input "description" ""
        site <- input "website" ""
        private <- getParam "private"
        new <- newRepository Repository
            { rID = Nothing
            , rRev = Nothing
            , rName = r ! "name"
            , rOwner = n
            , rDescription = desc
            , rWebsite = site
            , rCreated = now
            , rForkOf = Nothing
            , rMembers = []
            , rIsPrivate = isJust private
            , rIssueCount = 0
            }

        url <- input "bootstrap" ""
        when (length url > 0) (bootstrapRepository new url)

        success "Repository created." s
        redirectTo ("/" ++ n ++ "/" ++ (r ! "name")))
    (\(Invalid f) -> do
        is <- getInputs
        notify Warning s f
            >>= doPage (Page.init is))

explore :: Page
explore s = do
    page <- input "page" "1"

    let p = read page :: Int
        paginated rs =
            paginate 50 p $
                sortBy (comparing (map toLower . rName . fst)) rs

    rs <- fmap groupForks getRepositories

    let totalPages = ceiling ((fromIntegral (length rs) :: Double) / 50)

    doPage (Page.explore (paginated rs) p totalPages) s
  where
    groupForks rs = foldr addFork (map (flip (,) []) roots) forks
      where
        (forks, roots) = partition (isJust . rForkOf) rs

    addFork f [] = [(f, [])]
    addFork f ((r@(Repository { rID = y }), fs):rs)
        | rForkOf f == y = ((r, (f:fs)) : rs)
        | otherwise = (r, fs) : addFork f rs

browseRepo :: User -> Repository -> Page
browseRepo u r s = do
    Right dr <- liftIO $ getRepo (repoDir (rOwner r) (rName r))

    member <-
        case sUser s of
            Just un ->
                return (un `elem` rMembers r)
            Nothing ->
                return False

    f <- filePath
    fs <- liftIO $ getFiles dr f
    bl <- liftIO $ getBlob dr f

    case (fs, bl) of
        (Nothing, Nothing) -> notFound
        (Just fs', _) -> do
            readme <- liftIO $ getReadme dr f

            let files =
                    map (\i -> i
                        { iPath = pathToFile (f ++ [iName i])
                        }) fs'

            doPage (Page.repo u r files (crumb f) readme member) s
        (_, Just big) | isTooLarge big ->
            doPage (Page.blob u r (crumb f) Nothing) s
        (_, Just source) ->
            doPage
                (Page.blob u r
                    (crumb f)
                    (Just (highlightBlob (last f) (strictLBS source))))
                s
  where
    filePath = do
        rq <- getRequest
        return (wordsBy (== '/') (fromBS $ rqPathInfo rq))

    crumb = map pathToRepoItem . tail . inits

    pathToRepoItem p =
        RepoItem
            { iName = last p
            , iPath = pathToFile p
            , iIsDirectory = True
            }


repoChanges :: User -> Repository -> Page
repoChanges u r s = do
    p <- input "page" "1"
    let page = read p
    (patches, totalPages) <- liftIO $ getChanges (repoDir (rOwner r) (rName r)) page

    doPage (Page.changes u r patches page totalPages) s

repoChangesAtom :: User -> Repository -> Page
repoChangesAtom u r s = do
    (patches, _) <- liftIO $ getChanges (repoDir (rOwner r) (rName r)) 1
    doAtomPage (Page.changesAtom u r patches) s

repoPatch :: User -> Repository -> Page
repoPatch u r s = do
    p <- input "id" ""
    when (null p) (errorPage "No patch ID specified.")

    patch <- liftIO $ getPatch (repoDir (rOwner r) (rName r)) p

    doPage
        (Page.patch u r
            (pPatch patch)
            (summarize (pChanges patch))
            (filter isModification (pChanges patch)))
        s

editRepo :: User -> Repository -> Page
editRepo u r s = validate
    [ io "you do not own this repository" $
        return $ Just (rOwner r) == sUser s
    ]
    (\(OK _) -> do
        ms <- mapM getUser (rMembers r)

        let members = map fromJust . filter (/= Nothing) $ ms

        doPage (Page.edit u r members []) s)
    (\(Invalid f) -> notify Warning s f >> redirectTo "/")

doEditRepo :: User -> Repository -> Page
doEditRepo _ r s = validate
    [ io "you do not own this repository" $
        return $ Just (rOwner r) == sUser s

    , nonEmpty "name"

    , predicate "name" isSane
        "contain only alphanumeric characters, -, or _"
    ]
    (\(OK i) -> do
        removed <- removeMembers r (rMembers r)

        toAdd <- input "add-members" ""
        added <- addMembers removed (map strip . wordsBy (== ',') $ toAdd)
        new <- rename added (i ! "name")

        desc <- input "description" (rDescription r)
        site <- input "website" (rWebsite r)
        private <- getParam "private"
        updateRepository
            new { rDescription = desc
                , rWebsite = site
                , rIsPrivate = isJust private
                }

        success "Repository updated." s

        redirectTo ("/" ++ (rOwner r) ++ "/" ++ (i ! "name") ++ "/edit"))
    (\(Invalid f) -> do
        notify Warning s f
        redirectTo ("/" ++ (rOwner r) ++ "/" ++ (rName r) ++ "/edit"))
  where
    rename r' n
        = if rName r' /= n
             then do
                 res <- renameRepository n r'
                 case res of
                     Nothing -> do
                         warn "There was an error renaming the repository." s
                         return r'
                     Just r'' -> return r''
             else return r'

    addMembers r' [] = return r'
    addMembers r' (m:ms) = do
        user <- getUser m
        case user of
             Just (User { uName = un }) -> do
                 done <- addMember r' un
                 case done of
                     Just r'' ->
                         addMembers r'' ms
                     Nothing -> do
                         warn ("There was an error adding member " ++ un ++ ".") s
                         return r'
             _ -> do
                 warn ("Could not add member " ++ m ++ "; user does not exist.") s
                 addMembers r' ms

    removeMembers r' [] = return r'
    removeMembers r' (m:ms) = do
        remove <- getParam (toBS $ "remove-" ++ show m)
        maybe (removeMembers r' ms) (\_ -> do
            removed <- removeMember r m
            flip maybe (flip removeMembers ms) (do
                user <- getUser m
                case user of
                     Just u' -> do
                         warn ("There was an error removing member " ++ uName u' ++ ".") s
                         removeMembers r' ms
                     Nothing -> do
                         warn failDunno s
                         removeMembers r' ms) removed) remove

    failDunno = unwords
        [ "There was an error removing a member that doesn't exist."
        , "So I can't tell you who it was. Way to go."
        ]


deleteRepo :: User -> Repository -> Page
deleteRepo u r s = validate
    [ io "you do not own this repository" $
        return $ Just (rOwner r) == sUser s
    ]
    (\(OK _) -> doPage (Page.delete u r) s)
    (\(Invalid f) -> notify Warning s f >> redirectTo "/")

doDeleteRepo :: User -> Repository -> Page
doDeleteRepo _ r s = validate
    [ io "you do not own this repository" $
        return $ Just (rOwner r) == sUser s
    ]
    (\(OK _) -> do
        destroyed <- destroyRepository r

        if destroyed
           then do
               success "Repository deleted." s
               redirectTo "/"
           else do
               warn "Repository deletion failed." s
               redirectTo ('/' : rOwner r ++ "/" ++ rName r))
    (\(Invalid f) -> do
        notify Warning s f
        redirectTo ('/' : rOwner r ++ "/" ++ rName r))

forkRepo :: User -> Repository -> Page
forkRepo _ _ s@(Session { sUser = Nothing }) = do
    warn "You must be logged in to fork a repository." s
    redirectTo "/"
forkRepo u r s@(Session { sUser = Just n }) = validate
    [ io "destination repository already exists" $
        fmap isNothing (getRepository (n, rName r))
    ]
    (\(OK _) -> do
        forked <- forkRepository n (rName r) r
        success "Repository forked." s
        redirectTo ("/" ++ n ++ "/" ++ rName forked))
    (\(Invalid _) -> doPage (Page.fork u r (rName r)) s)

forkRepoAs :: User -> Repository -> Page
forkRepoAs _ _ s@(Session { sUser = Nothing }) = do
    warn "You must be logged in to fork a repository." s
    redirectTo "/"
forkRepoAs u r s@(Session { sUser = Just n }) = validate
    [ iff (nonEmpty "name") $ \(OK i) ->
        io "destination repository already exists" $
            fmap isNothing (getRepository (n, i ! "name"))
    ]
    (\(OK i) -> do
        forked <- forkRepository n (i ! "name") r
        success "Repository forked." s
        redirectTo ("/" ++ n ++ "/" ++ rName forked))
    (\(Invalid _) -> do
        name <- input "name" (rName r)
        doPage (Page.fork u r name) s)

repoPatches :: User -> Repository -> Page
repoPatches u r s = do
    fs <- getRepositoryForks (fromJust $ rID r)
    forks <- liftIO $ mapM getForkChanges fs

    ownPrivFs <- fmap (filter privateFork) (getOwnerRepositories (uName u))
    ownPrivForks <- liftIO $ mapM getForkChanges ownPrivFs

    doPage (Page.patches u r forks ownPrivForks) s
  where
    privateFork (Repository { rIsPrivate = True, rForkOf = f }) =
        f == rID r
    privateFork _ = False

repoMerge :: User -> Repository -> Page
repoMerge _ r s = validate
    [ io "you do not own this repository" $
        return $ Just (rOwner r) == sUser s
    ]
    (\(OK _) -> do
        is <- getInputs
        let ps = map (\(n, _) ->
                       let split = wordsBy (== ':') n
                       in (doc (split !! 1), split !! 2))
                     (filter (\(n, _) -> "merge:" `isPrefixOf` n) is)
            gps = groupBy (\a b -> fst a == fst b) ps
            groupedPatches = map (\r'@((k, _):_) -> (k, map snd r')) gps

        merge <- mapM (\(r', ps') -> do
                          Just f <- getRepositoryByID r'
                          liftIO $ mergePatches f ps' s) groupedPatches

        when (and merge) (success "Patches merged!" s >> return ())

        redirectTo ('/' : rOwner r ++ "/" ++ rName r ++ "/forks"))
    (\(Invalid f) -> notify Warning s f >> redirectTo "/")

repoIssues :: User -> Repository -> Page
repoIssues u r s = do
    issues <- fmap (sortBy (flip $ comparing iUpdated)) $ getIssues r
    doPage (Page.issues u r issues) s

repoIssuesTag :: User -> Repository -> Page
repoIssuesTag u r s = validate
    [ nonEmpty "tag"
    ]
    (\(OK is) -> do
        let tags = map (wordsBy (== '^')) $ wordsBy (== '~') (is ! "tag")

        issues <- orFind tags

        doPage
            (Page.issuesByTags u r
                (sortBy (flip $ comparing iUpdated) issues)
                tags)
            s)
    (\(Invalid f) -> do
        notify Warning s f
        redirectTo (repoURL r ++ "/issues"))
  where
    andFind :: [String] -> Snap [Issue]
    andFind (ft:ts) = do
        issues <- getIssuesByTag r ft
        return (foldr (\t i -> filter ((t `elem`) . iTags) i) issues ts)
    andFind _ = error "andFind: no tags"

    orFind :: [[String]] -> Snap [Issue]
    orFind as = do
        issues <- mapM andFind as
        return (nub (concat issues))

repoIssue :: User -> Repository -> Page
repoIssue u r s = validate
    [ numeric "number"
    ]
    (\(OK is) -> do
        mi <- getIssue (fromJust (rID r)) (read $ is ! "number")
        case mi of
            Just i -> do
                cs <- fmap (sortBy (comparing cUpdated)) $ getComments i
                doPage (Page.issue u r i cs) s
            Nothing -> notFound)
    (\(Invalid f) -> do
        notify Warning s f
        redirectTo (repoURL r))

newIssue :: User -> Repository -> Page
newIssue _ _ s@(Session { sUser = Nothing }) = do
    warn "You must be logged in to create an issue." s
    redirectTo "/"
newIssue u r s =
    doPage (Page.newIssue u r) s

doNewIssue :: User -> Repository -> Page
doNewIssue _ _ s@(Session { sUser = Nothing }) = do
    warn "You must be logged in to create an issue." s
    redirectTo "/"
doNewIssue _ r s@(Session { sUser = Just un }) = validate
    [ nonEmpty "summary"
    , Predicate "description" (const True) "be provided"
    , Predicate "tags" (const True) "be provided"
    ]
    (\(OK is) -> do
        now <- liftIO getCurrentTime
        i <- addIssue Issue
            { iID = Nothing
            , iRev = Nothing
            , iNumber = rIssueCount r + 1
            , iSummary = is ! "summary"
            , iOwner = un
            , iDescription = is ! "description"
            , iTags = map strip $ wordsBy (== ',') (is ! "tags")
            , iCreated = now
            , iUpdated = now
            , iIsClosed = False
            , iRepository = fromJust (rID r)
            }

        updateRepository r
            { rIssueCount = rIssueCount r + 1
            }

        redirectTo (issueURL r i))
    (\(Invalid f) -> do
        notify Warning s f
        redirectTo (repoURL r ++ "/new-issue"))

repoComment :: User -> Repository -> Page
repoComment _ _ s@(Session { sUser = Nothing }) = do
    warn "You must be logged in to comment on an issue." s
    redirectTo "/"
repoComment _ r s@(Session { sUser = Just un }) = validate
    [ iff (numeric "number") $ \(OK is) -> io "issue does not exist" $ 
        fmap isJust (getIssue (fromJust (rID r)) (read $ is ! "number"))
    ]
    (\(OK is) -> do
        Just i <- getIssue (fromJust (rID r)) (read $ is ! "number")
        submit <- input "submit" ""
        summary <- input "summary" ""
        description <- input "description" ""
        c <- input "comment" ""
        ts <- fmap (map strip . wordsBy (== ',')) $ input "tags" ""

        let closed =
                case submit of
                    "and close" -> True
                    "and reopen" -> False
                    _ -> iIsClosed i

            issueChanged = or
                [ iSummary i /= summary
                , iDescription i /= description
                , not (null (diffTags (iTags i) ts))
                , iIsClosed i /= closed
                ]

            diffTags ots nts = concat
                [ map AddTag (filter (`notElem` ots) nts)
                , map RemoveTag (filter (`notElem` nts) ots)
                ]

            changes = concat
                [ if iSummary i /= summary
                    then [Summary (iSummary i) summary]
                    else []
                , if iDescription i /= description
                    then [Description (iDescription i) description]
                    else []
                , if iIsClosed i /= closed then [Closed closed] else []
                , if iTags i /= ts then diffTags (iTags i) ts else []
                ]

        if not issueChanged && null c
            then do
                warn "no changes and no comment; do something!" s
                redirectTo (issueURL r i)
            else do

        now <- liftIO getCurrentTime
        addComment Comment
            { cID = Nothing
            , cRev = Nothing
            , cBody = c
            , cChanges = changes
            , cAuthor = un
            , cIssue = fromJust (iID i)
            , cCreated = now
            , cUpdated = now
            }

        mni <-
            if issueChanged
                then updateIssue i
                    { iSummary = summary
                    , iDescription = description
                    , iTags = ts
                    , iIsClosed = closed
                    , iUpdated = now
                    }
                else return (Just i)

        case mni of
            Nothing -> do
                warn "issue could not be updated" s
                redirectTo (issueURL r i)
            Just ni -> do
                if not (null c)
                    then success "comment added" s
                    else success "issue updated" s

                redirectTo (issueURL r ni))
    (\(Invalid f) -> notify Warning s f >> redirectTo "/")