func findEmail(c *githubAPI.Client, username string) string { events, resp, err := c.Activity.ListEventsPerformedByUser(username, true, nil) if err != nil { log.Println(kv.FormatLog("who-is-who", kayvee.Error, "Github API error", m{ "msg": err.Error(), })) return "" } else if resp.StatusCode != http.StatusOK { log.Println(kv.FormatLog("who-is-who", kayvee.Error, "Github API error", m{ "status code": resp.StatusCode, })) return "" } for _, e := range events { if e.RawPayload != nil { matches := emailRgx.FindAllStringSubmatch(string(*e.RawPayload), 1) if len(matches) == 1 && len(matches[0]) == 2 { return strings.ToLower(matches[0][1]) } } } return "" }
func main() { setupEnvVars() // setup dynamodb connection c, err := integrations.NewClient(dynamoTable, dynamoEndpoint, dynamoRegion, awsKey, awsSecret) if err != nil { log.Fatal(kv.FormatLog("who-is-who", kv.Error, "dynamo connection", m{ "message": err.Error(), })) } d := api.DynamoConn{ Dynamo: c, } // setup HTTP server log.Println(kv.FormatLog("who-is-who", kv.Info, "server startup", m{ "message": fmt.Sprintf("Listening on %s", port), })) err = http.ListenAndServe(port, d.HookUpRouter()) if err != nil { log.Fatal(kv.FormatLog("who-is-who", kv.Error, "server startup failure", m{ "msg": err.Error(), })) } }
func TestLogCritical(t *testing.T) { buf := &bytes.Buffer{} logger := New("logger-tester") logger.SetOutput(buf) logger.Critical("testlogcritical") assertLogFormatAndCompareContent(t, string(buf.Bytes()), kv.FormatLog( "logger-tester", kv.Critical, "testlogcritical", map[string]interface{}{})) buf.Reset() logger.CriticalD("testlogcritical", map[string]interface{}{"key1": "val1", "key2": "val2"}) assertLogFormatAndCompareContent(t, string(buf.Bytes()), kv.FormatLog( "logger-tester", kv.Critical, "testlogcritical", map[string]interface{}{"key1": "val1", "key2": "val2"})) }
func TestLogGaugeFloat(t *testing.T) { buf := &bytes.Buffer{} logger := New("logger-tester") logger.SetOutput(buf) logger.GaugeFloat("testloggauge", 0.0) assertLogFormatAndCompareContent(t, string(buf.Bytes()), kv.FormatLog( "logger-tester", kv.Info, "testloggauge", map[string]interface{}{"type": "gauge", "value": 0.0})) buf.Reset() logger.GaugeFloatD("testloggauge", 4.0, map[string]interface{}{"key1": "val1", "key2": "val2"}) assertLogFormatAndCompareContent(t, string(buf.Bytes()), kv.FormatLog( "logger-tester", kv.Info, "testloggauge", map[string]interface{}{"key1": "val1", "key2": "val2", "type": "gauge", "value": 4.0})) }
func TestLogCounter(t *testing.T) { buf := &bytes.Buffer{} logger := New("logger-tester") logger.SetOutput(buf) logger.Counter("testlogcounter") assertLogFormatAndCompareContent(t, string(buf.Bytes()), kv.FormatLog( "logger-tester", kv.Info, "testlogcounter", map[string]interface{}{"type": "counter", "value": 1})) buf.Reset() logger.CounterD("testlogcounter", 2, map[string]interface{}{"key1": "val1", "key2": "val2"}) assertLogFormatAndCompareContent(t, string(buf.Bytes()), kv.FormatLog( "logger-tester", kv.Info, "testlogcounter", map[string]interface{}{"key1": "val1", "key2": "val2", "type": "counter", "value": 2})) }
// Fill make the necessary API calls to get all members of a Github Org. Then we attempt to find // emails for every developer in their public history. func (l UserList) Fill(u integrations.UserMap) (integrations.UserMap, error) { emailRgx = regexp.MustCompile(fmt.Sprintf(`"email":"(\w+\.\w+@%s)"`, l.Domain)) ts := oauth2.StaticTokenSource( &oauth2.Token{AccessToken: l.Token}, ) tc := oauth2.NewClient(oauth2.NoContext, ts) gh := githubAPI.NewClient(tc) lo := githubAPI.ListMembersOptions{} for { members, resp, err := gh.Organizations.ListMembers(l.Org, &lo) if err != nil { return u, fmt.Errorf("Failed to form HTTP request for Github => {%s}", err) } for _, mbr := range members { if mbr.Login != nil && *mbr.Login != "" { email := findEmail(gh, *mbr.Login) if email == "" { continue } // add username to user if we find one with a matching email user, exists := u[email] if exists && user.Github == "" { // Don't override existing github usernames user.Github = *mbr.Login u[email] = user } else if exists { log.Println(kv.FormatLog("who-is-who", kv.Info, "github username exists", m{ "existing-github": user.Github, "new-github": *mbr.Login, "email": email, })) } else { log.Println(kv.FormatLog("who-is-who", kv.Info, "mismatched email", m{ "message": fmt.Sprintf("Found %s email but no user", l.Domain), "email": email, })) } } } // cycle through all pages of org users if resp.NextPage == 0 { break } else { lo.Page = resp.NextPage } } return u, nil }
// requiredEnv tries to find a value in the environment variables. If a value is not // found the program will panaic. func requiredEnv(key string) string { value := os.Getenv(key) if value == "" { log.Fatal(kayvee.FormatLog("who-is-who", kayvee.Error, "missing env var", m{ "var": key, })) } return value }
func TestMultipleLoggers(t *testing.T) { buf1 := &bytes.Buffer{} buf2 := &bytes.Buffer{} logger1 := New("logger-tester1") logger2 := New("logger-tester2") logger1.SetOutput(buf1) logger2.SetOutput(buf2) logger1.WarnD("testlogwarning", map[string]interface{}{"key1": "val1", "key2": "val2"}) logger2.Info("testloginfo") logOutput1 := string(buf1.Bytes()) assertLogFormatAndCompareContent(t, logOutput1, kv.FormatLog( "logger-tester1", kv.Warning, "testlogwarning", map[string]interface{}{"key1": "val1", "key2": "val2"})) assertLogFormatAndCompareContent(t, string(buf2.Bytes()), kv.FormatLog( "logger-tester2", kv.Info, "testloginfo", map[string]interface{}{})) logger2.SetOutput(buf1) logger2.Info("testloginfo") assert.NotEqual(t, logOutput1, string(buf1.Bytes())) }
func main() { // get DB conn client, err := integrations.NewClient(dynamoTable, dynamoEndpoint, dynamoRegion, awsKey, awsSecret) if err != nil { log.Fatalf("Failed to connect => {%s}", err) } // get Slack info slackData := slack.NewUserMap(domain, slackToken) // seed a map of User's with emails emails, err := slackData.EmailList() if err != nil { log.Fatal(kayvee.FormatLog("who-is-who", kayvee.Error, "bad slack conn", m{ "msg": err.Error(), })) } userMap := make(integrations.UserMap) for _, e := range emails { userMap[strings.ToLower(e)] = integrations.User{} } // declare all data sources to be used dataSources := []struct { Service integrations.InfoSource Name string }{ {slackData, "slack"}, {aws.AwsService{}, "aws"}, // Turning github integration off until bugs are fixed: INFRA-1124 // {github.UserList{ // Token: githubToken, // Org: githubOrg, // Domain: domain, // }, "github"}, } // add data from every data source to every User object for _, src := range dataSources { var err error userMap, err = src.Service.Fill(userMap) if err != nil { log.Printf("Failed to get data from source, %s => {%s}", src.Name, err) } } // try to save everything err = client.SaveUsers(userMap) if err != nil { log.Fatalf("Failed to save users in Dynamo, %s", err) } log.Printf("Saved %d users", len(userMap)) }