Skip to content

Commit

Permalink
OwnerType: add OwnerBot
Browse files Browse the repository at this point in the history
  • Loading branch information
domenkozar committed Sep 4, 2019
1 parent 6a44ccc commit f97a9b2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
32 changes: 32 additions & 0 deletions fixtures/user-bot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"login": "mike-burns",
"id": 4550,
"avatar_url": "https://avatars.githubusercontent.com/u/4550?v=3",
"gravatar_id": "",
"url": "https://api.github.com/users/mike-burns",
"html_url": "https://github.com/mike-burns",
"followers_url": "https://api.github.com/users/mike-burns/followers",
"following_url": "https://api.github.com/users/mike-burns/following{/other_user}",
"gists_url": "https://api.github.com/users/mike-burns/gists{/gist_id}",
"starred_url": "https://api.github.com/users/mike-burns/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/mike-burns/subscriptions",
"organizations_url": "https://api.github.com/users/mike-burns/orgs",
"repos_url": "https://api.github.com/users/mike-burns/repos",
"events_url": "https://api.github.com/users/mike-burns/events{/privacy}",
"received_events_url": "https://api.github.com/users/mike-burns/received_events",
"type": "Bot",
"site_admin": false,
"name": "Mike Burns",
"company": "thoughtbot",
"blog": "http://mike-burns.com/",
"location": "Stockholm, Sweden",
"email": "mburns@thoughtbot.com",
"hireable": true,
"bio": null,
"public_repos": 35,
"public_gists": 32,
"followers": 171,
"following": 0,
"created_at": "2008-04-03T17:54:24Z",
"updated_at": "2015-10-02T16:53:25Z"
}
4 changes: 4 additions & 0 deletions spec/GitHub/UsersSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ spec = do
let userInfo = eitherDecodeStrict $(embedFile "fixtures/user.json")
userLogin (fromRightS userInfo) `shouldBe` "mike-burns"

it "decodes user-bot json" $ do
let userInfo = eitherDecodeStrict $(embedFile "fixtures/user-bot.json")
userLogin (fromRightS userInfo) `shouldBe` "mike-burns"

it "returns information about the user" $ withAuth $ \auth -> do
userInfo <- userInfoFor' (Just auth) "mike-burns"
userLogin (fromRightS userInfo) `shouldBe` "mike-burns"
Expand Down
9 changes: 5 additions & 4 deletions src/GitHub/Data/Definitions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module GitHub.Data.Definitions where
import GitHub.Internal.Prelude
import Prelude ()

import Control.Monad (mfilter)
import Data.Aeson.Types (Parser)
import Network.HTTP.Client (HttpException)

Expand All @@ -32,7 +31,7 @@ data Error
instance E.Exception Error

-- | Type of the repository owners.
data OwnerType = OwnerUser | OwnerOrganization
data OwnerType = OwnerUser | OwnerOrganization | OwnerBot
deriving (Eq, Ord, Enum, Bounded, Show, Read, Generic, Typeable, Data)

instance NFData OwnerType
Expand Down Expand Up @@ -77,7 +76,7 @@ data User = User
{ userId :: !(Id User)
, userLogin :: !(Name User)
, userName :: !(Maybe Text)
, userType :: !OwnerType -- ^ Should always be 'OwnerUser'
, userType :: !OwnerType -- ^ Should always be 'OwnerUser' or 'OwnerBot'
, userCreatedAt :: !UTCTime
, userPublicGists :: !Int
, userAvatarUrl :: !URL
Expand Down Expand Up @@ -137,6 +136,7 @@ instance FromJSON OwnerType where
parseJSON = withText "OwnerType" $ \t -> case T.toLower t of
"user" -> pure $ OwnerUser
"organization" -> pure $ OwnerOrganization
"bot" -> pure $ OwnerBot
_ -> fail $ "Unknown OwnerType: " <> T.unpack t

instance FromJSON SimpleUser where
Expand Down Expand Up @@ -205,7 +205,7 @@ parseOrganization obj = Organization
<*> obj .: "created_at"

instance FromJSON User where
parseJSON = mfilter ((== OwnerUser) . userType) . withObject "User" parseUser
parseJSON = withObject "User" parseUser

instance FromJSON Organization where
parseJSON = withObject "Organization" parseOrganization
Expand All @@ -215,6 +215,7 @@ instance FromJSON Owner where
t <- obj .: "type"
case t of
OwnerUser -> Owner . Left <$> parseUser obj
OwnerBot -> Owner . Left <$> parseUser obj
OwnerOrganization -> Owner . Right <$> parseOrganization obj

-- | Filter members returned in the list.
Expand Down

0 comments on commit f97a9b2

Please sign in to comment.