Skip to content

Commit 652c756

Browse files
feat: Git committer should use username/email from plural creds (#582)
* git committer should use the username and email from the given plural creds * update mock client * dont use author flag * git committer set in cloneRepo now * swallow error
1 parent d3a68e5 commit 652c756

File tree

9 files changed

+107
-5
lines changed

9 files changed

+107
-5
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ require (
3737
github.com/openshift/api v0.0.0-20250908150922-8634aa495a26
3838
github.com/orcaman/concurrent-map/v2 v2.0.1
3939
github.com/pkg/errors v0.9.1
40-
github.com/pluralsh/console/go/client v1.54.2
40+
github.com/pluralsh/console/go/client v1.54.3
4141
github.com/pluralsh/controller-reconcile-helper v0.1.0
4242
github.com/pluralsh/gophoenix v0.1.3-0.20231201014135-dff1b4309e34
4343
github.com/pluralsh/polly v0.3.3

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -879,8 +879,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
879879
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
880880
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 h1:GFCKgmp0tecUJ0sJuv4pzYCqS9+RGSn52M3FUwPs+uo=
881881
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10/go.mod h1:t/avpk3KcrXxUnYOhZhMXJlSEyie6gQbtLq5NM3loB8=
882-
github.com/pluralsh/console/go/client v1.54.2 h1:fmu/xfs58YzY7gBloE+vlOeolTdi6EfQ5pOrgSOY/bQ=
883-
github.com/pluralsh/console/go/client v1.54.2/go.mod h1:EwMrcI23s61oLY3etCVc3NE5RYxgfoiokvU0O7KDOQg=
882+
github.com/pluralsh/console/go/client v1.54.3 h1:J/GDYuHFAUaTlsRv1tiUPfXrq5RElFTJKwqAvkn5+88=
883+
github.com/pluralsh/console/go/client v1.54.3/go.mod h1:EwMrcI23s61oLY3etCVc3NE5RYxgfoiokvU0O7KDOQg=
884884
github.com/pluralsh/controller-reconcile-helper v0.1.0 h1:BV3dYZFH5rn8ZvZjtpkACSv/GmLEtRftNQj/Y4ddHEo=
885885
github.com/pluralsh/controller-reconcile-helper v0.1.0/go.mod h1:RxAbvSB4/jkvx616krCdNQXPbpGJXW3J1L3rASxeFOA=
886886
github.com/pluralsh/gophoenix v0.1.3-0.20231201014135-dff1b4309e34 h1:ab2PN+6if/Aq3/sJM0AVdy1SYuMAnq4g20VaKhTm/Bw=

pkg/agentrun-harness/controller/controller.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
gqlclient "github.com/pluralsh/console/go/client"
99
"k8s.io/klog/v2"
1010

11+
"github.com/pluralsh/deployment-operator/pkg/client"
12+
1113
agentrunv1 "github.com/pluralsh/deployment-operator/pkg/agentrun-harness/agentrun/v1"
1214
"github.com/pluralsh/deployment-operator/pkg/agentrun-harness/environment"
1315
"github.com/pluralsh/deployment-operator/pkg/agentrun-harness/tool"
@@ -81,9 +83,11 @@ func (in *agentRunController) Start(ctx context.Context) (retErr error) {
8183

8284
// prepare sets up the agent run environment and AI credentials
8385
func (in *agentRunController) prepare() error {
86+
consoleTokenClient := client.New(fmt.Sprintf("%s/gql", in.consoleUrl), *in.agentRun.PluralCreds.Token)
8487
env := environment.New(
8588
environment.WithAgentRun(in.agentRun),
8689
environment.WithWorkingDir(in.dir),
90+
environment.WithConsoleTokenClient(consoleTokenClient),
8791
)
8892

8993
if err := env.Setup(); err != nil {

pkg/agentrun-harness/environment/environment.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,32 @@ func (in *environment) cloneRepository() error {
6262
return err
6363
}
6464

65+
var userName, userEmail string
66+
if in.consoleTokenClient != nil {
67+
user, _ := in.consoleTokenClient.Me()
68+
if user != nil && user.Name != "" && user.Email != "" {
69+
userName = user.Name
70+
userEmail = user.Email
71+
}
72+
}
73+
74+
if userName == "" && in.agentRun.ScmCreds != nil {
75+
userName = in.agentRun.ScmCreds.Username
76+
}
77+
if userEmail == "" {
78+
userEmail = "[email protected]" // fallback
79+
}
80+
6581
repoDirPath := path.Join(in.dir, repoDir)
6682
if err := exec.NewExecutable("git",
67-
exec.WithArgs([]string{"config", "user.name", in.agentRun.ScmCreds.Username}),
83+
exec.WithArgs([]string{"config", "user.name", userName}),
6884
exec.WithDir(repoDirPath),
6985
).Run(context.Background()); err != nil {
7086
return err
7187
}
7288

7389
if err := exec.NewExecutable("git",
74-
exec.WithArgs([]string{"config", "user.email", "[email protected]"}),
90+
exec.WithArgs([]string{"config", "user.email", userEmail}),
7591
exec.WithDir(repoDirPath),
7692
).Run(context.Background()); err != nil {
7793
return err

pkg/agentrun-harness/environment/environment_options.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package environment
22

33
import (
44
v1 "github.com/pluralsh/deployment-operator/pkg/agentrun-harness/agentrun/v1"
5+
console "github.com/pluralsh/deployment-operator/pkg/client"
56
)
67

78
// WithWorkingDir allows changing the default working directory of the Environment.
@@ -18,3 +19,9 @@ func WithAgentRun(agentRun *v1.AgentRun) Option {
1819
e.agentRun = agentRun
1920
}
2021
}
22+
23+
func WithConsoleTokenClient(client console.Client) Option {
24+
return func(e *environment) {
25+
e.consoleTokenClient = client
26+
}
27+
}

pkg/agentrun-harness/environment/environment_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package environment
22

33
import (
44
v1 "github.com/pluralsh/deployment-operator/pkg/agentrun-harness/agentrun/v1"
5+
console "github.com/pluralsh/deployment-operator/pkg/client"
56
)
67

78
// environment implements Environment interface.
@@ -13,6 +14,8 @@ type environment struct {
1314
agentRun *v1.AgentRun
1415
// dir is the working directory where the repository will be cloned.
1516
dir string
17+
// console is the Plural Console Client
18+
consoleTokenClient console.Client // Console token client for Me()
1619
}
1720

1821
// Option allows modifying Environment behavior.

pkg/client/common.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package client
22

33
import (
4+
"fmt"
5+
46
console "github.com/pluralsh/console/go/client"
57
)
68

@@ -21,3 +23,15 @@ func (c *client) GetGroup(name string) (*console.GroupFragment, error) {
2123

2224
return getGroup.Group, nil
2325
}
26+
27+
func (c *client) Me() (*console.Me_Me, error) {
28+
response, err := c.consoleClient.Me(c.ctx)
29+
if err != nil {
30+
return nil, err
31+
}
32+
if response == nil || response.Me == nil {
33+
return nil, fmt.Errorf("me query returned nil")
34+
}
35+
36+
return response.Me, nil
37+
}

pkg/client/console.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,5 @@ type Client interface {
9797
ListClusterSentinelRunJobs(after *string, first *int64) (*console.ListClusterSentinelRunJobs_ClusterSentinelRunJobs, error)
9898
UpdateSentinelRunJobStatus(id string, attr *console.SentinelRunJobUpdateAttributes) error
9999
CreateAgentMessage(ctx context.Context, runID string, attrs console.AgentMessageAttributes) (*console.CreateAgentMessage_CreateAgentMessage, error)
100+
Me() (*console.Me_Me, error)
100101
}

pkg/test/mocks/Client_mock.go

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)