Добавление автоматического обновления checksums

This commit is contained in:
2025-08-21 14:41:08 +03:00
parent 3e357e01b3
commit 90ba3c09eb
3 changed files with 170 additions and 0 deletions

View File

@@ -0,0 +1,75 @@
/*
* ALR Updater - Automated updater bot for ALR packages
* Copyright (C) 2025 The ALR Authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package builtins
import (
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"net/http"
"time"
"go.starlark.net/starlark"
"go.starlark.net/starlarkstruct"
)
func checksumModule() *starlarkstruct.Module {
return &starlarkstruct.Module{
Name: "checksum",
Members: starlark.StringDict{
"calculate_sha256": starlark.NewBuiltin("checksum.calculate_sha256", calculateSHA256),
},
}
}
func calculateSHA256(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var url string
err := starlark.UnpackArgs("checksum.calculate_sha256", args, kwargs, "url", &url)
if err != nil {
return nil, err
}
// Создаем HTTP клиент с таймаутом
client := &http.Client{
Timeout: 5 * time.Minute,
}
// Загружаем файл
resp, err := client.Get(url)
if err != nil {
return nil, fmt.Errorf("failed to download file: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("bad status: %s", resp.Status)
}
// Вычисляем SHA256
hasher := sha256.New()
_, err = io.Copy(hasher, resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to calculate hash: %w", err)
}
// Возвращаем хеш в виде hex строки
hashSum := hex.EncodeToString(hasher.Sum(nil))
return starlark.String(hashSum), nil
}

View File

@@ -46,5 +46,6 @@ func Register(sd starlark.StringDict, opts *Options) {
sd["json"] = starlarkjson.Module
sd["utils"] = utilsModule
sd["html"] = htmlModule
sd["checksum"] = checksumModule()
sd["register_webhook"] = registerWebhook(opts.Mux, opts.Config, opts.Name)
}

View File

@@ -19,6 +19,7 @@
package builtins
import (
"fmt"
"io"
"os"
"path/filepath"
@@ -45,6 +46,7 @@ func updaterModule(cfg *config.Config) *starlarkstruct.Module {
"push_changes": updaterPushChanges(cfg),
"get_package_file": getPackageFile(cfg),
"write_package_file": writePackageFile(cfg),
"update_checksums": updateChecksums(cfg),
},
}
}
@@ -235,3 +237,95 @@ func autoResetRelease(oldContent, newContent string) string {
return newContent
}
// updateChecksumsInContent обновляет значения checksums в содержимом файла
func updateChecksumsInContent(content string, checksums []string) string {
// Паттерн для поиска массива checksums
checksumsRegex := regexp.MustCompile(`checksums=\((.*?)\)`)
// Формируем новый массив checksums
var newChecksumsArray string
if len(checksums) == 1 && checksums[0] != "" {
// Если одна хеш-сумма, форматируем как ('hash')
newChecksumsArray = fmt.Sprintf("('%s')", checksums[0])
} else if len(checksums) > 1 {
// Если несколько хеш-сумм, форматируем как ('hash1' 'hash2' ...)
quotedChecksums := make([]string, len(checksums))
for i, cs := range checksums {
quotedChecksums[i] = fmt.Sprintf("'%s'", cs)
}
newChecksumsArray = "(" + strings.Join(quotedChecksums, " ") + ")"
} else {
// Если нет хеш-сумм, оставляем SKIP
newChecksumsArray = "('SKIP')"
}
// Заменяем старый массив checksums на новый
newContent := checksumsRegex.ReplaceAllString(content, "checksums="+newChecksumsArray)
return newContent
}
func updateChecksums(cfg *config.Config) *starlark.Builtin {
return starlark.NewBuiltin("updater.update_checksums", func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var pkg, filename, content string
var checksums *starlark.List
err := starlark.UnpackArgs("updater.update_checksums", args, kwargs,
"pkg", &pkg,
"filename", &filename,
"content", &content,
"checksums", &checksums)
if err != nil {
return nil, err
}
// Конвертируем starlark.List в []string
checksumStrings := make([]string, 0, checksums.Len())
iter := checksums.Iterate()
defer iter.Done()
var val starlark.Value
for iter.Next(&val) {
if s, ok := val.(starlark.String); ok {
checksumStrings = append(checksumStrings, string(s))
}
}
// Обновляем checksums в содержимом
updatedContent := updateChecksumsInContent(content, checksumStrings)
// Автоматически сбрасываем release='1' при изменении версии
repoMtx.Lock()
defer repoMtx.Unlock()
path := filepath.Join(cfg.Git.RepoDir, pkg, filename)
// Читаем старый файл для сравнения версий
var oldContent string
if oldData, err := os.ReadFile(path); err == nil {
oldContent = string(oldData)
}
// Применяем autoResetRelease
finalContent := autoResetRelease(oldContent, updatedContent)
// Записываем файл
fl, err := os.Create(path)
if err != nil {
return nil, err
}
defer fl.Close()
_, err = io.Copy(fl, strings.NewReader(finalContent))
if err != nil {
return nil, err
}
log.Debug("Updated package file with checksums").
Str("package", pkg).
Str("filename", filename).
Int("checksums_count", len(checksumStrings)).
Stringer("pos", thread.CallFrame(1).Pos).Send()
return starlark.None, nil
})
}