/
main.go
78 lines (66 loc) · 2.07 KB
/
main.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"context"
"fmt"
"github.com/fsamin/go-repo"
"github.com/golang/protobuf/ptypes/empty"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/grpcplugin/actionplugin"
)
type checkoutPlugin struct {
actionplugin.Common
}
func (actPlugin *checkoutPlugin) Manifest(_ context.Context, _ *empty.Empty) (*actionplugin.ActionPluginManifest, error) {
return &actionplugin.ActionPluginManifest{
Name: "plugin-checkout",
Author: "Steven GUIHEUX <steven.guiheux@ovhcloud.com>",
Description: `This action checkout a git repository`,
Version: sdk.VERSION,
}, nil
}
func (actPlugin *checkoutPlugin) Run(ctx context.Context, q *actionplugin.ActionQuery) (*actionplugin.ActionResult, error) {
gitURL := q.GetOptions()["git-url"]
ref := q.GetOptions()["ref"]
sshKey := q.GetOptions()["ssh-key"]
path := q.GetOptions()["path"]
authUsername := q.GetOptions()["username"]
authToken := q.GetOptions()["token"]
submodules := q.GetOptions()["submodules"]
var authOption repo.Option
if sshKey == "" {
authOption = repo.WithHTTPAuth(authUsername, authToken)
} else {
authOption = repo.WithSSHAuth([]byte(sshKey))
}
fmt.Printf("Start cloning %s\n", gitURL)
clonedRepo, err := repo.Clone(ctx, path, gitURL, authOption)
if err != nil {
return nil, fmt.Errorf("unable to clone the repository %s: %v\n", gitURL, err)
}
if err := clonedRepo.Checkout(ctx, ref); err != nil {
return nil, fmt.Errorf("unable to git checkout on ref %s: %v", ref, err)
}
if submodules == "true" || submodules == "recursive" {
subMod := repo.SubmoduleOpt{
Init: true,
}
if submodules == "recursive" {
subMod.Recursive = true
}
fmt.Printf("Start updating submodules\n")
if err := clonedRepo.SubmoduleUpdate(ctx, subMod); err != nil {
return nil, fmt.Errorf("unable to update submodule: %v", err)
}
}
fmt.Printf("Checkout completed\n")
return &actionplugin.ActionResult{
Status: sdk.StatusSuccess,
}, nil
}
func main() {
actPlugin := checkoutPlugin{}
if err := actionplugin.Start(context.Background(), &actPlugin); err != nil {
panic(err)
}
return
}