|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + "maintainerd/db" |
| 6 | + "os" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/spf13/cobra" |
| 10 | + "github.com/spf13/viper" |
| 11 | + "gorm.io/driver/sqlite" |
| 12 | + "gorm.io/gorm" |
| 13 | + "gorm.io/gorm/logger" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + apiTokenEnvVar = "FOSSA_API_TOKEN" |
| 18 | + defaultDBPath = "maintainers.db" |
| 19 | +) |
| 20 | + |
| 21 | +func main() { |
| 22 | + var dbPath string |
| 23 | + var projectName string |
| 24 | + var serviceName string |
| 25 | + |
| 26 | + rootCmd := &cobra.Command{ |
| 27 | + Use: "status", |
| 28 | + Short: "onboarding service for maintainerd", |
| 29 | + } |
| 30 | + |
| 31 | + onboardCmd := &cobra.Command{ |
| 32 | + Use: "onboard", |
| 33 | + Short: "Checks the Onboarding status of a project wrt FOSSA", |
| 34 | + Run: func(cmd *cobra.Command, args []string) { |
| 35 | + if projectName == "" { |
| 36 | + log.Fatal("ERROR: --project flag is required") |
| 37 | + } |
| 38 | + |
| 39 | + fossaToken := viper.GetString(apiTokenEnvVar) |
| 40 | + if fossaToken == "" { |
| 41 | + log.Fatalf("ERROR: environment variable %s is not set", apiTokenEnvVar) |
| 42 | + } |
| 43 | + |
| 44 | + newLogger := logger.New( |
| 45 | + log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer |
| 46 | + logger.Config{ |
| 47 | + SlowThreshold: time.Second, // Slow SQL threshold |
| 48 | + LogLevel: logger.Silent, // Log level |
| 49 | + IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger |
| 50 | + ParameterizedQueries: true, // Don't include params in the SQL log |
| 51 | + Colorful: false, // Disable color |
| 52 | + }, |
| 53 | + ) |
| 54 | + |
| 55 | + dbSession, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{ |
| 56 | + Logger: newLogger, |
| 57 | + }) |
| 58 | + if err != nil { |
| 59 | + log.Fatalf("failed to connect to database: %v", err) |
| 60 | + } |
| 61 | + s := db.NewSQLStore(dbSession) |
| 62 | + |
| 63 | + allProjects, err := s.GetProjectMapByName() |
| 64 | + if err != nil { |
| 65 | + log.Fatalf("failed to get project list from db %v", err) |
| 66 | + } else { |
| 67 | + log.Printf("Found %d projects in db", len(allProjects)) |
| 68 | + } |
| 69 | + project := allProjects[projectName] |
| 70 | + log.Printf("Status of %#v", project) |
| 71 | + maintainers, err := s.GetMaintainersByProject(project.ID) |
| 72 | + |
| 73 | + if err != nil { |
| 74 | + log.Fatalf("failed to get maintainers for project '%s': %v", projectName, err) |
| 75 | + } else { |
| 76 | + log.Printf("Found %d maintainers in db", len(maintainers)) |
| 77 | + } |
| 78 | + |
| 79 | + if len(maintainers) == 0 { |
| 80 | + log.Fatalf("No maintainers found for project '%s'", projectName) |
| 81 | + } |
| 82 | + |
| 83 | + log.Printf("Onboarding project '%s' to service '%s'", projectName, serviceName) |
| 84 | + serviceTeam, err := s.GetServiceTeamByProject(project.ID, 1) |
| 85 | + if err != nil { |
| 86 | + log.Fatalf("failed to get service team for project '%s' and service '%s': %v", projectName, serviceName, err) |
| 87 | + } |
| 88 | + log.Printf("Service Team: %#v", serviceTeam) |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + onboardCmd.Flags().StringVar(&projectName, "project", "", "Project name to onboard (required)") |
| 93 | + onboardCmd.Flags().StringVar(&serviceName, "service", "fossa", "Service name (default: fossa)") |
| 94 | + onboardCmd.MarkFlagRequired("project") |
| 95 | + |
| 96 | + rootCmd.AddCommand(onboardCmd) |
| 97 | + rootCmd.PersistentFlags().StringVar(&dbPath, "db", defaultDBPath, "Path to SQLite database file") |
| 98 | + |
| 99 | + viper.AutomaticEnv() // binds environment variables to viper config |
| 100 | + |
| 101 | + if err := rootCmd.Execute(); err != nil { |
| 102 | + log.Fatalf("command failed: %v", err) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +//func reportProjectStatus(projectName string) { |
| 107 | +// var actions []string |
| 108 | +// |
| 109 | +// // Check for maintainers registered for this project |
| 110 | +// maintainers, err := s.Store.GetMaintainersByProject(project.ID) |
| 111 | +// if err != nil { |
| 112 | +// actions = append(actions, fmt.Sprintf(":x: %s maintainers not present in db, @cncf-projects-team check maintainer-d db", project.Name)) |
| 113 | +// } |
| 114 | +// |
| 115 | +// actions = append(actions, fmt.Sprintf("✅ %s has %d maintainers registered in maintainer-d", project.Name, len(maintainers))) |
| 116 | +// |
| 117 | +// // Do we have a team already in FOSSA for @project? |
| 118 | +// serviceTeams, err := s.Store.GetProjectServiceTeamMap("FOSSA") |
| 119 | +// if err != nil { |
| 120 | +// actions = append(actions, fmt.Sprintf(":warning: Problem retrieving serviceTeams. %v", err)) |
| 121 | +// } |
| 122 | +// st, ok := serviceTeams[project.ID] |
| 123 | +// if ok { |
| 124 | +// actions = append( |
| 125 | +// actions, |
| 126 | +// fmt.Sprintf("👥 [%s team](https://app.fossa.com/account/settings/organization/teams/%d) was already in FOSSA", |
| 127 | +// project.Name, |
| 128 | +// st.ServiceTeamID)) |
| 129 | +// } else { |
| 130 | +// // create the team on FOSSA, add the team to the ServiceTeams |
| 131 | +// team, err := s.FossaClient.CreateTeam(project.Name) |
| 132 | +// |
| 133 | +// if err != nil { |
| 134 | +// actions = append(actions, fmt.Sprintf(":x: Problem creating team on FOSSA for %s: %v", project.Name, err)) |
| 135 | +// } else { |
| 136 | +// log.Printf("team created: %s", team.Name) |
| 137 | +// actions = append(actions, |
| 138 | +// fmt.Sprintf("👥 [%s team](https://app.fossa.com/account/settings/organization/teams/%d) has been created in FOSSA", |
| 139 | +// team.Name, team.ID)) |
| 140 | +// _, err := s.Store.CreateServiceTeam(project.ID, project.Name, team.ID, team.Name) |
| 141 | +// if err != nil { |
| 142 | +// fmt.Printf("handleWebhook: WRN, failed to create service team: %v", err) |
| 143 | +// } |
| 144 | +// } |
| 145 | +// if err != nil { |
| 146 | +// fmt.Printf("signProjectUpForFOSSA: Error creating team on FOSSA for %s: %v", project.Name, err) |
| 147 | +// } |
| 148 | +// } |
| 149 | +// if len(maintainers) == 0 { |
| 150 | +// actions = append(actions, fmt.Sprintf("Maintainers not yet registered, for project %s", project.Name)) |
| 151 | +// return actions, fmt.Errorf(":x: no maintainers found for project %d", project.ID) |
| 152 | +// } |
| 153 | +// for _, maintainer := range maintainers { |
| 154 | +// err := s.FossaClient.SendUserInvitation(maintainer.Email) // TODO See if I can Name the User on FOSSA! |
| 155 | +// |
| 156 | +// if errors.Is(err, fossa.ErrInviteAlreadyExists) { |
| 157 | +// actions = append(actions, fmt.Sprintf("@%s : you have a pending invitation to join CNCF FOSSA. Please check your registered email and accept the invitation within 48 hours.", maintainer.GitHubAccount)) |
| 158 | +// } else if errors.Is(err, fossa.ErrUserAlreadyMember) { |
| 159 | +// // TODO Edge case - maintainers already signed up to CNCF FOSSA, maintainer on an another project? |
| 160 | +// actions = append(actions, fmt.Sprintf("@%s : You are CNCF FOSSA User", maintainer.GitHubAccount)) |
| 161 | +// // TODO call fc.AddUserToTeamByEmail() |
| 162 | +// log.Printf("user is already a member, skipping") |
| 163 | +// } else if err != nil { |
| 164 | +// log.Printf("error sending invite: %v", err) |
| 165 | +// actions = append(actions, fmt.Sprintf("@%s : there was a problem sending a CNCF FOSSA invitation to you.", maintainer.GitHubAccount)) |
| 166 | +// } |
| 167 | +// } |
| 168 | +// |
| 169 | +// // check if the project team has imported their repos. If we label an onboarding issue with 'fossa' and the project |
| 170 | +// // has been manually setup in the past, better to report that repos have been imported into FOSSA. |
| 171 | +// teamMap, err := s.Store.GetProjectServiceTeamMap("FOSSA") |
| 172 | +// if err != nil { |
| 173 | +// return nil, err |
| 174 | +// } |
| 175 | +// |
| 176 | +// count, repos, err := s.FossaClient.FetchImportedRepos(teamMap[project.ID].ServiceTeamID) |
| 177 | +// importedRepos := s.FossaClient.ImportedProjectLinks(repos) |
| 178 | +// if count == 0 { |
| 179 | +// actions = append(actions, fmt.Sprintf("The %s project has not yet imported repos", project.Name)) |
| 180 | +// } else { |
| 181 | +// actions = append(actions, fmt.Sprintf("The %s project team have imported %d repo(s)<BR>%s", project.Name, count, importedRepos)) |
| 182 | +// } |
| 183 | +// |
| 184 | +// return actions, nil |
| 185 | +// |
| 186 | +//} |
0 commit comments