diff --git a/.gitignore b/.gitignore index 2bf31db..fd9ed59 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ pullwatch.cabal *~ .envrc +dist diff --git a/app/Main.hs b/app/Main.hs index a72e515..c13ba87 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -4,6 +4,7 @@ module Main where import PullWatch.PullWatch +import PullWatch.Types import PullWatch.Environment (getPAT) import qualified Data.Default as Default (def) @@ -13,7 +14,7 @@ doMonitor (RepoArgs owner repo) = do pat <- getPAT let ?pat = pat - monitorPRs Default.def [(owner, repo)] + monitorPRs Default.def [Repo owner repo] return () diff --git a/src/PullWatch/Environment.hs b/src/PullWatch/Environment.hs index 629369d..12e2d36 100644 --- a/src/PullWatch/Environment.hs +++ b/src/PullWatch/Environment.hs @@ -8,6 +8,7 @@ module PullWatch.Environment import System.Environment import Filesystem.Path +import PullWatch.Types import qualified Data.Yaml as Y import qualified GitHub.Auth as Auth @@ -17,8 +18,17 @@ getPAT = do (Just pat) <- lookupEnv "PERSONAL_ACCESS_TOKEN" return $ Just $ Auth.OAuth $ C.pack pat -getRepoConfig = do +getRepoConfigPath = do config <- lookupEnv "PULLWATCH_CONFIG" case config of Nothing -> return "~/.config/pullwatch.yml" Just configPath -> return configPath + +getRepoConfig :: IO (Maybe [Repo]) + +getRepoConfig = do + path <- getRepoConfigPath + config <- Y.decodeFileEither path + case config of + (Left _) -> return Nothing + (Right repos) -> return $ Just repos diff --git a/src/PullWatch/PullWatch.hs b/src/PullWatch/PullWatch.hs index 66bd317..e2c0f41 100644 --- a/src/PullWatch/PullWatch.hs +++ b/src/PullWatch/PullWatch.hs @@ -71,11 +71,10 @@ notifyPR client pr = (print $ (summary pr) ++ " was opened") >> notify client pr getLatest :: (?pat :: (Maybe Auth.Auth)) => - (PR.Name PR.Owner) -> - (PR.Name PR.Repo) -> + Repo -> IO (Maybe PullRequest) -getLatest owner repo = do +getLatest (Repo owner repo) = do prs <- PR.pullRequestsFor' ?pat owner repo let pr = case prs of (Left _) -> Nothing @@ -93,16 +92,16 @@ getLatest owner repo = do return pr getLatestPRs :: (?pat :: (Maybe Auth.Auth)) => - [(PR.Name PR.Owner, PR.Name PR.Repo)] -> + [Repo] -> IO (Maybe PullRequests) getLatestPRs repos = do - prs <- mapConcurrently (uncurry getLatest) repos + prs <- mapConcurrently getLatest repos return $ maybesToMap prs monitorPRs :: (?pat :: (Maybe Auth.Auth)) => Maybe PullRequests -> - [(PR.Name PR.Owner, PR.Name PR.Repo)] -> + [Repo] -> IO () monitorPRs previous repos = do currentPRs <- getLatestPRs repos diff --git a/src/PullWatch/Types.hs b/src/PullWatch/Types.hs index a0386bb..5149852 100644 --- a/src/PullWatch/Types.hs +++ b/src/PullWatch/Types.hs @@ -1,20 +1,32 @@ {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE FlexibleInstances #-} -module PullWatch.Types where +module PullWatch.Types + ( + Repo(..) + , PullRequest(..) + , PullRequests + , RepoArgs(..) + ) where import Data.Default import GitHub.Data.Name +import Data.Yaml (FromJSON, withObject, (.:)) import System.Console.ArgParser.QuickParams (RawRead, rawParse) -import qualified GitHub.Endpoints.PullRequests as PR +import qualified GitHub.Endpoints.PullRequests as G import qualified Data.IntMap as IntMap import qualified Data.Text as T +import qualified Data.Yaml as Y -- Type definitions type PullRequests = IntMap.IntMap PullRequest +data Repo = Repo (G.Name G.Owner) + (G.Name G.Repo) + deriving (Show, Eq) + data PullRequest = PR { prText :: T.Text , prTitle :: T.Text @@ -23,10 +35,10 @@ data PullRequest = PR { , prID :: Integer } - deriving (Show) + deriving (Show, Eq) -data RepoArgs = RepoArgs (PR.Name PR.Owner) - (PR.Name PR.Repo) +data RepoArgs = RepoArgs (G.Name G.Owner) + (G.Name G.Repo) deriving (Show) instance Default PullRequest where @@ -38,5 +50,15 @@ instance Default PullRequest where , prID = 0 } -instance RawRead (PR.Name a) where +instance RawRead (G.Name a) where rawParse x = Just (N $ T.pack x, x) + +instance FromJSON Repo where + parseJSON = withObject "Repo" $ \v -> makeRepo + <$> v .: "owner" + <*> v .: "repo" + +-- Smart constructor for Repo type +makeRepo owner repo = Repo (c owner) + (c repo) where + c = N . T.pack