From 52daa831a116cda031b700be2bd32cbee9c87613 Mon Sep 17 00:00:00 2001 From: Veronika Romashkina Date: Sun, 17 Mar 2019 00:03:21 +0800 Subject: [PATCH] [#43] Add --me option (#61) Resolves #43 --- hit-on.cabal | 1 + src/Hit/Cli.hs | 20 ++++++++++++++------ src/Hit/Git.hs | 2 ++ src/Hit/Issue.hs | 25 ++++++++++++++++++------- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/hit-on.cabal b/hit-on.cabal index 0ea9e0e..c4c250a 100644 --- a/hit-on.cabal +++ b/hit-on.cabal @@ -40,6 +40,7 @@ library , relude ^>= 0.4 , shellmet >= 0.0.0 , text + , vector ^>= 0.12 ghc-options: -Wall -Wincomplete-uni-patterns diff --git a/src/Hit/Cli.hs b/src/Hit/Cli.hs index 760aa95..ea91f43 100644 --- a/src/Hit/Cli.hs +++ b/src/Hit/Cli.hs @@ -14,8 +14,8 @@ import Options.Applicative (Parser, ParserInfo, argument, auto, command, execPar subparser, switch) import Hit.ColorTerminal (arrow, blueCode, boldCode, redCode, resetCode) -import Hit.Git (runAmend, runClone, runCommit, runCurrent, runFix, runFresh, runHop, runNew, - runPush, runResolve, runSync) +import Hit.Git (getUsername, runAmend, runClone, runCommit, runCurrent, runFix, runFresh, runHop, + runNew, runPush, runResolve, runSync) import Hit.Issue (runIssue) import qualified Data.Text as T @@ -27,14 +27,16 @@ hit = execParser cliParser >>= \case Hop branchName -> runHop branchName Fresh branchName -> runFresh branchName New issueNum -> runNew issueNum - Issue issueNum -> runIssue issueNum + Issue issueNum me -> if me + then getUsername >>= runIssue issueNum . Just + else runIssue issueNum Nothing Commit message noIssue -> runCommit message noIssue Fix message -> runFix message Amend -> runAmend Resolve branchName -> runResolve branchName Push isForce -> runPush isForce Sync -> runSync - Current -> runCurrent >>= flip whenJust (runIssue . Just) + Current -> runCurrent >>= flip whenJust (flip runIssue Nothing . Just) Clone name -> runClone name ---------------------------------------------------------------------------- @@ -51,7 +53,7 @@ data HitCommand = Hop (Maybe Text) | Fresh (Maybe Text) | New Int - | Issue (Maybe Int) + | Issue (Maybe Int) Bool | Commit Text Bool | Fix (Maybe Text) | Amend @@ -87,7 +89,13 @@ newP :: Parser HitCommand newP = New <$> issueNumP issueP :: Parser HitCommand -issueP = Issue <$> optional issueNumP +issueP = do + num <- optional issueNumP + me <- switch + $ long "me" + <> short 'm' + <> help "Assigned to me" + pure $ Issue num me commitP :: Parser HitCommand commitP = do diff --git a/src/Hit/Git.hs b/src/Hit/Git.hs index 99c232b..2606bba 100644 --- a/src/Hit/Git.hs +++ b/src/Hit/Git.hs @@ -14,6 +14,8 @@ module Hit.Git , runSync , runCurrent , runClone + + , getUsername ) where import Data.Char (isAlphaNum, isDigit, isSpace) diff --git a/src/Hit/Issue.hs b/src/Hit/Issue.hs index aa2d8dc..4f598e2 100644 --- a/src/Hit/Issue.hs +++ b/src/Hit/Issue.hs @@ -8,8 +8,9 @@ module Hit.Issue , parseOwnerRepo ) where +import Data.Vector (Vector) import GitHub (Error (..), Id, Issue (..), IssueLabel (..), IssueState (..), Name, Owner, Repo, - SimpleUser (..), getUrl, mkId, mkName, untagName) + SimpleUser (..), User, getUrl, mkId, mkName, untagName) import GitHub.Auth (Auth (OAuth)) import GitHub.Data.Options (stateOpen) import GitHub.Endpoints.Issues (issue', issuesForRepo') @@ -20,19 +21,29 @@ import Hit.ColorTerminal (arrow, blueBg, blueCode, boldCode, errorMessage, green resetCode) import qualified Data.Text as T +import qualified Data.Vector as V -- | Run the @issue@ command. -runIssue :: Maybe Int -> IO () -runIssue = \case +runIssue :: Maybe Int -> Maybe Text -> IO () +runIssue issue me = case issue of Just num -> getIssue $ mkIssueId num - Nothing -> getAllIssues + Nothing -> getAllIssues me -- | Get the list of the opened issues for the current project. -getAllIssues :: IO () -getAllIssues = withOwnerRepo (\t o r -> issuesForRepo' t o r stateOpen) >>= \case +getAllIssues :: Maybe Text -> IO () +getAllIssues me = withOwnerRepo (\t o r -> issuesForRepo' t o r stateOpen) >>= \case Left err -> errorMessage $ show err - Right is -> for_ is (putTextLn . showIssueName blueCode) + Right is -> for_ (my is) (putTextLn . showIssueName blueCode) + where + my :: Vector Issue -> Vector Issue + my issues = case me of + Just (makeName -> username) -> V.filter (assignedTo username . issueAssignees) issues + Nothing -> issues + + -- Is the username an element of assignees vector? + assignedTo :: Name User -> Vector SimpleUser -> Bool + assignedTo user = isJust . V.find ((user ==) . simpleUserLogin) -- | Get the 'Issue' by given issue number. getIssue :: Id Issue -> IO ()