Skip to content

Commit

Permalink
Use TypeApplication to lock down types for show function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-si committed Jan 8, 2024
1 parent 1215805 commit b66dd33
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 38 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Includes a crawler for metadata of GitHub repos.
- [ ] Add subcommand to load list of repos from Airsequel and update them
- [ ] Add CLI flag to choose between `OverwriteRepo` and `AddRepo`
- [ ] Store all languages for a repo
- [ ] Store if account is a person or an organization


## Related
Expand Down
15 changes: 9 additions & 6 deletions app/Airsequel.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Airsequel where
import Protolude (
Either (..),
IO,
Int,
Integer,
Maybe (..),
Text,
Expand Down Expand Up @@ -67,7 +68,6 @@ import Network.HTTP.Client.TLS (tlsManagerSettings)
import Network.HTTP.Types (statusCode)
import Text.RawString.QQ (r)

import Numeric (showInt)
import Types (GqlRes (..), Repo (..), SaveStrategy (..))
import Utils (loadAirsWriteToken, loadDbEndpoint)

Expand Down Expand Up @@ -177,10 +177,12 @@ loadRowids manager dbEndpoint airseqWriteToken repos = do
putErrLn err
pure repos
Right rowids -> do
P.putStrLn $
P.putText $
"\nℹ️ "
<> showInt (P.length rowids) " of "
<> showInt (P.length repos) " repos already exist in Airsequel"
<> show @Int (P.length rowids)
<> " of "
<> show @Int (P.length repos)
<> " repos already exist in Airsequel"

pure $
repos <&> \repo ->
Expand All @@ -198,7 +200,8 @@ via a POST request executed by http-client
-}
saveReposInAirsequel :: SaveStrategy -> [Repo] -> IO ()
saveReposInAirsequel saveStrategy repos = do
P.putText $ "\n⏳ Saving " <> show (P.length repos) <> " repos in Airsequel …"
P.putText $
"\n⏳ Saving " <> show @Int (P.length repos) <> " repos in Airsequel …"

dbEndpoint <- loadDbEndpoint
airseqWriteToken <- loadAirsWriteToken
Expand Down Expand Up @@ -254,7 +257,7 @@ saveReposInAirsequel saveStrategy repos = do
case gqlRes of
Left err -> do
putErrText "Error parsing GraphQL response:"
P.putStrLn err
putErrLn err
Right GqlRes{gqlErrors} ->
case gqlErrors of
Nothing -> pure ()
Expand Down
44 changes: 12 additions & 32 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Protolude (
Either (Left, Right),
IO,
Int,
Integer,
Maybe (..),
Text,
elem,
Expand All @@ -23,7 +24,6 @@ import Protolude (
mempty,
pure,
putErrText,
putStrLn,
putText,
show,
when,
Expand Down Expand Up @@ -78,7 +78,6 @@ import Options.Applicative (
import Text.RawString.QQ (r)

import Airsequel (saveReposInAirsequel)
import Numeric (showInt)
import Options.Applicative.Help.Pretty (vsep)
import Types (GqlRepoRes (..), Repo (..), SaveStrategy (..))
import Utils (loadGitHubToken)
Expand Down Expand Up @@ -132,26 +131,6 @@ commands = do
)


formatRepo :: Repo -> Text
formatRepo repo = do
let repoSlug =
(repo.owner & fromMaybe "")
<> "/"
<> (repo.name & fromMaybe "")

"\n\n"
<> ("repo_url: github.com/" <> repoSlug <> "\n")
<> ("description: " <> (repo.description & fromMaybe "") <> "\n")
<> ("homepage: " <> (repo.homepageUrl & fromMaybe "") <> "\n")
<> ("language: " <> (repo.primaryLanguage & fromMaybe "") <> "\n")
<> ("stargazers_count: " <> show repo.stargazerCount <> "\n")
<> ("commits_count: " <> show (repo.commitsCount & fromMaybe 0) <> "\n")
<> ("open_issues_count: " <> show repo.openIssuesCount <> "\n")
<> ("is_archived: " <> (repo.isArchived & show & T.toLower) <> "\n")
<> ("created_at: " <> show repo.createdAt <> "\n")
<> ("updated_at: " <> show repo.updatedAt <> "\n")


-- | Query @Link@ header with @rel=last@ from the request headers
getLastUrl :: Response a -> Maybe URI
getLastUrl req = do
Expand Down Expand Up @@ -266,9 +245,9 @@ execGithubGqlQuery ghTokenMb query variables initialRepos = do
Nothing -> pure ()

when (P.null initialRepos {- First call -}) $ do
putStrLn $
putText $
"\n📲 Total number of repos: "
<> showInt gqlResponse.repositoryCount ""
<> show @Integer gqlResponse.repositoryCount

when (gqlResponse.repositoryCount > 1000) $ do
putText $
Expand All @@ -278,10 +257,10 @@ execGithubGqlQuery ghTokenMb query variables initialRepos = do

let repos :: [Repo] = gqlResponse.repos

putStrLn $
putText $
"\n✅ Received "
<> showInt (P.length repos) " repos "
<> "from GitHub"
<> show @Int (P.length repos)
<> " repos from GitHub"

repos
<&> ( \repo ->
Expand All @@ -290,10 +269,10 @@ execGithubGqlQuery ghTokenMb query variables initialRepos = do
<> ("/" :: Text)
<> (repo.name & fromMaybe "")
<> (" | stars: " :: Text)
<> show repo.stargazerCount
<> show @Integer (repo.stargazerCount & fromMaybe 0)
<> (" | commits: " :: Text)
<> ( repo.commitsCount
<&> show
<&> show @Integer
& fromMaybe "ERROR: Should have a commits count"
)
)
Expand Down Expand Up @@ -414,9 +393,10 @@ run cliCmd = do

repos <- loadAndSaveReposViaSearch ghTokenMb searchQueryNorm 20 Nothing

putStrLn $
"\n🏁🏁🏁 Crawled "
<> showInt (P.length repos) " repos in total 🏁🏁🏁\n"
putText $
"\n🏁 Crawled "
<> show @Int (P.length repos)
<> " for search query 🏁\n"

pure ()

Expand Down

0 comments on commit b66dd33

Please sign in to comment.