-
Notifications
You must be signed in to change notification settings - Fork 2
/
sweepstakes.go
66 lines (53 loc) · 1.19 KB
/
sweepstakes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"math/rand"
"strings"
"time"
"github.com/opensourceways/community-robot-lib/giteeclient"
"k8s.io/apimachinery/pkg/util/sets"
)
func (bot *robot) handleSweepstakes(ne giteeclient.IssueNoteEvent, cfg *botConfig) error {
if !ne.IsIssue() || !ne.IsCreatingCommentEvent() {
return nil
}
n := parseCmd(ne.GetComment())
if n <= 0 {
return nil
}
org, repo := ne.GetOrgRep()
comments, err := bot.cli.ListIssueComments(org, repo, ne.GetIssueNumber())
if err != nil {
return err
}
commenter := ne.GetCommenter()
candidates := sets.NewString()
for i := range comments {
v := comments[i].User.Login
if v != bot.botName && v != commenter {
candidates.Insert(v)
}
}
max := candidates.Len()
if max == 0 {
return nil
}
v := candidates.UnsortedList()
topN := genRandomNumber(max, n)
r := make([]string, len(topN))
for i := range topN {
r[i] = v[topN[i]]
}
return bot.cli.CreateIssueComment(
org, repo, ne.GetIssueNumber(),
fmt.Sprintf(cfg.Congratulation, "@"+strings.Join(r, ", @")),
)
}
func genRandomNumber(max, n int) []int {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
v := r.Perm(max)
if max <= n {
return v
}
return v[:n]
}