Skip to content

Commit cb3c723

Browse files
authored
fix: Use language repo's commit value as the source_commit_hash (#2078)
Fixes: #2066
1 parent 506232f commit cb3c723

File tree

4 files changed

+18
-20
lines changed

4 files changed

+18
-20
lines changed

internal/conventionalcommits/conventional_commits.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type ConventionalCommit struct {
4545
// IsNested indicates if the commit is a nested commit.
4646
IsNested bool `yaml:"-" json:"-"`
4747
// SHA is the full commit hash.
48-
SHA string `yaml:"-" json:"-"`
48+
SHA string `yaml:"-" json:"source_commit_hash,omitempty"`
4949
// When is the timestamp of the commit.
5050
When time.Time `yaml:"-" json:"-"`
5151
}
@@ -55,12 +55,10 @@ func (c *ConventionalCommit) MarshalJSON() ([]byte, error) {
5555
type Alias ConventionalCommit
5656
return json.Marshal(&struct {
5757
*Alias
58-
PiperCLNumber string `json:"piper_cl_number,omitempty"`
59-
SourceCommitHash string `json:"source_commit_hash,omitempty"`
58+
PiperCLNumber string `json:"piper_cl_number,omitempty"`
6059
}{
61-
Alias: (*Alias)(c),
62-
PiperCLNumber: c.Footers["PiperOrigin-RevId"],
63-
SourceCommitHash: c.Footers["git-commit-hash"],
60+
Alias: (*Alias)(c),
61+
PiperCLNumber: c.Footers["PiperOrigin-RevId"],
6462
})
6563
}
6664

@@ -168,6 +166,8 @@ const (
168166
endNestedCommit = "END_NESTED_COMMIT"
169167
)
170168

169+
// extractCommitMessageOverride searches through the commit message
170+
// and parses for the text between the commit override tags.
171171
func extractCommitMessageOverride(message string) string {
172172
beginIndex := strings.Index(message, beginCommitOverride)
173173
if beginIndex == -1 {

internal/conventionalcommits/conventional_commits_test.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ END_NESTED_COMMIT
261261
LibraryID: "example-id",
262262
IsNested: false,
263263
Footers: map[string]string{},
264-
SHA: sha.String(),
264+
SHA: sha.String(), // For each nested commit, the SHA should be the same (points to language repo's commit hash)
265265
When: now,
266266
},
267267
{
@@ -272,7 +272,7 @@ END_NESTED_COMMIT
272272
LibraryID: "example-id",
273273
IsNested: true,
274274
Footers: map[string]string{},
275-
SHA: sha.String(),
275+
SHA: sha.String(), // For each nested commit, the SHA should be the same (points to language repo's commit hash)
276276
When: now,
277277
},
278278
{
@@ -283,12 +283,11 @@ END_NESTED_COMMIT
283283
LibraryID: "example-id",
284284
IsNested: true,
285285
Footers: map[string]string{},
286-
SHA: sha.String(),
286+
SHA: sha.String(), // For each nested commit, the SHA should be the same (points to language repo's commit hash)
287287
When: now,
288288
},
289289
},
290290
},
291-
292291
{
293292
name: "commit with empty nested commit",
294293
message: `feat(parser): main feature
@@ -341,7 +340,6 @@ PiperOrigin-RevId: 654321
341340
Source-link: fake-link
342341
END_NESTED_COMMIT
343342
END_COMMIT_OVERRIDE
344-
345343
`,
346344
want: []*ConventionalCommit{
347345
{
@@ -351,7 +349,7 @@ END_COMMIT_OVERRIDE
351349
LibraryID: "example-id",
352350
IsNested: true,
353351
Footers: map[string]string{"PiperOrigin-RevId": "123456", "Source-link": "fake-link"},
354-
SHA: sha.String(),
352+
SHA: sha.String(), // For each nested commit, the SHA should be the same (points to language repo's commit hash)
355353
When: now,
356354
},
357355
{
@@ -361,7 +359,7 @@ END_COMMIT_OVERRIDE
361359
Body: "body of nested commit 2\n...",
362360
LibraryID: "example-id",
363361
Footers: map[string]string{"PiperOrigin-RevId": "654321", "Source-link": "fake-link"},
364-
SHA: sha.String(),
362+
SHA: sha.String(), // For each nested commit, the SHA should be the same (points to language repo's commit hash)
365363
When: now,
366364
},
367365
},
@@ -491,14 +489,14 @@ func TestConventionalCommit_MarshalJSON(t *testing.T) {
491489
Body: "body of feature",
492490
Footers: map[string]string{
493491
"PiperOrigin-RevId": "12345",
494-
"git-commit-hash": "abcdef123456",
495492
},
493+
SHA: "1234",
496494
}
497495
b, err := c.MarshalJSON()
498496
if err != nil {
499497
t.Fatalf("MarshalJSON() failed: %v", err)
500498
}
501-
want := `{"type":"feat","subject":"new feature","body":"body of feature","piper_cl_number":"12345","source_commit_hash":"abcdef123456"}`
499+
want := `{"type":"feat","subject":"new feature","body":"body of feature","source_commit_hash":"1234","piper_cl_number":"12345"}`
502500
if diff := cmp.Diff(want, string(b)); diff != "" {
503501
t.Errorf("MarshalJSON() mismatch (-want +got):\n%s", diff)
504502
}

internal/docker/docker_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -839,8 +839,8 @@ func TestReleaseInitRequestContent(t *testing.T) {
839839
Body: "body of feature",
840840
Footers: map[string]string{
841841
"PiperOrigin-RevId": "12345",
842-
"git-commit-hash": "abcdef123456",
843842
},
843+
SHA: "1234",
844844
},
845845
},
846846
},
@@ -875,7 +875,7 @@ func TestReleaseInitRequestContent(t *testing.T) {
875875
t.Fatalf("ReadFile failed: %v", err)
876876
}
877877

878-
wantFile := filepath.Join("testdata", "release-init-request", "release-init-request.json")
878+
wantFile := filepath.Join("..", "..", "testdata", "docker", "release-init-request", "release-init-request.json")
879879
wantBytes, err := os.ReadFile(wantFile)
880880
if err != nil {
881881
t.Fatalf("ReadFile for want file failed: %v", err)

internal/docker/testdata/release-init-request/release-init-request.json renamed to testdata/docker/release-init-request/release-init-request.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
"type": "feat",
1111
"subject": "new feature",
1212
"body": "body of feature",
13-
"piper_cl_number": "12345",
14-
"source_commit_hash": "abcdef123456"
13+
"source_commit_hash": "1234",
14+
"piper_cl_number": "12345"
1515
}
1616
],
1717
"apis": null,
@@ -21,4 +21,4 @@
2121
"release_triggered": true
2222
}
2323
]
24-
}
24+
}

0 commit comments

Comments
 (0)