github.com/argoproj/argo-cd/v3@v3.2.1/applicationset/services/scm_provider/gitea.go (about)

     1  package scm_provider
     2  
     3  import (
     4  	"context"
     5  	"crypto/tls"
     6  	"fmt"
     7  	"net/http"
     8  	"net/http/cookiejar"
     9  	"os"
    10  
    11  	"code.gitea.io/sdk/gitea"
    12  )
    13  
    14  type GiteaProvider struct {
    15  	client      *gitea.Client
    16  	owner       string
    17  	allBranches bool
    18  }
    19  
    20  var _ SCMProviderService = &GiteaProvider{}
    21  
    22  func NewGiteaProvider(owner, token, url string, allBranches, insecure bool) (*GiteaProvider, error) {
    23  	if token == "" {
    24  		token = os.Getenv("GITEA_TOKEN")
    25  	}
    26  	httpClient := &http.Client{}
    27  	if insecure {
    28  		cookieJar, _ := cookiejar.New(nil)
    29  
    30  		tr := http.DefaultTransport.(*http.Transport).Clone()
    31  		tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
    32  
    33  		httpClient = &http.Client{
    34  			Jar:       cookieJar,
    35  			Transport: tr,
    36  		}
    37  	}
    38  	client, err := gitea.NewClient(url, gitea.SetToken(token), gitea.SetHTTPClient(httpClient))
    39  	if err != nil {
    40  		return nil, fmt.Errorf("error creating a new gitea client: %w", err)
    41  	}
    42  	return &GiteaProvider{
    43  		client:      client,
    44  		owner:       owner,
    45  		allBranches: allBranches,
    46  	}, nil
    47  }
    48  
    49  func (g *GiteaProvider) GetBranches(_ context.Context, repo *Repository) ([]*Repository, error) {
    50  	if !g.allBranches {
    51  		branch, status, err := g.client.GetRepoBranch(g.owner, repo.Repository, repo.Branch)
    52  		if status.StatusCode == http.StatusNotFound {
    53  			return nil, fmt.Errorf("got 404 while getting default branch %q for repo %q - check your repo config: %w", repo.Branch, repo.Repository, err)
    54  		}
    55  		if err != nil {
    56  			return nil, err
    57  		}
    58  		return []*Repository{
    59  			{
    60  				Organization: repo.Organization,
    61  				Repository:   repo.Repository,
    62  				Branch:       repo.Branch,
    63  				URL:          repo.URL,
    64  				SHA:          branch.Commit.ID,
    65  				Labels:       repo.Labels,
    66  				RepositoryId: repo.RepositoryId,
    67  			},
    68  		}, nil
    69  	}
    70  	repos := []*Repository{}
    71  	opts := gitea.ListRepoBranchesOptions{}
    72  	branches, _, err := g.client.ListRepoBranches(g.owner, repo.Repository, opts)
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  	for _, branch := range branches {
    77  		repos = append(repos, &Repository{
    78  			Organization: repo.Organization,
    79  			Repository:   repo.Repository,
    80  			Branch:       branch.Name,
    81  			URL:          repo.URL,
    82  			SHA:          branch.Commit.ID,
    83  			Labels:       repo.Labels,
    84  			RepositoryId: repo.RepositoryId,
    85  		})
    86  	}
    87  	return repos, nil
    88  }
    89  
    90  func (g *GiteaProvider) ListRepos(_ context.Context, cloneProtocol string) ([]*Repository, error) {
    91  	repos := []*Repository{}
    92  	repoOpts := gitea.ListOrgReposOptions{}
    93  	giteaRepos, _, err := g.client.ListOrgRepos(g.owner, repoOpts)
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  	for _, repo := range giteaRepos {
    98  		var url string
    99  		switch cloneProtocol {
   100  		// Default to SSH if unspecified (i.e. if "").
   101  		case "", "ssh":
   102  			url = repo.SSHURL
   103  		case "https":
   104  			url = repo.HTMLURL
   105  		default:
   106  			return nil, fmt.Errorf("unknown clone protocol for GitHub %v", cloneProtocol)
   107  		}
   108  		labelOpts := gitea.ListLabelsOptions{}
   109  		giteaLabels, _, err := g.client.ListRepoLabels(g.owner, repo.Name, labelOpts)
   110  		if err != nil {
   111  			return nil, err
   112  		}
   113  		labels := []string{}
   114  		for _, label := range giteaLabels {
   115  			labels = append(labels, label.Name)
   116  		}
   117  		repos = append(repos, &Repository{
   118  			Organization: g.owner,
   119  			Repository:   repo.Name,
   120  			Branch:       repo.DefaultBranch,
   121  			URL:          url,
   122  			Labels:       labels,
   123  			RepositoryId: int(repo.ID),
   124  		})
   125  	}
   126  	return repos, nil
   127  }
   128  
   129  func (g *GiteaProvider) RepoHasPath(_ context.Context, repo *Repository, path string) (bool, error) {
   130  	_, resp, err := g.client.GetContents(repo.Organization, repo.Repository, repo.Branch, path)
   131  	if resp != nil && resp.StatusCode == http.StatusNotFound {
   132  		return false, nil
   133  	}
   134  	if err != nil {
   135  		if err.Error() == "expect file, got directory" {
   136  			return true, nil
   137  		}
   138  		return false, err
   139  	}
   140  	return true, nil
   141  }