module Github.Repos (
userRepos
,userRepos'
,userRepo
,userRepo'
,organizationRepos
,organizationRepos'
,organizationRepo
,organizationRepo'
,contributors
,contributorsWithAnonymous
,languagesFor
,tagsFor
,branchesFor
,module Github.Data
,RepoPublicity(..)
,GithubAuth(..)
,createRepo
,createOrganizationRepo
,newRepo
,NewRepo(..)
,editRepo
,def
,Edit(..)
,deleteRepo
) where
import Data.Default
import Data.Aeson.Types
import Github.Data
import Github.Private
import Network.HTTP.Conduit
import Control.Applicative
import qualified Control.Exception as E
import Network.HTTP.Types
data RepoPublicity =
All
| Owner
| Public
| Private
| Member
deriving (Show, Eq)
userRepos :: String -> RepoPublicity -> IO (Either Error [Repo])
userRepos = userRepos' Nothing
userRepos' :: Maybe GithubAuth -> String -> RepoPublicity -> IO (Either Error [Repo])
userRepos' auth userName All =
githubGetWithQueryString' auth ["users", userName, "repos"] "type=all"
userRepos' auth userName Owner =
githubGetWithQueryString' auth ["users", userName, "repos"] "type=owner"
userRepos' auth userName Member =
githubGetWithQueryString' auth ["users", userName, "repos"] "type=member"
userRepos' auth userName Public =
githubGetWithQueryString' auth ["users", userName, "repos"] "type=public"
userRepos' _auth _userName Private =
return $ Left $ UserError "Cannot access private repos using userRepos"
organizationRepos :: String -> IO (Either Error [Repo])
organizationRepos = organizationRepos' Nothing
organizationRepos' :: Maybe GithubAuth -> String -> IO (Either Error [Repo])
organizationRepos' auth orgName = githubGet' auth ["orgs", orgName, "repos"]
organizationRepo :: String -> String -> IO (Either Error Repo)
organizationRepo = organizationRepo' Nothing
organizationRepo' :: Maybe GithubAuth -> String -> String -> IO (Either Error Repo)
organizationRepo' auth orgName reqRepoName = githubGet' auth ["orgs", orgName, reqRepoName]
userRepo :: String -> String -> IO (Either Error Repo)
userRepo = userRepo' Nothing
userRepo' :: Maybe GithubAuth -> String -> String -> IO (Either Error Repo)
userRepo' auth userName reqRepoName = githubGet' auth ["repos", userName, reqRepoName]
contributors :: String -> String -> IO (Either Error [Contributor])
contributors userName reqRepoName =
githubGet ["repos", userName, reqRepoName, "contributors"]
contributorsWithAnonymous :: String -> String -> IO (Either Error [Contributor])
contributorsWithAnonymous userName reqRepoName =
githubGetWithQueryString
["repos", userName, reqRepoName, "contributors"]
"anon=true"
languagesFor :: String -> String -> IO (Either Error [Language])
languagesFor userName reqRepoName = do
result <- githubGet ["repos", userName, reqRepoName, "languages"]
return $ either Left (Right . getLanguages) result
tagsFor :: String -> String -> IO (Either Error [Tag])
tagsFor userName reqRepoName =
githubGet ["repos", userName, reqRepoName, "tags"]
branchesFor :: String -> String -> IO (Either Error [Branch])
branchesFor userName reqRepoName =
githubGet ["repos", userName, reqRepoName, "branches"]
data NewRepo = NewRepo {
newRepoName :: String
, newRepoDescription :: (Maybe String)
, newRepoHomepage :: (Maybe String)
, newRepoPrivate :: (Maybe Bool)
, newRepoHasIssues :: (Maybe Bool)
, newRepoHasWiki :: (Maybe Bool)
, newRepoAutoInit :: (Maybe Bool)
} deriving Show
instance ToJSON NewRepo where
toJSON (NewRepo { newRepoName = name
, newRepoDescription = description
, newRepoHomepage = homepage
, newRepoPrivate = private
, newRepoHasIssues = hasIssues
, newRepoHasWiki = hasWiki
, newRepoAutoInit = autoInit
}) = object
[ "name" .= name
, "description" .= description
, "homepage" .= homepage
, "private" .= private
, "has_issues" .= hasIssues
, "has_wiki" .= hasWiki
, "auto_init" .= autoInit
]
newRepo :: String -> NewRepo
newRepo name = NewRepo name Nothing Nothing Nothing Nothing Nothing Nothing
createRepo :: GithubAuth -> NewRepo -> IO (Either Error Repo)
createRepo auth = githubPost auth ["user", "repos"]
createOrganizationRepo :: GithubAuth -> String -> NewRepo -> IO (Either Error Repo)
createOrganizationRepo auth org = githubPost auth ["orgs", org, "repos"]
data Edit = Edit {
editName :: Maybe String
, editDescription :: Maybe String
, editHomepage :: Maybe String
, editPublic :: Maybe Bool
, editHasIssues :: Maybe Bool
, editHasWiki :: Maybe Bool
, editHasDownloads :: Maybe Bool
} deriving Show
instance Default Edit where
def = Edit def def def def def def def
instance ToJSON Edit where
toJSON (Edit { editName = name
, editDescription = description
, editHomepage = homepage
, editPublic = public
, editHasIssues = hasIssues
, editHasWiki = hasWiki
, editHasDownloads = hasDownloads
}) = object
[ "name" .= name
, "description" .= description
, "homepage" .= homepage
, "public" .= public
, "has_issues" .= hasIssues
, "has_wiki" .= hasWiki
, "has_downloads" .= hasDownloads
]
editRepo :: GithubAuth
-> String
-> String
-> Edit
-> IO (Either Error Repo)
editRepo auth user repo body = githubPatch auth ["repos", user, repo] b
where
b = body {editName = editName body <|> Just repo}
deleteRepo :: GithubAuth
-> String
-> String
-> IO (Either Error ())
deleteRepo auth owner repo = do
result <- doHttps "DELETE" url (Just auth) Nothing
case result of
Left e -> return (Left (HTTPConnectionError e))
Right resp ->
let status = responseStatus resp
headers = responseHeaders resp
in if status == notFound404
then return (Left (HTTPConnectionError
(E.toException
(StatusCodeException status headers
#if MIN_VERSION_http_conduit(1, 9, 0)
(responseCookieJar resp)
#endif
))))
else return (Right ())
where
url = "https://api.github.com/repos/" ++ owner ++ "/" ++ repo