Skip to content

Add support for collaborator permission endpoint #425

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/GitHub.hs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ module GitHub (
-- ** Collaborators
-- | See <https://developer.github.com/v3/repos/collaborators/>
collaboratorsOnR,
collaboratorPermissionOnR,
isCollaboratorOnR,
addCollaboratorR,

Expand Down
41 changes: 41 additions & 0 deletions src/GitHub/Data/Repos.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import GitHub.Internal.Prelude
import Prelude ()

import qualified Data.HashMap.Strict as HM
import qualified Data.Text as T
#if MIN_VERSION_aeson(1,0,0)
import Data.Aeson.Types (FromJSONKey (..), fromJSONKeyCoerce)
#else
Expand Down Expand Up @@ -158,6 +159,27 @@ contributorToSimpleUser (AnonymousContributor _ _) = Nothing
contributorToSimpleUser (KnownContributor _contributions avatarUrl name url uid _gravatarid) =
Just $ SimpleUser uid name avatarUrl url

-- | The permission of a collaborator on a repository.
-- See <https://developer.github.com/v3/repos/collaborators/#review-a-users-permission-level>
data CollaboratorPermission
= CollaboratorPermissionAdmin
| CollaboratorPermissionWrite
| CollaboratorPermissionRead
| CollaboratorPermissionNone
deriving (Show, Data, Enum, Bounded, Typeable, Eq, Ord, Generic)

instance NFData CollaboratorPermission where rnf = genericRnf
instance Binary CollaboratorPermission

-- | A collaborator and its permission on a repository.
-- See <https://developer.github.com/v3/repos/collaborators/#review-a-users-permission-level>
data CollaboratorWithPermission
= CollaboratorWithPermission SimpleUser CollaboratorPermission
deriving (Show, Data, Typeable, Eq, Ord, Generic)

instance NFData CollaboratorWithPermission where rnf = genericRnf
instance Binary CollaboratorWithPermission

-- JSON instances

instance FromJSON Repo where
Expand Down Expand Up @@ -303,3 +325,22 @@ instance IsPathPart ArchiveFormat where
toPathPart af = case af of
ArchiveFormatTarball -> "tarball"
ArchiveFormatZipball -> "zipball"

instance FromJSON CollaboratorPermission where
parseJSON = withText "CollaboratorPermission" $ \t -> case T.toLower t of
"admin" -> pure CollaboratorPermissionAdmin
"write" -> pure CollaboratorPermissionWrite
"read" -> pure CollaboratorPermissionRead
"none" -> pure CollaboratorPermissionNone
_ -> fail $ "Unknown CollaboratorPermission: " <> T.unpack t

instance ToJSON CollaboratorPermission where
toJSON CollaboratorPermissionAdmin = "admin"
toJSON CollaboratorPermissionWrite = "write"
toJSON CollaboratorPermissionRead = "read"
toJSON CollaboratorPermissionNone = "none"

instance FromJSON CollaboratorWithPermission where
parseJSON = withObject "CollaboratorWithPermission" $ \o -> CollaboratorWithPermission
<$> o .: "user"
<*> o .: "permission"
11 changes: 11 additions & 0 deletions src/GitHub/Endpoints/Repos/Collaborators.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
-- <http://developer.github.com/v3/repos/collaborators/>.
module GitHub.Endpoints.Repos.Collaborators (
collaboratorsOnR,
collaboratorPermissionOnR,
isCollaboratorOnR,
addCollaboratorR,
module GitHub.Data,
Expand All @@ -22,6 +23,16 @@ collaboratorsOnR :: Name Owner -> Name Repo -> FetchCount -> Request k (Vector S
collaboratorsOnR user repo =
pagedQuery ["repos", toPathPart user, toPathPart repo, "collaborators"] []

-- | Review a user's permission level.
-- <https://developer.github.com/v3/repos/collaborators/#review-a-users-permission-level>
collaboratorPermissionOnR
:: Name Owner -- ^ Repository owner
-> Name Repo -- ^ Repository name
-> Name User -- ^ Collaborator to check permissions of.
-> GenRequest 'MtJSON rw CollaboratorWithPermission
collaboratorPermissionOnR owner repo coll =
query ["repos", toPathPart owner, toPathPart repo, "collaborators", toPathPart coll, "permission"] []

-- | Check if a user is a collaborator.
-- See <https://developer.github.com/v3/repos/collaborators/#check-if-a-user-is-a-collaborator>
isCollaboratorOnR
Expand Down