wont bump if same version + notify on status
This commit is contained in:
parent
d7a1276f1f
commit
4d458e27e8
1 changed files with 85 additions and 15 deletions
100
main.go
100
main.go
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
@ -14,7 +15,7 @@ import (
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func updateVersion(repoPath string, serviceName string) error {
|
func updateVersion(repoPath string, serviceName string) (string, error) {
|
||||||
versionPath := fmt.Sprintf("%s/version.txt", repoPath)
|
versionPath := fmt.Sprintf("%s/version.txt", repoPath)
|
||||||
newVersion := getVersion(serviceName)
|
newVersion := getVersion(serviceName)
|
||||||
|
|
||||||
|
@ -27,6 +28,27 @@ func updateVersion(repoPath string, serviceName string) error {
|
||||||
bumpVersionTv(repoPath)
|
bumpVersionTv(repoPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
os.WriteFile(versionPath, []byte(newVersion), 0644)
|
os.WriteFile(versionPath, []byte(newVersion), 0644)
|
||||||
cmds := [][]string{
|
cmds := [][]string{
|
||||||
{"git", "-C", repoPath, "add", "."},
|
{"git", "-C", repoPath, "add", "."},
|
||||||
|
@ -44,11 +66,22 @@ func updateVersion(repoPath string, serviceName string) error {
|
||||||
for _, cmd := range cmds {
|
for _, cmd := range cmds {
|
||||||
err := exec.Command(cmd[0], cmd[1:]...).Run()
|
err := exec.Command(cmd[0], cmd[1:]...).Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to execute command %v: %w", cmd, err)
|
return "", fmt.Errorf("failed to execute command %v: %w", cmd, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
func bumpVersionTv(repoPath string) {
|
func bumpVersionTv(repoPath string) {
|
||||||
|
@ -92,7 +125,7 @@ func bumpVersionTv(repoPath string) {
|
||||||
|
|
||||||
func notifyHandler(c *gin.Context) {
|
func notifyHandler(c *gin.Context) {
|
||||||
var data struct {
|
var data struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -116,8 +149,8 @@ func notifyHandler(c *gin.Context) {
|
||||||
|
|
||||||
telegramAPI := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", botToken)
|
telegramAPI := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", botToken)
|
||||||
resp, err := http.PostForm(telegramAPI, url.Values{
|
resp, err := http.PostForm(telegramAPI, url.Values{
|
||||||
"chat_id": {chatid},
|
"chat_id": {chatid},
|
||||||
"text": {message},
|
"text": {message},
|
||||||
"parse_mode": {"HTML"},
|
"parse_mode": {"HTML"},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -129,34 +162,71 @@ func notifyHandler(c *gin.Context) {
|
||||||
c.JSON(http.StatusOK, gin.H{"message": "Notification sent"})
|
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()
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
|
|
||||||
router.POST("/api/alaskarfin", func(c *gin.Context) {
|
router.POST("/api/alaskarfin", func(c *gin.Context) {
|
||||||
err := updateVersion("/data/alaskartv/docker-ci/alaskarfin", "alaskarfin")
|
jsonResponse, err := updateVersion("/data/alaskartv/docker-ci/alaskarfin", "alaskarfin")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.String(http.StatusInternalServerError, "Something went wrong: %s", err.Error())
|
c.JSON(http.StatusInternalServerError, gin.H{
|
||||||
|
"error": err.Error(),
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.String(http.StatusOK, "Bumped Alaskarfin!")
|
c.Header("Content-Type", "application/json")
|
||||||
|
c.String(http.StatusOK, jsonResponse)
|
||||||
|
notify(jsonResponse)
|
||||||
})
|
})
|
||||||
|
|
||||||
router.POST("/api/alaskarseer", func(c *gin.Context) {
|
router.POST("/api/alaskarseer", func(c *gin.Context) {
|
||||||
err := updateVersion("/data/alaskartv/docker-ci/alaskarseer", "alaskarseer")
|
jsonResponse, err := updateVersion("/data/alaskartv/docker-ci/alaskarseer", "alaskarseer")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.String(http.StatusInternalServerError, "Something went wrong: %s", err.Error())
|
c.JSON(http.StatusInternalServerError, gin.H{
|
||||||
|
"error": err.Error(),
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.String(http.StatusOK, "Bumped Alaskarseer!")
|
c.Header("Content-Type", "application/json")
|
||||||
|
c.String(http.StatusOK, jsonResponse)
|
||||||
|
notify(jsonResponse)
|
||||||
})
|
})
|
||||||
|
|
||||||
router.POST("/api/alaskartv", func(c *gin.Context) {
|
router.POST("/api/alaskartv", func(c *gin.Context) {
|
||||||
err := updateVersion("/data/alaskartv/androidtv-ci", "alaskartv")
|
jsonResponse, err := updateVersion("/data/alaskartv/androidtv-ci", "alaskartv")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.String(http.StatusInternalServerError, "Something went wrong: %s", err.Error())
|
c.JSON(http.StatusInternalServerError, gin.H{
|
||||||
|
"error": err.Error(),
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.String(http.StatusOK, "Bumped AlaskarTV!")
|
|
||||||
|
c.Header("Content-Type", "application/json")
|
||||||
|
c.String(http.StatusOK, jsonResponse)
|
||||||
|
notify(jsonResponse)
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
router.POST("/notify", notifyHandler)
|
router.POST("/notify", notifyHandler)
|
||||||
|
|
Loading…
Reference in a new issue