-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
104 lines (85 loc) · 2.91 KB
/
server.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package cli implements the commands for the github plugin CLI.
package cli
import (
"context"
"fmt"
"github.com/abcxyz/jvs-plugin-github/pkg/plugin"
"github.com/abcxyz/jvs-plugin-github/pkg/plugin/keyutil"
"github.com/abcxyz/pkg/cli"
"github.com/abcxyz/pkg/githubapp"
"github.com/abcxyz/pkg/logging"
"github.com/google/go-github/v55/github"
jvspb "github.com/abcxyz/jvs/apis/v0"
goplugin "github.com/hashicorp/go-plugin"
)
type ServerCommand struct {
cli.BaseCommand
cfg *plugin.PluginConfig
}
func (c *ServerCommand) Desc() string {
return `Start GitHub Plugin`
}
func (c *ServerCommand) Help() string {
return `
Usage: {{ COMMAND }} [options]
Start a GitHub Plugin.
`
}
func (c *ServerCommand) Flags() *cli.FlagSet {
c.cfg = &plugin.PluginConfig{}
set := c.NewFlagSet()
return c.cfg.ToFlags(set)
}
func (c *ServerCommand) Run(ctx context.Context, args []string) error {
p, err := c.RunUnstarted(ctx, args)
if err != nil {
return fmt.Errorf("failed to instantiate github plugin: %w", err)
}
goplugin.Serve(&goplugin.ServeConfig{
HandshakeConfig: jvspb.Handshake,
Plugins: map[string]goplugin.Plugin{
"jvs-plugin-github": &jvspb.ValidatorPlugin{Impl: p},
},
// A non-nil value here enables gRPC serving for this plugin.
GRPCServer: goplugin.DefaultGRPCServer,
})
return nil
}
func (c *ServerCommand) RunUnstarted(ctx context.Context, args []string) (*plugin.GitHubPlugin, error) {
f := c.Flags()
if err := f.Parse(args); err != nil {
return nil, fmt.Errorf("failed to parse flags: %w", err)
}
args = f.Args()
if len(args) > 0 {
return nil, fmt.Errorf("unexpected arguments: %q", args)
}
logger := logging.FromContext(ctx)
if err := c.cfg.Validate(); err != nil {
return nil, fmt.Errorf("invalid configuration: %w", err)
}
logger.DebugContext(ctx, "loaded configuration", "GitHubAppID", c.cfg.GitHubAppID, "GitHubAppInstallationID", c.cfg.GitHubAppInstallationID)
// If a nil httpClient is provided, a new http.Client will be used.
ghClient := github.NewClient(nil)
pk, err := keyutil.ReadRSAPrivateKey(c.cfg.GitHubAppPrivateKeyPEM)
if err != nil {
return nil, fmt.Errorf("invalid private key pem: %w", err)
}
ghAppCfg := githubapp.NewConfig(c.cfg.GitHubAppID, c.cfg.GitHubAppInstallationID, pk)
ghApp := githubapp.New(ghAppCfg)
p := plugin.NewGitHubPlugin(ctx, ghClient, ghApp, c.cfg)
return p, nil
}