kowainik / hit-on

:octocat: Kowainik Git Workflow Helper Tool
https://kowainik.github.io/projects/hit-on
Mozilla Public License 2.0
77 stars 13 forks source link

Add --me option for issue command #43

Closed chshersh closed 5 years ago

chshersh commented 5 years ago

This should give the list of the issues where the current user is the assignee

Jigar3 commented 5 years ago

Can I try out this. Also could you give me a way forward on this one

vrom911 commented 5 years ago

@Jigar3 sure! At the moment we are gathering the list of Issue elements. You can se how issue data type looks like:

https://hackage.haskell.org/package/github-0.20/docs/GitHub-Data-Issues.html#t:Issue

As we can see there is the information about assignees to the issue:

issueAssignees :: !(Vector SimpleUser)

So, for --me option in the hit issue command the person should be able to see the issues where the user is the assignee.

You can see that we can have the info about user github name, like here: https://github.com/kowainik/hit-on/blob/7af006e5e4a619b75e9420987eb843b2b47fc376/src/Hit/Git.hs#L42

So you would need to filter the list of received issues and show only the issues for the current user.

Hope this make sense 🙂

vrom911 commented 5 years ago

Hey, @Jigar3! Just wondering if you need any clarifications/help? There is no rush in this issue, I just wonder if we can help you with something 🙂

Jigar3 commented 5 years ago

Actually I thought to wait and study different concepts of Haskell first before solvng this issue. Its taking time to get my head around Typeclasses, Functors & Monads. I am learning them from http://learnyouahaskell.com/chapters here. It would be helpful if you could suggest some other resource. :smiley:

sshine commented 5 years ago

There is https://github.com/bitemyapp/learnhaskell/blob/master/README.md :)

Jigar3 commented 5 years ago

@vrom911

Issue (Bool (Maybe Int))

And I have my IssueP defined as

issueP :: Parser HitCommand
issueP = Issue <$> switch
    ( long "me"
    <> help "Issues Assigned to me"
    )
    <*> optional issueNumP

It gives me error

• Expected kind ‘* -> *’, but ‘Bool’ has kind ‘*’
    • In the type ‘(Bool (Maybe Int))’
      In the definition of data constructor ‘Issue’
      In the data declaration for ‘HitCommand’
   |
53 |     | Issue (Bool (Maybe Int))

Can you help? :sweat_smile:

vrom911 commented 5 years ago

@Jigar3 I see what's happening here. To add another field to the constructor named Issue you can just add it like this:

Issue Bool (Maybe Int)

When you say (Bool (Maybe Int)) it thinks of it as one field that you specifying, but Bool can not have any valiables as its definition is data Bool = True | False, but here you are saying that Maybe Int is the variable of the Bool.

Jigar3 commented 5 years ago

@vrom911 How can I filter from list of issues to get issues where the current user is assignee

I am going forward like this

runIssue :: Bool -> Maybe Int -> IO ()
runIssue isMe = \case
    Just num -> getIssue $ mkIssueId num
    Nothing -> if isMe then getIssueWithAssignee else getAllIssues
