commit d3b2d85f56841a33a798c1bbe524a9d9ada7c836 Author: Imbus <> Date: Tue Sep 3 20:25:40 2024 +0200 Initial diff --git a/config.json b/config.json new file mode 100644 index 0000000..c3e4a0b --- /dev/null +++ b/config.json @@ -0,0 +1,15 @@ +{ + "repos": [ + { + "owner": "golang", + "repo": "go" + }, + { + "owner": "torvalds", + "repo": "linux" + } + ], + "interval": 5, + "ntfy_topic": "your-ntfy-topic" +} + diff --git a/src/main.go b/src/main.go new file mode 100644 index 0000000..b2ad3a8 --- /dev/null +++ b/src/main.go @@ -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) + } +}