Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made CI checks during telemetry a bit more thorough. #1586

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions waspc/cli/src/Wasp/Cli/Command/Telemetry/Project.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Wasp.Cli.Command.Telemetry.Project
where

import Control.Monad (void, when)
import Control.Monad.Except (catchError)
import Crypto.Hash (SHA256 (..), hashWith)
import Data.Aeson ((.=))
import qualified Data.Aeson as Aeson
Expand All @@ -28,6 +29,7 @@ import Paths_waspc (version)
import StrongPath (Abs, Dir, File', Path')
import qualified StrongPath as SP
import qualified System.Environment as ENV
import qualified System.IO as SIO
import qualified System.Info
import Wasp.Cli.Command (Command)
import qualified Wasp.Cli.Command.Call as Command.Call
Expand Down Expand Up @@ -76,17 +78,34 @@ considerSendingData telemetryCacheDirPath userSignature projectHash cmdCall = do
}

getTelemetryContext :: IO String
getTelemetryContext =
getTelemetryContext = do
unwords . filter (not . null)
<$> sequence
[ fromMaybe "" <$> ENV.lookupEnv "WASP_TELEMETRY_CONTEXT",
checkIfOnCi <&> \case True -> "CI"; False -> ""
checkIfOnCi <&> \case True -> "CI"; False -> "",
-- NOTE(martin): We are not 100% sure how correctly does `hIsTerminalDevice` detect
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Martinsos can this call fail for what ever reason? Do we need exception handling or smth here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point -> so I didn't think this one is particularly likely to throw an error (but every IO operation can throw an error!), so I haven't taken special measures here.
We do have one big catch already for telemetry, so any fail in it can't crash the CLI but will just not send telemetry.

That said, this function is a bit of a mystery to us, and it would be a shame if it started failing our telemetry silently, so better to be safe than sorry I think, so now prompted by your comment I added an additional catch to it that will interpret any error as "True".

-- if shell is non-interactive or not, so let's use this info with regard to that and
-- revise this in the future once we have more data.
(SIO.hIsTerminalDevice SIO.stdout `catchError` const (return True))
<&> \case True -> ""; False -> "NON_INTERACTIVE_SHELL"
]
where
-- This function was inspired by https://github.com/watson/ci-info/blob/master/index.js .
checkIfOnCi :: IO Bool
checkIfOnCi =
any checkIfEnvValueIsTruthy <$> mapM ENV.lookupEnv ["CI", "BUILD_ID", "CI_BUILD_ID"]
any checkIfEnvValueIsTruthy
<$> mapM
ENV.lookupEnv
[ "BUILD_ID", -- Jenkins, Codeship
"BUILD_NUMBER", -- Jenkins, TeamCity
"CI", -- Github actions, Travis CI, CircleCI, Cirrus CI, Gitlab CI, Appveyor, Codeship, dsari
"CI_APP_ID", -- Appflow
"CI_BUILD_ID", -- Appflow
"CI_BUILD_NUMBER", -- Appflow
"CI_NAME", -- Codeship and others
"CONTINUOUS_INTEGRATION", -- Travis CI, Cirrus CI
"RUN_ID" -- TaskCluster, dsari
]

checkIfEnvValueIsTruthy :: Maybe String -> Bool
checkIfEnvValueIsTruthy Nothing = False
Expand Down
Loading