Skip to content

Commit

Permalink
Add ability to infer from the branch name
Browse files Browse the repository at this point in the history
  • Loading branch information
gpunto committed Jul 14, 2020
1 parent df5a942 commit 7ffc4c0
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 23 deletions.
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,27 @@ A small program to generate release notes files in the format:
## Usage

```
relnot ((-i|--infer) | TICKET [MESSAGE])
Usage: relnot ((-i|--infer) [-b|--branch] | TICKET [MESSAGE])
[-d|--dir DIRECTORY]
[-o|--overwrite]
[-g|--git-add]
```

- `TICKET`: the ticket number, e.g. CAP-1234, CX-1010
- `MESSAGE`: the release notes message
- `-i` or `--infer`: try to infer both ticket and message from the last commit
- `-i|--infer`: try to infer both ticket and message from the last commit
It expects a commit message in the format: `[TICKET] MESSAGE`, e.g. `[CAP-1234] Add juicy stuff`
- `-b|--branch`: try to infer using the branch name instead of the last commit
It expects a branch name in the format `type/ticket/name` or `ticket/name`.
Only valid when used with `-i|--infer`
- `-d|--dir DIRECTORY`: the directory where to put the release notes file
Defaults to: `./release_notes`
- `[-o|--overwrite]`: force overwrite the target file if it already exists
- `[-g|--git-add]`: run `git add` on the generated file
- `-o|--overwrite`: force overwrite the target file if it already exists
- `-g|--git-add`: run `git add` on the generated file

## Examples

**Explicitly specify ticket and message**
### Explicitly specify ticket and message

The command:
`relnot cap-1234 "This is an explicit message"`
Expand All @@ -35,7 +38,9 @@ Generates `./release_notes/cap-1234` with content:
[CaP] This is an explicit message (CAP-1234)
```

**Let infer do the work**
### Let infer do the work

**Using the latest commit**

Assuming the latest commit message is `[CX-1010] This is a commit message`, the command:
`relnot -i`
Expand All @@ -46,6 +51,17 @@ Generates `./release_notes/CX-1010` with content:
[CX] This is a commit message (CX-1010)
```

**Using the branch name**

Assuming the branch name is `cd-9999/beautiful-feature`, the command:
`relnot -ib`

Generates `./release_notes/cd-9999` with content:

```
[CD] (CD-9999)
```

## Building

1. Install [Stack](https://docs.haskellstack.org/en/stable/README/)
Expand Down
4 changes: 2 additions & 2 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ main = do

toFileData :: Mode -> String -> IO (Either String FileData)
toFileData mode dir = case mode of
Infer -> inferred dir
(Ticket ticket message) -> return . Right $ explicit dir ticket message
Infer branch -> inferred branch dir
Ticket ticket message -> return . Right $ explicit dir ticket message
12 changes: 9 additions & 3 deletions src/Git.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Git
( latestCommitMessage,
branchName,
gitAdd,
)
where
Expand All @@ -9,9 +10,14 @@ import System.Process (callProcess, readProcess)

latestCommitMessage :: IO String
latestCommitMessage = trim <$> readProcess "git" ["show-branch", "--no-name", "HEAD"] ""
where
trim = f . f
f = reverse . dropWhile isSpace

branchName :: IO String
branchName = trim <$> readProcess "git" ["rev-parse", "--abbrev-ref", "HEAD"] ""

gitAdd :: String -> IO ()
gitAdd path = callProcess "git" ["add", path]

trim :: String -> String
trim = f . f
where
f = reverse . dropWhile isSpace
32 changes: 26 additions & 6 deletions src/Lib.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{-# LANGUAGE TupleSections #-}

module Lib
( FileData (..),
inferred,
Expand All @@ -10,11 +12,20 @@ import Git

data FileData = FileData {dir :: FilePath, path :: FilePath, content :: String}

inferred :: String -> IO (Either String FileData)
inferred dir =
inferred :: Bool -> String -> IO (Either String FileData)
inferred useBranch = if useBranch then branchInfer else commitInfer

commitInfer :: String -> IO (Either String FileData)
commitInfer dir =
fdata <$> latestCommitMessage
where
fdata s = buildFileData dir <$> extractTicketMessage s
fdata s = buildFileData dir <$> commitExtractTicketMessage s

branchInfer :: String -> IO (Either String FileData)
branchInfer dir =
fdata <$> branchName
where
fdata s = buildFileData dir . (, "") <$> branchExtractTicket s

explicit :: FilePath -> String -> String -> FileData
explicit dir ticket message = buildFileData dir (ticket, message)
Expand All @@ -40,14 +51,23 @@ capHack :: String -> String
capHack "CAP" = "CaP"
capHack s = s

extractTicketMessage :: String -> Either String (String, String)
extractTicketMessage ('[' : cs) = case split ']' cs of
commitExtractTicketMessage :: String -> Either String (String, String)
commitExtractTicketMessage ('[' : cs) = case split ']' cs of
(_, "") ->
Left $
"Couldn't parse the commit, did you forget a bracket or the message?\n"
++ "Commit message: ["
++ cs
(ticket, ' ' : message) -> Right (ticket, message)
pair -> Right pair
extractTicketMessage commit =
commitExtractTicketMessage commit =
Left $ "Couldn't parse the commit, did you forget the ticket?\nCommit message: " ++ commit

branchExtractTicket :: String -> Either String String
branchExtractTicket name = case split '/' name of
(name, "") -> Left "Couldn't parse the branch name, is it in the \"[type/]ticket/name\" format?"
("feat", rest) -> branchExtractTicket rest
("feature", rest) -> branchExtractTicket rest
("fix", rest) -> branchExtractTicket rest
("tech", rest) -> branchExtractTicket rest
(ticket, _) -> Right ticket
26 changes: 20 additions & 6 deletions src/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import Options.Applicative

data Mode
= Infer
{ branch :: Bool
}
| Ticket
{ name :: String,
message :: String
Expand All @@ -24,7 +26,7 @@ data Opts = Opts
deriving (Show)

parseOpts :: IO Opts
parseOpts = execParser (info optsParser (progDesc "Create release notes file"))
parseOpts = execParser (info (optsParser <**> helper) (progDesc "Create release notes file"))

optsParser :: Parser Opts
optsParser = Opts <$> modeParser <*> pathParser <*> overwriteParser <*> gitAddParser
Expand All @@ -34,14 +36,26 @@ modeParser = inferParser <|> ticketParser

inferParser :: Parser Mode
inferParser =
flag' Infer $
long "infer"
<> short 'i'
<> help "Try to infer ticket and message"
Infer
<$> ( flag'
()
( long "infer"
<> short 'i'
<> help "Try to infer ticket and message"
)
*> branchParser
)

ticketParser :: Parser Mode
ticketParser = Ticket <$> nameParser <*> messageParser

branchParser :: Parser Bool
branchParser =
switch $
long "branch"
<> short 'b'
<> help "Whether to use the branch name for inferring"

nameParser :: Parser String
nameParser =
strArgument $
Expand Down Expand Up @@ -79,4 +93,4 @@ overwriteParser =
switch $
long "overwrite"
<> short 'o'
<> help "Whether the generate file can overwrite an existing one"
<> help "Whether the generated file can overwrite an existing one"

0 comments on commit 7ffc4c0

Please sign in to comment.