791 lines
25 KiB
Go
791 lines
25 KiB
Go
package forgejo
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type RepoManager struct {
|
|
BaseURL string
|
|
Token string
|
|
Owner string
|
|
Username string
|
|
ManifestsRepoName string
|
|
FluxRepoName string
|
|
Branch string
|
|
MigrationBranch string
|
|
MigrationPending bool
|
|
HTTPClient *http.Client
|
|
}
|
|
|
|
type createRepoRequest struct {
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
Private bool `json:"private"`
|
|
AutoInit bool `json:"auto_init"`
|
|
DefaultBranch string `json:"default_branch"`
|
|
}
|
|
|
|
type createOrganizationRequest struct {
|
|
Username string `json:"username"`
|
|
}
|
|
|
|
type pullRequestRequest struct {
|
|
Title string `json:"title"`
|
|
Head string `json:"head"`
|
|
Base string `json:"base"`
|
|
}
|
|
|
|
type pullRequest struct {
|
|
Number int `json:"number"`
|
|
}
|
|
|
|
type mergePullRequestRequest struct {
|
|
Do string `json:"Do"`
|
|
}
|
|
|
|
type hook struct {
|
|
ID int64 `json:"id"`
|
|
URL string `json:"url"`
|
|
}
|
|
|
|
type hookRequest struct {
|
|
Type string `json:"type,omitempty"`
|
|
Active bool `json:"active"`
|
|
AuthorizationHeader string `json:"authorization_header"`
|
|
Config map[string]string `json:"config"`
|
|
Events []string `json:"events"`
|
|
}
|
|
|
|
type branchProtection struct {
|
|
ID int64 `json:"id"`
|
|
BranchName string `json:"branch_name"`
|
|
RuleName string `json:"rule_name"`
|
|
EnablePush bool `json:"enable_push"`
|
|
EnablePushWhitelist bool `json:"enable_push_whitelist"`
|
|
}
|
|
|
|
type branchProtectionRequest struct {
|
|
BranchName string `json:"branch_name"`
|
|
EnablePush bool `json:"enable_push"`
|
|
EnablePushWhitelist bool `json:"enable_push_whitelist"`
|
|
}
|
|
|
|
type APIError struct {
|
|
StatusCode int
|
|
Status string
|
|
}
|
|
|
|
type createTokenRequest struct {
|
|
Name string `json:"name"`
|
|
Scopes []string `json:"scopes"`
|
|
}
|
|
|
|
type accessToken struct {
|
|
SHA1 string `json:"sha1"`
|
|
}
|
|
|
|
var copyGit = runGit
|
|
|
|
func (e *APIError) Error() string {
|
|
return fmt.Sprintf("forgejo returned %s", e.Status)
|
|
}
|
|
|
|
func NewRepoManager(baseURL, token, owner, username, manifestsRepo, fluxRepo, branch, migrationBranch string) *RepoManager {
|
|
return &RepoManager{
|
|
BaseURL: strings.TrimRight(baseURL, "/"),
|
|
Token: token,
|
|
Owner: owner,
|
|
Username: username,
|
|
ManifestsRepoName: manifestsRepo,
|
|
FluxRepoName: fluxRepo,
|
|
Branch: branch,
|
|
MigrationBranch: migrationBranch,
|
|
HTTPClient: &http.Client{Timeout: 15 * time.Second},
|
|
}
|
|
}
|
|
|
|
// CreateRegistryToken creates the package-registry token that Forgejo returns once.
|
|
func CreateRegistryToken(baseURL, username, password, otp, name string) (string, error) {
|
|
return createRegistryToken(&http.Client{Timeout: 15 * time.Second}, baseURL, username, password, otp, name)
|
|
}
|
|
|
|
func createRegistryToken(client *http.Client, baseURL, username, password, otp, name string) (string, error) {
|
|
if strings.TrimSpace(baseURL) == "" || username == "" || password == "" || strings.TrimSpace(name) == "" {
|
|
return "", fmt.Errorf("Forgejo base URL, username, password, and token name are required")
|
|
}
|
|
body, err := json.Marshal(createTokenRequest{Name: name, Scopes: []string{"read:package", "write:package"}})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
endpoint := fmt.Sprintf("%s/api/v1/users/%s/tokens", strings.TrimRight(baseURL, "/"), url.PathEscape(username))
|
|
request, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewReader(body))
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
request.SetBasicAuth(username, password)
|
|
request.Header.Set("Content-Type", "application/json")
|
|
if otp != "" {
|
|
request.Header.Set("X-Forgejo-OTP", otp)
|
|
}
|
|
response, err := client.Do(request)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer response.Body.Close()
|
|
if response.StatusCode != http.StatusCreated {
|
|
return "", &APIError{StatusCode: response.StatusCode, Status: response.Status}
|
|
}
|
|
var token accessToken
|
|
if err := json.NewDecoder(response.Body).Decode(&token); err != nil {
|
|
return "", fmt.Errorf("parse Forgejo registry token response: %w", err)
|
|
}
|
|
if token.SHA1 == "" {
|
|
return "", fmt.Errorf("Forgejo did not return a registry token")
|
|
}
|
|
return token.SHA1, nil
|
|
}
|
|
|
|
func (rm *RepoManager) InitializeAll(createRepo func(string) error, createFlux func(string) error) error {
|
|
if err := rm.ensureRepo(rm.ManifestsRepoName, "Centralized deployment manifests for Flux CD", createRepo); err != nil {
|
|
return err
|
|
}
|
|
return rm.ensureRepo(rm.FluxRepoName, "Flux CD cluster configurations", createFlux)
|
|
}
|
|
|
|
// EnsureOrganization creates the configured owner only when explicitly allowed.
|
|
func (rm *RepoManager) EnsureOrganization(create bool) (bool, error) {
|
|
exists, err := rm.organizationExists()
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if exists {
|
|
return false, nil
|
|
}
|
|
if !create {
|
|
return false, errors.New("Forgejo organization does not exist; rerun with --create-organization")
|
|
}
|
|
body, err := json.Marshal(createOrganizationRequest{Username: rm.Owner})
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
status, err := rm.apiRequest(http.MethodPost, fmt.Sprintf("%s/api/v1/orgs", rm.BaseURL), body)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if status == http.StatusCreated {
|
|
return true, nil
|
|
}
|
|
if status == http.StatusConflict {
|
|
exists, err = rm.organizationExists()
|
|
if err == nil && exists {
|
|
return false, nil
|
|
}
|
|
}
|
|
return false, fmt.Errorf("unexpected Forgejo organization create status %d", status)
|
|
}
|
|
|
|
func (rm *RepoManager) organizationExists() (bool, error) {
|
|
status, err := rm.apiRequest(http.MethodGet, fmt.Sprintf("%s/api/v1/orgs/%s", rm.BaseURL, url.PathEscape(rm.Owner)), nil)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
switch status {
|
|
case http.StatusOK:
|
|
return true, nil
|
|
case http.StatusNotFound:
|
|
return false, nil
|
|
default:
|
|
return false, fmt.Errorf("unexpected Forgejo organization lookup status %d", status)
|
|
}
|
|
}
|
|
|
|
func (rm *RepoManager) ensureRepo(name, description string, createStructure func(string) error) error {
|
|
exists, err := rm.repoExists(name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !exists {
|
|
if err := rm.createRepo(name, description, true); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return rm.setupRepository(CloneURL(rm.BaseURL, rm.Owner, name), name, exists, createStructure)
|
|
}
|
|
|
|
func (rm *RepoManager) repoExists(name string) (bool, error) {
|
|
status, err := rm.apiRequest(http.MethodGet, fmt.Sprintf("%s/api/v1/repos/%s/%s", rm.BaseURL, rm.Owner, name), nil)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
switch status {
|
|
case http.StatusOK:
|
|
return true, nil
|
|
case http.StatusNotFound:
|
|
return false, nil
|
|
default:
|
|
return false, fmt.Errorf("unexpected Forgejo repository lookup status %d", status)
|
|
}
|
|
}
|
|
|
|
func (rm *RepoManager) createRepo(name, description string, autoInit bool) error {
|
|
body, err := json.Marshal(createRepoRequest{Name: name, Description: description, Private: true, AutoInit: autoInit, DefaultBranch: rm.Branch})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
status, err := rm.apiRequest(http.MethodPost, rm.repositoryCreateURL(), body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if status != http.StatusCreated && status != http.StatusConflict {
|
|
return fmt.Errorf("unexpected Forgejo create repository status %d", status)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (rm *RepoManager) EnsureRepository(name, description string) (bool, error) {
|
|
return rm.ensureRepository(name, description, false)
|
|
}
|
|
|
|
// EnsureInitializedRepository creates a base branch without application content.
|
|
func (rm *RepoManager) EnsureInitializedRepository(name, description string) (bool, error) {
|
|
return rm.ensureRepository(name, description, true)
|
|
}
|
|
|
|
// EnsureRepositoryCopy creates an independent, user-owned copy of source.
|
|
// Existing repositories are left untouched so user-managed catalog changes are never overwritten.
|
|
func (rm *RepoManager) EnsureRepositoryCopy(name, description, source string) (bool, error) {
|
|
sourceURL, err := catalogSourceURL(source)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
exists, err := rm.repoExists(name)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if exists {
|
|
return false, rm.ensureRepositoryCopyRef(name)
|
|
}
|
|
if err := rm.createRepositoryCopy(name, description); err != nil {
|
|
return false, err
|
|
}
|
|
temporary, err := os.MkdirTemp("", "maidn-catalog-*")
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
defer os.RemoveAll(temporary)
|
|
cleanupAskPass, environment, err := rm.gitEnvironment()
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
defer cleanupAskPass()
|
|
if err := copyGit("", environment, "clone", "--mirror", sourceURL, temporary); err != nil {
|
|
return false, err
|
|
}
|
|
if err := copyGit(temporary, environment, "push", "--mirror", CloneURL(rm.BaseURL, rm.Owner, name)); err != nil {
|
|
return false, err
|
|
}
|
|
return true, rm.ensureRepositoryCopyRef(name)
|
|
}
|
|
|
|
func (rm *RepoManager) ensureRepositoryCopyRef(name string) error {
|
|
cleanupAskPass, environment, err := rm.gitEnvironment()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer cleanupAskPass()
|
|
if err := copyGit("", environment, "ls-remote", "--exit-code", CloneURL(rm.BaseURL, rm.Owner, name), "refs/heads/"+rm.Branch); err != nil {
|
|
return errors.New("Tekton catalog repository does not contain the configured catalog ref; refusing to use ambiguous state")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (rm *RepoManager) createRepositoryCopy(name, description string) error {
|
|
body, err := json.Marshal(createRepoRequest{Name: name, Description: description, Private: true, AutoInit: false, DefaultBranch: rm.Branch})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
status, err := rm.apiRequest(http.MethodPost, rm.repositoryCreateURL(), body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if status != http.StatusCreated {
|
|
return fmt.Errorf("Forgejo catalog repository creation returned status %d; refusing to copy into an ambiguous existing repository", status)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (rm *RepoManager) repositoryCreateURL() string {
|
|
if rm.Owner == rm.Username {
|
|
return fmt.Sprintf("%s/api/v1/user/repos", rm.BaseURL)
|
|
}
|
|
return fmt.Sprintf("%s/api/v1/orgs/%s/repos", rm.BaseURL, rm.Owner)
|
|
}
|
|
|
|
func catalogSourceURL(source string) (string, error) {
|
|
parsed, err := url.Parse(source)
|
|
if err != nil || parsed.Scheme != "https" || parsed.Host == "" || parsed.User != nil || parsed.RawQuery != "" || parsed.Fragment != "" || strings.Trim(parsed.Path, "/") == "" {
|
|
return "", fmt.Errorf("Tekton catalog source must be a credential-free HTTPS repository URL")
|
|
}
|
|
return parsed.String(), nil
|
|
}
|
|
|
|
func (rm *RepoManager) ensureRepository(name, description string, autoInit bool) (bool, error) {
|
|
exists, err := rm.repoExists(name)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if exists {
|
|
return false, nil
|
|
}
|
|
return true, rm.createRepo(name, description, autoInit)
|
|
}
|
|
|
|
func (rm *RepoManager) setupRepository(repoURL, repoName string, existing bool, createStructure func(string) error) error {
|
|
tempDir, err := os.MkdirTemp("", "repo-setup-*")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer os.RemoveAll(tempDir)
|
|
cleanupAskPass, environment, err := rm.gitEnvironment()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer cleanupAskPass()
|
|
if err := runGit("", environment, "clone", "--branch", rm.Branch, repoURL, tempDir); err != nil {
|
|
return err
|
|
}
|
|
targetBranch := rm.Branch
|
|
if existing {
|
|
targetBranch = rm.MigrationBranch
|
|
if err := runGit(tempDir, environment, "checkout", "-B", targetBranch, "origin/"+rm.Branch); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if err := createStructure(tempDir); err != nil {
|
|
return err
|
|
}
|
|
changed, err := commitAndPush(tempDir, repoName, targetBranch, environment)
|
|
if err != nil || !changed || !existing {
|
|
return err
|
|
}
|
|
rm.MigrationPending = true
|
|
return rm.createMigrationPullRequest(repoName, targetBranch)
|
|
}
|
|
|
|
func (rm *RepoManager) createMigrationPullRequest(repo, branch string) error {
|
|
return rm.CreatePullRequest(repo, "feat: bootstrap Maidn CI/CD structure", branch, rm.Branch)
|
|
}
|
|
|
|
func (rm *RepoManager) CreatePullRequest(repo, title, head, base string) error {
|
|
if head == "" || base == "" || head == base {
|
|
return fmt.Errorf("Forgejo pull request head and base must be different non-empty branches")
|
|
}
|
|
body, err := json.Marshal(pullRequestRequest{Title: title, Head: head, Base: base})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
status, err := rm.apiRequest(http.MethodPost, fmt.Sprintf("%s/api/v1/repos/%s/%s/pulls", rm.BaseURL, rm.Owner, repo), body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if status != http.StatusCreated && status != http.StatusUnprocessableEntity {
|
|
return fmt.Errorf("unexpected Forgejo pull request status %d", status)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (rm *RepoManager) MergePullRequest(repo, head string) error {
|
|
values := url.Values{"state": {"open"}, "head": {head}}
|
|
endpoint := fmt.Sprintf("%s/api/v1/repos/%s/%s/pulls?%s", rm.BaseURL, rm.Owner, repo, values.Encode())
|
|
var pullRequests []pullRequest
|
|
status, err := rm.apiJSONRequest(http.MethodGet, endpoint, nil, &pullRequests)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if status != http.StatusOK || len(pullRequests) != 1 {
|
|
return fmt.Errorf("expected one open Forgejo pull request for branch %q", head)
|
|
}
|
|
body, err := json.Marshal(mergePullRequestRequest{Do: "merge"})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
status, err = rm.apiRequest(http.MethodPost, fmt.Sprintf("%s/api/v1/repos/%s/%s/pulls/%d/merge", rm.BaseURL, rm.Owner, repo, pullRequests[0].Number), body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if status != http.StatusOK {
|
|
return fmt.Errorf("unexpected Forgejo pull request merge status %d", status)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (rm *RepoManager) PushBranch(dir, repoURL, branch string) error {
|
|
return rm.PushRef(dir, repoURL, "HEAD", branch)
|
|
}
|
|
|
|
func (rm *RepoManager) PushRef(dir, repoURL, sourceRef, targetBranch string) error {
|
|
cleanupAskPass, environment, err := rm.gitEnvironment()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer cleanupAskPass()
|
|
return runGit(dir, environment, "push", repoURL, sourceRef+":refs/heads/"+targetBranch)
|
|
}
|
|
|
|
// DeliveryBranch returns the dedicated branch that carries generated delivery content.
|
|
func DeliveryBranch(appName, baseBranch string) (string, error) {
|
|
branch := "maidn/delivery-" + appName
|
|
if appName == "" || branch == baseBranch {
|
|
return "", fmt.Errorf("delivery branch and configured base branch must differ")
|
|
}
|
|
return branch, nil
|
|
}
|
|
|
|
// PublishDeliveryBranch generates and commits delivery content in a temporary clone.
|
|
func (rm *RepoManager) PublishDeliveryBranch(sourceDir, sourceBranch, repoURL, deliveryBranch string, generate func(string) error) error {
|
|
if sourceDir == "" || sourceBranch == "" || deliveryBranch == "" || deliveryBranch == rm.Branch {
|
|
return fmt.Errorf("delivery source branch and dedicated delivery branch are required and must differ from the base branch")
|
|
}
|
|
temporary, err := os.MkdirTemp("", "maidn-delivery-*")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer os.RemoveAll(temporary)
|
|
if err := runGit("", os.Environ(), "clone", "--no-local", "--branch", sourceBranch, sourceDir, temporary); err != nil {
|
|
return err
|
|
}
|
|
if err := generate(temporary); err != nil {
|
|
return err
|
|
}
|
|
cleanupAskPass, environment, err := rm.gitEnvironment()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer cleanupAskPass()
|
|
if err := runGit(temporary, environment, "checkout", "-B", deliveryBranch); err != nil {
|
|
return err
|
|
}
|
|
if err := runGit(temporary, environment, "add", ".tekton"); err != nil {
|
|
return err
|
|
}
|
|
changed, err := gitDiffQuiet(temporary, environment, "--cached")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if changed {
|
|
for _, args := range [][]string{{"config", "user.name", "Maidn"}, {"config", "user.email", "maidn@free-maidn.com"}, {"commit", "-m", "feat: add Maidn delivery pipeline"}} {
|
|
if err := runGit(temporary, environment, args...); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
hasBranch, err := rm.HasRemoteBranch(repoURL, deliveryBranch)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if hasBranch {
|
|
if err := runGit(temporary, environment, "fetch", repoURL, "refs/heads/"+deliveryBranch); err != nil {
|
|
return err
|
|
}
|
|
different, err := gitDiffQuiet(temporary, environment, "HEAD", "FETCH_HEAD")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !different {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("dedicated delivery branch %q differs from generated content; refusing to overwrite it", deliveryBranch)
|
|
}
|
|
return rm.PushBranch(temporary, repoURL, deliveryBranch)
|
|
}
|
|
|
|
func gitDiffQuiet(dir string, environment []string, args ...string) (bool, error) {
|
|
command := exec.Command("git", append([]string{"diff", "--quiet"}, args...)...)
|
|
command.Dir = dir
|
|
command.Env = environment
|
|
if err := command.Run(); err != nil {
|
|
if exitError, ok := err.(*exec.ExitError); ok && exitError.ExitCode() == 1 {
|
|
return true, nil
|
|
}
|
|
return false, err
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
func (rm *RepoManager) HasRemoteBranch(repoURL, branch string) (bool, error) {
|
|
cleanupAskPass, environment, err := rm.gitEnvironment()
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
defer cleanupAskPass()
|
|
command := exec.Command("git", "ls-remote", "--exit-code", repoURL, "refs/heads/"+branch)
|
|
command.Env = environment
|
|
if err := command.Run(); err != nil {
|
|
if exitError, ok := err.(*exec.ExitError); ok && exitError.ExitCode() == 2 {
|
|
return false, nil
|
|
}
|
|
return false, err
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
func CurrentBranch(dir string) (string, error) {
|
|
command := exec.Command("git", "branch", "--show-current")
|
|
command.Dir = dir
|
|
branch, err := command.Output()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if value := strings.TrimSpace(string(branch)); value != "" {
|
|
return value, nil
|
|
}
|
|
return "", fmt.Errorf("source repository is in detached HEAD state")
|
|
}
|
|
|
|
// EnsureCleanCheckout refuses to publish an ambiguous local worktree.
|
|
func EnsureCleanCheckout(dir string) error {
|
|
command := exec.Command("git", "status", "--porcelain=v1", "--untracked-files=all")
|
|
command.Dir = dir
|
|
status, err := command.Output()
|
|
if err != nil {
|
|
return fmt.Errorf("inspect application checkout: %w", err)
|
|
}
|
|
if len(status) != 0 {
|
|
return errors.New("application checkout has uncommitted changes; commit or discard them before publishing delivery")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// CheckoutOrigin returns the credential-free origin identity used before publishing.
|
|
func CheckoutOrigin(dir string) (string, error) {
|
|
command := exec.Command("git", "remote", "get-url", "origin")
|
|
command.Dir = dir
|
|
output, err := command.Output()
|
|
if err != nil {
|
|
return "", fmt.Errorf("read application checkout origin: %w", err)
|
|
}
|
|
origin := strings.TrimSpace(string(output))
|
|
if origin == "" {
|
|
return "", errors.New("application checkout origin is empty")
|
|
}
|
|
return origin, nil
|
|
}
|
|
|
|
func RepositoryFromURL(repoURL string) (string, string, error) {
|
|
parsed, err := url.Parse(repoURL)
|
|
if err != nil {
|
|
return "", "", err
|
|
}
|
|
parts := strings.Split(strings.Trim(strings.TrimSuffix(parsed.Path, ".git"), "/"), "/")
|
|
if len(parts) < 2 || parts[len(parts)-2] == "" || parts[len(parts)-1] == "" {
|
|
return "", "", fmt.Errorf("invalid Forgejo repository URL")
|
|
}
|
|
return parts[len(parts)-2], parts[len(parts)-1], nil
|
|
}
|
|
|
|
func (rm *RepoManager) EnsureWebhook(repo, webhookURL, authorization string) error {
|
|
if webhookURL == "" || authorization == "" {
|
|
return fmt.Errorf("Forgejo webhook URL and authorization are required")
|
|
}
|
|
endpoint := fmt.Sprintf("%s/api/v1/repos/%s/%s/hooks", rm.BaseURL, rm.Owner, repo)
|
|
var hooks []hook
|
|
status, err := rm.apiJSONRequest(http.MethodGet, endpoint, nil, &hooks)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if status != http.StatusOK {
|
|
return fmt.Errorf("unexpected Forgejo webhook lookup status %d", status)
|
|
}
|
|
createRequest := hookRequest{
|
|
Type: "forgejo",
|
|
Active: true,
|
|
AuthorizationHeader: authorization,
|
|
Config: map[string]string{"url": webhookURL, "content_type": "json"},
|
|
Events: []string{"push", "pull_request"},
|
|
}
|
|
for _, existing := range hooks {
|
|
if existing.URL != webhookURL {
|
|
continue
|
|
}
|
|
request, err := json.Marshal(hookRequest{Active: createRequest.Active, AuthorizationHeader: createRequest.AuthorizationHeader, Config: createRequest.Config, Events: createRequest.Events})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
status, err = rm.apiRequest(http.MethodPatch, fmt.Sprintf("%s/%d", endpoint, existing.ID), request)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if status != http.StatusOK {
|
|
return fmt.Errorf("unexpected Forgejo webhook update status %d", status)
|
|
}
|
|
return nil
|
|
}
|
|
request, err := json.Marshal(createRequest)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
status, err = rm.apiRequest(http.MethodPost, endpoint, request)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if status != http.StatusCreated {
|
|
return fmt.Errorf("unexpected Forgejo webhook create status %d", status)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EnsureProtectedBranch disables direct pushes to the configured production branch.
|
|
func (rm *RepoManager) EnsureProtectedBranch(repo, branch string) error {
|
|
if repo == "" || branch == "" {
|
|
return errors.New("Forgejo repository and production branch are required")
|
|
}
|
|
endpoint := fmt.Sprintf("%s/api/v1/repos/%s/%s/branch_protections", rm.BaseURL, rm.Owner, repo)
|
|
var protections []branchProtection
|
|
status, err := rm.apiJSONRequest(http.MethodGet, endpoint, nil, &protections)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if status != http.StatusOK {
|
|
return fmt.Errorf("unexpected Forgejo branch protection lookup status %d", status)
|
|
}
|
|
var matching []branchProtection
|
|
for _, protection := range protections {
|
|
if protection.BranchName == branch || protection.RuleName == branch {
|
|
matching = append(matching, protection)
|
|
}
|
|
}
|
|
if len(matching) > 1 {
|
|
return fmt.Errorf("multiple Forgejo branch protections match production branch %q", branch)
|
|
}
|
|
if len(matching) == 1 {
|
|
if matching[0].EnablePush || matching[0].EnablePushWhitelist {
|
|
return fmt.Errorf("Forgejo production branch %q permits direct pushes", branch)
|
|
}
|
|
return nil
|
|
}
|
|
body, err := json.Marshal(branchProtectionRequest{BranchName: branch})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
status, err = rm.apiRequest(http.MethodPost, endpoint, body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if status != http.StatusCreated {
|
|
return fmt.Errorf("unexpected Forgejo branch protection create status %d", status)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func commitAndPush(tempDir, repoName, branch string, environment []string) (bool, error) {
|
|
for _, args := range [][]string{{"config", "user.name", "Maidn"}, {"config", "user.email", "maidn@free-maidn.com"}, {"add", "."}} {
|
|
if err := runGit(tempDir, environment, args...); err != nil {
|
|
return false, err
|
|
}
|
|
}
|
|
cmd := exec.Command("git", "status", "--porcelain")
|
|
cmd.Dir = tempDir
|
|
cmd.Env = environment
|
|
output, err := cmd.Output()
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if len(output) == 0 {
|
|
return false, nil
|
|
}
|
|
if err := runGit(tempDir, environment, "commit", "-m", "feat: initialize repository structure for CI/CD"); err != nil {
|
|
return false, err
|
|
}
|
|
if err := runGit(tempDir, environment, "push", "origin", "HEAD:"+branch); err != nil {
|
|
return false, err
|
|
}
|
|
fmt.Printf("[SUCCESS] Changes pushed to %s successfully.\n", repoName)
|
|
return true, nil
|
|
}
|
|
|
|
func (rm *RepoManager) gitEnvironment() (func(), []string, error) {
|
|
return GitEnvironment(rm.Username, rm.Token)
|
|
}
|
|
|
|
// GitEnvironment returns a temporary Git askpass environment without persisting credentials.
|
|
func GitEnvironment(username, token string) (func(), []string, error) {
|
|
if token == "" {
|
|
return func() {}, append(os.Environ(), "GIT_TERMINAL_PROMPT=0"), nil
|
|
}
|
|
askPassDir, err := os.MkdirTemp("", "maidn-askpass-*")
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
path := filepath.Join(askPassDir, "maidn-askpass")
|
|
content := "#!/bin/sh\ncase \"$1\" in *Username*) printf '%s\\n' \"$MAIDN_GIT_USERNAME\" ;; *) printf '%s\\n' \"$MAIDN_GIT_TOKEN\" ;; esac\n"
|
|
if runtime.GOOS == "windows" {
|
|
path += ".cmd"
|
|
content = "@echo off\r\necho %~1 | findstr /I Username >nul\r\nif not errorlevel 1 (echo %MAIDN_GIT_USERNAME%) else (echo %MAIDN_GIT_TOKEN%)\r\n"
|
|
}
|
|
if err := os.WriteFile(path, []byte(content), 0700); err != nil {
|
|
_ = os.RemoveAll(askPassDir)
|
|
return nil, nil, err
|
|
}
|
|
environment := append(os.Environ(), "GIT_ASKPASS="+path, "GIT_TERMINAL_PROMPT=0", "MAIDN_GIT_USERNAME="+username, "MAIDN_GIT_TOKEN="+token)
|
|
return func() { _ = os.RemoveAll(askPassDir) }, environment, nil
|
|
}
|
|
|
|
func runGit(dir string, environment []string, args ...string) error {
|
|
cmd := exec.Command("git", args...)
|
|
cmd.Dir = dir
|
|
cmd.Env = environment
|
|
cmd.Stdout = os.Stdout
|
|
cmd.Stderr = os.Stderr
|
|
return cmd.Run()
|
|
}
|
|
|
|
func (rm *RepoManager) apiRequest(method, endpoint string, body []byte) (int, error) {
|
|
return rm.apiJSONRequest(method, endpoint, body, nil)
|
|
}
|
|
|
|
func (rm *RepoManager) apiJSONRequest(method, endpoint string, body []byte, result any) (int, error) {
|
|
client := rm.HTTPClient
|
|
if client == nil {
|
|
client = &http.Client{Timeout: 15 * time.Second}
|
|
}
|
|
request, err := http.NewRequest(method, endpoint, bytes.NewReader(body))
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
request.Header.Set("Authorization", "token "+rm.Token)
|
|
request.Header.Set("Content-Type", "application/json")
|
|
response, err := client.Do(request)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
defer response.Body.Close()
|
|
if response.StatusCode >= 500 || response.StatusCode == http.StatusUnauthorized || response.StatusCode == http.StatusForbidden {
|
|
return 0, &APIError{StatusCode: response.StatusCode, Status: response.Status}
|
|
}
|
|
if result != nil && response.StatusCode != http.StatusNoContent {
|
|
if err := json.NewDecoder(response.Body).Decode(result); err != nil {
|
|
return 0, err
|
|
}
|
|
}
|
|
return response.StatusCode, nil
|
|
}
|
|
|
|
func FluxSourceURL(baseURL, owner, repo string) string {
|
|
return fmt.Sprintf("%s/%s/%s.git", strings.TrimRight(baseURL, "/"), owner, repo)
|
|
}
|
|
|
|
func CloneURL(baseURL, owner, repo string) string {
|
|
return fmt.Sprintf("%s/%s/%s.git", strings.TrimRight(baseURL, "/"), owner, repo)
|
|
}
|