-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
105 lines (84 loc) · 2.92 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"fmt"
"log"
"math/rand"
"net/url"
"time"
"confluence-gardner/conf"
"github.com/spf13/viper"
goconfluence "github.com/virtomize/confluence-go-api"
)
type Article struct {
ID string
Title string
LastUpdateTime time.Time
}
func getDomainName(confluenceURL string) string {
parsedURL, err := url.Parse(confluenceURL)
if err != nil {
log.Fatal(err)
}
return parsedURL.Scheme + "://" + parsedURL.Host
}
func getOldArticles(api *goconfluence.API, pageID string, now time.Time, maxDepth int, ageThresholdHours float64) ([]Article, error) {
var oldArticles []Article
err := getOldArticlesRecursive(api, pageID, now, maxDepth, 0, ageThresholdHours, &oldArticles)
return oldArticles, err
}
func getOldArticlesRecursive(api *goconfluence.API, pageID string, now time.Time, maxDepth, currentDepth int, ageThresholdHours float64, oldArticles *[]Article) error {
if currentDepth > maxDepth {
return nil
}
childPages, err := api.GetChildPages(pageID)
if err != nil {
return fmt.Errorf("error getting child pages: %w", err)
}
for _, page := range childPages.Results {
history, err := api.GetHistory(page.ID)
if err != nil {
return fmt.Errorf("failed to fetch history for page %s: %w", page.ID, err)
}
lastUpdateTime, err := time.Parse("2006-01-02T15:04:05.000Z", history.LastUpdated.When)
if err != nil {
return fmt.Errorf("error parsing last update time: %w", err)
}
if now.Sub(lastUpdateTime).Hours() > ageThresholdHours {
*oldArticles = append(*oldArticles, Article{ID: page.ID, Title: page.Title, LastUpdateTime: lastUpdateTime})
}
// use Recursion to dive into each Layer of Confluence pages
err = getOldArticlesRecursive(api, page.ID, now, maxDepth, currentDepth+1, ageThresholdHours, oldArticles)
if err != nil {
return err
}
}
return nil
}
func main() {
conf.ReadConf()
conf.ParseCliOpts()
cURL := viper.GetString("confluence_url")
cToken := viper.GetString("confluence_token")
cPageID := viper.GetString("confluence_page_id")
maxDepth := viper.GetInt("max_depth")
ageThresholdHours := viper.GetFloat64("age_threshold_hours")
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)
}
now := time.Now()
oldArticles, err := getOldArticles(api, cPageID, now, maxDepth, ageThresholdHours)
if err != nil {
log.Fatal(err)
}
if len(oldArticles) > 0 {
r := rand.New((rand.NewSource(time.Now().UnixNano())))
randIdx := r.Intn(len(oldArticles))
selectedArticle := oldArticles[randIdx]
fmt.Printf("Confluence page [%s](%s/pages/viewpage.action?pageId=%s) was last updated at %s. Please check its contents.\n", selectedArticle.Title, domain, selectedArticle.ID, selectedArticle.LastUpdateTime.Format("2006-01-02"))
} else {
fmt.Println("There are no old Confluence Pages. Congratulations!")
}
}