Skip to content

Commit d66f4c1

Browse files
GCP Buildpacks Teamcopybara-github
authored andcommitted
Add monorepo name to the builder metadata and set monorepo metadata within monorepo buildpacks.
PiperOrigin-RevId: 799611698 Change-Id: Ia7a205e0db5519fadc86c53f19fddcb4f546f316
1 parent 13dd8bb commit d66f4c1

File tree

9 files changed

+53
-15
lines changed

9 files changed

+53
-15
lines changed

cmd/nodejs/firebasenx/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ go_binary(
3636
"-w",
3737
],
3838
deps = [
39+
"//pkg/buildermetadata",
3940
"//pkg/env",
4041
"//pkg/firebase/util",
4142
"//pkg/gcpbuildpack",
@@ -50,5 +51,6 @@ go_test(
5051
rundir = ".",
5152
deps = [
5253
"//internal/buildpacktest",
54+
"//pkg/buildermetadata",
5355
],
5456
)

cmd/nodejs/firebasenx/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"strings"
2222

23+
"github.com/GoogleCloudPlatform/buildpacks/pkg/buildermetadata"
2324
"github.com/GoogleCloudPlatform/buildpacks/pkg/env"
2425
"github.com/GoogleCloudPlatform/buildpacks/pkg/firebase/util"
2526
gcp "github.com/GoogleCloudPlatform/buildpacks/pkg/gcpbuildpack"
@@ -98,5 +99,8 @@ func buildFn(ctx *gcp.Context) error {
9899
// to the Nx API.
99100
nxl.BuildEnvironment.Override(nxNoCloud, "true")
100101

102+
// add nx as the monorepo name to the builder metadata
103+
buildermetadata.GlobalBuilderMetadata().SetValue(buildermetadata.MonorepoName, "nx")
104+
101105
return nil
102106
}

cmd/nodejs/firebasenx/main_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
bpt "github.com/GoogleCloudPlatform/buildpacks/internal/buildpacktest"
7+
bmd "github.com/GoogleCloudPlatform/buildpacks/pkg/buildermetadata"
78
)
89

