-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
87 lines (67 loc) · 2 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
82
83
84
85
86
87
package main
import (
"fmt"
"log"
"math/rand"
"net/url"
"time"
"confluence-gardner/conf"
"github.com/spf13/viper"
goconfluence "github.com/virtomize/confluence-go-api"
)
func getDomainName(confluenceURL string) string {
url, err := url.Parse(confluenceURL)
if err != nil {
log.Fatal(err)
}
return url.Scheme + "://" + url.Host
}
func main() {
conf.ReadConf()
conf.ParseCliOpts()
// initialize a new api instance
cURL := viper.GetString("confluence_url")
cToken := viper.GetString("confluence_token")
cPageID := viper.GetString("confluence_page_id")
domain := getDomainName(cURL)
goconfluence.SetDebug(viper.GetViper().GetBool("debug"))
api, err := goconfluence.NewAPI(cURL, "", cToken)
if err != nil {
log.Fatal("Error connecting to Confluence: ", err)
}
childPages, err := api.GetChildPages(cPageID)
if err != nil {
log.Fatal("Error getting child pages: ", err)
}
now := time.Now()
type Articles struct {
ID string
Title string
lastUpdateTime time.Time
}
var oldArticles []Articles
for _, v := range childPages.Results {
hist, err := api.GetHistory(v.ID)
if err != nil {
log.Fatal("Error getting history: ", err)
}
lastUpdateTimeString := hist.LastUpdated.When
lastUpdateTime, err := time.Parse("2006-01-02T15:04:05.000Z", lastUpdateTimeString)
if err != nil {
fmt.Println(err)
}
difference := now.Sub(lastUpdateTime)
// 3000h ~ 3 months
if difference.Hours() > 3000 {
oldArticles = append(oldArticles, Articles{ID: v.ID, Title: v.Title, lastUpdateTime: lastUpdateTime})
}
}
if len(oldArticles) > 0 {
// get random article
rand.Seed(time.Now().UnixNano())
randIdx := rand.Intn(len(oldArticles))
fmt.Printf("Confluence page [%s](%s/pages/viewpage.action?pageId=%s) was last updated at %s. Please check its contents.\n", oldArticles[randIdx].Title, domain, oldArticles[randIdx].ID, oldArticles[randIdx].lastUpdateTime.Format("2006-01-02"))
} else {
fmt.Println("There are no old Confluence Pages. Congratulations!")
}
}