Skip to content

Commit 2d1652e

Browse files
committed
I have no Internet but I think this will POST a new Comment. Github.Repos.Commits.postCommentOn .
1 parent 746779d commit 2d1652e

6 files changed

Lines changed: 54 additions & 3 deletions

File tree

Github/Data.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import qualified Data.Map as Map
99
import qualified Data.Text as T
1010
import Data.Aeson.Types
1111
import System.Locale (defaultTimeLocale)
12+
import Data.Attoparsec.Number (Number(..))
1213

1314
import Github.Data.Definitions
1415

@@ -96,6 +97,13 @@ instance FromJSON Comment where
9697
<*> o .: "id"
9798
parseJSON _ = fail "Could not build a Comment"
9899

100+
instance ToJSON NewComment where
101+
toJSON newComment =
102+
Object $ Map.fromList
103+
[("body", String $ T.pack $ newCommentBody newComment)
104+
,("line_number", Number $ Data.Attoparsec.Number.I $ fromIntegral $ newCommentLineNumber newComment)
105+
,("path", String $ T.pack $ newCommentPath newComment)]
106+
99107
(.:<) :: (FromJSON a) => Object -> T.Text -> Parser [a]
100108
obj .:< key = case Map.lookup key obj of
101109
Nothing -> pure []

Github/Data/Definitions.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,11 @@ data Comment = Comment {
7777
,commentUser :: GithubUser
7878
,commentId :: Int
7979
} deriving (Show, Data, Typeable, Eq, Ord)
80+
81+
-- A comment that has not yet been sent to the API.
82+
data NewComment = NewComment {
83+
newCommentBody :: String
84+
,newCommentLineNumber :: Int
85+
,newCommentPath :: String
86+
,newCommentPosition :: Int
87+
} deriving (Show, Eq, Ord)

Github/Repos/Commits.hs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Github.Repos.Commits (
33
,commit
44
,commentsFor
55
,commitCommentsFor
6+
,postCommentOn
67
,module Github.Data
78
) where
89

@@ -21,3 +22,8 @@ commentsFor user repo = fullGithubGet ["repos", user, repo, "comments"]
2122
commitCommentsFor :: String -> String -> String -> IO (Either String [Comment])
2223
commitCommentsFor user repo sha1 =
2324
fullGithubGet ["repos", user, repo, "commits", sha1, "comments"]
25+
26+
postCommentOn :: String -> String -> String -> NewComment -> IO (Either String Comment)
27+
postCommentOn user repo sha1 newComment =
28+
fullGithubPost ["repos", user, repo, "commits", sha1, "comments"]
29+
newComment

Github/Repos/Commits/Private.hs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import Control.Applicative
88
import Data.List
99
import qualified Data.ByteString.Char8 as BS
1010
import Network.Curl.Download
11+
import Network.HTTP
12+
import Network.URI
1113

1214
buildUrl :: [String] -> String
1315
buildUrl paths = "https://api.github.com/" ++ intercalate "/" paths
@@ -16,8 +18,17 @@ fullGithubGet paths = do
1618
commitsJsonString <- openURI $ buildUrl paths
1719
return $ either Left parseJson commitsJsonString
1820

19-
githubApiGet :: String -> IO (Either String BS.ByteString)
20-
githubApiGet = openURI
21+
-- fullGithubPost :: (ToJSON a, Show a, FromJSON b, Show b) => [String] -> a -> IO (Either String b)
22+
fullGithubPost paths body = do
23+
let (Just uri) = parseURI $ buildUrl paths
24+
request = Request {
25+
rqURI = uri
26+
,rqMethod = POST
27+
,rqHeaders = []
28+
,rqBody = show $ toJSON body
29+
}
30+
result <- simpleHTTP request
31+
return $ either (Left . show) (parseJson . BS.pack . rspBody) result
2132

2233
parseJson :: (FromJSON b, Show b) => BS.ByteString -> Either String b
2334
parseJson jsonString =

github.cabal

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ Library
5959
containers,
6060
text,
6161
old-locale,
62-
download-curl
62+
download-curl,
63+
HTTP,
64+
network
6365

6466
-- Modules not exported by this package.
6567
-- Other-modules:

samples/CommentOnCommit.hs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module CommentOnCommit where
2+
3+
import qualified Github.Repos.Commits as Github
4+
5+
main = do
6+
let commentPoster = Github.postCommentOn "mike-burns" "github" "746779d28dbbeece2593ba37a30a1b457edf3f6e"
7+
comment = Github.NewComment {
8+
Github.newCommentBody = "Good call"
9+
,Github.newCommentLineNumber = 20
10+
,Github.newCommentPath = "Github/Data.hs"
11+
,Github.newCommentPosition = 20
12+
}
13+
didItWork <- commentPoster comment
14+
putStrLn $ either (\error -> "Error: " ++ (show error))
15+
(\comment -> "Posted: " ++ (show comment))
16+
didItWork

0 commit comments

Comments
 (0)