/
client.go
41 lines (35 loc) · 1.03 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package client
import (
"context"
"github.com/drone/drone-go/drone"
"golang.org/x/oauth2"
)
const (
DroneServerVariable = "DRONE_SERVER"
DroneTokenVariable = "DRONE_TOKEN"
)
type Credential struct {
Server string
Token string
}
// Creates a Drone client with the provided credential.
//
// This function takes a Credential struct as input, containing the Drone host and token.
// It creates an OAuth2 client configuration and authorizes it using the provided access token.
// Then, it constructs a Drone client using the specified host and the authorized OAuth2 client.
//
// Parameters:
// - credential: A Credential struct containing the Drone host and access token.
//
// Returns:
// - drone.Client: A configured Drone client for making API requests to the specified Drone instance.
func CreateClient(credential Credential) drone.Client {
config := new(oauth2.Config)
auther := config.Client(
context.Background(),
&oauth2.Token{
AccessToken: credential.Token,
},
)
return drone.NewClient(credential.Server, auther)
}