Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ba49a48

Browse files
committed
Merge pull request haskell-github#46 from erantapaa/add-search-rebased
Add search rebased
2 parents bfaeab2 + b0595b3 commit ba49a48

File tree

6 files changed

+96
-3
lines changed

6 files changed

+96
-3
lines changed

Github/Data.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,12 @@ instance FromJSON PullRequestCommit where
398398
return PullRequestCommit
399399
parseJSON _ = fail "Could not build a PullRequestCommit"
400400

401+
instance FromJSON SearchReposResult where
402+
parseJSON (Object o) =
403+
SearchReposResult <$> o .: "total_count"
404+
<*> o .:< "items"
405+
parseJSON _ = fail "Could not build a SearchReposResult"
406+
401407
instance FromJSON Repo where
402408
parseJSON (Object o) =
403409
Repo <$> o .: "ssh_url"

Github/Data/Definitions.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,11 @@ data PullRequestLinks = PullRequestLinks {
364364
data PullRequestCommit = PullRequestCommit {
365365
} deriving (Show, Data, Typeable, Eq, Ord)
366366

367+
data SearchReposResult = SearchReposResult {
368+
searchReposTotalCount :: Int
369+
,searchReposRepos :: [ Repo ]
370+
} deriving (Show, Data, Typeable, Eq, Ord)
371+
367372
data Repo = Repo {
368373
repoSshUrl :: String
369374
,repoDescription :: Maybe String

Github/Private.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ doHttps reqMethod url auth body = do
117117
, requestBody = reqBody
118118
, requestHeaders = reqHeaders <>
119119
[("User-Agent", "github.hs/0.7.0")]
120+
<> [("Accept", "application/vnd.github.preview")]
120121
, checkStatus = successOrMissing
121122
}
122123
authRequest = getAuthRequest auth request

Github/Search.hs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
-- | The Github Search API, as described at
2+
-- <http://developer.github.com/v3/search/>.
3+
module Github.Search(
4+
searchRepos'
5+
,searchRepos
6+
,module Github.Data
7+
,GithubAuth(..)
8+
) where
9+
10+
import Github.Data
11+
import Github.Private
12+
13+
-- | Perform a repository search.
14+
-- | With authentication.
15+
--
16+
-- > searchRepos' (Just $ GithubBasicAuth "github-username" "github-password') "q=a in%3Aname language%3Ahaskell created%3A>2013-10-01&per_page=100"
17+
searchRepos' :: Maybe GithubAuth -> String -> IO (Either Error SearchReposResult)
18+
searchRepos' auth queryString = githubGetWithQueryString' auth ["search/repositories"] queryString
19+
20+
-- | Perform a repository search.
21+
-- | Without authentication.
22+
--
23+
-- > searchRepos "q=a in%3Aname language%3Ahaskell created%3A>2013-10-01&per_page=100"
24+
searchRepos :: String -> IO (Either Error SearchReposResult)
25+
searchRepos = searchRepos' Nothing
26+

github.cabal

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ Extra-source-files: README.md
7777
,samples/Pulls/ReviewComments/ShowComment.hs
7878
,samples/Pulls/ShowCommits.hs
7979
,samples/Pulls/ShowPull.hs
80+
,samples/Search/SearchRepos.hs
8081
,samples/Repos/Collaborators/IsCollaborator.hs
8182
,samples/Repos/Collaborators/ListCollaborators.hs
8283
,samples/Repos/Commits/CommitComment.hs
@@ -110,12 +111,10 @@ source-repository head
110111
type: git
111112
location: git://github.com/fpco/github.git
112113

113-
114114
Library
115115
-- Modules exported by the library.
116116
Exposed-modules: Github.Data,
117117
Github.Data.Definitions,
118-
119118
Github.Gists,
120119
Github.Gists.Comments,
121120
Github.GitData.Commits,
@@ -138,7 +137,7 @@ Library
138137
Github.Repos.Starring,
139138
Github.Users,
140139
Github.Users.Followers
141-
140+
Github.Search
142141

143142
-- Packages needed in order to build this package.
144143
Build-depends: base >= 4.0 && < 5.0,

samples/Search/SearchRepos.hs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
module SearchRepos where
3+
4+
import qualified Github.Search as Github
5+
import qualified Github.Data as Github
6+
import Control.Monad (forM,forM_)
7+
import Data.Maybe (fromMaybe)
8+
import Data.List (intercalate)
9+
import System.Environment (getArgs)
10+
import Text.Printf (printf)
11+
import Data.Time.Clock (getCurrentTime, UTCTime(..))
12+
import Data.Time.LocalTime (utc,utcToLocalTime,localDay,localTimeOfDay,TimeOfDay(..))
13+
import Data.Time.Calendar (toGregorian)
14+
15+
main = do
16+
args <- getArgs
17+
date <- case args of
18+
(x:_) -> return x
19+
otherwise -> today
20+
let query = "q=language%3Ahaskell created%3A>" ++ date ++ "&per_page=100"
21+
let auth = Nothing
22+
result <- Github.searchRepos' auth query
23+
case result of
24+
Left e -> putStrLn $ "Error: " ++ show e
25+
Right r -> do forM_ (Github.searchReposRepos r) (\r -> do
26+
putStrLn $ formatRepo r
27+
putStrLn ""
28+
)
29+
putStrLn $ "Count: " ++ show n ++ " Haskell repos created since " ++ date
30+
where n = Github.searchReposTotalCount r
31+
32+
-- | return today (in UTC) formatted as YYYY-MM-DD
33+
today :: IO String
34+
today = do
35+
now <- getCurrentTime
36+
let day = localDay $ utcToLocalTime utc now
37+
(y,m,d) = toGregorian day
38+
in return $ printf "%d-%02d-%02d" y m d
39+
40+
formatRepo :: Github.Repo -> String
41+
formatRepo r =
42+
let fields = [ ("Name", Github.repoName)
43+
,("URL", Github.repoHtmlUrl)
44+
,("Description", orEmpty . Github.repoDescription)
45+
,("Created-At", formatDate . Github.repoCreatedAt)
46+
,("Pushed-At", formatMaybeDate . Github.repoPushedAt)
47+
]
48+
in intercalate "\n" $ map fmt fields
49+
where fmt (s,f) = fill 12 (s ++ ":") ++ " " ++ f r
50+
orEmpty = fromMaybe ""
51+
fill n s = s ++ replicate n' ' '
52+
where n' = max 0 (n - length s)
53+
54+
formatMaybeDate = maybe "???" formatDate
55+
56+
formatDate = show . Github.fromGithubDate

0 commit comments

Comments
 (0)