A peer-to-peer stack for code collaboration
GitHub issues exporter (#216)
James Haydon08487c145144
diff --git a/exe/GithubIssues.hs b/exe/GithubIssues.hsnew file mode 100644index 0000000..8a62cd2--- /dev/null+++ b/exe/GithubIssues.hs@@ -0,0 +1,127 @@+{-# LANGUAGE QuasiQuotes #-}++module GithubIssues (main) where++import Protolude hiding (state)++import qualified Data.Map.Strict as Map+import Data.Time+import GitHub.Data.Issues+import GitHub.Data.Name (Name(..))+import qualified GitHub.Data.Options as GH+import qualified GitHub.Endpoints.Issues as GH+import qualified GitHub.Endpoints.Issues.Comments as GH+import Options.Applicative++import Radicle++main :: IO ()+main = do+ Opts{..} <- execParser allOpts+ let auth = GH.OAuth . toS <$> token+ is_ <- runExceptT (getIssues auth owner repo)+ case is_ of+ Right is -> do+ let t = renderPrettyDef (toRad (uncurry radIssue <$> is) :: Value)+ case output of+ Just file -> writeFile file t+ Nothing -> putStrLn t+ Left e -> print e+ where+ allOpts = info (opts <**> helper)+ ( fullDesc+ <> progDesc "Exports all issues from a GitHub repo as radicle data."+ <> header "Radicle GitHub issues exporter"+ )++getIssues :: Maybe GH.Auth -> Text -> Text -> ExceptT GH.Error IO [(Issue, [IssueComment])]+getIssues auth owner repo = do+ is <- ExceptT $ GH.issuesForRepo' auth (N owner) (N repo) GH.stateAll+ let issues = filter notPR (toList is)+ css <- traverse (getComments auth owner repo) issues+ pure $ zip issues css+ where+ notPR Issue{ issuePullRequest = Nothing} = True+ notPR _ = False++getComments :: Maybe GH.Auth -> Text -> Text -> Issue -> ExceptT GH.Error IO [IssueComment]+getComments auth owner repo Issue{..} = do+ -- You would think we should query by issue ID, given the types, but nonono!+ -- That would be too easy!+ -- (This will probably be fixed upstream, soon, by me.)+ cs <- ExceptT $ GH.comments' auth (N owner) (N repo) (GH.mkId (Proxy :: Proxy Issue) issueNumber)+ pure (toList cs)++radIssue :: Issue -> [IssueComment] -> Value+radIssue Issue{..} cs = dict $+ [ ( [kword|author-github-username|], author issueUser )+ , ( [kword|title|], String issueTitle )+ , ( [kword|body|], mt issueBody )+ , ( [kword|github-assignees|], toRad $ author <$> toList issueAssignees)+ , ( [kword|state|], state issueState )+ , ( [kword|labels|]+ , toRad $ GH.untagName . GH.labelName <$> toList issueLabels+ )+ , ( [kword|created-at|], date issueCreatedAt )+ , ( [kword|comments|], toRad (radComment <$> cs) )+ ] <> catMaybes+ [ ( [kword|closed-at|],) . date <$> issueClosedAt ]+ where+ mt = String . fromMaybe ""+ state = \case GH.StateOpen -> [kword|open|]+ GH.StateClosed -> [kword|closed|]++radComment :: IssueComment -> Value+radComment IssueComment{..} = dict+ [ ( [kword|body|], String issueCommentBody )+ , ( [kword|github-author|], author issueCommentUser )+ , ( [kword|created-at|], date issueCommentCreatedAt )+ ]++dict :: [(Value, Value)] -> Value+dict = Dict . Map.fromList++date :: UTCTime -> Value+date = String . toS . formatTime defaultTimeLocale fmt+ where+ fmt = iso8601DateFormat (Just "%H:%M:%S")++author :: GH.SimpleUser -> Value+author = String . GH.untagName . GH.simpleUserLogin++-- CLI++data Opts = Opts+ { owner :: Text+ , repo :: Text+ , output :: Maybe FilePath+ , token :: Maybe Text+ }++opts :: Parser Opts+opts = Opts+ <$> strArgument+ ( metavar "OWNER"+ <> help "GitHub owner username."+ )+ <*> strArgument+ ( metavar "REPO"+ <> help "GitHub repo name."+ )+ <*> optional (strOption+ ( long "output"+ <> short 'o'+ <> metavar "FILE"+ <> help "Output file with the GitHub issues as radicle data."+ ))+ <*> optional (strOption+ ( long "token"+ <> short 't'+ <> metavar "TOKEN"+ <> help+ ( "GitHub personal access token. "+ <> "If the repo has a large amount of issues, this will "+ <> "be needed because of rate-limiting. "+ <> "Go to https://github.com/settings/tokens to generate a token."+ )+ ))diff --git a/package.yaml b/package.yamlindex 678b1e8..44a46c5 100644--- a/package.yaml+++ b/package.yaml@@ -160,6 +160,21 @@ executables:- data-defaultghc-options: -main-is ReferenceDoc+ radicle-github-issues:+ main: GithubIssues.hs+ source-dirs: exe/+ other-modules: []+ when:+ - condition: impl(ghcjs)+ buildable: false+ dependencies:+ - radicle+ - errors+ - time+ - github+ - optparse-applicative+ ghc-options: -main-is GithubIssues+default-extensions:- AutoDeriveTypeable- ConstraintKindsdiff --git a/src/Radicle.hs b/src/Radicle.hsindex d8e4a6d..420c3aa 100644--- a/src/Radicle.hs+++ b/src/Radicle.hs@@ -44,6 +44,7 @@ module Radicle, Reference(..), Ident, ident+ , kword, mkIdent, unsafeToIdent, fromIdentdiff --git a/src/Radicle/Internal.hs b/src/Radicle/Internal.hsindex 2c3e690..014b0dd 100644--- a/src/Radicle/Internal.hs+++ b/src/Radicle/Internal.hs@@ -3,12 +3,14 @@ module Radicle.Internal( module X, mkIdent, ident+ , kword) where-import Prelude (error)+import Prelude (String)import Protoludeimport Language.Haskell.TH.Quote+import qualified Language.Haskell.TH.Syntax as Syntaximport Radicle.Internal.Annotation as Ximport Radicle.Internal.CLI as Ximport Radicle.Internal.Core as X@@ -27,13 +29,25 @@ mkIdent t = case runIdentity (M.runParserT valueP "" t) ofRight (Atom i) -> pure i_ -> Nothing+expQuot :: Text -> (String -> Syntax.Q Syntax.Exp) -> QuasiQuoter+expQuot name e = QuasiQuoter+ { quoteExp = e+ , quoteType = panic err+ , quotePat = panic err+ , quoteDec = panic err+ }+ where+ err = name <> " only works for expressions"+-- | A quasiquoter that checks that an identifier is valid at compile-time.ident :: QuasiQuoter-ident = QuasiQuoter- { quoteExp = \s -> [| case mkIdent s of- Nothing -> error $ "Not a valid identifier: " <> s- Just i -> i |]- , quoteType = error "ident only works for expressions"- , quotePat = error "ident only works for expressions"- , quoteDec = error "ident only works for expressions"- }+ident =+ expQuot "ident" $ \s -> [| case mkIdent s of+ Nothing -> panic $ "Not a valid identifier: " <> s+ Just i -> i |]++kword :: QuasiQuoter+kword =+ expQuot "kword" $ \s -> [| case mkIdent s of+ Nothing -> panic $ "Not a valid keyword: " <> s+ Just i -> Keyword i |]