forked from purescript/purescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-changelog.hs
executable file
·215 lines (183 loc) · 7.67 KB
/
update-changelog.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
#!/usr/bin/env stack
{- stack
--resolver lts-20.9 script
--package bytestring
--package filepath
--package text
--package github-rest
--package directory
--package simple-cmd
--package time
--package bifunctors
--package attoparsec
--package aeson
--package protolude
-}
{-# LANGUAGE
DeriveFoldable
, DeriveFunctor
, DeriveTraversable
, FlexibleContexts
, LambdaCase
, NoImplicitPrelude
, OverloadedStrings
, PackageImports
, RecordWildCards
, TupleSections
, ViewPatterns
#-}
-- |
-- This script updates CHANGELOG.md with the contents of CHANGELOG.d, and
-- empties CHANGELOG.d. It takes care of:
--
-- * Sorting entries by the order in which their PRs were merged
-- * Appending (#1234 by @author) to the first line of each fragment,
-- optionally adding multiple PR numbers and/or authors as applicable
-- * Grouping entries by type and adding non-empty group headings to the
-- changelog
-- * Syncing any affected files to the Git index, preparing for you to make
-- your release commit
--
-- Be sure to run this *after* updating the version number in
-- npm-package/package.json, as that's where this script gets the new section
-- header from.
--
module Main (main) where
import Protolude hiding (intercalate, readFile, writeFile)
import qualified Protolude
import Control.Monad.Fail (fail)
import qualified Data.Aeson as JSON
import qualified Data.Aeson.KeyMap as KM
import Data.Attoparsec.ByteString (maybeResult, parse)
import "bifunctors"
Data.Bifunctor.Flip (Flip(..))
import qualified Data.ByteString as BS
import qualified Data.List.NonEmpty as NEL
import Data.String (String)
import qualified Data.String as String
import qualified Data.Text as T
import Data.Time.Clock (UTCTime)
import Data.Time.Format.ISO8601 (iso8601ParseM)
import Data.Time.LocalTime (zonedTimeToUTC)
import GitHub.REST (GHEndpoint(..), GitHubSettings(..), KeyValue(..), MonadGitHubREST, StdMethod(..), queryGitHub, runGitHubT)
import qualified SimpleCmd.Git as IOGit
import System.Directory (setCurrentDirectory)
import System.FilePath (normalise, takeFileName, (</>))
main = runGitHubT gitHubSettings $ do
git "rev-parse" ["--show-toplevel"] >>= liftIO . setCurrentDirectory
entries <- String.lines <$> git "ls-tree" ["--name-only", "HEAD", "CHANGELOG.d/"]
breaks <- processEntriesStartingWith "break" entries
features <- processEntriesStartingWith "feat" entries
fixes <- processEntriesStartingWith "fix" entries
internal <- processEntriesStartingWith "int" entries
misc <- processEntriesStartingWith "misc" entries
let entryFiles = ceFile <$> breaks <> features <> fixes <> internal <> misc
unless (null entryFiles) $ do
changes <- git "status" ("-s" : "--" : "CHANGELOG.md" : entryFiles)
unless (null changes) . liftIO . die $
"You have uncommitted changes to changelog files. " <>
"Please commit, stash, or revert them before running this script."
version <- getVersion
(changelogPreamble, changelogRest) <- T.breakOn "\n## " <$> readFile "CHANGELOG.md"
writeFile "CHANGELOG.md" $
changelogPreamble
<> "\n## " <> version <> "\n"
<> conditionalSection "Breaking changes" breaks
<> conditionalSection "New features" features
<> conditionalSection "Bugfixes" fixes
<> conditionalSection "Other improvements" misc
<> conditionalSection "Internal" internal
<> changelogRest
git_ "add" ["CHANGELOG.md"]
git_ "rm" $ "-q" : entryFiles
gitHubSettings :: GitHubSettings
gitHubSettings = GitHubSettings Nothing "purescript/purescript update-changelog.hs" "v3"
processEntriesStartingWith :: (MonadFail m, MonadGitHubREST m, MonadIO m) => String -> [String] -> m [ChangelogEntry]
processEntriesStartingWith prefix
= fmap (sortOn ceDate)
. traverse updateEntry
. filter ((prefix `isPrefixOf`) . map toLower . takeFileName)
updateEntry :: (MonadFail m, MonadGitHubREST m, MonadIO m) => String -> m ChangelogEntry
updateEntry file = do
(header, body) <- T.breakOn "\n" . T.strip <$> (readFile . normalise) file
allCommits <-
fmap (NEL.fromList . sortOn glcTime)
. traverse (\(T.breakOn " " -> (h, T.breakOn " " . T.tail -> (c, s))) ->
GitLogCommit (T.tail s) h . zonedTimeToUTC <$> iso8601ParseM (toS c))
=<< gitLines "log" ["-m", "--follow", "--format=%H %cI %s", file]
prCommits <-
filterM isInterestingCommit
. mapMaybe (traverse parsePRNumber)
$ NEL.toList allCommits
let prNumbers = map (snd . glcData) prCommits
prAuthors <- ordNub <$> traverse lookupPRAuthor prNumbers
let headerSuffix = if null prNumbers then "" else
" ("
<> commaSeparate (map (("#" <>) . show) prNumbers)
<> " by "
<> commaSeparate (map ("@" <>) prAuthors)
<> ")"
pure $ ChangelogEntry file (header <> headerSuffix <> body <> "\n") (glcTime $ NEL.head allCommits)
parsePRNumber :: Text -> Maybe (CommitType, Int)
parsePRNumber = liftA2 (<|>)
(fmap (MergeCommit, ) . readMaybe . (toS :: T.Text -> String) . fst . T.breakOn " " <=< T.stripPrefix "Merge pull request #")
(fmap (SquashCommit, ) . readMaybe . (toS :: T.Text -> String) <=< T.stripSuffix ")" . snd . T.breakOnEnd "(#")
-- |
-- This function helps us exclude PRs that are just fixups of changelog
-- wording. An interesting commit is one that has either edited a file that
-- isn't part of the changelog, or is a merge commit.
--
isInterestingCommit :: MonadIO m => GitLogCommit (CommitType, Int) -> m Bool
isInterestingCommit GitLogCommit{..} = case fst glcData of
MergeCommit -> pure True
SquashCommit ->
not . all (\path -> "CHANGELOG.md" == path || "CHANGELOG.d/" `T.isPrefixOf` path)
<$> gitLines "show" ["--format=", "--name-only", toS glcHash]
lookupPRAuthor :: (MonadFail m, MonadGitHubREST m) => Int -> m Text
lookupPRAuthor prNum =
queryGitHub GHEndpoint{ method = GET
, endpoint = "/repos/purescript/purescript/pulls/:pr"
, endpointVals = ["pr" := prNum]
, ghData = []
}
>>= \case
JSON.Object (KM.lookup "user" -> Just (JSON.Object (KM.lookup "login" -> Just (JSON.String name)))) -> pure name
_ -> fail "error accessing GitHub API"
commaSeparate :: [Text] -> Text
commaSeparate = \case
[] -> ""
[a] -> a
[a, b] -> a <> " and " <> b
more | Just (init, last) <- unsnoc more -> T.intercalate ", " init <> ", and " <> last
getVersion :: (MonadFail m, MonadIO m) => m Text
getVersion =
(liftIO . BS.readFile) ("npm-package" </> "package.json") >>= \case
(maybeResult . parse JSON.json -> Just (JSON.Object (KM.lookup "version" -> Just (JSON.String v)))) -> pure v
_ -> fail "could not read version from npm-package/package.json"
conditionalSection :: Text -> [ChangelogEntry] -> Text
conditionalSection header = \case
[] -> ""
entries ->
"\n" <> header <> ":\n\n" <> T.intercalate "\n" (map ceContent entries)
git :: MonadIO m => String -> [String] -> m String
git cmd = liftIO . IOGit.git cmd
git_ :: MonadIO m => String -> [String] -> m ()
git_ cmd = liftIO . IOGit.git_ cmd
gitLines :: MonadIO m => String -> [String] -> m [Text]
gitLines cmd args = lines . toS <$> git cmd args
readFile :: MonadIO m => FilePath -> m Text
readFile = liftIO . Protolude.readFile
writeFile :: MonadIO m => FilePath -> Text -> m ()
writeFile path = liftIO . Protolude.writeFile path
data ChangelogEntry = ChangelogEntry
{ ceFile :: String
, ceContent :: Text
, ceDate :: UTCTime
}
data GitLogCommit a = GitLogCommit
{ glcData :: a
, glcHash :: Text
, glcTime :: UTCTime
}
deriving (Functor, Foldable, Traversable)
data CommitType = MergeCommit | SquashCommit