|
| 1 | +/* |
| 2 | +Copyright © 2025 NAME HERE <EMAIL ADDRESS> |
| 3 | +*/ |
| 4 | +package cmd |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "mona-actions/gh-migration-validator/internal/export" |
| 9 | + "mona-actions/gh-migration-validator/internal/validator" |
| 10 | + "os" |
| 11 | + |
| 12 | + "github.com/spf13/cobra" |
| 13 | + "github.com/spf13/viper" |
| 14 | +) |
| 15 | + |
| 16 | +// validateFromExportCmd represents the validate-from-export command |
| 17 | +var validateFromExportCmd = &cobra.Command{ |
| 18 | + Use: "validate-from-export", |
| 19 | + Short: "Validate target repository against exported source data", |
| 20 | + Long: `Validate a target repository against previously exported source repository data. |
| 21 | +
|
| 22 | +This command allows you to validate a migration by comparing the target repository |
| 23 | +against a point-in-time snapshot of the source repository that was previously |
| 24 | +exported using the 'export' command. |
| 25 | +
|
| 26 | +This is useful for: |
| 27 | +- Validating migrations against an active repository that may have changed since migration |
| 28 | +- Comparing target repositories to source state at migration time |
| 29 | +- Ensuring migration integrity when source data may have changed |
| 30 | +
|
| 31 | +The validation compares the same metrics as the standard validate command: |
| 32 | +- Issues count |
| 33 | +- Pull requests count (open, closed, merged) |
| 34 | +- Tags count |
| 35 | +- Releases count |
| 36 | +- Commits count |
| 37 | +- Latest commit hash`, |
| 38 | + Run: func(cmd *cobra.Command, args []string) { |
| 39 | + // Get parameters from flags |
| 40 | + exportFile := cmd.Flag("export-file").Value.String() |
| 41 | + targetOrganization := cmd.Flag("target-organization").Value.String() |
| 42 | + targetToken := cmd.Flag("target-token").Value.String() |
| 43 | + targetHostname := cmd.Flag("target-hostname").Value.String() |
| 44 | + targetRepo := cmd.Flag("target-repo").Value.String() |
| 45 | + markdownTable, err := cmd.Flags().GetBool("markdown-table") |
| 46 | + if err != nil { |
| 47 | + fmt.Printf("Failed to parse 'markdown-table' flag: %v\n", err) |
| 48 | + os.Exit(1) |
| 49 | + } |
| 50 | + |
| 51 | + // Only set ENV variables if flag values are provided (not empty) |
| 52 | + if targetToken != "" { |
| 53 | + os.Setenv("GHMV_TARGET_TOKEN", targetToken) |
| 54 | + } |
| 55 | + if targetHostname != "" { |
| 56 | + os.Setenv("GHMV_TARGET_HOSTNAME", targetHostname) |
| 57 | + } |
| 58 | + if markdownTable { |
| 59 | + os.Setenv("GHMV_MARKDOWN_TABLE", "true") |
| 60 | + } |
| 61 | + |
| 62 | + // Bind ENV variables in Viper (for optional parameters that can use env vars) |
| 63 | + viper.BindEnv("TARGET_TOKEN") |
| 64 | + viper.BindEnv("TARGET_HOSTNAME") |
| 65 | + viper.BindEnv("TARGET_PRIVATE_KEY") |
| 66 | + viper.BindEnv("TARGET_APP_ID") |
| 67 | + viper.BindEnv("TARGET_INSTALLATION_ID") |
| 68 | + viper.BindEnv("MARKDOWN_TABLE") |
| 69 | + |
| 70 | + // Validate required parameters (using flag values directly for required flags) |
| 71 | + if err := checkExportValidationVars(exportFile); err != nil { |
| 72 | + fmt.Printf("Export validation configuration failed: %v\n", err) |
| 73 | + os.Exit(1) |
| 74 | + } |
| 75 | + |
| 76 | + // Load export data from file |
| 77 | + exportData, err := export.LoadExportData(exportFile) |
| 78 | + if err != nil { |
| 79 | + fmt.Printf("Failed to load export file: %v\n", err) |
| 80 | + os.Exit(1) |
| 81 | + } |
| 82 | + |
| 83 | + // Initialize API for target |
| 84 | + initializeAPI() |
| 85 | + |
| 86 | + // Create validator and perform validation |
| 87 | + migrationValidator := validator.New(ghAPI) |
| 88 | + |
| 89 | + // Set source data from export instead of fetching from API |
| 90 | + migrationValidator.SetSourceDataFromExport(&exportData.Repository) |
| 91 | + |
| 92 | + // Perform validation against target (now returns results directly) |
| 93 | + results, err := migrationValidator.ValidateFromExport(targetOrganization, targetRepo) |
| 94 | + if err != nil { |
| 95 | + fmt.Printf("Validation failed: %v\n", err) |
| 96 | + os.Exit(1) |
| 97 | + } |
| 98 | + |
| 99 | + // Display results using existing method |
| 100 | + migrationValidator.PrintValidationResults(results) |
| 101 | + }, |
| 102 | +} |
| 103 | + |
| 104 | +func init() { |
| 105 | + // Add validate-from-export command to root |
| 106 | + rootCmd.AddCommand(validateFromExportCmd) |
| 107 | + |
| 108 | + // Define flags specific to validate-from-export command |
| 109 | + validateFromExportCmd.Flags().StringP("export-file", "e", "", "Path to the exported JSON file to use as source data") |
| 110 | + validateFromExportCmd.MarkFlagRequired("export-file") |
| 111 | + |
| 112 | + validateFromExportCmd.Flags().StringP("target-organization", "t", "", "Target Organization to validate against") |
| 113 | + validateFromExportCmd.MarkFlagRequired("target-organization") |
| 114 | + |
| 115 | + validateFromExportCmd.Flags().StringP("target-token", "b", "", "Target Organization GitHub token. Scopes: read:org, read:user, user:email") |
| 116 | + |
| 117 | + validateFromExportCmd.Flags().StringP("target-hostname", "v", "", "GitHub Enterprise target hostname url (optional) Ex. https://github.example.com") |
| 118 | + |
| 119 | + validateFromExportCmd.Flags().String("target-repo", "", "Target repository name to validate (just the repo name, not owner/repo)") |
| 120 | + validateFromExportCmd.MarkFlagRequired("target-repo") |
| 121 | + |
| 122 | + validateFromExportCmd.Flags().BoolP("markdown-table", "m", false, "Output results in markdown table format") |
| 123 | +} |
| 124 | + |
| 125 | +// checkExportValidationVars validates the configuration for validate-from-export command |
| 126 | +func checkExportValidationVars(exportFile string) error { |
| 127 | + |
| 128 | + // Check if export file exists |
| 129 | + if _, err := os.Stat(exportFile); os.IsNotExist(err) { |
| 130 | + return fmt.Errorf("export file does not exist: %s", exportFile) |
| 131 | + } |
| 132 | + |
| 133 | + // Check for target token (can come from flag or environment variable) |
| 134 | + targetToken := viper.GetString("TARGET_TOKEN") |
| 135 | + if targetToken == "" { |
| 136 | + return fmt.Errorf("target token is required. Set it via --target-token flag or GHMV_TARGET_TOKEN environment variable") |
| 137 | + } |
| 138 | + |
| 139 | + return nil |
| 140 | +} |
0 commit comments