[#65] Add issue option to hit new (#118)

* [#65]: Add issue options to hit new

If it is set to `true`, the new issue will be created instead.

* [#65]: Create an issue if the flag is set

* [#65]: Fix styling

* [#65]: Fix styling take 2

* [#65]: Remove redundant do

* [#65]: Fix hlint issues

* [#65]: Use separate function to get GITHUB_TOKEN

* [#65]: Follow workflow

- Create an issue
- Stash current changes
- Checkout master
- Create corresponding branch
- Switch to that branch
This commit is contained in:
bangn 2019-12-06 22:35:39 +11:00 committed by Dmitrii Kovanikov
parent b574ab08d3
commit a924bcf2ae
3 changed files with 55 additions and 15 deletions

View File

@ -28,7 +28,7 @@ hit :: IO ()
hit = execParser cliParser >>= \case
Hop branchName -> runHop branchName
Fresh branchName -> runFresh branchName
New issueNum -> runNew issueNum
New createIssue issueNum -> runNew createIssue issueNum
Issue issueNum me -> if me
then getUsername >>= runIssue issueNum . Just
else runIssue issueNum Nothing
@ -60,7 +60,7 @@ cliParser = info ( helper <*> versionP <*> hitP )
data HitCommand
= Hop (Maybe Text)
| Fresh (Maybe Text)
| New Text
| New Bool Text
| Issue (Maybe Int) Bool
| Stash
| Unstash
@ -112,7 +112,13 @@ freshP :: Parser HitCommand
freshP = Fresh <$> maybeBranchP
newP :: Parser HitCommand
newP = New <$> strArgument (metavar "ISSUE_NUMBER_OR_BRANCH_NAME")
newP = do
createIssue <- switch
$ long "issue"
<> short 'i'
<> help "Create new issue instead of branch"
issueNumOrBranch <- strArgument (metavar "ISSUE_NUMBER_OR_BRANCH_NAME")
pure $ New createIssue issueNumOrBranch
issueP :: Parser HitCommand
issueP = do

View File

@ -30,11 +30,11 @@ import Shellmet (($|))
import System.Directory (findExecutable)
import System.Process (callCommand)
import Hit.ColorTerminal (Answer (..), arrow, errorMessage, greenCode, infoMessage, prompt,
resetCode, yesOrNoText)
import Hit.ColorTerminal (Answer (..), arrow, blueCode, errorMessage, greenCode, infoMessage,
prompt, resetCode, yesOrNoText)
import Hit.Core (CommitOptions (..), PushBool (..))
import Hit.Git.Status (showPrettyDiff)
import Hit.Issue (getIssueTitle, mkIssueId)
import Hit.Issue (createIssue', getIssueTitle, issueNumber, mkIssueId, showIssueName, unIssueNumber)
import qualified Data.Text as T
@ -52,8 +52,8 @@ runFresh (nameOrMaster -> branch) = do
"git" ["rebase", "origin/" <> branch]
-- | @hit new@ command.
runNew :: Text -> IO ()
runNew issueOrName = do
runNew :: Bool -> Text -> IO ()
runNew False issueOrName = do
login <- getUsername
title <- case readMaybe @Int $ toString issueOrName of
Just issueNum -> do
@ -73,6 +73,14 @@ runNew issueOrName = do
|| isSpace c
|| c `elem` ("_-./" :: String)
)
runNew _ title =
createIssue' title >>= \case
Left err -> errorMessage $ show err
Right issue -> do
putTextLn . showIssueName blueCode 0 $ issue
runStash
"git" ["checkout", "master"]
runNew False $ show $ unIssueNumber . issueNumber $ issue
-- | @hit commit@ command.
runCommit :: CommitOptions -> IO ()

View File

@ -1,11 +1,15 @@
module Hit.Issue
( runIssue
, createIssue'
-- * Internal helpers
, mkIssueId
, getIssueTitle
, getOwnerRepo
, parseOwnerRepo
, showIssueName
, issueNumber
, unIssueNumber
) where
import Data.Vector (Vector)
@ -13,7 +17,7 @@ import GitHub (Error (..), Id, Issue (..), IssueLabel (..), IssueState (..), Nam
SimpleUser (..), User, getUrl, mkId, mkName, unIssueNumber, untagName)
import GitHub.Auth (Auth (OAuth))
import GitHub.Data.Options (stateOpen)
import GitHub.Endpoints.Issues (issue', issuesForRepo')
import GitHub.Endpoints.Issues (createIssue, issue', issuesForRepo', newIssue)
import Shellmet (($|))
import System.Environment (lookupEnv)
@ -99,6 +103,21 @@ showIssueFull i@Issue{..} = T.intercalate "\n" $
highlight :: Text -> Text
highlight x = boldCode <> greenCode <> x <> resetCode
-- | Create an 'Issue' by given 'Text'
createIssue' :: Text -> IO (Either Error Issue)
createIssue' title = getOwnerRepo >>= \case
Just (owner, repo) -> do
token <- gitHubToken
case token of
Just oAuth -> createIssue oAuth owner repo (newIssue title)
Nothing -> do
let errTxt = "Can not get GITHUB_TOKEN"
errorMessage errTxt
pure $ Left $ ParseError errTxt
Nothing -> do
errorMessage noOwnerRepoError
pure $ Left $ ParseError noOwnerRepoError
mkIssueId :: Int -> Id Issue
mkIssueId = mkId $ Proxy @Issue
@ -118,14 +137,21 @@ withOwnerRepo
-> IO (Either Error a)
withOwnerRepo action = getOwnerRepo >>= \case
Just (owner, repo) -> do
token <- lookupEnv "GITHUB_TOKEN"
let gitHubToken = OAuth . encodeUtf8 <$> token
action gitHubToken owner repo
token <- gitHubToken
action token owner repo
Nothing -> do
let errTxt = "Can not get the owner/repo names"
errorMessage errTxt
pure $ Left $ ParseError errTxt
errorMessage noOwnerRepoError
pure $ Left $ ParseError noOwnerRepoError
-- | Get GITHUB_TOKEN from environment variables
gitHubToken :: IO (Maybe Auth)
gitHubToken = do
token <- lookupEnv "GITHUB_TOKEN"
pure $ OAuth . encodeUtf8 <$> token
-- | Error message when the owner/repo cannot be get
noOwnerRepoError :: Text
noOwnerRepoError = "Cannot get the owner/repo names"
-- | Get the owner and the repository name.
getOwnerRepo :: IO (Maybe (Name Owner, Name Repo))