aboutsummaryrefslogtreecommitdiffstats
path: root/src/Plugin/GitRemoteSetOrigin.hs
blob: 5b2aad27c5647564902c7dd1c03de778fbe24b68 (plain)
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
{-# LANGUAGE OverloadedStrings #-}

module Plugin.GitRemoteSetOrigin
    ( gitRemoteSetOrigin
    ) where

import Control.Monad.IO.Class (liftIO)
import qualified Data.Text as T

import Database.SQLite.Simple
import Text.Regex.TDFA ((=~))

import qualified Message as M
import Plugin.Base

gitRemoteSetOrigin = Plugin
    { matchRegex = "^git remote set origin ([^ ]+)$"
    , perform = gitRemoteSetOriginAction
    , command = "git remote set origin URL"
    , description = "Set the git remote URL for this channel."
    }

gitRemoteSetOriginAction :: PluginAction
gitRemoteSetOriginAction message = do
    case M.textStr message =~ matchRegex gitRemoteSetOrigin :: [[String]] of
        []    -> return $ Left "blast"
        (m:_) -> do
            let url = last m

            dbConn <- liftIO $ open "db/sorbot_development.sqlite3"
            liftIO $ withTransaction dbConn $ do
                let params =
                        [":channel"   := M.channel message
                        , ":repo_url" := url
                        ]

                -- Upsert repo URL for channel
                executeNamed dbConn "UPDATE \
                    \ plugin_github_commit_channel_repo_urls \
                    \ SET channel = :channel, \
                    \     repo_url = :repo_url \
                    \ WHERE channel = :channel"
                    params
                executeNamed dbConn "INSERT INTO \
                    \ plugin_github_commit_channel_repo_urls \
                    \ (channel, repo_url) \
                    \ SELECT \
                    \     :channel, \
                    \     :repo_url \
                    \ WHERE changes() = 0"
                    params
            liftIO $ close dbConn

            return $ Right $ "I updated the channel's repo URL to '"
                `T.append` T.pack url
                `T.append` "'"