Browse Source

Support a YAML config file

master
Wesley Kerfoot 5 years ago
parent
commit
1d1bdb82dc
  1. 1
      .gitignore
  2. 3
      app/Main.hs
  3. 12
      src/PullWatch/Environment.hs
  4. 11
      src/PullWatch/PullWatch.hs
  5. 34
      src/PullWatch/Types.hs

1
.gitignore

@ -2,3 +2,4 @@
pullwatch.cabal
*~
.envrc
dist

3
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 ()

12
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

11
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

34
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

Loading…
Cancel
Save