Migrate pipelines - Best Effort
This commit is contained in:
parent
4e7cf06abf
commit
111f06ddf6
6 changed files with 620 additions and 5 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package forgejo
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
|
@ -14,6 +15,11 @@ type Client struct {
|
|||
baseURL string
|
||||
}
|
||||
|
||||
type Secret struct {
|
||||
Name string `json:"name"`
|
||||
Data string `json:"data"`
|
||||
}
|
||||
|
||||
type MigrateRequest struct {
|
||||
CloneAddr string `json:"clone_addr"`
|
||||
AuthUsername string `json:"auth_username"`
|
||||
|
|
@ -133,3 +139,58 @@ func (c *Client) DeleteRepository(owner, name string) error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateSecret creates a repository secret for Actions
|
||||
func (c *Client) CreateSecret(owner, repo, name, data string) error {
|
||||
// Forgejo/Gitea API for secrets
|
||||
url := fmt.Sprintf("/repos/%s/%s/actions/secrets/%s", owner, repo, name)
|
||||
|
||||
body := map[string]string{
|
||||
"data": data,
|
||||
}
|
||||
|
||||
resp, err := c.client.R().
|
||||
SetBody(body).
|
||||
Put(url)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create secret: %w", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode() != 201 && resp.StatusCode() != 204 {
|
||||
return fmt.Errorf("failed to create secret, status: %d", resp.StatusCode())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateFile creates a file in the repository (for workflow files)
|
||||
func (c *Client) CreateFile(owner, repo, path, content, message string) error {
|
||||
// Content needs to be base64 encoded
|
||||
encoded := base64.StdEncoding.EncodeToString([]byte(content))
|
||||
|
||||
body := map[string]interface{}{
|
||||
"content": encoded,
|
||||
"message": message,
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("/repos/%s/%s/contents/%s", owner, repo, path)
|
||||
|
||||
resp, err := c.client.R().
|
||||
SetBody(body).
|
||||
Post(url)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 201 = created, 422 = already exists (should handle update in production)
|
||||
if resp.StatusCode() != 201 {
|
||||
if resp.StatusCode() == 422 {
|
||||
return fmt.Errorf("file %s already exists", path)
|
||||
}
|
||||
return fmt.Errorf("failed to create file, status %d: %s", resp.StatusCode(), resp.String())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue