Skip to content

Commit

Permalink
[#45] Add sync function for synchronizing all repositories and issues
Browse files Browse the repository at this point in the history
Resolves #45
  • Loading branch information
rashadg1030 committed Aug 17, 2019
1 parent f0363e0 commit ce4549d
Showing 1 changed file with 46 additions and 4 deletions.
50 changes: 46 additions & 4 deletions src/IW/Sync/Update.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,36 @@ data and insert it into the database.
-}

module IW.Sync.Update
( syncRepos
( sync
) where

import Control.Monad.IO.Unlift (MonadUnliftIO)
import UnliftIO.Async (mapConcurrently_)

import IW.App (WithError)
import IW.Core.Repo (Repo (..))
import IW.Db (WithDb, upsertRepos, updateRepoCategories)
import IW.Db (WithDb, upsertRepos, updateRepoCategories, upsertIssues)
import IW.Effects.Cabal (MonadCabal (..), getCabalCategories)
import IW.Sync.Search (searchAllHaskellRepos)
import IW.Sync.Search (searchAllHaskellRepos, searchAllHaskellIssues)
import IW.Time (getToday)


sync
:: forall env m.
( MonadCabal m
, MonadUnliftIO m
, WithDb env m
, WithLog env m
, WithError m
)
=> Integer -- ^ The starting date interval used in the search function
-> m ()
sync interval = do
log I "Starting synchronization of all repositories and issues..."
syncRepos interval
syncIssues interval
log I "All repositories and issues successfully synchronized"

-- | This function fetches all repos from the GitHub API, downloads their @.cabal@ files,
-- and upserts them into the database.
syncRepos
Expand All @@ -31,12 +47,17 @@ syncRepos
=> Integer -- ^ The starting date interval used in the search function
-> m ()
syncRepos interval = do
log I "Starting synchronization of repositories..."
today <- liftIO getToday
log I "Searching for all Haskell repositories..."
repos <- searchAllHaskellRepos today interval
log I "Upserting repositories into the database..."
upsertRepos repos
log I "Updating all repositories' category fields..."
mapConcurrently_ syncCategories repos
log I "Repositories successfully synchronized"

-- | This function takes a @Repo@ and attempts to download its @.cabal@ file.
-- | This function takes a @Repo@ and updates its category field in the database.
syncCategories
:: forall env m.
( MonadCabal m
Expand All @@ -48,5 +69,26 @@ syncCategories
=> Repo
-> m ()
syncCategories Repo{..} = do
log I "Updating repository's categories..."
categories <- getCabalCategories repoOwner repoName
updateRepoCategories repoOwner repoName categories
log I "Categories successfully updated"

-- | This function fetches all issues from the GitHub API and upserts them into the database.
syncIssues
:: forall env m.
( MonadUnliftIO m
, WithDb env m
, WithLog env m
, WithError m
)
=> Integer
-> m ()
syncIssues interval = do
log I "Starting synchronization of issues..."
today <- liftIO getToday
log I "Searching for all Haskell issues..."
issues <- searchAllHaskellIssues today interval
log I "Upserting issues into the database..."
upsertIssues issues
log I "Issues successfully synchronized"

0 comments on commit ce4549d

Please sign in to comment.