Example #1
0
// InitUserCache is called by a main goroutine
// to prepopulate the user cache
func InitUserCache(client *slack.Client) error {
	if Users == nil {
		log.Printf("Initializing the user cache")

		Users = &UserCache{
			client: client,
			cache:  common.NewBiMap()}

		users, err := client.GetUsers()
		if err != nil {
			return err
		}

		for _, v := range users {
			Users.cache.Put(v.ID, v.Name)
			SB.ChanPersist <- &common.SlackItem{
				ID:   v.ID,
				Name: v.Name,
				Type: common.SlackItemTypeUser}
		}

		log.Printf("Done initializing the user cache")
	}

	return nil
}
Example #2
0
func dumpUsers(api *slack.Client, dir string) {
	users, err := api.GetUsers()
	check(err)

	data, err := MarshalIndent(users, "", "    ")
	check(err)
	err = ioutil.WriteFile(path.Join(dir, "users.json"), data, 0644)
	check(err)
}
// findSlackUser uses the slack API to find the user who made the commit that
// is being built.
func findSlackUser(api *slack.Client, p Plugin) (*slack.User, error) {
	// get the mapping
	mapping := userMapping(p.Config.Mapping)

	// determine the search function to use
	var search searchFunc
	var find string

	if val, ok := mapping[p.Build.Email]; ok {
		log.WithFields(log.Fields{
			"username": val,
		}).Info("Searching for user by name")
		search = checkUsername
		find = val
	} else {
		log.WithFields(log.Fields{
			"email": p.Build.Email,
		}).Info("Searching for user by email")
		search = checkEmail
		find = p.Build.Email
	}

	if len(find) == 0 {
		return nil, errors.New("No user to search for")
	}

	// search for the user
	users, err := api.GetUsers()

	if err != nil {
		log.Error("Could not query users")
		return nil, err
	}

	var blameUser *slack.User

	for _, user := range users {
		if search(&user, find) {
			log.WithFields(log.Fields{
				"username": user.Name,
				"email":    user.Profile.Email,
			}).Info("Found user")

			blameUser = &user
			break
		} else {
			log.WithFields(log.Fields{
				"username": user.Name,
				"email":    user.Profile.Email,
			}).Debug("User")
		}
	}

	return blameUser, nil
}
func getUsers(api slack.Client) error {
	users, err := api.GetUsers()
	if err != nil {
		return err
	}

	var userList = ""

	for _, member := range users {
		if member.Has2FA == false && member.Deleted == false && member.IsBot == false && member.IsRestricted == false && member.IsUltraRestricted == false && member.ID != "USLACKBOT" {
			if err := annoyUser(api, member.Name); err != nil {
				return err
			}
			userList += "@" + member.Name + " "
			fmt.Printf("%s\n", member.Name)
		}
	}

	if err := shameUsers(api, userList); err != nil {
		return err
	}

	return nil
}