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

Skip to content

Commit cb65642

Browse files
committed
Merge pull request haskell-github#36 from ian-ross/create-issues
Create and edit issues and comments
2 parents 8367951 + 4b7d362 commit cb65642

File tree

6 files changed

+173
-0
lines changed

6 files changed

+173
-0
lines changed

Github/Data.hs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ instance FromJSON Comment where
122122
<*> o .: "id"
123123
parseJSON _ = fail "Could not build a Comment"
124124

125+
instance ToJSON NewComment where
126+
toJSON (NewComment b) = object [ "body" .= b ]
127+
128+
instance ToJSON EditComment where
129+
toJSON (EditComment b) = object [ "body" .= b ]
130+
125131
instance FromJSON Diff where
126132
parseJSON (Object o) =
127133
Diff <$> o .: "status"
@@ -218,6 +224,26 @@ instance FromJSON Issue where
218224
<*> o .:? "milestone"
219225
parseJSON _ = fail "Could not build an Issue"
220226

227+
instance ToJSON NewIssue where
228+
toJSON (NewIssue t b a m ls) =
229+
object
230+
[ "title" .= t
231+
, "body" .= b
232+
, "assignee" .= a
233+
, "milestone" .= m
234+
, "labels" .= ls ]
235+
236+
instance ToJSON EditIssue where
237+
toJSON (EditIssue t b a s m ls) =
238+
object $ filter notNull $ [ "title" .= t
239+
, "body" .= b
240+
, "assignee" .= a
241+
, "state" .= s
242+
, "milestone" .= m
243+
, "labels" .= ls ]
244+
where notNull (_, Null) = False
245+
notNull (_, _) = True
246+
221247
instance FromJSON Milestone where
222248
parseJSON (Object o) =
223249
Milestone <$> o .: "creator"

Github/Data/Definitions.hs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,14 @@ data Comment = Comment {
107107
,commentId :: Int
108108
} deriving (Show, Data, Typeable, Eq, Ord)
109109

