Skip to content

Commit

Permalink
Fix auto-formatting of code
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-si committed Jan 3, 2024
1 parent 9c48cee commit c379954
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 36 deletions.
93 changes: 58 additions & 35 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}

{-# HLINT ignore "Use maybe" #-}
{-# HLINT ignore "Use unless" #-}

module Main where

Expand Down Expand Up @@ -80,43 +81,51 @@ import Text.RawString.QQ (r)

import Utils (RepoObject, mapMSequentially, repoObjectToRepo)


-- | Replaces a variable in a string with a value
var :: Text -> Text -> Text -> Text
var idName =
T.replace ("<<" <> idName <> ">>")


data SaveStrategy = OverwriteRepo | AddRepo
deriving (Show, Eq)


data ExtendedRepo = ExtendedRepo
{ core :: GH.Repo
, commitsCount :: Maybe Integer
}
deriving (Show, Eq)


-- | The ID of the Airsequel database loaded from the environment
loadDbId :: IO Text
loadDbId =
lookupEnv "AIRSEQUEL_DB_ID" <&> (fromMaybe "" >>> T.pack)


loadDbEndpoint :: IO Text
loadDbEndpoint = do
dbId <- loadDbId
pure $ "https://www.airsequel.com/dbs/" <> dbId <> "/graphql"


loadAsWriteToken :: IO Text
loadAsWriteToken =
lookupEnv "AIRSEQUEL_API_TOKEN" <&> (fromMaybe "" >>> T.pack)


loadGitHubToken :: IO (Maybe Text)
loadGitHubToken =
lookupEnv "GITHUB_TOKEN" <&> (<&> T.pack)


formatRepo :: ExtendedRepo -> Text
formatRepo extendedRepo =
let
repo = core extendedRepo
in
in
"\n\n"
<> ("repo_url: " <> show (GH.repoHtmlUrl repo) <> "\n")
<> ( "description: "
Expand Down Expand Up @@ -145,6 +154,7 @@ formatRepo extendedRepo =
<> "\n"
)


-- | Query @Link@ header with @rel=last@ from the request headers
getLastUrl :: Response a -> Maybe URI
getLastUrl req = do
Expand All @@ -160,7 +170,8 @@ getLastUrl req = do
nextURI <- find isRelNext links
pure $ href nextURI

{- | Workaround to get the number of commits for a repo

{-| Workaround to get the number of commits for a repo
| https://stackoverflow.com/a/70610670
-}
getNumberOfCommits :: Maybe Text -> Repo -> IO (Maybe Integer)
Expand All @@ -173,7 +184,9 @@ getNumberOfCommits ghTokenMb repo = do
putText $ "⏳ Get number of commits for repo " <> repoSlug

let apiEndpoint =
"https://api.github.com/repos/" <> repoSlug <> "/commits?per_page=1"
"https://api.github.com/repos/"
<> repoSlug
<> "/commits?per_page=1"

manager <- newManager tlsManagerSettings
initialRequest <- parseRequest $ T.unpack apiEndpoint
Expand All @@ -193,13 +206,14 @@ getNumberOfCommits ghTokenMb repo = do
<&> (show >>> T.pack >>> T.splitOn "&page=")
>>= lastMay
>>= readMaybe
& pure
& pure


deleteRepoQuery :: ExtendedRepo -> Text
deleteRepoQuery extendedRepo =
let
repo = extendedRepo.core
in
in
[r|
mutation DeleteRepo {
delete_repos(
Expand All @@ -213,6 +227,7 @@ deleteRepoQuery extendedRepo =
|]
& var "github_id" (repo & GH.repoId & GH.untagId & show)


-- | TODO: Airsequel's GraphQL API doesn't support userting yet
upsertRepoQuery :: Text -> ExtendedRepo -> Text
upsertRepoQuery utc extendedRepo =
Expand All @@ -225,7 +240,7 @@ upsertRepoQuery utc extendedRepo =
<&> iso8601Show
& fromMaybe ""
& T.pack
in
in
[r|
mutation {
upsert_repos(
Expand Down Expand Up @@ -273,6 +288,7 @@ upsertRepoQuery utc extendedRepo =
& var "updated_utc" (getTimestamp GH.repoUpdatedAt)
& var "crawled_utc" utc


-- | TODO: Use GraphQL variables instead of string interpolation
insertRepoQuery :: Text -> ExtendedRepo -> Text
insertRepoQuery utc extendedRepo =
Expand All @@ -285,7 +301,7 @@ insertRepoQuery utc extendedRepo =
<&> iso8601Show
& fromMaybe ""
& T.pack
in
in
[r|
mutation InsertRepo {
insert_repos(objects: [
Expand Down Expand Up @@ -325,7 +341,8 @@ insertRepoQuery utc extendedRepo =
& var "updated_utc" (getTimestamp GH.repoUpdatedAt)
& var "crawled_utc" utc

-- | Save the repo in Airsequel via a POST request executed by http-client

-- | Save the repo in Airsequel via a POST request executed by http-client
saveRepoInAirsequel :: SaveStrategy -> ExtendedRepo -> IO ()
saveRepoInAirsequel saveStrategy extendedRepo = do
dbEndpoint <- loadDbEndpoint
Expand All @@ -350,9 +367,9 @@ saveRepoInAirsequel saveStrategy extendedRepo = do
)
]
, requestBody =
RequestBodyLBS
$ encode
$ object ["query" .= deleteRepoQuery extendedRepo]
RequestBodyLBS $
encode $
object ["query" .= deleteRepoQuery extendedRepo]
}
deleteResponse <- httpLbs deleteRequest manager
print deleteResponse
Expand All @@ -366,14 +383,15 @@ saveRepoInAirsequel saveStrategy extendedRepo = do
, ("Authorization", "Bearer " <> airseqWriteToken & encodeUtf8)
]
, requestBody =
RequestBodyLBS
$ encode
$ object ["query" .= insertRepoQuery now extendedRepo]
RequestBodyLBS $
encode $
object ["query" .= insertRepoQuery now extendedRepo]
}
insertResponse <- httpLbs insertRequest manager
print insertResponse

{- | Loads a single repo from GitHub, adds number of commits,

{-| Loads a single repo from GitHub, adds number of commits,
| and saves it to Airsequel
-}
loadAndSaveRepo :: Maybe Text -> SaveStrategy -> Text -> Text -> IO ()
Expand All @@ -396,13 +414,15 @@ loadAndSaveRepo ghTokenMb saveStrategy owner name = do
putText $ formatRepo extendedRepo
saveRepoInAirsequel saveStrategy extendedRepo


data GqlResponse = GqlResponse
{ repos :: [Repo]
, errorsMb :: Maybe Value
, nextCursorMb :: Maybe Text
}
deriving (Show, Eq, Generic)


instance FromJSON GqlResponse where
parseJSON = withObject "GqlResponse" $ \o -> do
data_ <- o .: "data"
Expand All @@ -421,6 +441,7 @@ instance FromJSON GqlResponse where
, nextCursorMb
}


getGhHeaders :: (P.IsString a) => Maybe Text -> [(a, P.ByteString)]
getGhHeaders tokenMb =
[ ("User-Agent", "repos-uploader")
Expand All @@ -432,13 +453,14 @@ getGhHeaders tokenMb =
[("Authorization", "Bearer " <> token & encodeUtf8)]
Nothing -> []

execGqlQuery ::
Text ->
Maybe Text ->
Text ->
Maybe Text ->
[ExtendedRepo] ->
IO [ExtendedRepo]

execGqlQuery
:: Text
-> Maybe Text
-> Text
-> Maybe Text
-> [ExtendedRepo]
-> IO [ExtendedRepo]
execGqlQuery apiEndpoint ghTokenMb query nextCursorMb initialRepos = do
manager <- newManager tlsManagerSettings

Expand All @@ -448,19 +470,19 @@ execGqlQuery apiEndpoint ghTokenMb query nextCursorMb initialRepos = do
{ method = "POST"
, requestHeaders = getGhHeaders ghTokenMb
, requestBody =
RequestBodyLBS
$ encode
$ object
[ "query"
.= ( query
& var
"optionalAfter"
( nextCursorMb
<&> (\cursor -> "after: \"" <> cursor <> "\"")
& fromMaybe ""
)
)
]
RequestBodyLBS $
encode $
object
[ "query"
.= ( query
& var
"optionalAfter"
( nextCursorMb
<&> (\cursor -> "after: \"" <> cursor <> "\"")
& fromMaybe ""
)
)
]
}
response <- httpLbs request manager
let gqlResult :: Either String GqlResponse =
Expand Down Expand Up @@ -558,6 +580,7 @@ loadAndSaveReposViaSearch githubToken searchQuery = do
Nothing
[]


main :: IO ()
main = do
ghTokenMb <- loadGitHubToken
Expand Down
8 changes: 7 additions & 1 deletion app/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import Data.Aeson.Types (parseJSON)
import Data.Time (UTCTime)
import GitHub.Data qualified as GH


emptyOwner :: GH.SimpleOwner
emptyOwner =
GH.SimpleOwner
Expand All @@ -42,6 +43,7 @@ emptyOwner =
, GH.simpleOwnerType = GH.OwnerUser
}


emptyRepo :: GH.Repo
emptyRepo =
GH.Repo
Expand Down Expand Up @@ -79,12 +81,14 @@ emptyRepo =
, GH.repoSshUrl = Nothing
}


mapMSequentially :: Int -> (a -> IO b) -> [a] -> IO [b]
mapMSequentially delayInMs f xs = do
let delayM = liftIO $ threadDelay (delayInMs * 1000)
mapM (\x -> f x <* delayM) xs

{- | To make loading data from GitHub GraphQL API easier

{-| To make loading data from GitHub GraphQL API easier
| we also have this simpler (in comparison to GH.Repo) data type
-}
data RepoObject = RepoObject
Expand All @@ -101,6 +105,7 @@ data RepoObject = RepoObject
}
deriving (Show, Eq, Generic)


instance FromJSON RepoObject where
parseJSON = withObject "RepoObject" $ \o -> do
owner <- o .: "owner" >>= (.: "login")
Expand All @@ -116,6 +121,7 @@ instance FromJSON RepoObject where

pure RepoObject{..}


repoObjectToRepo :: RepoObject -> GH.Repo
repoObjectToRepo repoObj =
emptyRepo
Expand Down
12 changes: 12 additions & 0 deletions fourmolu.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
indentation: 2
function-arrows: leading
comma-style: leading
import-export-style: diff-friendly
indent-wheres: true
record-brace-space: false
newlines-between-decls: 2
haddock-style: multi-line-compact
let-style: auto
in-style: left-align
respectful: true
unicode: never

0 comments on commit c379954

Please sign in to comment.