This repository has been archived by the owner on Apr 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move git contrib functions to new module
- Loading branch information
1 parent
3f60e64
commit 8073b27
Showing
3 changed files
with
73 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -184,6 +184,7 @@ library | |
Strategy.Ruby.BundleShow | ||
Strategy.Ruby.GemfileLock | ||
Types | ||
VCS.Git | ||
|
||
hs-source-dirs: src | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
module VCS.Git | ||
( gitLogCmd, | ||
fetchGitContributors, | ||
) | ||
where | ||
|
||
import App.Fossa.FossaAPIV1 (Contributors (..)) | ||
import qualified Control.Carrier.Diagnostics as Diag | ||
import Data.ByteString.Lazy (toStrict) | ||
import qualified Data.Map as M | ||
import Data.Maybe (mapMaybe) | ||
import qualified Data.Text as T | ||
import qualified Data.Text.Encoding as TE | ||
import Data.Time | ||
import Data.Time.Format.ISO8601 (iso8601Show) | ||
import Effect.Exec | ||
import Prologue | ||
|
||
gitLogCmd :: UTCTime -> Command | ||
gitLogCmd now = | ||
Command | ||
{ cmdName = "git", | ||
cmdArgs = ["log", "--since", sinceArg, "--date=short", "--format=%ae|%cd"], | ||
cmdAllowErr = Never | ||
} | ||
where | ||
sinceArg = iso8601Show $ utctDay wayBack | ||
delta = nominalDay * (-90) | ||
wayBack = addUTCTime delta now | ||
|
||
fetchGitContributors :: | ||
( Has Diag.Diagnostics sig m, | ||
Has Exec sig m, | ||
MonadIO m | ||
) => | ||
Path x Dir -> | ||
m Contributors | ||
fetchGitContributors basedir = do | ||
now <- liftIO getCurrentTime | ||
rawContrib <- execThrow basedir $ gitLogCmd now | ||
textContrib <- Diag.fromEitherShow . TE.decodeUtf8' $ toStrict rawContrib | ||
pure . Contributors | ||
. M.map (T.pack . iso8601Show) | ||
. M.fromListWith max | ||
. mapMaybe readLine | ||
$ T.lines textContrib | ||
where | ||
readLine :: Text -> Maybe (Text, Day) | ||
readLine entry = do | ||
let (email, textDate) = splitOnceOn "|" entry | ||
date <- parseTimeM True defaultTimeLocale "%Y-%-m-%-d" $ T.unpack textDate | ||
Just (email, date) | ||
|
||
splitOnceOn :: Text -> Text -> (Text, Text) | ||
splitOnceOn needle haystack = (head, strippedTail) | ||
where | ||
len = T.length needle | ||
(head, tail) = T.breakOn needle haystack | ||
strippedTail = T.drop len tail |