2024-12-19 19:42:51 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-12-19 22:03:45 +00:00
|
|
|
"bufio"
|
2025-01-07 14:25:07 +00:00
|
|
|
"bytes"
|
2024-12-30 02:02:53 +00:00
|
|
|
"encoding/json"
|
2024-12-19 19:42:51 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2024-12-23 23:25:54 +00:00
|
|
|
"net/url"
|
2024-12-19 19:42:51 +00:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2024-12-19 22:03:45 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2024-12-19 19:42:51 +00:00
|
|
|
|
2024-12-31 03:44:56 +00:00
|
|
|
"github.com/gin-contrib/cors"
|
2024-12-19 19:42:51 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2024-12-20 16:28:10 +00:00
|
|
|
"github.com/joho/godotenv"
|
2024-12-19 19:42:51 +00:00
|
|
|
)
|
|
|
|
|
2024-12-30 02:02:53 +00:00
|
|
|
func updateVersion(repoPath string, serviceName string) (string, error) {
|
2024-12-19 19:56:29 +00:00
|
|
|
versionPath := fmt.Sprintf("%s/version.txt", repoPath)
|
2025-01-07 13:07:04 +00:00
|
|
|
if serviceName == "alaskartv" {
|
|
|
|
versionPath = fmt.Sprintf("%s/release.txt", repoPath)
|
|
|
|
}
|
2024-12-19 19:42:51 +00:00
|
|
|
newVersion := getVersion(serviceName)
|
|
|
|
|
2024-12-20 17:07:02 +00:00
|
|
|
godotenv.Load(".botenv")
|
2024-12-20 16:28:10 +00:00
|
|
|
botToken := os.Getenv("BOT_TOKEN")
|
|
|
|
chatid := os.Getenv("CHAT_ID")
|
|
|
|
|
2024-12-30 02:02:53 +00:00
|
|
|
currentVersion, err := os.ReadFile(versionPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to read version file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compare versions
|
|
|
|
if strings.TrimSpace(string(currentVersion)) == strings.TrimSpace(newVersion) {
|
|
|
|
message := map[string]interface{}{
|
|
|
|
"status": "unchanged",
|
|
|
|
"message": fmt.Sprintf("Version is already at %s", newVersion),
|
|
|
|
"service": serviceName,
|
|
|
|
"version": newVersion,
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonResponse, err := json.Marshal(message)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to marshal JSON: %w", err)
|
|
|
|
}
|
|
|
|
return string(jsonResponse), nil
|
|
|
|
}
|
|
|
|
|
2025-01-07 12:39:07 +00:00
|
|
|
if serviceName == "alaskartv" {
|
|
|
|
bumpVersionTv(repoPath)
|
|
|
|
}
|
2024-12-19 19:56:29 +00:00
|
|
|
os.WriteFile(versionPath, []byte(newVersion), 0644)
|
2024-12-19 19:42:51 +00:00
|
|
|
cmds := [][]string{
|
2024-12-19 22:19:42 +00:00
|
|
|
{"git", "-C", repoPath, "add", "."},
|
2024-12-19 19:42:51 +00:00
|
|
|
{"git", "-C", repoPath, "commit", "-m", fmt.Sprintf("Bump version to %s", newVersion)},
|
|
|
|
{"git", "-C", repoPath, "push"},
|
2024-12-20 16:28:10 +00:00
|
|
|
{
|
|
|
|
"curl", "-X", "POST",
|
|
|
|
fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", botToken),
|
|
|
|
"-d", fmt.Sprintf("chat_id=%s", chatid),
|
2024-12-30 00:28:36 +00:00
|
|
|
"-d", fmt.Sprintf("text=<b>Alaskar-api</b>: <b>%s</b> updated to <b>%s</b>", serviceName, newVersion),
|
|
|
|
"-d", fmt.Sprintf("parse_mode=HTML"),
|
2024-12-20 16:28:10 +00:00
|
|
|
},
|
2024-12-19 19:42:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, cmd := range cmds {
|
|
|
|
err := exec.Command(cmd[0], cmd[1:]...).Run()
|
|
|
|
if err != nil {
|
2024-12-30 02:02:53 +00:00
|
|
|
return "", fmt.Errorf("failed to execute command %v: %w", cmd, err)
|
2024-12-19 19:42:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-30 02:02:53 +00:00
|
|
|
message := map[string]interface{}{
|
|
|
|
"status": "updated",
|
|
|
|
"message": fmt.Sprintf("Successfully updated from %s to %s", string(currentVersion), newVersion),
|
|
|
|
"service": serviceName,
|
|
|
|
"version": newVersion,
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonResponse, err := json.Marshal(message)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to marshal JSON: %w", err)
|
|
|
|
}
|
|
|
|
return string(jsonResponse), nil
|
2025-01-07 13:07:04 +00:00
|
|
|
}
|
2024-12-19 19:42:51 +00:00
|
|
|
|
2024-12-19 22:03:45 +00:00
|
|
|
func bumpVersionTv(repoPath string) {
|
|
|
|
filePath := fmt.Sprintf("%s/version.txt", repoPath)
|
|
|
|
file, _ := os.Open(filePath)
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
var updatedContent string
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(line, "VERSION_NAME="):
|
|
|
|
parts := strings.Split(line, "=")
|
|
|
|
currentPatch := parts[1]
|
2024-12-19 22:33:51 +00:00
|
|
|
newVersion, _ := BumpWithRollover(currentPatch, "patch")
|
2024-12-19 22:03:45 +00:00
|
|
|
updatedContent += fmt.Sprintf("VERSION_NAME=%s\n", newVersion)
|
|
|
|
|
|
|
|
case strings.HasPrefix(line, "VERSION_CODE="):
|
|
|
|
parts := strings.Split(line, "=")
|
|
|
|
currentCode, _ := strconv.Atoi(parts[1])
|
|
|
|
newVersionCode := currentCode + 1
|
|
|
|
updatedContent += fmt.Sprintf("VERSION_CODE=%d\n", newVersionCode)
|
|
|
|
|
|
|
|
default:
|
|
|
|
updatedContent += line + "\n"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
fmt.Errorf("error reading %s: %w", filePath, err)
|
|
|
|
}
|
2024-12-19 22:19:42 +00:00
|
|
|
if err := os.WriteFile(filePath, []byte(updatedContent), 0644); err != nil {
|
|
|
|
fmt.Errorf("failed to write to %s: %w", filePath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Version bumped successfully!")
|
2024-12-19 22:03:45 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-12-23 22:48:23 +00:00
|
|
|
func notifyHandler(c *gin.Context) {
|
|
|
|
var data struct {
|
2024-12-30 02:02:53 +00:00
|
|
|
Name string `json:"name"`
|
2024-12-30 00:28:36 +00:00
|
|
|
Message string `json:"message"`
|
2024-12-23 22:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.ShouldBindJSON(&data); err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid JSON"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if data.Name == "" {
|
|
|
|
data.Name = "unknown"
|
|
|
|
}
|
2024-12-30 00:28:36 +00:00
|
|
|
if data.Message == "" {
|
|
|
|
data.Message = "unknown"
|
2024-12-23 22:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
godotenv.Load(".botenv")
|
|
|
|
botToken := os.Getenv("BOT_TOKEN")
|
|
|
|
chatid := os.Getenv("CHAT_ID")
|
|
|
|
|
2024-12-30 00:28:36 +00:00
|
|
|
message := fmt.Sprintf("<b>Alaskar-api</b>: <b>%s</b> \n \n %s", data.Name, data.Message)
|
2024-12-23 22:48:23 +00:00
|
|
|
|
2024-12-23 23:22:50 +00:00
|
|
|
telegramAPI := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", botToken)
|
2024-12-23 22:48:23 +00:00
|
|
|
resp, err := http.PostForm(telegramAPI, url.Values{
|
2024-12-30 02:02:53 +00:00
|
|
|
"chat_id": {chatid},
|
|
|
|
"text": {message},
|
2024-12-30 00:28:36 +00:00
|
|
|
"parse_mode": {"HTML"},
|
2024-12-23 22:48:23 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to send Telegram message"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "Notification sent"})
|
|
|
|
}
|
|
|
|
|
2024-12-30 02:02:53 +00:00
|
|
|
func notify(jsonResponse string) {
|
|
|
|
var data struct {
|
|
|
|
Status string `json:"status"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
Service string `json:"service"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
}
|
|
|
|
godotenv.Load(".botenv")
|
|
|
|
botToken := os.Getenv("BOT_TOKEN")
|
|
|
|
chatid := os.Getenv("CHAT_ID")
|
|
|
|
|
|
|
|
json.Unmarshal([]byte(jsonResponse), &data)
|
|
|
|
|
|
|
|
message := fmt.Sprintf("<b>Alaskar-api</b>: <b>%s %s</b> \n \n %s", data.Service, data.Status, data.Message)
|
|
|
|
telegramAPI := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", botToken)
|
|
|
|
resp, _ := http.PostForm(telegramAPI, url.Values{
|
|
|
|
"chat_id": {chatid},
|
|
|
|
"text": {message},
|
|
|
|
"parse_mode": {"HTML"},
|
|
|
|
})
|
|
|
|
defer resp.Body.Close()
|
|
|
|
}
|
|
|
|
|
2025-01-07 14:25:07 +00:00
|
|
|
func TriggerPublish() {
|
|
|
|
godotenv.Load(".botenv")
|
|
|
|
FORGEJO := os.Getenv("FORGEJO_TOKEN")
|
|
|
|
payload := `{"ref": "main"}`
|
|
|
|
apiURL := "https://git.askar.tv/api/v1/repos/nomad/alaskartv-app/actions/workflows/publish.yml/dispatches"
|
|
|
|
apiToken := FORGEJO
|
|
|
|
|
|
|
|
req, _ := http.NewRequest("POST", apiURL, bytes.NewBuffer([]byte(payload)))
|
|
|
|
req.Header.Set("Authorization", "token "+apiToken)
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
client.Do(req)
|
|
|
|
}
|
|
|
|
|
2024-12-19 19:42:51 +00:00
|
|
|
func main() {
|
|
|
|
router := gin.Default()
|
2024-12-31 03:44:56 +00:00
|
|
|
router.Use(cors.New(cors.Config{
|
2024-12-31 04:27:23 +00:00
|
|
|
AllowOrigins: []string{"*"},
|
2024-12-31 03:44:56 +00:00
|
|
|
AllowMethods: []string{"POST", "GET", "OPTIONS"},
|
|
|
|
AllowHeaders: []string{"Content-Type", "Authorization"},
|
|
|
|
ExposeHeaders: []string{"Content-Length"},
|
|
|
|
AllowCredentials: true,
|
|
|
|
}))
|
2024-12-19 19:42:51 +00:00
|
|
|
|
|
|
|
router.POST("/api/alaskarfin", func(c *gin.Context) {
|
2025-01-06 14:01:45 +00:00
|
|
|
jsonResponse, err := updateVersion("/data/alaskartv-forge/alaskartv-docker/alaskarfin", "alaskarfin")
|
2024-12-19 19:42:51 +00:00
|
|
|
if err != nil {
|
2024-12-30 02:02:53 +00:00
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
|
|
"error": err.Error(),
|
|
|
|
})
|
2024-12-19 19:42:51 +00:00
|
|
|
return
|
|
|
|
}
|
2024-12-30 02:02:53 +00:00
|
|
|
c.Header("Content-Type", "application/json")
|
|
|
|
c.String(http.StatusOK, jsonResponse)
|
|
|
|
notify(jsonResponse)
|
2024-12-19 19:42:51 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
router.POST("/api/alaskarseer", func(c *gin.Context) {
|
2025-01-06 14:01:45 +00:00
|
|
|
jsonResponse, err := updateVersion("/data/alaskartv-forge/alaskartv-docker/alaskarseer", "alaskarseer")
|
2024-12-19 19:42:51 +00:00
|
|
|
if err != nil {
|
2024-12-30 02:02:53 +00:00
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
|
|
"error": err.Error(),
|
|
|
|
})
|
2024-12-19 19:42:51 +00:00
|
|
|
return
|
|
|
|
}
|
2024-12-30 02:02:53 +00:00
|
|
|
c.Header("Content-Type", "application/json")
|
|
|
|
c.String(http.StatusOK, jsonResponse)
|
|
|
|
notify(jsonResponse)
|
2024-12-19 19:42:51 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
router.POST("/api/alaskartv", func(c *gin.Context) {
|
2025-01-06 14:01:45 +00:00
|
|
|
jsonResponse, err := updateVersion("/data/alaskartv-forge/alaskartv-app", "alaskartv")
|
2024-12-19 19:42:51 +00:00
|
|
|
if err != nil {
|
2024-12-30 02:02:53 +00:00
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
|
|
"error": err.Error(),
|
|
|
|
})
|
2024-12-19 19:42:51 +00:00
|
|
|
return
|
|
|
|
}
|
2024-12-30 02:02:53 +00:00
|
|
|
|
|
|
|
c.Header("Content-Type", "application/json")
|
|
|
|
c.String(http.StatusOK, jsonResponse)
|
|
|
|
notify(jsonResponse)
|
|
|
|
|
2024-12-19 19:42:51 +00:00
|
|
|
})
|
|
|
|
|
2025-01-07 14:25:07 +00:00
|
|
|
router.POST("/api/publish", func(c *gin.Context) {
|
|
|
|
TriggerPublish()
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "Workflow triggered successfully"})
|
|
|
|
})
|
|
|
|
|
2024-12-23 22:48:23 +00:00
|
|
|
router.POST("/notify", notifyHandler)
|
2024-12-31 04:33:24 +00:00
|
|
|
router.GET("/", func(c *gin.Context) {
|
2024-12-31 04:37:11 +00:00
|
|
|
c.File("index.html")
|
2024-12-31 04:33:24 +00:00
|
|
|
})
|
2024-12-23 22:48:23 +00:00
|
|
|
|
2024-12-19 19:42:51 +00:00
|
|
|
fmt.Println("Webhook server running on port 8080")
|
|
|
|
router.Run(":8080")
|
|
|
|
}
|