Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
Sebastian Gumprich committed Jul 27, 2023
0 parents commit 69a6304
Showing 10 changed files with 1,439 additions and 0 deletions.
46 changes: 46 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
name: goreleaser

on:
push:
# run only against tags
tags:
- '*'

permissions:
contents: write
packages: write
issues: write

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- run: git fetch --force --tags
- uses: actions/setup-go@v4
with:
go-version: stable
# More assembly might be required: Docker logins, GPG, etc. It all depends
# on your needs.
- uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: goreleaser/goreleaser-action@v4
with:
# either 'goreleaser' (default) or 'goreleaser-pro':
distribution: goreleaser
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
-
name: Upload assets
uses: actions/upload-artifact@v3
with:
name: confluence-gardner
path: dist/*
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
confluence-gardener
dist/
12 changes: 12 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
project_name: confluence-gardner

builds:
- env: [CGO_ENABLED=0]
dockers:
-
goos: linux
goarch: amd64
dockerfile: goreleaser.dockerfile
image_templates:
- 'ghcr.io/telekom-mms/confluence-gardner:{{ .Tag }}'
- 'ghcr.io/telekom-mms/confluence-gardner:latest'
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ARG DISTROLESS_IMAGE=gcr.io/distroless/base

# using base nonroot image
# user:group is nobody:nobody, uid:gid = 65534:65534
FROM ${DISTROLESS_IMAGE}

# Copy our static executable
COPY confluence-gardner /confluence-gardner

# Run the hello binary.
ENTRYPOINT ["/confluence-gardner"]
675 changes: 675 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Confluence Gardner

Confluence-Gardner is a simple tool designed to help maintain up-to-date Confluence pages. The software checks the age of Confluence pages and returns the URL of the pages older than 90 days, indicating a potential need for review and update.

# Getting Started

Follow these instructions to get the Confluence-Gardner up and running on your local machine.


## Prerequisites

Golang 1.16 or later
Confluence API access

## Installation

### Download the binary

Just download the binary from the releases-page and execute it.

### Manually

Clone the Confluence-Gardner repository:

git clone https://github.com/telekom-mms/confluence-gardner.git
cd confluence-gardner

Install the required Go packages using go mod:

go mod download

Build the tool:

go build

## Usage

confluence-gardner --help
-i, --confluence_page_id string The ID for which to crawl child pages
-t, --confluence_token string The token to authenticate against the Confluence REST-API
-u, --confluence_url string The URL to the Confluence REST-API with http(s) (default "https://confluence.example.com/rest/api")

## Contributing

Contributions to Confluence-Gardner are welcome! Please follow these steps:

Fork the repository on GitHub.
Make your changes in a new branch.
Create a pull request with a clear description of your changes.
We will review and merge your contribution if applicable.

## License

This project is licensed under the GPLv3 - see the LICENSE file for details.

48 changes: 48 additions & 0 deletions conf/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package conf

import (
"fmt"
"log"
"os"

"github.com/spf13/pflag"
"github.com/spf13/viper"
)

func ReadConf() {
viper.SetConfigName("application") // name of config file (without extension)
viper.AddConfigPath(".") // optionally look for config in the working directory
viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name

viper.AutomaticEnv()
}

func ParseCliOpts() {
pflag.StringP("confluence_url", "u", "https://confluence.example.com/rest/api", "The URL to the Confluence REST-API with http(s)")
pflag.StringP("confluence_token", "t", "", "The token to authenticate against the Confluence REST-API")
pflag.StringP("confluence_page_id", "i", "", "The ID for which to crawl child pages")

pflag.Parse()
err := viper.BindPFlags(pflag.CommandLine)
if err != nil {
log.Fatal(err)
}

if viper.GetString("confluence_url") == "" {
fmt.Println("Please add a confluence url")
pflag.PrintDefaults()
os.Exit(1)
}

if viper.GetString("confluence_page_id") == "" {
fmt.Println("Please add a page id to crawl")
pflag.PrintDefaults()
os.Exit(1)
}

if viper.GetString("confluence_token") == "" {
fmt.Println("Please add a token")
pflag.PrintDefaults()
os.Exit(1)
}
}
27 changes: 27 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module confluence-gardner

go 1.18

require github.com/virtomize/confluence-go-api v1.4.5

require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
github.com/magefile/mage v1.14.0 // indirect
github.com/spf13/viper v1.16.0
)
477 changes: 477 additions & 0 deletions go.sum

Large diffs are not rendered by default.

86 changes: 86 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package main

import (
"confluence-gardner/conf"
"fmt"
"log"
"math/rand"
"net/url"
"time"

"github.com/spf13/viper"
goconfluence "github.com/virtomize/confluence-go-api"
)

func getDomainName(confluence_url string) string {
url, err := url.Parse(confluence_url)
if err != nil {
log.Fatal(err)
}

return url.Scheme + "://" + url.Host

}

func main() {

conf.ReadConf()
conf.ParseCliOpts()

// initialize a new api instance

c_url := viper.GetString("confluence_url")
c_token := viper.GetString("confluence_token")
c_page_id := viper.GetString("confluence_page_id")

domain := getDomainName(c_url)

api, err := goconfluence.NewAPI(c_url, "", c_token)
if err != nil {
log.Fatal(err)
}

childPages, err := api.GetChildPages(c_page_id)
if err != nil {
log.Fatal(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(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.", oldArticles[randIdx].Title, domain, oldArticles[randIdx].ID, oldArticles[randIdx].lastUpdateTime.Format("2006-01-02"))
} else {
fmt.Printf("There are no old Confluence Pages. Congratulations!")
}
}

0 comments on commit 69a6304

Please sign in to comment.