Skip to content

Commit e61d845

Browse files
committed
fix: address go vet errors and code cleanup
- Fix apis/maintainers/v1alpha1: add missing addKnownTypes function - Fix cmd/onboard/main.go: correct log.Fatalf format string - Add .gocache/ and coverage.out to .gitignore - Add *.env to .gitignore for environment file protection - Refactor code formatting and imports in various files
1 parent 678907c commit e61d845

File tree

8 files changed

+292
-143
lines changed

8 files changed

+292
-143
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
.DS_Store
22
.idea
33
*.log
4+
*.env
45
tmp/
56
.envrc
67
credentials.json
78
demo.db
89
.dirlocals.el
910
./db/db
11+
.gocache/
12+
coverage.out
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Package v1alpha1 contains API Schema definitions for the maintainer-d resources.
2+
// +kubebuilder:object:generate=true
3+
// +groupName=maintainer-d.cncf.io
4+
package v1alpha1
5+
6+
import (
7+
"k8s.io/apimachinery/pkg/runtime"
8+
"k8s.io/apimachinery/pkg/runtime/schema"
9+
)
10+
11+
const (
12+
// GroupName identifies the API group for maintainer-d resources.
13+
GroupName = "maintainer-d.cncf.io"
14+
// Version is the API version for maintainer-d resources.
15+
Version = "v1alpha1"
16+
)
17+
18+
// SchemeGroupVersion is group version used to register these objects.
19+
var SchemeGroupVersion = schema.GroupVersion{Group: GroupName, Version: Version}
20+
21+
// SchemeBuilder is used to add go types to the GroupVersionKind scheme.
22+
var SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)
23+
24+
// AddToScheme adds the types in this group-version to the given scheme.
25+
var AddToScheme = SchemeBuilder.AddToScheme
26+
27+
// addKnownTypes adds the set of types defined in this package to the supplied scheme.
28+
func addKnownTypes(scheme *runtime.Scheme) error {
29+
scheme.AddKnownTypes(SchemeGroupVersion)
30+
return nil
31+
}
32+
33+
// Resource takes an unqualified resource and returns a Group qualified GroupResource.
34+
func Resource(resource string) schema.GroupResource {
35+
return SchemeGroupVersion.WithResource(resource).GroupResource()
36+
}

cmd/onboard/main.go

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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+
//}

db/bootstrap.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func loadMaintainersAndProjects(db *gorm.DB, spreadsheetID, credentialsPath stri
151151
if github == "" {
152152
missingMaintainerFields = append(missingMaintainerFields, ":"+GitHubHdr)
153153
}
154-
154+
log.Printf("DEBUG, processing maintainer %s, missing fields %v \n", row[MaintainerNameHdr], missingMaintainerFields)
155155
var parent model.Project
156156
if parentName := row[ParentProjectHdr]; parentName != "" {
157157
parent = model.Project{}
@@ -357,7 +357,7 @@ func CreateServiceTeamsForUser(
357357
}
358358
teams = append(teams, st)
359359
} else {
360-
return nil, fmt.Errorf("CreateServiceTeamsForUser: ERROR %s is NOT A registered project!\n", team.Team.Name)
360+
return nil, fmt.Errorf("CreateServiceTeamsForUser: ERROR %s is NOT A registered project", team.Team.Name)
361361
}
362362
}
363363

@@ -373,8 +373,7 @@ func CreateServiceTeamsForUser(
373373
}
374374

375375
func MapFossaUserCollaborator(db *gorm.DB, email string, github string, user fossa.User) *model.Collaborator {
376-
var c model.Collaborator
377-
c = model.Collaborator{
376+
c := model.Collaborator{
378377
Model: gorm.Model{},
379378
Name: user.FullName,
380379
Email: user.Email,

0 commit comments

Comments
 (0)