aboutsummaryrefslogtreecommitdiffstats
path: root/src/Plugin/GitRemoteSetOrigin.hs
blob: 8cf2e51bb579e45d1028e263a7602aa160cef083 (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
{-# 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 :: [[String]] of
        []    -> return $ Left "blast"
        (m:_) -> do
            let url = last m

            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