An IRC bot for learning, fun and collaboration in the Freepost community.
Clone
HTTPS:
git clone https://vervis.peers.community/repos/VvM9v
SSH:
git clone USERNAME@vervis.peers.community:VvM9v
Branches
Tags
Repos.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 | {- This file is part of funbot.
-
- Written in 2015, 2016 by fr33domlover <fr33domlover@riseup.net>.
-
- ♡ Copying is an act of love. Please copy, reuse and share.
-
- The author(s) have dedicated all copyright and related and neighboring
- rights to this software to the public domain worldwide. This software is
- distributed without any warranty.
-
- You should have received a copy of the CC0 Public Domain Dedication along
- with this software. If not, see
- <http://creativecommons.org/publicdomain/zero/1.0/>.
-}
{-# LANGUAGE OverloadedStrings #-}
module FunBot.Settings.Sections.Repos
( hostSection
, addRepoAnnSpec
, deleteRepoAnnSpec
, addRepo
, deleteRepo
)
where
import Data.Maybe (fromMaybe)
import Data.Monoid ((<>))
import Data.Sequence (Seq, (|>), (><), ViewL (..))
import Data.Settings.Section
import Data.Settings.Types
import FunBot.Settings.MkOption
import FunBot.Settings.Persist
import FunBot.Types
import Network.IRC.Fun.Bot.State (modifyState)
import Network.IRC.Fun.Types.Base (Channel (..))
import qualified Data.CaseInsensitive as CI
import qualified Data.HashMap.Lazy as M
import qualified Data.Sequence as Q
import qualified Data.Text as T
specSection :: DevHostLabel -> RepoSpace -> RepoName -> Int -> SettingsTree
specSection host space repo pos = Section
{ secOpts = M.fromList
[ ( "channel"
, mkopt
"set-channel-here"
(unChannel . rasChannel)
(\ chan spec -> spec { rasChannel = Channel chan })
)
, ( "branches"
, mkopt
[]
(\ spec ->
case rasBranches spec of
Accept l -> map unBranchName l
Reject l -> map unBranchName l
)
(\ branches spec ->
let bs = case rasBranches spec of
Accept _ -> Accept $ map BranchName branches
Reject _ -> Reject $ map BranchName branches
in spec { rasBranches = bs }
)
)
, ( "accept"
, mkopt
False
(\ spec ->
case rasBranches spec of
Accept _ -> True
Reject _ -> False
)
(\ b spec ->
let ctor = if b then Accept else Reject
bs = case rasBranches spec of
Accept l -> ctor l
Reject l -> ctor l
in spec { rasBranches = bs }
)
)
, ( "all-commits"
, mkopt
False
rasAllCommits
(\ b spec -> spec { rasAllCommits = b })
)
, ( "commits"
, mkopt
True
rasCommits
(\ b spec -> spec { rasCommits = b })
)
, ( "issues"
, mkopt
True
rasIssues
(\ b spec -> spec { rasIssues = b })
)
, ( "merge-requests"
, mkopt
True
rasMergeRequests
(\ b spec -> spec { rasMergeRequests = b })
)
, ( "snippets"
, mkopt
True
rasSnippets
(\ b spec -> spec { rasSnippets = b })
)
, ( "notes"
, mkopt
True
rasNotes
(\ b spec -> spec { rasNotes = b })
)
, ( "new"
, mkopt
True
rasNew
(\ b spec -> spec { rasNew = b })
)
, ( "old"
, mkopt
True
rasOld
(\ b spec -> spec { rasOld = b })
)
, ( "untimed"
, mkopt
True
rasUntimed
(\ b spec -> spec { rasUntimed = b })
)
]
, secSubs = M.empty
}
where
setSpecField f val sets = fromMaybe sets $ do
let hosts = stGitAnnChans sets
repos <- M.lookup host hosts
specs <- M.lookup (space, repo) repos
let specs' = Q.adjust (f val) pos specs
repos' = M.insert (space, repo) specs' repos
hosts' = M.insert host repos' hosts
return sets { stGitAnnChans = hosts' }
getSpecField defval f sets = fromMaybe defval $ do
let hosts = stGitAnnChans sets
repos <- M.lookup host hosts
specs <- M.lookup (space, repo) repos
spec <- if 0 <= pos && pos < Q.length specs
then Just $ Q.index specs pos
else Nothing
return $ f spec
mkopt defval get set =
mkOptionF (getSpecField defval get) (setSpecField set) defval
repoSection
:: DevHostLabel
-> RepoSpace
-> RepoName
-> Seq RepoAnnSpec
-> (T.Text, SettingsTree)
repoSection h s r specs =
( CI.original (unRepoSpace s) <> "/" <> CI.original (unRepoName r)
, Section
{ secOpts = M.empty
, secSubs = M.fromList $ map mksub [1 .. Q.length specs]
}
)
where
mksub i = (T.pack $ show i, specSection h s r (i - 1))
hostSection
:: DevHostLabel
-> M.HashMap (RepoSpace, RepoName) (Seq RepoAnnSpec)
-> SettingsTree
hostSection host repos = Section
{ secOpts = M.empty
, secSubs = M.fromList $ map (uncurry f) $ M.toList repos
}
where
f (space, repo) specs = repoSection host space repo specs
mkDefSpec :: Channel -> RepoAnnSpec
mkDefSpec chan = RepoAnnSpec
{ rasChannel = chan
, rasBranches = Reject []
, rasAllCommits = False
, rasCommits = True
, rasIssues = True
, rasMergeRequests = True
, rasSnippets = True
, rasNotes = True
, rasNew = True
, rasOld = True
, rasUntimed = True
}
-- | Append a new repo ann spec to the settings and a matching tree under the
-- repo section. Return whether succeeded.
addRepoAnnSpec
:: DevHostLabel
-> RepoSpace
-> RepoName
-> Channel
-> BotSession Bool
addRepoAnnSpec host space repo chan = do
hosts <- fmap stGitAnnChans getSettings
case M.lookup host hosts of
Just repos ->
case M.lookup (space, repo) repos of
Just specs -> do
let specs' = specs |> defSpec
repos' = M.insert (space, repo) specs' repos
hosts' = M.insert host repos' hosts
modifySettings $ \ s -> s { stGitAnnChans = hosts' }
saveBotSettings
let (t, sec) = repoSection host space repo specs'
ins = insertSub ["repos", unDevHostLabel host, t] sec
modifyState $ \ s -> s { bsSTree = ins $ bsSTree s }
return True
Nothing -> return False
Nothing -> return False
where
defSpec = mkDefSpec chan
-- | Remove a spec from a repo. Return 'Nothing' on success. Otherwise return
-- whether the error was repo not found ('False') or index too big ('True').
-- The position given is 0-based.
deleteRepoAnnSpec
:: DevHostLabel
-> RepoSpace
-> RepoName
-> Int
-> BotSession (Maybe Bool)
deleteRepoAnnSpec host space repo pos = do
hosts <- fmap stGitAnnChans getSettings
case M.lookup host hosts of
Just repos ->
case M.lookup (space, repo) repos of
Just specs ->
let (u, v) = Q.splitAt pos specs
in case Q.viewl v of
EmptyL -> return $ Just True
s :< r -> do
let specs' = u >< r
repos' =
M.insert (space, repo) specs' repos
hosts' = M.insert host repos' hosts
modifySettings $
\ s -> s { stGitAnnChans = hosts' }
saveBotSettings
let (t, sec) =
repoSection host space repo specs'
ins =
insertSub
["repos", unDevHostLabel host, t]
sec
modifyState $
\ s -> s { bsSTree = ins $ bsSTree s }
return Nothing
Nothing -> return $ Just False
Nothing -> return $ Just False
-- | Add a new repo to settings and tree. Return 'Nothing' on success.
-- Otherwise return whether the host doesn't exist ('False') or the repo
-- already exists ('True').
addRepo
:: DevHostLabel
-> RepoSpace
-> RepoName
-> Channel
-> BotSession (Maybe Bool)
addRepo host space repo chan = do
hosts <- fmap stGitAnnChans getSettings
case M.lookup host hosts of
Nothing -> return $ Just False
Just repos ->
case M.lookup (space, repo) repos of
Just _ -> return $ Just True
Nothing -> do
let specs = Q.singleton defSpec
repos' =
M.insert (space, repo) specs repos
hosts' = M.insert host repos' hosts
modifySettings $ \ s -> s { stGitAnnChans = hosts' }
saveBotSettings
let (t, sec) = repoSection host space repo specs
ins = insertSub ["repos", unDevHostLabel host, t] sec
modifyState $ \ s -> s { bsSTree = ins $ bsSTree s }
return Nothing
where
defSpec = mkDefSpec chan
-- | Remove a repo from settings and tree. Return whether success, i.e. whether
-- the repo did exist and indeed has been deleted.
deleteRepo :: DevHostLabel -> RepoSpace -> RepoName -> BotSession Bool
deleteRepo host space repo = do
hosts <- fmap stGitAnnChans getSettings
case M.lookup host hosts of
Just repos ->
if M.member (space, repo) repos
then do
let repos' = M.delete (space, repo) repos
hosts' = M.insert host repos' hosts
modifySettings $ \ s -> s { stGitAnnChans = hosts' }
saveBotSettings
let name =
CI.original (unRepoSpace space) <>
"/" <>
CI.original (unRepoName repo)
del = deleteSub ["repos", unDevHostLabel host, name]
modifyState $ \ s -> s { bsSTree = del $ bsSTree s }
return True
else return False
Nothing -> return False
|