This commit is contained in:
Imbus 2024-09-03 20:25:40 +02:00
commit d3b2d85f56
2 changed files with 121 additions and 0 deletions

15
config.json Normal file
View file

@ -0,0 +1,15 @@
{
"repos": [
{
"owner": "golang",
"repo": "go"
},
{
"owner": "torvalds",
"repo": "linux"
}
],
"interval": 5,
"ntfy_topic": "your-ntfy-topic"
}

106
src/main.go Normal file
View file

@ -0,0 +1,106 @@
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"os"
"net/http"
"time"
)
type Config struct {
Repos []RepoConfig `json:"repos"`
Interval int `json:"interval"` // Polling interval in seconds
NtfyTopic string `json:"ntfy_topic"`
}
type RepoConfig struct {
Owner string `json:"owner"`
Repo string `json:"repo"`
}
type Commit struct {
Sha string `json:"sha"`
URL string `json:"url"`
}
type Tag struct {
Name string `json:"name"`
ZipballURL string `json:"zipball_url"`
TarballURL string `json:"tarball_url"`
Commit Commit `json:"commit"`
NodeID string `json:"node_id"`
}
func main() {
// Load configuration
config := loadConfig("config.json")
// Main loop
for {
for _, repo := range config.Repos {
checkForUpdates(repo, config.NtfyTopic)
}
time.Sleep(time.Duration(config.Interval) * time.Second)
}
}
func loadConfig(file string) Config {
fh, err := os.OpenFile(file, os.O_RDONLY, 0644)
if err != nil {
log.Fatalf("Failed to open config file: %v", err)
}
data, err := io.ReadAll(fh)
if err != nil {
log.Fatalf("Failed to read config file: %v", err)
}
var config Config
if err := json.Unmarshal(data, &config); err != nil {
log.Fatalf("Failed to parse config file: %v", err)
}
return config
}
func checkForUpdates(repo RepoConfig, ntfyTopic string) {
url := fmt.Sprintf("https://api.github.com/repos/%s/%s/tags", repo.Owner, repo.Repo)
fmt.Printf(url)
resp, err := http.Get(url)
if err != nil {
log.Printf("Failed to fetch releases for %s/%s: %v", repo.Owner, repo.Repo, err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Printf("Unexpected response status for %s/%s: %d", repo.Owner, repo.Repo, resp.StatusCode)
return
}
var tags = []Tag{}
if err := json.NewDecoder(resp.Body).Decode(&tags); err != nil {
log.Printf("Failed to decode response for %s/%s: %v", repo.Owner, repo.Repo, err)
return
}
latestTag := tags[0].Name
fmt.Printf("Latest release for %s/%s is %s\n", repo.Owner, repo.Repo, latestTag)
// Notify via ntfy
sendNotification(ntfyTopic, repo, latestTag)
}
func sendNotification(topic string, repo RepoConfig, tag string) {
ntfyURL := fmt.Sprintf("https://ntfy.sh/%s", topic)
message := fmt.Sprintf("New release for %s/%s: %s", repo.Owner, repo.Repo, tag)
_, err := http.Post(ntfyURL, "text/plain", bytes.NewBuffer([]byte(message)))
if err != nil {
log.Printf("Failed to send notification: %v", err)
} else {
log.Printf("Notification sent: %s", message)
}
}