Skip to content

Commit

Permalink
[#43] Add --me option (#61)
Browse files Browse the repository at this point in the history
Resolves #43
  • Loading branch information
vrom911 authored and chshersh committed Mar 16, 2019
1 parent 19744c9 commit 52daa83
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
1 change: 1 addition & 0 deletions hit-on.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ library
, relude ^>= 0.4
, shellmet >= 0.0.0
, text
, vector ^>= 0.12

ghc-options: -Wall
-Wincomplete-uni-patterns
Expand Down
20 changes: 14 additions & 6 deletions src/Hit/Cli.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

----------------------------------------------------------------------------
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/Hit/Git.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ module Hit.Git
, runSync
, runCurrent
, runClone

, getUsername
) where

import Data.Char (isAlphaNum, isDigit, isSpace)
Expand Down
25 changes: 18 additions & 7 deletions src/Hit/Issue.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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 ()
Expand Down

0 comments on commit 52daa83

Please sign in to comment.