mirror of
https://gitea.com/gitea/act_runner.git
synced 2024-12-30 21:07:09 +01:00
db662b3690
- Removed `deadcode`, `structcheck`, and `varcheck` linters from `.golangci.yml` - Fixed a typo in a comment in `daemon.go` - Renamed `defaultActionsUrl` to `defaultActionsURL` in `exec.go` - Removed unnecessary else clause in `exec.go` and `runner.go` - Simplified variable initialization in `exec.go` - Changed function name from `getHttpClient` to `getHTTPClient` in `http.go` - Removed unnecessary else clause in `labels_test.go` Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com> Reviewed-on: https://gitea.com/gitea/act_runner/pulls/289 Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com> Co-committed-by: Bo-Yi Wu <appleboy.tw@gmail.com>
83 lines
2.0 KiB
Go
83 lines
2.0 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package client
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"code.gitea.io/actions-proto-go/ping/v1/pingv1connect"
|
|
"code.gitea.io/actions-proto-go/runner/v1/runnerv1connect"
|
|
"github.com/bufbuild/connect-go"
|
|
)
|
|
|
|
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
|
|
}
|
|
|
|
// New returns a new runner client.
|
|
func New(endpoint string, insecure bool, uuid, token, version string, opts ...connect.ClientOption) *HTTPClient {
|
|
baseURL := strings.TrimRight(endpoint, "/") + "/api/actions"
|
|
|
|
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 != "" {
|
|
req.Header().Set(UUIDHeader, uuid)
|
|
}
|
|
if token != "" {
|
|
req.Header().Set(TokenHeader, token)
|
|
}
|
|
// TODO: version will be removed from request header after Gitea 1.20 released.
|
|
if version != "" {
|
|
req.Header().Set(VersionHeader, version)
|
|
}
|
|
return next(ctx, req)
|
|
}
|
|
})))
|
|
|
|
return &HTTPClient{
|
|
PingServiceClient: pingv1connect.NewPingServiceClient(
|
|
getHTTPClient(endpoint, insecure),
|
|
baseURL,
|
|
opts...,
|
|
),
|
|
RunnerServiceClient: runnerv1connect.NewRunnerServiceClient(
|
|
getHTTPClient(endpoint, insecure),
|
|
baseURL,
|
|
opts...,
|
|
),
|
|
endpoint: endpoint,
|
|
insecure: insecure,
|
|
}
|
|
}
|
|
|
|
func (c *HTTPClient) Address() string {
|
|
return c.endpoint
|
|
}
|
|
|
|
func (c *HTTPClient) Insecure() bool {
|
|
return c.insecure
|
|
}
|
|
|
|
var _ Client = (*HTTPClient)(nil)
|
|
|
|
// An HTTPClient manages communication with the runner API.
|
|
type HTTPClient struct {
|
|
pingv1connect.PingServiceClient
|
|
runnerv1connect.RunnerServiceClient
|
|
endpoint string
|
|
insecure bool
|
|
}
|