910
func TestDetect(t *testing.T) {
@@ -47,10 +48,11 @@ func TestDetect(t *testing.T) {
4748

4849
func TestBuild(t *testing.T) {
4950
testCases := []struct {
50-
name string
51-
envs []string
52-
files map[string]string
53-
wantExitCode int
51+
name string
52+
envs []string
53+
files map[string]string
54+
wantExitCode int
55+
wantBuilderMetadata map[bmd.MetadataID]bmd.MetadataValue
5456
}{
5557
{
5658
name: "successfully read project.json",
@@ -70,6 +72,9 @@ func TestBuild(t *testing.T) {
7072
}`,
7173
},
7274
wantExitCode: 0,
75+
wantBuilderMetadata: map[bmd.MetadataID]bmd.MetadataValue{
76+
bmd.MonorepoName: bmd.MetadataValue("nx"),
77+
},
7378
},
7479
{
7580
name: "ambiguous project name",
@@ -105,6 +110,11 @@ func TestBuild(t *testing.T) {
105110
if result.ExitCode != tc.wantExitCode {
106111
t.Fatalf("build exit code mismatch, got: %d, want: %d", result.ExitCode, tc.wantExitCode)
107112
}
113+
for id, m := range tc.wantBuilderMetadata {
114+
if m != result.MetadataAdded()[id] {
115+
t.Errorf("builder metadata %q mismatch, got: %s, want: %s", bmd.MetadataIDNames[id], result.MetadataAdded()[id], m)
116+
}
117+
}
108118
})
109119
}
110120
}

cmd/nodejs/turborepo/BUILD.bazel

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ go_binary(
3636
"-w",
3737
],
3838
deps = [
39+
"//pkg/buildermetadata",
3940
"//pkg/firebase/util",
4041
"//pkg/gcpbuildpack",
4142
"//pkg/nodejs",
@@ -47,5 +48,8 @@ go_test(
4748
srcs = ["main_test.go"],
4849
embed = [":main"],
4950
rundir = ".",
50-
deps = ["//internal/buildpacktest"],
51+
deps = [
52+
"//internal/buildpacktest",
53+
"//pkg/buildermetadata",
54+
],
5155
)

cmd/nodejs/turborepo/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"fmt"
2121
"strings"
2222

23+
"github.com/GoogleCloudPlatform/buildpacks/pkg/buildermetadata"
2324
"github.com/GoogleCloudPlatform/buildpacks/pkg/firebase/util"
2425
gcp "github.com/GoogleCloudPlatform/buildpacks/pkg/gcpbuildpack"
2526
"github.com/GoogleCloudPlatform/buildpacks/pkg/nodejs"
@@ -85,5 +86,8 @@ func buildFn(ctx *gcp.Context) error {
8586
turbol.BuildEnvironment.Override(monorepoCommand, "turbo")
8687
turbol.BuildEnvironment.Override(monorepoBuildArgs, strings.Join(buildArgs, ","))
8788

89+
// add turbo as the monorepo name to the builder metadata
90+
buildermetadata.GlobalBuilderMetadata().SetValue(buildermetadata.MonorepoName, "turbo")
91+
8892
return nil
8993
}

cmd/nodejs/turborepo/main_test.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
bpt "github.com/GoogleCloudPlatform/buildpacks/internal/buildpacktest"
7+
bmd "github.com/GoogleCloudPlatform/buildpacks/pkg/buildermetadata"
78
)
89

910
func TestDetect(t *testing.T) {
@@ -38,10 +39,11 @@ func TestDetect(t *testing.T) {
3839

3940
func TestBuild(t *testing.T) {
4041
testCases := []struct {
41-
name string
42-
envs []string
43-
files map[string]string
44-
wantExitCode int
42+
name string
43+
envs []string
44+
files map[string]string
45+
wantExitCode int
46+
wantBuilderMetadata map[bmd.MetadataID]bmd.MetadataValue
4547
}{
4648
{
4749
name: "successfully_reads_turbo_json_and_app_package_json",
@@ -61,6 +63,9 @@ func TestBuild(t *testing.T) {
6163
}`,
6264
},
6365
wantExitCode: 0,
66+
wantBuilderMetadata: map[bmd.MetadataID]bmd.MetadataValue{
67+
bmd.MonorepoName: bmd.MetadataValue("turbo"),
68+
},
6469
},
6570
{
6671
name: "ambiguous_application_name",
@@ -96,6 +101,11 @@ func TestBuild(t *testing.T) {
96101
if result.ExitCode != tc.wantExitCode {
97102
t.Fatalf("build exit code mismatch, got: %d, want: %d", result.ExitCode, tc.wantExitCode)
98103
}
104+
for id, m := range tc.wantBuilderMetadata {
105+
if m != result.MetadataAdded()[id] {
106+
t.Errorf("builder metadata %q mismatch, got: %s, want: %s", bmd.MetadataIDNames[id], result.MetadataAdded()[id], m)
107+
}
108+
}
99109
})
100110
}
101111
}

pkg/buildermetadata/buildermetadata.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ const (
5656
FrameworkVersion MetadataID = "4" // The framework version used in the build.
5757
AdapterName MetadataID = "5" // The adapter name used in the build.
5858
AdapterVersion MetadataID = "6" // The adapter version used in the build.
59+
MonorepoName MetadataID = "7" // The monorepo name used in the build.
5960
)
6061

6162
// MetadataIDNames is a lookup map from MetadataID to its name.
@@ -66,6 +67,7 @@ var MetadataIDNames = map[MetadataID]string{
6667
FrameworkVersion: "FrameworkVersion",
6768
AdapterName: "AdapterName",
6869
AdapterVersion: "AdapterVersion",
70+
MonorepoName: "MonorepoName",
6971
}
7072

7173
// GetValue returns the Metadata value with MetadataID id, or creates it if it doesn't exist.

pkg/buildermetadata/buildermetadata_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ func TestBuilderMetadataUnmarshalJSON(t *testing.T) {
3737
},
3838
{
3939
name: "basic metadata",
40-
input: []byte(`{"m":{"1":"true","2":"false","3":"angular","4":"17.0.0","5":"@apphosting/adapter-angular","6":"17.2.3"}}`),
40+
input: []byte(`{"m":{"1":"true","2":"false","3":"angular","4":"17.0.0","5":"@apphosting/adapter-angular","6":"17.2.3","7":"nx"}}`),
4141
want: BuilderMetadata{
42-
map[MetadataID]MetadataValue{"1": "true", "2": "false", "3": "angular", "4": "17.0.0", "5": "@apphosting/adapter-angular", "6": "17.2.3"},
42+
map[MetadataID]MetadataValue{"1": "true", "2": "false", "3": "angular", "4": "17.0.0", "5": "@apphosting/adapter-angular", "6": "17.2.3", "7": "nx"},
4343
},
4444
},
4545
}
@@ -72,8 +72,8 @@ func TestBuilderMetadataMarshalJSON(t *testing.T) {
7272
},
7373
{
7474
name: "basic metadata",
75-
input: BuilderMetadata{map[MetadataID]MetadataValue{"1": "true", "2": "false", "3": "angular", "4": "17.0.0", "5": "@apphosting/adapter-angular", "6": "17.2.3"}},
76-
want: []byte(`{"m":{"1":"true","2":"false","3":"angular","4":"17.0.0","5":"@apphosting/adapter-angular","6":"17.2.3"}}`),
75+
input: BuilderMetadata{map[MetadataID]MetadataValue{"1": "true", "2": "false", "3": "angular", "4": "17.0.0", "5": "@apphosting/adapter-angular", "6": "17.2.3", "7": "nx"}},
76+
want: []byte(`{"m":{"1":"true","2":"false","3":"angular","4":"17.0.0","5":"@apphosting/adapter-angular","6":"17.2.3","7":"nx"}}`),
7777
},
7878
}
7979

