feat: move to forgejo sdk
This commit is contained in:
parent
d9bf396917
commit
9e053a1c5c
35 changed files with 316 additions and 714 deletions
81
forgejo/config.go
Normal file
81
forgejo/config.go
Normal file
|
@ -0,0 +1,81 @@
|
|||
package forgejo
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"codeberg.org/mvdkleijn/forgejo-sdk/forgejo/v2"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
|
||||
)
|
||||
|
||||
// Config is per-provider, specifies where to connect to forgejo
|
||||
type Config struct {
|
||||
Token string
|
||||
Username string
|
||||
Password string
|
||||
BaseURL string
|
||||
Insecure bool
|
||||
CACertFile string
|
||||
}
|
||||
|
||||
// Client returns a *forgejo.Client to interact with the configured forgejo instance
|
||||
func (c *Config) Client() (interface{}, error) {
|
||||
|
||||
if c.Token == "" && c.Username == "" {
|
||||
return nil, fmt.Errorf("either a token or a username needs to be used")
|
||||
}
|
||||
// Configure TLS/SSL
|
||||
var tlsConfig tls.Config
|
||||
// If a CACertFile has been specified, use that for cert validation
|
||||
if c.CACertFile != "" {
|
||||
caCert, err := ioutil.ReadFile(c.CACertFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
caCertPool := x509.NewCertPool()
|
||||
caCertPool.AppendCertsFromPEM(caCert)
|
||||
tlsConfig.RootCAs = caCertPool
|
||||
}
|
||||
|
||||
// If configured as insecure, turn off SSL verification
|
||||
tlsConfig.InsecureSkipVerify = c.Insecure
|
||||
|
||||
t := http.DefaultTransport.(*http.Transport).Clone()
|
||||
t.TLSClientConfig = &tlsConfig
|
||||
t.MaxIdleConnsPerHost = 100
|
||||
t.TLSHandshakeTimeout = 10 * time.Second
|
||||
|
||||
httpClient := &http.Client{
|
||||
Transport: logging.NewTransport("Gitea", t),
|
||||
}
|
||||
|
||||
if c.BaseURL == "" {
|
||||
c.BaseURL = "https://forgejo.com"
|
||||
}
|
||||
|
||||
var client *forgejo.Client
|
||||
var err error
|
||||
if c.Token != "" {
|
||||
client, err = forgejo.NewClient(c.BaseURL, forgejo.SetToken(c.Token), forgejo.SetHTTPClient(httpClient))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if c.Username != "" {
|
||||
client, err = forgejo.NewClient(c.BaseURL, forgejo.SetBasicAuth(c.Username, c.Password), forgejo.SetHTTPClient(httpClient))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
// Test the credentials by checking we can get information about the authenticated user.
|
||||
_, _, err = client.GetMyUserInfo()
|
||||
|
||||
return client, err
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue