Skip to content

Commit b06bc40

Browse files
committed
Github.Repos.contributorsWithAnonymous - anonymous and public contributors to a repo. Change Contributor record to KnownContributor and AnonymousContributor sum types. Introduce obj key for producing the value of an object in a FromJSON.
1 parent 3a5651b commit b06bc40

5 files changed

Lines changed: 56 additions & 19 deletions

File tree

‎Github/Data.hs‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -392,16 +392,21 @@ instance FromJSON Repo where
392392
parseJSON _ = fail "Could not build a Repo"
393393

394394
instance FromJSON Contributor where
395-
parseJSON (Object o) =
396-
Contributor <$> o .: "contributions"
397-
<*> o .: "avatar_url"
398-
<*> o .: "login"
399-
<*> o .: "url"
400-
<*> o .: "id"
401-
<*> o .: "gravatar_id"
395+
parseJSON (Object o)
396+
| o `at` "type" == (Just "Anonymous") =
397+
AnonymousContributor <$> o .: "contributions"
398+
<*> o .: "name"
399+
| otherwise =
400+
KnownContributor <$> o .: "contributions"
401+
<*> o .: "avatar_url"
402+
<*> o .: "login"
403+
<*> o .: "url"
404+
<*> o .: "id"
405+
<*> o .: "gravatar_id"
402406
parseJSON _ = fail "Could not build a Contributor"
403407

404-
-- | A better version of Aeson's .:?, using `mzero' instead of `Nothing'.
408+
-- | A slightly more generic version of Aeson's .:?, using `mzero' instead of
409+
-- `Nothing'.
405410
(.:<) :: (FromJSON a) => Object -> T.Text -> Parser [a]
406411
obj .:< key = case Map.lookup key obj of
407412
Nothing -> pure mzero
@@ -417,3 +422,7 @@ obj <.:> [key] = obj .: key
417422
obj <.:> (key:keys) =
418423
let (Object nextObj) = Map.findWithDefault (Object Map.empty) key obj in
419424
nextObj <.:> keys
425+
426+
-- | Produce the value for the given key, maybe.
427+
at :: Object -> T.Text -> Maybe Value
428+
obj `at` key = Map.lookup key obj

‎Github/Data/Definitions.hs‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -367,11 +367,15 @@ data RepoPublicity =
367367
| Member -- | Only repos to which the user is a member but not an owner.
368368
deriving (Show, Eq)
369369

370-
data Contributor = Contributor {
371-
contributorContributions :: Int
372-
,contributorAvatarUrl :: String
373-
,contributorLogin :: String
374-
,contributorUrl :: String
375-
,contributorId :: Int
376-
,contributorGravatarId :: String
377-
} deriving (Show, Data, Typeable, Eq, Ord)
370+
data Contributor =
371+
KnownContributor
372+
Int -- | Number of contributions.
373+
String -- | Avatar URL.
374+
String -- | Login.
375+
String -- | URL.
376+
Int -- | ID.
377+
String -- | Gravatar ID.
378+
| AnonymousContributor
379+
Int -- | Number of contributions.
380+
String -- | Name.
381+
deriving (Show, Data, Typeable, Eq, Ord)

‎Github/Repos.hs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Github.Repos (
33
,organizationRepos
44
,userRepo
55
,contributors
6+
,contributorsWithAnonymous
67
,module Github.Data
78
) where
89

@@ -30,3 +31,9 @@ userRepo userName repoName = githubGet ["repos", userName, repoName]
3031
contributors :: String -> String -> IO (Either Error [Contributor])
3132
contributors userName repoName =
3233
githubGet ["repos", userName, repoName, "contributors"]
34+
35+
contributorsWithAnonymous :: String -> String -> IO (Either Error [Contributor])
36+
contributorsWithAnonymous userName repoName =
37+
githubGetWithQueryString
38+
["repos", userName, repoName, "contributors"]
39+
"anon=true"

‎samples/Repos/ListContributors.hs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ main = do
1010
(Right contributors) ->
1111
putStrLn $ intercalate "\n" $ map formatContributor contributors
1212

13-
formatContributor contributor =
14-
(show $ Github.contributorContributions contributor) ++ "\t" ++
15-
(Github.contributorLogin contributor)
13+
formatContributor (Github.KnownContributor contributions _ login _ _ _) =
14+
(show $ contributions) ++ "\t" ++ login
15+
formatContributor (Github.AnonymousContributor contributions name) =
16+
(show $ contributions) ++ "\t" ++ name
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module ListContributorsWithAnonymous where
2+
3+
import qualified Github.Repos as Github
4+
import Data.List
5+
6+
main = do
7+
possibleContributors <- Github.contributorsWithAnonymous "thoughtbot" "paperclip"
8+
case possibleContributors of
9+
(Left error) -> putStrLn $ "Error: " ++ (show error)
10+
(Right contributors) ->
11+
putStrLn $ intercalate "\n" $ map formatContributor contributors
12+
13+
formatContributor (Github.KnownContributor contributions _ login _ _ _) =
14+
(show $ contributions) ++ "\t" ++ login
15+
formatContributor (Github.AnonymousContributor contributions name) =
16+
(show $ contributions) ++ "\t" ++ name

0 commit comments

Comments
 (0)