Skip to content

Commit 02f73ef

Browse files
Add parser.GitCommit field into ParseInfo struct (#1005)
* Add parser.GitCommit field into ParseInfo struct * Fixing styling * Updating GitCommit to include the complete hash
1 parent 0d44861 commit 02f73ef

File tree

8 files changed

+55
-7
lines changed

8 files changed

+55
-7
lines changed

parser/annotation.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ func (ap *AnnotationParser) ParseAndInsert(meta map[string]bigquery.Value, testN
8383
Time: time.Now(),
8484
ArchiveURL: meta["filename"].(string),
8585
Filename: testName,
86+
GitCommit: GitCommit(),
8687
},
8788
}
8889

parser/annotation_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package parser_test
22

33
import (
44
"io/ioutil"
5+
"strings"
56
"testing"
67

78
"cloud.google.com/go/bigquery"
89
"cloud.google.com/go/civil"
10+
"github.com/go-test/deep"
911
"github.com/m-lab/etl/parser"
12+
"github.com/m-lab/etl/schema"
1013
"github.com/m-lab/go/rtx"
1114
)
1215

@@ -46,6 +49,24 @@ func TestAnnotationParser_ParseAndInsert(t *testing.T) {
4649
if err := n.ParseAndInsert(meta, tt.file, data); (err != nil) != tt.wantErr {
4750
t.Errorf("AnnotationParser.ParseAndInsert() error = %v, wantErr %v", err, tt.wantErr)
4851
}
52+
53+
if n.Accepted() == 1 {
54+
n.Flush()
55+
row := ins.data[0].(*schema.AnnotationRow)
56+
57+
expPI := schema.ParseInfo{
58+
Version: "https://github.com/m-lab/etl/tree/foobar",
59+
Time: row.Parser.Time,
60+
ArchiveURL: "gs://mlab-test-bucket/ndt/ndt7/2020/03/18/ndt-njp6l_1585004303_00000000000170FA.json",
61+
Filename: "ndt-njp6l_1585004303_00000000000170FA.json",
62+
Priority: 0,
63+
GitCommit: "12345678",
64+
}
65+
66+
if diff := deep.Equal(row.Parser, expPI); diff != nil {
67+
t.Errorf("AnnotationParser.ParseAndInsert() different summary: %s", strings.Join(diff, "\n"))
68+
}
69+
}
4970
})
5071
}
5172
}

parser/export_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ package parser
77
// InitParserVersionForTest allows tests to rerun initParserVersion after initializing
88
// environment variables.
99
var InitParserVersionForTest = initParserVersion
10+
11+
// InitParserGitCommitForTest allows test to rerun initParseGitCommit after initializing
12+
// environement variables.
13+
var InitParserGitCommitForTest = initParserGitCommit

parser/ndt7_result.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ func (dp *NDT7ResultParser) ParseAndInsert(meta map[string]bigquery.Value, testN
8181
Time: time.Now(),
8282
ArchiveURL: meta["filename"].(string),
8383
Filename: testName,
84+
GitCommit: GitCommit(),
8485
},
8586
}
8687

parser/ndt7_result_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ func TestNDT7ResultParser_ParseAndInsert(t *testing.T) {
7070
ArchiveURL: "gs://mlab-test-bucket/ndt/ndt7/2020/03/18/ndt_ndt7_2020_03_18_20200318T003853.425987Z-ndt7-mlab3-syd03-ndt.tgz",
7171
Filename: "ndt7-download-20200318T000657.568382877Z.ndt-knwp4_1583603744_000000000000590E.json",
7272
Priority: 0,
73+
GitCommit: "12345678",
7374
}
7475
if diff := deep.Equal(row.Parser, expPI); diff != nil {
7576
pretty.Print(row.Parser)
@@ -108,6 +109,7 @@ func TestNDT7ResultParser_ParseAndInsert(t *testing.T) {
108109
ArchiveURL: "gs://mlab-test-bucket/ndt/ndt7/2020/03/18/ndt_ndt7_2020_03_18_20200318T003853.425987Z-ndt7-mlab3-syd03-ndt.tgz",
109110
Filename: "ndt7-upload-20200318T001352.496224022Z.ndt-knwp4_1583603744_0000000000005CF2.json",
110111
Priority: 0,
112+
GitCommit: "12345678",
111113
}
112114
if diff := deep.Equal(row.Parser, expPI); diff != nil {
113115
t.Errorf("NDT7ResultParser.ParseAndInsert() different summary: %s", strings.Join(diff, "\n"))

parser/parser.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,46 @@ import (
2121

2222
func init() {
2323
initParserVersion()
24+
initParserGitCommit()
2425
}
2526

26-
var gParserVersion = "uninitialized"
27+
const uninitialized = "uninitialized"
28+
29+
var (
30+
gParserVersion = uninitialized
31+
gParserGitCommit = uninitialized
32+
)
2733

2834
// initParserVersion initializes the gParserVersion variable for use by all parsers.
2935
func initParserVersion() string {
3036
release := etl.Version
3137
if release != "noversion" {
3238
gParserVersion = "https://github.com/m-lab/etl/tree/" + release
3339
} else {
34-
hash := etl.GitCommit
35-
if hash != "nocommit" && len(hash) >= 8 {
36-
gParserVersion = "https://github.com/m-lab/etl/tree/" + hash[0:8]
37-
} else {
38-
gParserVersion = "local development"
39-
}
40+
gParserVersion = "local development"
4041
}
4142
return gParserVersion
4243
}
4344

45+
// initParserGitCommit initializes the gParserGitCommit variable for use by all parsers.
46+
func initParserGitCommit() string {
47+
hash := etl.GitCommit
48+
if hash != "nocommit" {
49+
gParserGitCommit = hash
50+
}
51+
return gParserGitCommit
52+
}
53+
4454
// Version returns the parser version used by parsers to annotate data rows.
4555
func Version() string {
4656
return gParserVersion
4757
}
4858

59+
// GitCommit returns the git commit hash of the build.
60+
func GitCommit() string {
61+
return gParserGitCommit
62+
}
63+
4964
// NewSinkParser creates an appropriate parser for a given data type.
5065
// Eventually all datatypes will use this instead of NewParser.
5166
func NewSinkParser(dt etl.DataType, sink row.Sink, table string, ann api.Annotator) etl.Parser {

parser/parser_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import (
1919
func init() {
2020
etl.Version = "foobar"
2121
parser.InitParserVersionForTest()
22+
23+
etl.GitCommit = "12345678"
24+
parser.InitParserGitCommitForTest()
2225
}
2326

2427
// countingInserter counts the calls to InsertRows and Flush.

schema/schema.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type ParseInfo struct {
1818
ArchiveURL string
1919
Filename string
2020
Priority int64
21+
GitCommit string
2122
}
2223

2324
// FindSchemaDocsFor should be used by parser row types to associate bigquery

0 commit comments

Comments
 (0)