This repository has been archived by the owner on Jul 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Mailer.hs
349 lines (291 loc) · 13.5 KB
/
Mailer.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
{-
Copyright 2014 Google Inc. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file or at
https://developers.google.com/open-source/licenses/bsd
-}
{-# LANGUAGE NamedFieldPuns, DeriveGeneric, DeriveDataTypeable #-}
module Github.PullRequests.Mailer where
import Control.Applicative
import Control.Exception
import Control.Monad
import Control.Monad.Error
import Data.Aeson (FromJSON(..), ToJSON(..), genericParseJSON, genericToJSON,
fromJSON, json')
import qualified Data.Aeson as JSON
import qualified Data.Aeson.Types as JSON
import qualified Data.Attoparsec.ByteString as A
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BS8
import qualified Data.ByteString.Lazy.Char8 as BSL8
import Data.Char (toLower)
import Data.Foldable (for_)
import Data.List (stripPrefix)
import Data.Maybe
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Data.Typeable
import GHC.Generics
import Github.Auth
import qualified Github.Issues.Comments as GH
import Github.PullRequests hiding (Error)
import qualified Github.PullRequests as GH
import System.Command (cmd, Stdout(..), Exit(..))
import System.Directory (setCurrentDirectory)
import System.Exit (ExitCode(..))
import System.FilePath ((</>))
import System.IO (hPutStrLn, stderr)
import System.IO.Temp (withSystemTempDirectory)
import Github.PullRequests.Mailer.MsgId
-- | Terminates the program with exit code 1 and the given error message.
die :: (MonadError e m, Error e) => String -> m a
die = throwError . strMsg
-- | Logs a given message.
logInfo :: String -> IO ()
logInfo = hPutStrLn stderr
-- | Newtype around Github `Error`s so that we can throw them as exceptions.
data GithubError = GithubError String GH.Error
deriving (Show, Typeable)
instance Exception GithubError
-- | Throws a Github Error as an Exception, adding a description of what
-- what action failed.
throwGithub :: String -> GH.Error -> IO a
throwGithub msg e = throwIO $ GithubError msg e
-- | If the value is 'Left', throw an error using `throwGithub` using the
-- supplied error message. Otherwise return the 'Right' part.
rightOrThrowGithub :: String -> Either GH.Error a -> IO a
rightOrThrowGithub msg = either (throwGithub msg) return
-- | Read-replace-write the contents of a file.
replaceTextInFile :: FilePath -> (Text -> Text) -> IO ()
replaceTextInFile path f = T.readFile path >>= T.writeFile path . f
-- | Remove leading and trailing white space from a string.
strip :: String -> String
strip = T.unpack . T.strip . T.pack
-- | Identifies a pull request.
data PRID = PRID
{ pridUser :: String
, pridName :: String
, pridNumber :: Int
} deriving (Eq, Ord, Show)
-- | Downloads a pull request from Github. Dies on error.
downloadPullRequest :: Maybe GithubAuth -> PRID -> IO DetailedPullRequest
downloadPullRequest auth PRID{ pridUser, pridName, pridNumber } = do
GH.pullRequest' auth pridUser pridName pridNumber >>=
rightOrThrowGithub "Error getting pull request"
-- | Information about an email thread.
data ThreadInfo = ThreadInfo
{ tiMsgId :: MsgId -- ^ the email message ID
, tiIteration :: Int -- ^ patch iteration (`reroll-count` in Git),
-- starting at 1
} deriving (Eq, Ord, Show, Generic)
threadInfoJsonOptions :: JSON.Options
threadInfoJsonOptions = JSON.defaultOptions
{ JSON.fieldLabelModifier = \s -> case stripPrefix "ti" s of
Just (x:xs) -> toLower x : xs
Just xs -> xs
Nothing -> error "threadInfoJsonOptions: bad prefix"
}
instance FromJSON ThreadInfo where
parseJSON = genericParseJSON threadInfoJsonOptions
instance ToJSON ThreadInfo where
toJSON = genericToJSON threadInfoJsonOptions
-- | The string in a pull request comment that directly precedes a JSON
-- representation of `ThreadInfo`.
_THREAD_INFO_JSON_HEADER :: String
_THREAD_INFO_JSON_HEADER = "pull-request-mailer-data "
-- | Parses a Github issue comment body into a `ThreadInfo`, if it contains
-- one.
parseThreadInfo :: String -> Maybe ThreadInfo
parseThreadInfo body = do
let bodyBs = BS8.pack body
dataHeader = BS8.pack _THREAD_INFO_JSON_HEADER
-- Find where the data starts.
rest = BS.drop (BS8.length dataHeader)
. snd . BS.breakSubstring dataHeader $ bodyBs
-- Parse what follows as `ThreadInfo`, drop everything behind.
case fromJSON <$> A.parseOnly json' rest of
Right (JSON.Success threadInfo) -> Just threadInfo
_ -> Nothing
-- | Get the most recent `ThreadInfo` contained in a pull request's comments.
getMostRecentThreadInfo :: Maybe GithubAuth -- ^ Github authentication
-> PRID -- ^ from wich PR to get the info
-> IO (Maybe ThreadInfo)
getMostRecentThreadInfo auth PRID{ pridUser, pridName, pridNumber } =
liftM (last . (Nothing :) . map (parseThreadInfo . issueCommentBody)) .
rightOrThrowGithub "Failed to get pull request comments" =<<
GH.comments' auth pridUser pridName pridNumber
-- | Converts a pull request to a patch series using `git format-patch`.
sendPatchSeries :: String -- ^ recipient email address
-> Maybe String -- ^ reply-to address
-> Maybe ThreadInfo -- ^ thread to reply to
-- (previous iteration of the PR)
-> Maybe String -- ^ post-checkout hook program
-> DetailedPullRequest -- ^ the pull request to convert
-> IO ThreadInfo
sendPatchSeries recipient replyTo prevThreadInfo checkoutHookCmd
DetailedPullRequest
{ detailedPullRequestHtmlUrl = url
, detailedPullRequestUser = prOwner
, detailedPullRequestTitle = title
, detailedPullRequestBody = body
, detailedPullRequestHead = PullRequestCommit
{ pullRequestCommitRef = tipBranch
, pullRequestCommitRepo = Repo
{ repoName = tipRepoName
, repoOwner = tipRepoOwner
}
}
, detailedPullRequestBase = PullRequestCommit
{ pullRequestCommitRef = baseBranch
, pullRequestCommitRepo = Repo
{ repoName = baseRepoName
, repoOwner = baseRepoOwner
}
}
} = do
withSystemTempDirectory "pull-request-mailer" $ \tmpDir -> do
-- Clone the base.
let uri = githubOwnerLogin baseRepoOwner ++ "/" ++ baseRepoName
logInfo $ "Cloning " ++ uri
() <- cmd ("git clone git://github.com/" ++ uri) "-b" baseBranch tmpDir
-- ^ We don't use --depth 1 here because git will send the whole history
-- as patches if it cannot see a common base between our two commits.
setCurrentDirectory tmpDir
-- Add the pull request tipBranch as a remote.
let uriR = githubOwnerLogin tipRepoOwner ++ "/" ++ tipRepoName
logInfo $ "Adding remote " ++ uriR
() <- cmd ("git remote add pullrequest git://github.com/" ++ uriR)
logInfo $ "Fetching from the remote: " ++ tipBranch
-- We would prefer to do `git fetch pullrequest tipBranch` here, but
-- in git < 1.8.4 this doesn't make pullrequest/tipBranch available,
-- so we have to fetch the whole remote in order to support this version.
() <- cmd "git fetch pullrequest"
() <- cmd "git branch -rv"
() <- cmd "git remote -v"
-- Run the post-checkout hook command if given.
for_ checkoutHookCmd $ \hookCmd -> do
logInfo $ "Running " ++ hookCmd
Exit code <- cmd hookCmd
when (code /= ExitSuccess) $ die "Post checkout hook failed. Aborting."
let _PATCH_DIR_NAME = "patch-dir"
patchVersionPrefix = case prevThreadInfo of
Just (ThreadInfo _ lastN) -> "v" ++ show (lastN + 1) ++ "-"
Nothing -> ""
coverLetterPath = _PATCH_DIR_NAME
</> patchVersionPrefix ++ "0000-cover-letter.patch"
-- Create the patch series.
() <- cmd "git format-patch"
"--cover-letter"
(["--subject-prefix=PR PATCH " ++ baseBranch]
++ case prevThreadInfo of
Nothing -> []
Just (ThreadInfo msgId lastN) ->
-- `--in-reply-to` allows email header injection.
-- `msgId` being of type MsgId makes it safe to use here
-- as it carries the invariant of being a valid msg-id
-- /only/.
[ "--in-reply-to=" ++ fromMsgId msgId
, "--reroll-count=" ++ show (lastN + 1)
]
)
[ "--add-header=Reply-To: " ++ addr | Just addr <- [replyTo] ]
"--output-directory" _PATCH_DIR_NAME
"--thread=shallow"
("origin/" ++ baseBranch ++ "..pullrequest/" ++ tipBranch)
-- Get the Message-Id git format-patch assgined.
msgId <- mkMsgId =<<
fromMaybe (error "format-patch failed to create Message-Id")
. fmap T.unpack
. listToMaybe
. mapMaybe (T.stripPrefix $ T.pack "Message-Id: ")
. T.lines
<$> T.readFile coverLetterPath
-- Fill out the cover letter.
Stdout gitUserEmailOutput <- cmd "git config --get user.email"
let seriesSubmitter = strip gitUserEmailOutput
let prUser = githubOwnerLogin prOwner
let note = "******** NOTE: pull-request-mailer ********\n\
\* User '" ++ prUser ++ "' has submitted a pull request on\n\
\* " ++ url ++ "\n\
\* \n\
\* It has been converted to a patch series by \n\
\* " ++ seriesSubmitter ++ "\n\
\***************************************************\n\
\\n"
replaceTextInFile coverLetterPath
$ T.replace (T.pack "*** SUBJECT HERE ***") (T.pack title)
. T.replace (T.pack "*** BLURB HERE ***") (T.pack $ note ++ body)
-- Send the email.
() <- cmd "git send-email"
"--no-thread" -- we do threading with `format-patch` above
"--confirm=never" -- be non-interactive
["--to=" ++ recipient, "--from=" ++ seriesSubmitter]
_PATCH_DIR_NAME
return $ ThreadInfo msgId (maybe 1 (succ . tiIteration) prevThreadInfo)
-- | Creates a comment in the pull request stating that the PR was converted
-- to an email thread using this program and that users shall not reply to the
-- PR, but continue the discussion at the given location instead.
-- It states the used message ID of the first email in a parsable format so
-- that subsequent invocations can be sent as replies to the previous threads.
--
-- This way we can achieve threading as given in the example in
-- `man git send-email`.
--
-- [PATCH branch 0/2] Here is what I did...
-- [PATCH branch 1/2] Clean up and tests
-- [PATCH branch 2/2] Implementation
-- [PATCH branch v2 0/3] Here is a reroll
-- [PATCH branch v2 1/3] Clean up
-- [PATCH branch v2 2/3] New tests
-- [PATCH branch v2 3/3] Implementation
--
-- It also includes the number of times the pull request was turned into
-- a thread so that force-pushed improvements to the PR can be sent with
-- the correct `--reroll-count` option to `git format-patch`.
-- The first iteration (original patch) should be passed as reroll-count=1.
postMailerInfoComment :: GithubAuth -- ^ Github authentication
-> PRID -- ^ on which PR to comment
-> String -- ^ discussion location
-> ThreadInfo -- ^ information to post for subsequent
-- invocations
-> IO ()
postMailerInfoComment auth prid discussionLocation threadInfo = do
let PRID{ pridUser, pridName, pridNumber } = prid
-- Note: The message ID in `threadInfo` contains "<" and ">".
-- aeson's `encode` turns them into not-so-nice "\u003c" and "\u003e"
-- for silly reasons, see:
-- https://github.com/bos/aeson/issues/180#issuecomment-54386449
let dat = BSL8.unpack . JSON.encode $ threadInfo
let msg =
"`AUTOGENERATED MESSAGE by pull-request-mailer`\n\
\This pull request has been converted to an email thread on\
\ " ++ discussionLocation ++ ". Discussion continues there.\n\
\\n\
\**Please do not post comments to this pull request.**\
\ The email thread will not get notified.\n\
\\n\
\<!-- " ++ _THREAD_INFO_JSON_HEADER ++ dat ++ " -->"
GH.createComment auth pridUser pridName pridNumber msg >>=
rightOrThrowGithub "Failed to create comment" >>
return ()
-- | Converts a detailed pull request into a 'PRID'.
detailedPullRequestToPRID :: DetailedPullRequest -> PRID
detailedPullRequestToPRID dpr =
PRID (githubOwnerLogin . repoOwner $ repo)
(repoName repo)
(detailedPullRequestNumber dpr)
where
repo = pullRequestCommitRepo . detailedPullRequestBase $ dpr
-- | Converts a GitHub pull request to a mail thread and sends it.
pullRequestToThread :: Maybe GithubAuth -- ^ Github authentication
-> PRID -- ^ wich PR to convert to an email
-- thread
-> String -- ^ recipient email address
-> Maybe String -- ^ reply-to address
-> Maybe String -- ^ post-checkout hook program
-> IO ThreadInfo
pullRequestToThread m'auth prid recipient replyTo checkoutHookCmd = do
pr <- downloadPullRequest m'auth prid
prevThreadInfo <- getMostRecentThreadInfo m'auth prid
sendPatchSeries recipient replyTo prevThreadInfo checkoutHookCmd pr