alaskartv-api/main.go

237 lines
6.1 KiB
Go
Raw Normal View History

2024-12-19 19:42:51 +00:00
package main
import (
"bufio"
"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"
"strconv"
"strings"
2024-12-19 19:42:51 +00:00
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
2024-12-19 19:42:51 +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)
2024-12-19 19:42:51 +00:00
newVersion := getVersion(serviceName)
2024-12-20 17:07:02 +00:00
godotenv.Load(".botenv")
botToken := os.Getenv("BOT_TOKEN")
chatid := os.Getenv("CHAT_ID")
if serviceName == "alaskartv" {
2024-12-19 19:56:29 +00:00
versionPath = fmt.Sprintf("%s/release.txt", repoPath)
bumpVersionTv(repoPath)
2024-12-19 19:42:51 +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
}
2024-12-19 19:56:29 +00:00
os.WriteFile(versionPath, []byte(newVersion), 0644)
2024-12-19 19:42:51 +00:00
cmds := [][]string{
{"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"},
{
"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-19 19:42:51 +00:00
}
for _, cmd := range cmds {
err := exec.Command(cmd[0], cmd[1:]...).Run()
if err != nil {
return "", fmt.Errorf("failed to execute command %v: %w", cmd, err)
2024-12-19 19:42:51 +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
2024-12-19 19:42:51 +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]
newVersion, _ := BumpWithRollover(currentPatch, "patch")
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)
}
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!")
}
func notifyHandler(c *gin.Context) {
var data struct {
Name string `json:"name"`
2024-12-30 00:28:36 +00:00
Message string `json:"message"`
}
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"
}
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)
telegramAPI := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", botToken)
resp, err := http.PostForm(telegramAPI, url.Values{
"chat_id": {chatid},
"text": {message},
2024-12-30 00:28:36 +00:00
"parse_mode": {"HTML"},
})
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"})
}
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()
}
2024-12-19 19:42:51 +00:00
func main() {
router := gin.Default()
router.POST("/api/alaskarfin", func(c *gin.Context) {
jsonResponse, err := updateVersion("/data/alaskartv/docker-ci/alaskarfin", "alaskarfin")
2024-12-19 19:42:51 +00:00
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
2024-12-19 19:42:51 +00:00
return
}
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) {
jsonResponse, err := updateVersion("/data/alaskartv/docker-ci/alaskarseer", "alaskarseer")
2024-12-19 19:42:51 +00:00
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
2024-12-19 19:42:51 +00:00
return
}
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) {
jsonResponse, err := updateVersion("/data/alaskartv/androidtv-ci", "alaskartv")
2024-12-19 19:42:51 +00:00
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": err.Error(),
})
2024-12-19 19:42:51 +00:00
return
}
c.Header("Content-Type", "application/json")
c.String(http.StatusOK, jsonResponse)
notify(jsonResponse)
2024-12-19 19:42:51 +00:00
})
router.POST("/notify", notifyHandler)
2024-12-19 19:42:51 +00:00
fmt.Println("Webhook server running on port 8080")
router.Run(":8080")
}