-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
81 lines (66 loc) · 1.63 KB
/
main.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"context"
"flag"
"fmt"
"os"
"time"
"github.com/hori-ryota/esa-go/esa"
)
var (
dryrun = flag.Bool("dry-run", false, "skip posting comment")
teamName = flag.String("team", "camphor", "team name")
)
func main() {
flag.Parse()
token := os.Getenv("ESA_API_TOKEN")
if token == "" {
fmt.Println("Environment variable ESA_API_TOKEN is not set.")
os.Exit(1)
return
}
aMonthBefore := time.Now().Add(-30 * 24 * time.Hour)
client := esa.NewClient(token, *teamName)
page := uint(0)
for {
nonUpdatedPosts, err := client.ListPosts(context.Background(), esa.ListPostsParam{
Q: "updated:<" + aMonthBefore.Format("2006-01-02") + " wip:true -in:Users -in:Archived -in:ポエム",
}, page, 100)
if err != nil {
fmt.Println(err)
os.Exit(1)
return
}
for _, post := range nonUpdatedPosts.Posts {
if err := postComment(client, post); err != nil {
fmt.Println(err)
os.Exit(1)
return
}
}
if nonUpdatedPosts.NextPage == nil {
break
}
page = *nonUpdatedPosts.NextPage
}
}
func postComment(client esa.Client, post esa.Post) error {
if dryrun != nil && *dryrun {
fmt.Printf("skip posting comment: #%d %s\n", post.Number, post.Name)
return nil
}
bodyTemplate := `
@%s WIPのまま記事が1ヶ月以上放置されています!
記事を更新してShip Itしましょう!
`
body := fmt.Sprintf(bodyTemplate, post.UpdatedBy.ScreenName)
botUser := "esa_bot"
_, err := client.CreateComment(context.Background(), post.Number, esa.CreateCommentParam{
BodyMD: body,
User: &botUser,
})
if err != nil {
return fmt.Errorf("create comment: %w", err)
}
return nil
}