pkg/builderoutput/builderoutput_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestFromJSON(t *testing.T) {
3838
"errorMessage": "error-message",
3939
"anotherThing": 123
4040
},
41-
"metadata": {"m":{"1":"true", "2":"false", "3":"angular", "4":"17.0.0", "5":"@apphosting/adapter-angular", "6":"17.2.3"}},
41+
"metadata": {"m":{"1":"true", "2":"false", "3":"angular", "4":"17.0.0", "5":"@apphosting/adapter-angular", "6":"17.2.3", "7":"nx"}},
4242
"stats": [
4343
{
4444
"buildpackId": "buildpack-1",
@@ -77,6 +77,7 @@ func TestFromJSON(t *testing.T) {
7777
fm.SetValue(buildermetadata.FrameworkVersion, "17.0.0")
7878
fm.SetValue(buildermetadata.AdapterName, "@apphosting/adapter-angular")
7979
fm.SetValue(buildermetadata.AdapterVersion, "17.2.3")
80+
fm.SetValue(buildermetadata.MonorepoName, "nx")
8081
want := BuilderOutput{
8182
InstalledRuntimeVersions: []string{"6.0.6"},
8283
Metrics: bm,
@@ -125,6 +126,7 @@ func TestJSON(t *testing.T) {
125126
fm.SetValue(buildermetadata.FrameworkVersion, "17.0.0")
126127
fm.SetValue(buildermetadata.AdapterName, "@apphosting/adapter-angular")
127128
fm.SetValue(buildermetadata.AdapterVersion, "17.2.3")
129+
fm.SetValue(buildermetadata.MonorepoName, "nx")
128130
b := BuilderOutput{
129131
InstalledRuntimeVersions: []string{"6.0.6"},
130132
Metrics: bm,
@@ -146,7 +148,7 @@ func TestJSON(t *testing.T) {
146148
if want := `{"c":{"1":3}}`; !strings.Contains(string(s), want) {
147149
t.Errorf(`Expected string %q not found in %s`, want, s)
148150
}
149-
if want := `{"m":{"1":"true","2":"false","3":"angular","4":"17.0.0","5":"@apphosting/adapter-angular","6":"17.2.3"}}`; !strings.Contains(string(s), want) {
151+
if want := `{"m":{"1":"true","2":"false","3":"angular","4":"17.0.0","5":"@apphosting/adapter-angular","6":"17.2.3","7":"nx"}}`; !strings.Contains(string(s), want) {
150152
t.Errorf(`Expected string %q not found in %s`, want, s)
151153
}
152154
}

0 commit comments

Comments
 (0)