This commit is contained in:
Sergey Zhemoytel 2024-11-21 14:36:35 +03:00
parent f40022f203
commit 662c68cfaa
2 changed files with 59 additions and 37 deletions

View file

@ -60,7 +60,6 @@ func (a *App) Run() error {
}
log.Printf("Артефакт %s загружен из репозитория Nexus", artifact)
// Изменение функции Run для загрузки артефактов в Gitea
parts := strings.Split(artifact, "/")
if len(parts) < 4 {
log.Printf("Некорректный формат артефакта %s", artifact)
@ -74,8 +73,8 @@ func (a *App) Run() error {
log.Printf("Некорректный формат файла %s", parts[3])
continue
}
if fileParts[len(fileParts)-1] == "md5" || fileParts[len(fileParts)-1] == "sha1" {
log.Printf("Пропускаем файл %s, так как он имеет расширение .md5 или .sha1", artifact)
if strings.HasSuffix(artifact, ".md5") || strings.HasSuffix(artifact, ".sha1") || strings.HasSuffix(artifact, ".sha256") || strings.HasSuffix(artifact, ".sha512") {
log.Printf("Пропускаем файл %s, так как он имеет расширение .md5, .sha1, .sha256 или .sha512", artifact)
continue
}
packaging := fileParts[len(fileParts)-1]

View file

@ -1,15 +1,14 @@
package gitea
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"mime/multipart"
"net/http"
"os"
"tvoygit.ru/djam/artmigrator/config"
"tvoygit.ru/djam/artmigrator/logger"
)
type Client struct {
@ -60,50 +59,74 @@ type UploadArtifactRequest struct {
func (c *Client) UploadArtifact(groupId, artifactId, version, packaging string, data []byte) error {
log.Printf("Загрузка артефакта %s-%s-%s.%s в репозиторий Gitea", groupId, artifactId, version, packaging)
url := fmt.Sprintf("%s/api/v1/packages/%s/maven", c.BaseURL, c.Config.Gitea.Repo)
request := map[string]string{
"groupId": groupId,
"artifactId": artifactId,
"version": version,
"packaging": packaging,
"file": fmt.Sprintf("%s-%s-%s.%s", artifactId, version, packaging, packaging),
}
jsonRequest, err := json.Marshal(request)
file, err := os.CreateTemp("", "artifact")
if err != nil {
return err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonRequest))
defer os.Remove(file.Name())
_, err = file.Write(data)
if err != nil {
return err
}
err = file.Close()
if err != nil {
return err
}
pr, pw := io.Pipe()
go func() {
multipartWriter := multipart.NewWriter(pw)
part, err := multipartWriter.CreateFormFile("file", file.Name())
if err != nil {
log.Println(err)
return
}
_, err = io.Copy(part, file)
if err != nil {
log.Println(err)
return
}
err = multipartWriter.WriteField("groupId", groupId)
if err != nil {
log.Println(err)
return
}
err = multipartWriter.WriteField("artifactId", artifactId)
if err != nil {
log.Println(err)
return
}
err = multipartWriter.WriteField("version", version)
if err != nil {
log.Println(err)
return
}
err = multipartWriter.WriteField("packaging", packaging)
if err != nil {
log.Println(err)
return
}
err = multipartWriter.Close()
if err != nil {
log.Println(err)
return
}
pw.Close()
}()
req, err := http.NewRequest("POST", url, pr)
if err != nil {
return err
}
req.Header.Add("Authorization", fmt.Sprintf("token %s", c.Token))
req.Header.Add("Content-Type", "application/json")
logger.Logger.Printf("Upload artifact request: %s", req.URL)
logger.Logger.Printf("Upload artifact headers: %v", req.Header)
logger.Logger.Printf("Upload artifact body: %s", jsonRequest)
req.Header.Add("Content-Type", "multipart/form-data")
resp, err := c.HTTPClient.Do(req)
if err != nil {
logger.Logger.Printf("Upload artifact error: %v", err)
return err
}
defer resp.Body.Close()
logger.Logger.Printf("Upload artifact response status code: %d", resp.StatusCode)
logger.Logger.Printf("Upload artifact response headers: %v", resp.Header)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
logger.Logger.Printf("Upload artifact error reading response body: %v", err)
return err
}
logger.Logger.Printf("Upload artifact response body: %s", body)
if resp.StatusCode != http.StatusCreated {
logger.Logger.Printf("Failed to upload artifact: %s", body)
return fmt.Errorf("failed to upload artifact")
log.Printf("Ошибка загрузки артефакта: %s", resp.Status)
return fmt.Errorf("failed to upload artifact: %s", resp.Status)
}
log.Printf("Артефакт %s-%s-%s.%s загружен в репозиторий Gitea", groupId, artifactId, version, packaging)
return nil
}