aboutsummaryrefslogtreecommitdiffstats
path: root/src/Plugin/GitRemoteSetOrigin.hs
blob: 0e8f8177d4d6d81089bc1de264d9115196833587 (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
{-# 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
    }

gitRemoteSetOriginAction :: PluginAction
gitRemoteSetOriginAction message = do
    case M.textStr message =~ matchRegex gitRemoteSetOrigin of
        ""  -> return $ Left "blast"
        url -> do
            dbConn <- liftIO $ open "db/sorbot_development.sqlite3"
            liftIO $ execute dbConn "INSERT INTO \
                \ plugin_github_commit_channel_repo_urls \
                \ (channel, repo_url) \
                \ VALUES \
                \ (?, ?)"
                (M.channel message, url)
            liftIO $ close dbConn

            return $ Right $ T.pack url