diff --git a/config.yaml.template b/config.yaml.template index a2ae75b..5dbe533 100644 --- a/config.yaml.template +++ b/config.yaml.template @@ -1,10 +1,10 @@ nexus: - url: "https://nexus.example.com" + url: "http://127.0.0.1:8081" username: "your-nexus-username" password: "your-nexus-password" gitea: - url: "https://gitea.example.com" + url: "http://127.0.0.1:3000" token: "your-gitea-token" - - + username: "your-gitea-username" + repo: "your-gitea-repo" diff --git a/config/config.go b/config/config.go index 9858cb4..49ee95b 100644 --- a/config/config.go +++ b/config/config.go @@ -15,6 +15,8 @@ type Config struct { Gitea struct { URL string `yaml:"url"` Token string `yaml:"token"` + Username string `yaml:"username"` + Repo string `yaml:"repo"` } `yaml:"gitea"` } @@ -30,4 +32,4 @@ func LoadConfig(filename string) (*Config, error) { return nil, err } return &config, nil -} \ No newline at end of file +} diff --git a/core/core.go b/core/core.go index b37a351..7299e4f 100644 --- a/core/core.go +++ b/core/core.go @@ -4,9 +4,9 @@ import ( "flag" "tvoygit.ru/djam/artmigrator/config" + "tvoygit.ru/djam/artmigrator/gitea" "tvoygit.ru/djam/artmigrator/logger" - "tvoygit.ru/djam/artmigrator/repository/gitea" - "tvoygit.ru/djam/artmigrator/repository/nexus" + "tvoygit.ru/djam/artmigrator/nexus" ) type App struct { @@ -31,7 +31,7 @@ func NewApp(configFile string) (*App, error) { func (a *App) Run() error { nexusClient := nexus.NewClient(a.Config.Nexus.URL, a.Config.Nexus.Username, a.Config.Nexus.Password) - giteaClient := gitea.NewClient(a.Config.Gitea.URL, a.Config.Gitea.Token) + giteaClient := gitea.NewClient(a.Config.Gitea.URL, a.Config.Gitea.Token, a.Config) repository := "your-repository-name" artifacts, err := nexusClient.GetArtifacts(repository) @@ -40,7 +40,7 @@ func (a *App) Run() error { } for _, artifact := range artifacts { - exists, err := giteaClient.ArtifactExists("owner", "repo", artifact) + exists, err := giteaClient.ArtifactExists(artifact) if err != nil { return err } @@ -55,11 +55,11 @@ func (a *App) Run() error { return err } - err = giteaClient.UploadArtifact("owner", "repo", artifact, data) + err = giteaClient.UploadArtifact(artifact, data) if err != nil { return err } logger.Logger.Printf("Artifact %s uploaded to Gitea", artifact) } return nil -} \ No newline at end of file +} diff --git a/repository/gitea/gitea.go b/repository/gitea/gitea.go index 7e272a0..b7a5856 100644 --- a/repository/gitea/gitea.go +++ b/repository/gitea/gitea.go @@ -1,10 +1,13 @@ package gitea import ( + "bytes" + "encoding/json" "fmt" "io/ioutil" "net/http" + "tvoygit.ru/djam/artmigrator/config" "tvoygit.ru/djam/artmigrator/logger" ) @@ -12,18 +15,20 @@ type Client struct { BaseURL string HTTPClient *http.Client Token string + Config *config.Config } -func NewClient(baseURL, token string) *Client { +func NewClient(baseURL, token string, config *config.Config) *Client { return &Client{ BaseURL: baseURL, HTTPClient: &http.Client{}, Token: token, + Config: config, } } -func (c *Client) ArtifactExists(owner, repo, artifact string) (bool, error) { - url := fmt.Sprintf("%s/api/v1/repos/%s/%s/contents/%s", c.BaseURL, owner, repo, artifact) +func (c *Client) ArtifactExists(artifact string) (bool, error) { + url := fmt.Sprintf("%s/api/v1/repos/%s/%s/contents/%s", c.BaseURL, c.Config.Gitea.Username, c.Config.Gitea.Repo, artifact) req, err := http.NewRequest("GET", url, nil) if err != nil { return false, err @@ -42,25 +47,52 @@ func (c *Client) ArtifactExists(owner, repo, artifact string) (bool, error) { return true, nil } -func (c *Client) UploadArtifact(owner, repo, artifactPath string, data []byte) error { - url := fmt.Sprintf("%s/api/v1/repos/%s/%s/contents/%s", c.BaseURL, owner, repo, artifactPath) - req, err := http.NewRequest("POST", url, nil) +type UploadArtifactRequest struct { + Content string `json:"content"` + Message string `json:"message"` +} + +func (c *Client) UploadArtifact(artifactPath string, data []byte) error { + url := fmt.Sprintf("%s/api/v1/repos/%s/%s/contents/%s?ref=master", c.BaseURL, c.Config.Gitea.Username, c.Config.Gitea.Repo, artifactPath) + request := UploadArtifactRequest{ + Content: string(data), + Message: "Upload artifact", + } + jsonRequest, err := json.Marshal(request) + if err != nil { + return err + } + req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonRequest)) 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) - // Здесь нужно заполнить тело запроса данными о загружаемом артефакте resp, err := c.HTTPClient.Do(req) if err != nil { + logger.Logger.Printf("Upload artifact error: %v", err) return err } defer resp.Body.Close() - if resp.StatusCode != http.StatusOK { - body, _ := ioutil.ReadAll(resp.Body) + 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") } return nil -} \ No newline at end of file +}