2023-02-28 10:44:46 +00:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2022-08-13 15:41:01 +01:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2023-02-15 08:51:14 +00:00
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2022-12-06 08:37:38 +00:00
|
|
|
"code.gitea.io/actions-proto-go/ping/v1/pingv1connect"
|
|
|
|
"code.gitea.io/actions-proto-go/runner/v1/runnerv1connect"
|
2022-11-29 02:35:59 +00:00
|
|
|
"github.com/bufbuild/connect-go"
|
2022-08-13 15:41:01 +01:00
|
|
|
)
|
|
|
|
|
2023-02-15 08:51:14 +00:00
|
|
|
func getHttpClient(endpoint string, insecure bool) *http.Client {
|
|
|
|
if strings.HasPrefix(endpoint, "https://") && insecure {
|
|
|
|
return &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return http.DefaultClient
|
|
|
|
}
|
|
|
|
|
2022-08-13 15:41:01 +01:00
|
|
|
// New returns a new runner client.
|
2023-04-04 14:32:04 +01:00
|
|
|
func New(endpoint string, insecure bool, uuid, token, version string, opts ...connect.ClientOption) *HTTPClient {
|
2022-12-06 08:37:38 +00:00
|
|
|
baseURL := strings.TrimRight(endpoint, "/") + "/api/actions"
|
2022-08-13 15:41:01 +01:00
|
|
|
|
2022-11-29 02:35:59 +00:00
|
|
|
opts = append(opts, connect.WithInterceptors(connect.UnaryInterceptorFunc(func(next connect.UnaryFunc) connect.UnaryFunc {
|
|
|
|
return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
|
|
|
|
if uuid != "" {
|
2023-04-02 15:41:48 +01:00
|
|
|
req.Header().Set(UUIDHeader, uuid)
|
2022-11-29 02:35:59 +00:00
|
|
|
}
|
|
|
|
if token != "" {
|
2023-04-02 15:41:48 +01:00
|
|
|
req.Header().Set(TokenHeader, token)
|
2022-11-29 02:35:59 +00:00
|
|
|
}
|
2023-06-15 04:59:15 +01:00
|
|
|
// TODO: version will be removed from request header after Gitea 1.20 released.
|
2023-04-04 14:32:04 +01:00
|
|
|
if version != "" {
|
|
|
|
req.Header().Set(VersionHeader, version)
|
2023-03-13 10:57:35 +00:00
|
|
|
}
|
2022-11-29 02:35:59 +00:00
|
|
|
return next(ctx, req)
|
2022-08-13 15:41:01 +01:00
|
|
|
}
|
2022-11-29 02:35:59 +00:00
|
|
|
})))
|
2022-11-23 03:42:02 +00:00
|
|
|
|
2022-09-25 11:54:00 +01:00
|
|
|
return &HTTPClient{
|
|
|
|
PingServiceClient: pingv1connect.NewPingServiceClient(
|
2023-02-15 08:51:14 +00:00
|
|
|
getHttpClient(endpoint, insecure),
|
2022-11-23 03:42:02 +00:00
|
|
|
baseURL,
|
2022-11-29 02:35:59 +00:00
|
|
|
opts...,
|
2022-09-25 11:54:00 +01:00
|
|
|
),
|
|
|
|
RunnerServiceClient: runnerv1connect.NewRunnerServiceClient(
|
2023-02-15 08:51:14 +00:00
|
|
|
getHttpClient(endpoint, insecure),
|
2022-11-23 03:42:02 +00:00
|
|
|
baseURL,
|
2022-11-29 02:35:59 +00:00
|
|
|
opts...,
|
2022-09-25 11:54:00 +01:00
|
|
|
),
|
2022-11-15 14:42:41 +00:00
|
|
|
endpoint: endpoint,
|
2023-02-15 08:51:14 +00:00
|
|
|
insecure: insecure,
|
2022-09-25 11:54:00 +01:00
|
|
|
}
|
2022-08-13 15:41:01 +01:00
|
|
|
}
|
|
|
|
|
2022-11-15 14:42:41 +00:00
|
|
|
func (c *HTTPClient) Address() string {
|
|
|
|
return c.endpoint
|
|
|
|
}
|
|
|
|
|
2023-02-15 08:51:14 +00:00
|
|
|
func (c *HTTPClient) Insecure() bool {
|
|
|
|
return c.insecure
|
|
|
|
}
|
|
|
|
|
2022-08-28 07:05:56 +01:00
|
|
|
var _ Client = (*HTTPClient)(nil)
|
|
|
|
|
2022-08-13 15:41:01 +01:00
|
|
|
// An HTTPClient manages communication with the runner API.
|
|
|
|
type HTTPClient struct {
|
2022-09-25 11:54:00 +01:00
|
|
|
pingv1connect.PingServiceClient
|
|
|
|
runnerv1connect.RunnerServiceClient
|
2022-11-15 14:42:41 +00:00
|
|
|
endpoint string
|
2023-02-15 08:51:14 +00:00
|
|
|
insecure bool
|
2022-09-03 13:57:32 +01:00
|
|
|
}
|