diff options
author | Ben Sima <ben@bsima.me> | 2020-12-24 13:33:10 -0500 |
---|---|---|
committer | Ben Sima <ben@bsima.me> | 2020-12-24 13:33:10 -0500 |
commit | 6a4a8aa37044d578c95dea145b9605908b8dc28d (patch) | |
tree | 806b71d8c9bd9ffebbd765a032b372baaddc1665 | |
parent | 20aad0c282c0c3bd685863252b3c464b7d74a9e8 (diff) |
Devalloc: refactoring and organizing
-rw-r--r-- | Biz/Devalloc.hs | 205 |
1 files changed, 106 insertions, 99 deletions
diff --git a/Biz/Devalloc.hs b/Biz/Devalloc.hs index af83535..d1172ee 100644 --- a/Biz/Devalloc.hs +++ b/Biz/Devalloc.hs @@ -82,8 +82,8 @@ main = Exception.bracket startup shutdown run run :: (Config, Wai.Application) -> IO () run (cfg, app) = Warp.run (port cfg) (logStdout app) -test :: IO () -test = test_analyzeGitHub >> pure () +test :: IO Analysis +test = test_analyzeGitHub data Config = Config { port :: Warp.Port, @@ -151,6 +151,7 @@ instance Lucid.ToHtml a => Lucid.ToHtml (HtmlApp a) where data Paths path = Paths { home :: path :- Get '[HTML] (HtmlApp Home), githubAuth :: + -- auth/github/callback?code=... path :- "auth" :> "github" :> "callback" :> QueryParam "code" Text :> Get '[HTML] (HtmlApp SelectRepo), @@ -183,13 +184,43 @@ htmlApp cfg oAuthArgs = Biz.Look.fuckingStyle "body" Clay.? Biz.Look.fontStack +-- | The front page pitch. Eventually I'd like to load the content from markdown +-- files or some other store of data so I can A/B test. newtype Home = Home OAuthArgs instance Lucid.ToHtml Home where toHtmlRaw = Lucid.toHtml - toHtml (Home oAuthArgs) = - Lucid.toHtml <| pitch oAuthArgs + toHtml (Home oAuthArgs) = do + Lucid.h1_ "Devalloc" + Lucid.p_ + "Devalloc analyzes your codebase trends, finds patterns \ + \ in how your developers work, and protects against tech debt." + Lucid.p_ "Just hook it up to your CI system - it will warn you when it finds a problem." + Lucid.toHtml <| loginButton oAuthArgs + Lucid.h2_ "Identify blackholes in your codebase" + Lucid.p_ + "What if none of your active employees have touched some part of the codebase? \ + \ This happens too often with legacy code, and then it turns into a huge source of tech debt. \ + \ Devalloc finds these \"blackholes\" and warns you about them so you can be proactive in eliminating tech debt." + Lucid.toHtml <| loginButton oAuthArgs + Lucid.h2_ "Protect against lost knowledge" + Lucid.p_ "Not everyone can know every part of a codebase. By finding pieces of code that only 1 or 2 people have touched, devalloc identifes siloed knowledge. This allows you to protect against the risk of this knowledge leaving the company if an employee leaves." + Lucid.toHtml <| loginButton oAuthArgs + Lucid.h2_ "Don't just measure code coverage - also know your dev coverage" + Lucid.p_ "No matter how smart your employees are, if you are under- or over-utilizing your developers then you will never get optimal performance from your team." + Lucid.ul_ <| do + Lucid.li_ "Find developer hot spots in your code: which pieces of code get continually rewritten, taking up valuable dev time?" + Lucid.li_ "Know how your devs work best: which ones have depth of knowledge, and which ones have breadth?" + + Lucid.p_ "(Paid only)" + Lucid.toHtml <| loginButton oAuthArgs + Lucid.h2_ "See how your teams *actually* organize themselves with cluster analysis" + Lucid.p_ "Does your team feel splintered or not cohesive? Which developers work best together? Devalloc analyzes the collaboration patterns between devs and helps you form optimal pairings and teams based on shared code and mindspace." + Lucid.p_ "(Paid only)" + Lucid.toHtml <| loginButton oAuthArgs +-- | A type for parsing JSON auth responses, used in 'getAccessToken' below. +-- Should be moved to Biz.Auth with others. data OAuthResponse = OAuthResponse { access_token :: Text, scope :: Text, @@ -227,10 +258,54 @@ auth OAuthArgs {..} (Just code) = <> "code" =: code <> "state" =: githubState +-- | This view presents a list of repos to select for analysis. +newtype SelectRepo = SelectRepo (Vector GitHub.Repo) + +instance Lucid.ToHtml SelectRepo where + toHtmlRaw = Lucid.toHtml + toHtml (SelectRepo repos) = do + Lucid.h1_ "Select a repo to analyze" + Lucid.ul_ <| forM_ (Vector.toList repos) <| \repo -> + Lucid.li_ + <. Lucid.a_ + [ Lucid.linkHref_ "/" + <| fieldLink + githubAnalysis + (GitHub.untagName <| GitHub.simpleOwnerLogin <| GitHub.repoOwner repo) + (GitHub.untagName <| GitHub.repoName repo) + ] + <. Lucid.toHtml + <. GitHub.untagName + <| GitHub.repoName repo + +-- * parts + +-- | Utility for turning a list of tuples into a URL querystring. +encodeParams :: [(Text, Text)] -> Text +encodeParams = + Encoding.decodeUtf8 + <. LBS.toStrict + <. Web.FormUrlEncoded.urlEncodeParams + +-- | Login button for GitHub. +loginButton :: OAuthArgs -> Lucid.Html () +loginButton OAuthArgs {..} = + Lucid.a_ + [ Lucid.href_ + <| "https://github.com/login/oauth/authorize?" + <> encodeParams + [ ("client_id", githubClientId), + ("state", githubState) + ] + ] + "Get Started with GitHub" + -- * analysis +-- | The result of analyzing a git repo. data Analysis = Analysis - { bareRepo :: FilePath, + { -- | Where the repo is stored on the local disk. + bareRepo :: FilePath, -- | A path with no active contributors blackholes :: [Text], -- | A path with < 3 active contributors @@ -238,8 +313,11 @@ data Analysis = Analysis -- | Files that have not been touched in 6 months stale :: [Text], -- | Total score for the repo - score :: Int + score :: Int, + -- | List of all the active users we care about + activeAuthors :: [Text] } + deriving (Show) instance Lucid.ToHtml Analysis where toHtmlRaw = Lucid.toHtml @@ -287,14 +365,18 @@ analyze activeAuthors bareRepo = do | (path, authors_) <- authorMap, null (map third authors_ `List.intersect` activeAuthors) ], - liabilities = [], + liabilities = + [ Text.pack path + | (path, authors_) <- authorMap, + length (map third authors_ `List.intersect` activeAuthors) < 3 + ], stale = [], -- actually a map of path->staleness score = 10, .. } - -third :: (a, b, c) -> c -third (_, _, a) = a + where + third :: (a, b, c) -> c + third (_, _, a) = a -- | Given a git dir and a path inside the git repo, return a list of tuples -- with number of commits and author. @@ -331,6 +413,7 @@ authorsFor gitDir path = do ) -- | Clones a repo from GitHub and does the analysis. +-- TODO: break this up into fetchGitHub and analyze functions. analyzeGitHub :: Config -> -- | GitHub owner @@ -359,9 +442,10 @@ analyzeGitHub cfg o r = do <| GitHub.mkName (Proxy :: Proxy GitHub.User) o ) -- assume the only active author is the owner, for now + -- TODO: should be userEmail but that requires authentication? let activeAuthors = [require "user email" <| GitHub.userName user] Right repo <- GitHub.github () (GitHub.repositoryR ghOwner ghRepo) - bareRepo <- gitBareClone cfg <. GitHub.getUrl <| GitHub.repoHtmlUrl repo + bareRepo <- fetchBareRepo cfg <. GitHub.getUrl <| GitHub.repoHtmlUrl repo analyze activeAuthors bareRepo where ghOwner = GitHub.mkName (Proxy :: Proxy GitHub.Owner) o @@ -370,95 +454,18 @@ analyzeGitHub cfg o r = do test_analyzeGitHub :: IO Analysis test_analyzeGitHub = analyzeGitHub Envy.defConfig "bsima" "bin" --- | Clone the repo to @<Config.depo>/<url>@. Return the full path to the local --- repo. -gitBareClone :: Config -> Text -> IO FilePath -gitBareClone Config {depo} url = do - worktreeExists <- Directory.doesPathExist worktree - let args = - if worktreeExists - then ["--git-dir", worktree, "fetch", "origin"] - else ["clone", "--bare", "--", Text.unpack url, worktree] - Process.callProcess "git" args - return worktree +-- | Clone the repo to @<Config.depo>/<url>@. If repo already exists, just do a +-- @git fetch@. Returns the full path to the local repo. +fetchBareRepo :: Config -> Text -> IO FilePath +fetchBareRepo Config {depo} url = + Directory.doesPathExist worktree + >>= fetchOrClone + >> return worktree where + fetchOrClone True = + Process.callProcess "git" ["--git-dir", worktree, "fetch", "origin"] + fetchOrClone False = + Process.callProcess "git" ["clone", "--bare", "--", Text.unpack url, worktree] removeScheme :: Text -> FilePath removeScheme u = Text.unpack <. Text.dropWhile (== '/') <. snd <| Text.breakOn "//" u worktree = depo </> removeScheme url <.> "git" - --- * parts - -encodeParams :: [(Text, Text)] -> Text -encodeParams = - Encoding.decodeUtf8 - <. LBS.toStrict - <. Web.FormUrlEncoded.urlEncodeParams - -newtype SelectRepo = SelectRepo (Vector GitHub.Repo) - -instance Lucid.ToHtml SelectRepo where - toHtmlRaw = Lucid.toHtml - toHtml (SelectRepo repos) = - Lucid.toHtml <| do - Lucid.h1_ "Select a repo to analyze" - selectRepo repos - -selectRepo :: Vector GitHub.Repo -> Lucid.Html () -selectRepo = Lucid.ul_ <. mapM_ render <. Vector.toList - where - render :: GitHub.Repo -> Lucid.Html () - render repo = - Lucid.li_ - <. Lucid.a_ - [ Lucid.linkHref_ "/" - <| fieldLink - githubAnalysis - (GitHub.untagName <| GitHub.simpleOwnerLogin <| GitHub.repoOwner repo) - (GitHub.untagName <| GitHub.repoName repo) - ] - <. Lucid.toHtml - <. GitHub.untagName - <| GitHub.repoName repo - -loginButton :: OAuthArgs -> Lucid.Html () -loginButton OAuthArgs {..} = - Lucid.a_ - [ Lucid.href_ - <| "https://github.com/login/oauth/authorize?" - <> encodeParams - [ ("client_id", githubClientId), - ("state", githubState) - ] - ] - "Get Started with GitHub" - -pitch :: OAuthArgs -> Lucid.Html () -pitch oAuthArgs = - Lucid.div_ <| do - Lucid.h1_ "Devalloc" - Lucid.p_ - "Devalloc analyzes your codebase trends, finds patterns \ - \ in how your developers work, and protects against tech debt." - Lucid.p_ "Just hook it up to your CI system - it will warn you when it finds a problem." - loginButton oAuthArgs - Lucid.h2_ "Identify blackholes in your codebase" - Lucid.p_ - "What if none of your active employees have touched some part of the codebase? \ - \ This happens too often with legacy code, and then it turns into a huge source of tech debt. \ - \ Devalloc finds these \"blackholes\" and warns you about them so you can be proactive in eliminating tech debt." - loginButton oAuthArgs - Lucid.h2_ "Protect against lost knowledge" - Lucid.p_ "Not everyone can know every part of a codebase. By finding pieces of code that only 1 or 2 people have touched, devalloc identifes siloed knowledge. This allows you to protect against the risk of this knowledge leaving the company if an employee leaves." - loginButton oAuthArgs - Lucid.h2_ "Don't just measure code coverage - also know your dev coverage" - Lucid.p_ "No matter how smart your employees are, if you are under- or over-utilizing your developers then you will never get optimal performance from your team." - Lucid.ul_ <| do - Lucid.li_ "Find developer hot spots in your code: which pieces of code get continually rewritten, taking up valuable dev time?" - Lucid.li_ "Know how your devs work best: which ones have depth of knowledge, and which ones have breadth?" - - Lucid.p_ "(Paid only)" - loginButton oAuthArgs - Lucid.h2_ "See how your teams *actually* organize themselves with cluster analysis" - Lucid.p_ "Does your team feel splintered or not cohesive? Which developers work best together? Devalloc analyzes the collaboration patterns between devs and helps you form optimal pairings and teams based on shared code and mindspace." - Lucid.p_ "(Paid only)" - loginButton oAuthArgs |