getIssueWithAssignee :: IO ()
getIssueWithAssignee = withOwnerRepo (\t o r -> issuesForRepo' t o r [stateOpen]) >>= \case
    Left err -> errorMessage $ show err
    Right is -> for_ isWA (putTextLn . showIssueName blueCode)
        where
            isWA = [issue | issue <- is, issueAssignees ]  -- What should go here?? 

Can you help me out here ?? :sweat_smile:

vrom911 commented 5 years ago

@Jigar3 I guess you should take a look at the Issue type from the github library: https://hackage.haskell.org/package/github-0.20/docs/GitHub-Data-Issues.html#t:Issue

You can see that there is issueAssignees :: !(Vector SimpleUser) field in there. Just check if the username is the member of this Vector and if yes then you should display the issue. Does this make sense?

I understand that this could not be the easiest issue, sorry for that..

Jigar3 commented 5 years ago

@vrom911

import GitHub.Data.Issues (issueAssignees)
import qualified Data.Vector as V

getUser :: IO Text
getUser = "git" $| ["config", "user.login"]
getIssueWithAssignee :: IO ()
getIssueWithAssignee = withOwnerRepo (\t o r -> issuesForRepo' t o r stateOpen) >>= \case
    Left err -> errorMessage $ show err
    Right is -> for_ isWA (putTextLn . showIssueName blueCode)
        where isWA = [issue | issue <- is, getUser >>= (\user -> user `V.elem` issueAssignees)]
/home/jigar/demo/haskell-demo/hit-on/src/Hit/Issue.hs:42:40: error:
    • Couldn't match expected type ‘[a]’
                  with actual type ‘V.Vector Issue’
    • In the expression: is
      In a stmt of a list comprehension: issue <- is
      In the expression:
        [issue |
           issue <- is, getUser >>= (\ user -> user `V.elem` issueAssignees)]
    • Relevant bindings include
        isWA :: [a] (bound at src/Hit/Issue.hs:42:15)
   |
42 |         where isWA = [issue | issue <- is, getUser >>= (\user -> user `V.elem` issueAssignees)]
   |                                        ^^

/home/jigar/demo/haskell-demo/hit-on/src/Hit/Issue.hs:42:44: error:
    • Couldn't match expected type ‘Bool’ with actual type ‘IO b0’
    • In the expression:
        getUser >>= (\ user -> user `V.elem` issueAssignees)
      In a stmt of a list comprehension:
        getUser >>= (\ user -> user `V.elem` issueAssignees)
      In the expression:
        [issue |
           issue <- is, getUser >>= (\ user -> user `V.elem` issueAssignees)]
   |
42 |         where isWA = [issue | issue <- is, getUser >>= (\user -> user `V.elem` issueAssignees)]
   |                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

/home/jigar/demo/haskell-demo/hit-on/src/Hit/Issue.hs:42:66: error:
    • Couldn't match expected type ‘IO b0’ with actual type ‘Bool’
    • In the expression: user `V.elem` issueAssignees
      In the second argument of ‘(>>=)’, namely
        ‘(\ user -> user `V.elem` issueAssignees)’
      In the expression:
        getUser >>= (\ user -> user `V.elem` issueAssignees)
   |
42 |         where isWA = [issue | issue <- is, getUser >>= (\user -> user `V.elem` issueAssignees)]
   |                                                                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

/home/jigar/demo/haskell-demo/hit-on/src/Hit/Issue.hs:42:80: error:
    • Couldn't match expected type ‘V.Vector Text’
                  with actual type ‘Issue
                                    -> V.Vector GitHub.Data.Definitions.SimpleUser’
    • Probable cause: ‘issueAssignees’ is applied to too few arguments
      In the second argument of ‘V.elem’, namely ‘issueAssignees’
      In the expression: user `V.elem` issueAssignees
      In the second argument of ‘(>>=)’, namely
        ‘(\ user -> user `V.elem` issueAssignees)’
   |
42 |         where isWA = [issue | issue <- is, getUser >>= (\user -> user `V.elem` issueAssignees)]
   |                                                                                ^^^^^^^^^^^^^^

These are the erros I am facing. Am I going in right direction ??

vrom911 commented 5 years ago

@Jigar3

getUser >>= (\user -> user `V.elem` issueAssignees)

is not pure so you can not use it in the list comprehensions. You can move out the implementation of the filtering issues list 🙂

Jigar3 commented 5 years ago
-- / Get Issue with current user assignee
getIssueWithAssignee :: IO ()
getIssueWithAssignee = withOwnerRepo (\t o r -> issuesForRepo' t o r stateOpen) >>= \case
    Left err -> errorMessage $ show err
    Right is -> for_ isWA (putTextLn . showIssueName blueCode)
        where isWA = [issue | issue <- toList is, filterIssue issue]

-- / Filter issue with current user as assignee
filterIssue :: Issue -> Bool
filterIssue is = getUser >> \user -> user `V.elem` issueAssignees simpleUserLogin userName
/home/jigar/demo/haskell-demo/hit-on/src/Hit/Issue.hs:47:18: error:
    • Couldn't match expected type ‘Bool’ with actual type ‘IO b0’
    • In the expression:
        getUser
          >> \ user -> user `V.elem` issueAssignees simpleUserLogin userName
      In an equation for ‘filterIssue’:
          filterIssue is
            = getUser
                >> \ user -> user `V.elem` issueAssignees simpleUserLogin userName
   |
47 | filterIssue is = getUser >> \user -> user `V.elem` issueAssignees simpleUserLogin userName
   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

/home/jigar/demo/haskell-demo/hit-on/src/Hit/Issue.hs:47:29: error:
    • Couldn't match expected type ‘IO b0’
                  with actual type ‘a0 -> Bool’
    • The lambda expression ‘\ user
                               -> user `V.elem` issueAssignees simpleUserLogin userName’
      has one argument,
      but its type ‘IO b0’ has none
      In the second argument of ‘(>>)’, namely
        ‘\ user -> user `V.elem` issueAssignees simpleUserLogin userName’
      In the expression:
        getUser
          >> \ user -> user `V.elem` issueAssignees simpleUserLogin userName
   |
47 | filterIssue is = getUser >> \user -> user `V.elem` issueAssignees simpleUserLogin userName
   |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

/home/jigar/demo/haskell-demo/hit-on/src/Hit/Issue.hs:47:52: error:
    • Couldn't match expected type ‘(GitHub.Data.Definitions.User
                                     -> Maybe Text)
                                    -> V.Vector a0’
                  with actual type ‘V.Vector GitHub.Data.Definitions.SimpleUser’
    • The function ‘issueAssignees’ is applied to two arguments,
      but its type ‘Issue -> V.Vector GitHub.Data.Definitions.SimpleUser’
      has only one
      In the second argument of ‘V.elem’, namely
        ‘issueAssignees simpleUserLogin userName’
      In the expression:
        user `V.elem` issueAssignees simpleUserLogin userName
    • Relevant bindings include
        user :: a0 (bound at src/Hit/Issue.hs:47:30)
   |
47 | filterIssue is = getUser >> \user -> user `V.elem` issueAssignees simpleUserLogin userName
   |                                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

/home/jigar/demo/haskell-demo/hit-on/src/Hit/Issue.hs:47:67: error:
    • Couldn't match expected type ‘Issue’
                  with actual type ‘GitHub.Data.Definitions.SimpleUser
                                    -> Name GitHub.Data.Definitions.User’
    • Probable cause: ‘simpleUserLogin’ is applied to too few arguments
      In the first argument of ‘issueAssignees’, namely ‘simpleUserLogin’
      In the second argument of ‘V.elem’, namely
        ‘issueAssignees simpleUserLogin userName’
      In the expression:
        user `V.elem` issueAssignees simpleUserLogin userName
   |
47 | filterIssue is = getUser >> \user -> user `V.elem` issueAssignees simpleUserLogin userName
   |                                                                   ^^^^^^^^^^^^^^^

--  While building package hit-on-0.0.0 using:
      /home/jigar/.stack/setup-exe-cache/x86_64-linux-tinfo6/Cabal-simple_mPHDZzAJ_2.4.0.1_ghc-8.6.3 --builddir=.stack-work/dist/x86_64-linux-tinfo6/Cabal-2.4.0.1 build lib:hit-on exe:hit --ghc-options " -ddump-hi -ddump-to-file -fdiagnostics-color=always"
    Process exited with code: ExitFailure 1

I am trying hard but can't seem to figure out how to solve these errors. :sweat_smile:

vrom911 commented 5 years ago

@Jigar3 , sorry for misunderstanding! I meant that getUsers in your case is not pure so the filtering function you're trying to write should also work in IO monad. Its type should be

filterIssue :: Issue -> IO Bool
Jigar3 commented 5 years ago

carbon

Although the picture shows filterIssue :: Issue -> Bool but the below points are in regards to filterIssue :: Issue -> IO Bool

I tried changing the function signature from Bool to IO Bool but error pops up on line stating filterIssue issue needs to be of type Bool but IO Bool is present.

Also, on Line 10, where it says user `V.elem` issueAssignees simpleUserLogin userName, error pops up saying that expected type is IO Bool but I am returning Bool

And I don't understand how will I match the userName which I get from getUser function which returns a Text with the Vector of SimpleUser as mentioned in the docs.

Like I have getUser >>= \user -> user `V.elem` issueAssignees simpleUserLogin userName V.elem compares the user with the vector on right hand side, I am not getting how will I get the Vector of Text from data Issue.

I hope I am able to make you understand what problems I am facing.

vrom911 commented 5 years ago

@Jigar3 , yes, all errors make sense. You can't make a pure function out of the function that works in IO. So, that means that you can't use list comprehension with the filterIssue function. You have to make this list filtering function not pure, as the filter for each issue is not pure and everything should work in IO monad. While working with not pure functions you can not afford to have any pure functions as once in IO always in IO.