110+
data NewComment = NewComment {
111+
newCommentBody :: String
112+
} deriving (Show, Data, Typeable, Eq, Ord)
113+
114+
data EditComment = EditComment {
115+
editCommentBody :: String
116+
} deriving (Show, Data, Typeable, Eq, Ord)
117+
110118
data Diff = Diff {
111119
diffStatus :: String
112120
,diffBehindBy :: Int
@@ -195,6 +203,24 @@ data Issue = Issue {
195203
,issueMilestone :: Maybe Milestone
196204
} deriving (Show, Data, Typeable, Eq, Ord)
197205

206+
data NewIssue = NewIssue {
207+
newIssueTitle :: String
208+
, newIssueBody :: Maybe String
209+
, newIssueAssignee :: Maybe String
210+
, newIssueMilestone :: Maybe Int
211+
, newIssueLabels :: Maybe [String]
212+
} deriving (Show, Data, Typeable, Eq, Ord)
213+
214+
data EditIssue = EditIssue {
215+
editIssueTitle :: Maybe String
216+
, editIssueBody :: Maybe String
217+
, editIssueAssignee :: Maybe String
218+
, editIssueState :: Maybe String
219+
, editIssueMilestone :: Maybe Int
220+
, editIssueLabels :: Maybe [String]
221+
} deriving (Show, Data, Typeable, Eq, Ord)
222+
223+
198224
data Milestone = Milestone {
199225
milestoneCreator :: GithubOwner
200226
,milestoneDueOn :: Maybe GithubDate

Github/Issues.hs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
1+
{-# LANGUAGE OverloadedStrings #-}
12
-- | The issues API as described on <http://developer.github.com/v3/issues/>.
23
module Github.Issues (
34
issue
45
,issue'
56
,issuesForRepo
67
,issuesForRepo'
78
,IssueLimitation(..)
9+
10+
-- * Modifying Issues
11+
-- |
12+
-- Only authenticated users may create and edit issues.
13+
,GithubAuth(..)
14+
15+
,createIssue
16+
,newIssue
17+
,editIssue
18+
,editOfIssue
819
,module Github.Data
920
) where
1021

1122
import Github.Data
1223
import Github.Private
24+
import Data.Aeson.Types
25+
import qualified Data.Aeson as A
1326
import Data.List (intercalate)
1427
import Data.Time.Format (formatTime)
1528
import System.Locale (defaultTimeLocale)
@@ -84,3 +97,36 @@ issuesForRepo' auth user repoName issueLimitations =
8497
-- > issuesForRepo "thoughtbot" "paperclip" [NoMilestone, OnlyClosed, Mentions "jyurek", Ascending]
8598
issuesForRepo :: String -> String -> [IssueLimitation] -> IO (Either Error [Issue])
8699
issuesForRepo = issuesForRepo' Nothing
100+
101+
102+
-- Creating new issues.
103+
104+
newIssue :: String -> NewIssue
105+
newIssue title = NewIssue title Nothing Nothing Nothing Nothing
106+
107+
108+
-- |
109+
-- Create a new issue.
110+
--
111+
-- > createIssue (GithubUser (user, password)) user repo
112+
-- > (newIssue "some_repo") {...}
113+
createIssue :: GithubAuth -> String -> String -> NewIssue
114+
-> IO (Either Error Issue)
115+
createIssue auth user repo = githubPost auth ["repos", user, repo, "issues"]
116+
117+
118+
-- Editing issues.
119+
120+
editOfIssue :: EditIssue
121+
editOfIssue = EditIssue Nothing Nothing Nothing Nothing Nothing Nothing
122+
123+
124+
-- |
125+
-- Edit an issue.
126+
--
127+
-- > editIssue (GithubUser (user, password)) user repo issue
128+
-- > editOfIssue {...}
129+
editIssue :: GithubAuth -> String -> String -> Int -> EditIssue
130+
-> IO (Either Error Issue)
131+
editIssue auth user repo iss =
132+
githubPatch auth ["repos", user, repo, "issues", show iss]

Github/Issues/Comments.hs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ module Github.Issues.Comments (
44
comment
55
,comments
66
,comments'
7+
8+
-- * Modifying Comments
9+
-- |
10+
-- Only authenticated users may create and edit comments.
11+
,GithubAuth(..)
12+
13+
,createComment
14+
,editComment
715
,module Github.Data
816
) where
917

@@ -31,3 +39,27 @@ comments' :: Maybe GithubAuth -> String -> String -> Int -> IO (Either Error [Is
3139
comments' auth user repoName issueNumber =
3240
githubGet' auth ["repos", user, repoName, "issues", show issueNumber, "comments"]
3341

42+
43+
44+
-- |
45+
-- Create a new comment.
46+
--
47+
-- > createComment (GithubUser (user, password)) user repo issue
48+
-- > "some words"
49+
createComment :: GithubAuth -> String -> String -> Int -> String
50+
-> IO (Either Error Comment)
51+
createComment auth user repo iss body =
52+
githubPost auth
53+
["repos", user, repo, "issues", show iss, "comments"] (NewComment body)
54+
55+
56+
-- |
57+
-- Edit a comment.
58+
--
59+
-- > editComment (GithubUser (user, password)) user repo commentid
60+
-- > "new words"
61+
editComment :: GithubAuth -> String -> String -> Int -> String
62+
-> IO (Either Error Comment)
63+
editComment auth user repo commid body =
64+
githubPatch auth ["repos", user, repo, "issues", "comments", show commid]
65+
(EditComment body)

samples/Issues/CreateIssue.hs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
module CreateIssue where
3+
4+
import qualified Github.Issues as Github
5+
6+
main = do
7+
let auth = Github.GithubBasicAuth "user" "password"
8+
newiss = (Github.newIssue "A new issue") {
9+
Github.newIssueBody = Just "Issue description text goes here"
10+
}
11+
possibleIssue <- Github.createIssue auth "thoughtbot" "paperclip" newiss
12+
putStrLn $ either (\e -> "Error: " ++ show e)
13+
formatIssue
14+
possibleIssue
15+
16+
formatIssue issue =
17+
(Github.githubOwnerLogin $ Github.issueUser issue) ++
18+
" opened this issue " ++
19+
(show $ Github.fromGithubDate $ Github.issueCreatedAt issue) ++ "\n" ++
20+
(Github.issueState issue) ++ " with " ++
21+
(show $ Github.issueComments issue) ++ " comments" ++ "\n\n" ++
22+
(Github.issueTitle issue)

samples/Issues/EditIssue.hs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
module EditIssue where
3+
4+
import qualified Github.Issues as Github
5+
6+
main = do
7+
let auth = Github.GithubBasicAuth "user" "password"
8+
issueid = 3
9+
edit = Github.editOfIssue { Github.editIssueState = Just "closed" }
10+
possibleIssue <- Github.editIssue auth "thoughtbot" "paperclip" issueid edit
11+
putStrLn $ either (\e -> "Error: " ++ show e)
12+
formatIssue
13+
possibleIssue
14+
15+
formatIssue issue =
16+
(Github.githubOwnerLogin $ Github.issueUser issue) ++
17+
" opened this issue " ++
18+
(show $ Github.fromGithubDate $ Github.issueCreatedAt issue) ++ "\n" ++
19+
(Github.issueState issue) ++ " with " ++
20+
(show $ Github.issueComments issue) ++ " comments" ++ "\n\n" ++
21+
(Github.issueTitle issue)

0 commit comments

Comments
 (0)