func newScanCommand()

in internal/cmd/scan.go [42:167]


func newScanCommand() *cobra.Command {
	cliOptions := &platformcmd.CliOptions{}
	c := &cobra.Command{
		Use:   "scan",
		Short: "Scan project with Qodana",
		Long: `Scan a project with Qodana. It runs one of Qodana's Docker images (https://www.jetbrains.com/help/qodana/docker-images.html) and reports the results.

Note that most options can be configured via qodana.yaml (https://www.jetbrains.com/help/qodana/qodana-yaml.html) file.
But you can always override qodana.yaml options with the following command-line options.
`,
		Run: func(cmd *cobra.Command, args []string) {
			qdenv.InitializeQodanaGlobalEnv(cliOptions)

			ctx := cmd.Context()

			commonCtx := commoncontext.Compute(
				cliOptions.Linter,
				cliOptions.Ide,
				cliOptions.Image,
				cliOptions.WithinDocker,
				cliOptions.CacheDir,
				cliOptions.ResultsDir,
				cliOptions.ReportDir,
				qdenv.GetQodanaGlobalEnv(qdenv.QodanaToken),
				cliOptions.ClearCache,
				cliOptions.ProjectDir,
				cliOptions.RepositoryRoot,
				cliOptions.ConfigName,
			)
			oldReportUrl := cloud.GetReportUrl(commonCtx.ResultsDir)
			checkProjectDir(commonCtx.ProjectDir)

			preparedHost := startup.PrepareHost(commonCtx)

			effectiveConfigFiles := effectiveconfig.Files{}
			qodanaYamlConfig := corescan.QodanaYamlConfig{}
			if !commonCtx.Analyzer.IsContainer() {
				var err error
				localQodanaYamlFullPath := qdyaml.GetLocalNotEffectiveQodanaYamlFullPath(
					commonCtx.ProjectDir,
					cliOptions.ConfigName,
				)

				effectiveConfigDir, cleanup, err := utils.CreateTempDir("qd-effective-config")
				if err != nil {
					log.Fatalf("Failed to create effective config directory: %v", err)
				}
				defer cleanup()

				effectiveConfigFiles, err = effectiveconfig.CreateEffectiveConfigFiles(
					localQodanaYamlFullPath,
					cliOptions.GlobalConfigurationsDir,
					cliOptions.GlobalConfigurationId,
					preparedHost.Prod.JbrJava(),
					effectiveConfigDir,
					commonCtx.LogDir(),
				)
				if err != nil {
					log.Fatalf("Failed to load Qodana configuration %s", err)
				}
				if effectiveConfigFiles.EffectiveQodanaYamlPath != "" {
					yaml := qdyaml.LoadQodanaYamlByFullPath(effectiveConfigFiles.EffectiveQodanaYamlPath)
					qodanaYamlConfig = corescan.YamlConfig(yaml)
				}
			}
			scanContext := corescan.CreateContext(
				*cliOptions,
				commonCtx,
				preparedHost,
				qodanaYamlConfig,
				effectiveConfigFiles.ConfigDir,
			)

			exitCode := core.RunAnalysis(ctx, scanContext)
			if qdenv.IsContainer() {
				err := platform.ChangeResultsPermissionsRecursively(scanContext.ResultsDir())
				if err != nil {
					msg.ErrorMessage("Unable to change permissions in %s: %s", scanContext.ResultsDir(), err)
				}
			}
			checkExitCode(exitCode, scanContext)
			newReportUrl := cloud.GetReportUrl(scanContext.ResultsDir())
			platform.ProcessSarif(
				filepath.Join(scanContext.ResultsDir(), commoncontext.QodanaSarifName),
				scanContext.AnalysisId(),
				newReportUrl,
				scanContext.PrintProblems(),
				scanContext.GenerateCodeClimateReport(),
				scanContext.SendBitBucketInsights(),
			)

			showReport := scanContext.ShowReport()
			if msg.IsInteractive() {
				showReport = msg.AskUserConfirm("Do you want to open the latest report")
			}

			if newReportUrl != oldReportUrl && newReportUrl != "" && !qdenv.IsContainer() {
				msg.SuccessMessage("Report is successfully uploaded to %s", newReportUrl)
			}

			if showReport {
				commoncontext.ShowReport(scanContext.ResultsDir(), scanContext.ReportDir(), scanContext.Port())
			} else if !qdenv.IsContainer() && msg.IsInteractive() {
				msg.WarningMessage(
					"To view the Qodana report later, run %s in the current directory or add %s flag to %s",
					msg.PrimaryBold("qodana show"),
					msg.PrimaryBold("--show-report"),
					msg.PrimaryBold("qodana scan"),
				)
			}

			if exitCode == utils.QodanaFailThresholdExitCode {
				msg.EmptyMessage()
				msg.ErrorMessage("The number of problems exceeds the fail threshold")
				os.Exit(exitCode)
			}
		},
	}

	err := platformcmd.ComputeFlags(c, cliOptions)
	if err != nil {
		return nil
	}

	return c
}