Skip to content

Commit d4b4fa0

Browse files
authored
Tag: Fix tag queries to return commits as well (#280)
1 parent 1f74b37 commit d4b4fa0

File tree

4 files changed

+111
-148
lines changed

4 files changed

+111
-148
lines changed

pkg/github/contributors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type GitActor struct {
1414
Name string
1515
Email string
1616
User models.User
17+
Date githubv4.GitTimestamp
1718
}
1819

1920
// GitActors is a list of GitHub users

pkg/github/tags.go

Lines changed: 88 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,35 @@ import (
99
"github.com/shurcooL/githubv4"
1010
)
1111

12-
// Tag is a GitHub tag. Every tag has an associated commit
13-
type Tag struct {
12+
type tagDTO struct {
1413
Name string
15-
Tagger struct {
16-
Date githubv4.DateTime
17-
User models.User
18-
}
19-
Target struct {
20-
OID string
21-
Commit Commit `graphql:"... on Commit"`
22-
}
14+
Author author
15+
OID string
16+
}
17+
18+
type user struct {
19+
Login string
20+
Name string
21+
Company string
22+
}
23+
24+
type author struct {
25+
Email string
26+
User user
27+
Date githubv4.GitTimestamp
28+
}
29+
type commit struct {
30+
OID string
31+
Author author
32+
}
33+
34+
type tag struct {
35+
OID string
36+
Tagger author
2337
}
2438

2539
// Tags is a list of GitHub tags
26-
type Tags []Tag
40+
type Tags []tagDTO
2741

2842
// Frames converts the list of tags to a Grafana DataFrame
2943
func (t Tags) Frames() data.Frames {
@@ -35,22 +49,18 @@ func (t Tags) Frames() data.Frames {
3549
data.NewField("author_login", nil, []string{}),
3650
data.NewField("author_email", nil, []string{}),
3751
data.NewField("author_company", nil, []string{}),
38-
data.NewField("pushed_at", nil, []time.Time{}),
39-
data.NewField("committed_at", nil, []time.Time{}),
40-
data.NewField("commit_pushed_at", nil, []time.Time{}),
52+
data.NewField("date", nil, []time.Time{}), // The timestamp of the Git action (authoring or committing).
4153
)
4254

4355
for _, v := range t {
4456
frame.AppendRow(
4557
v.Name,
46-
v.Target.Commit.OID,
47-
v.Tagger.User.Name,
48-
v.Tagger.User.Login,
49-
v.Target.Commit.Author.Email,
50-
v.Target.Commit.Author.User.Company,
51-
v.Tagger.Date.Time,
52-
v.Target.Commit.CommittedDate.Time,
53-
v.Target.Commit.PushedDate.Time,
58+
v.OID,
59+
v.Author.User.Name,
60+
v.Author.User.Login,
61+
v.Author.Email,
62+
v.Author.User.Company,
63+
v.Author.Date.Time,
5464
)
5565
}
5666

@@ -59,35 +69,56 @@ func (t Tags) Frames() data.Frames {
5969

6070
// QueryListTags is the GraphQL query for listing GitHub tags in a repository
6171
//
62-
// repository(name: "grafana", owner: "grafana") {
63-
// refs(refPrefix: "refs/tags/", orderBy: {field: TAG_COMMIT_DATE, direction: DESC}, first: 10, query: "") {
64-
// nodes {
65-
// target {
66-
// oid
67-
// ... on Tag {
68-
// name
69-
// tagger {
70-
// date
71-
// }
72-
// target {
73-
// oid
74-
// ... on Commit {
75-
// committedDate
76-
// pushedDate
77-
// }
78-
// }
79-
// }
80-
// }
81-
// }
82-
// }
72+
// {
73+
// repository(name: "grafana", owner: "grafana") {
74+
// refs(
75+
// refPrefix: "refs/tags/"
76+
// orderBy: {field: TAG_COMMIT_DATE, direction: DESC}
77+
// first: 30
78+
// query: ""
79+
// ) {
80+
// nodes {
81+
// name
82+
// target {
83+
// __typename
84+
// ... on Commit {
85+
// oid
86+
// author {
87+
// date
88+
// email
89+
// user {
90+
// login
91+
// name
92+
// company
93+
// }
94+
// }
95+
// }
96+
// ... on Tag {
97+
// oid
98+
// tagger {
99+
// date
100+
// email
101+
// user {
102+
// login
103+
// name
104+
// company
105+
// }
106+
// }
107+
// }
108+
// }
109+
// }
110+
// }
111+
// }
83112
// }
84-
// }
85113
type QueryListTags struct {
86114
Repository struct {
87115
Refs struct {
88116
Nodes []struct {
117+
Name string
89118
Target struct {
90-
Tag Tag `graphql:"... on Tag"`
119+
TypeName string `graphql:"__typename"`
120+
Tag tag `graphql:"... on Tag"`
121+
Commit commit `graphql:"... on Commit"`
91122
}
92123
}
93124
PageInfo models.PageInfo
@@ -104,17 +135,24 @@ func GetAllTags(ctx context.Context, client models.Client, opts models.ListTagsO
104135
"name": githubv4.String(opts.Repository),
105136
}
106137

107-
tags = []Tag{}
138+
tags = []tagDTO{}
108139
)
109140

110141
for {
111142
q := &QueryListTags{}
112143
if err := client.Query(ctx, q, variables); err != nil {
113144
return nil, err
114145
}
115-
t := make([]Tag, len(q.Repository.Refs.Nodes))
146+
t := make([]tagDTO, len(q.Repository.Refs.Nodes))
116147
for i, v := range q.Repository.Refs.Nodes {
117-
t[i] = v.Target.Tag
148+
t[i].Name = v.Name
149+
if v.Target.TypeName == "Commit" {
150+
t[i].Author = v.Target.Commit.Author
151+
t[i].OID = v.Target.Commit.OID
152+
} else if v.Target.TypeName == "Tag" {
153+
t[i].Author = v.Target.Tag.Tagger
154+
t[i].OID = v.Target.Tag.OID
155+
}
118156
}
119157

120158
tags = append(tags, t...)
@@ -134,10 +172,10 @@ func GetTagsInRange(ctx context.Context, client models.Client, opts models.ListT
134172
return nil, err
135173
}
136174

137-
filtered := []Tag{}
175+
filtered := []tagDTO{}
138176

139177
for i, v := range tags {
140-
if v.Tagger.Date.After(from) && v.Tagger.Date.Before(to) {
178+
if v.Author.Date.After(from) && v.Author.Date.Before(to) {
141179
filtered = append(filtered, tags[i])
142180
}
143181
}

pkg/github/tags_test.go

Lines changed: 12 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -57,96 +57,42 @@ func TestListTags(t *testing.T) {
5757
}
5858

5959
func TestTagsDataFrames(t *testing.T) {
60-
committedAt, err := time.Parse(time.RFC3339, "2020-08-25T16:21:56+00:00")
61-
if err != nil {
62-
t.Fatal(err)
63-
}
64-
6560
createdAt, err := time.Parse(time.RFC3339, "2020-08-25T16:21:56+00:00")
6661
if err != nil {
6762
t.Fatal(err)
6863
}
6964

70-
user := GitActor{
71-
Name: "firstCommitter",
65+
user := author{
7266
73-
User: models.User{
74-
ID: "1",
67+
User: user{
7568
Login: "firstCommitter",
7669
Name: "First Committer",
7770
Company: "ACME Corp",
78-
79-
},
80-
}
81-
82-
commit1 := Commit{
83-
OID: "",
84-
PushedDate: githubv4.DateTime{
85-
Time: committedAt.Add(time.Minute * 2),
86-
},
87-
AuthoredDate: githubv4.DateTime{
88-
Time: committedAt,
8971
},
90-
CommittedDate: githubv4.DateTime{
91-
Time: committedAt,
92-
},
93-
Message: "commit #1",
94-
Author: user,
95-
}
96-
97-
commit2 := Commit{
98-
OID: "",
99-
PushedDate: githubv4.DateTime{
100-
Time: committedAt.Add(time.Hour * 2),
101-
},
102-
AuthoredDate: githubv4.DateTime{
103-
Time: committedAt.Add(time.Hour),
104-
},
105-
CommittedDate: githubv4.DateTime{
106-
Time: committedAt.Add(time.Hour),
107-
},
108-
Message: "commit #2",
109-
Author: user,
11072
}
11173

11274
tags := Tags{
113-
Tag{
75+
tagDTO{
11476
Name: "v1.0.0",
115-
Tagger: struct {
116-
Date githubv4.DateTime
117-
User models.User
118-
}{
119-
Date: githubv4.DateTime{
77+
OID: "",
78+
Author: author{
79+
Email: user.Email,
80+
Date: githubv4.GitTimestamp{
12081
Time: createdAt,
12182
},
12283
User: user.User,
12384
},
124-
Target: struct {
125-
OID string
126-
Commit Commit "graphql:\"... on Commit\""
127-
}{
128-
OID: "",
129-
Commit: commit1,
130-
},
13185
},
132-
Tag{
86+
tagDTO{
13387
Name: "v1.1.0",
134-
Tagger: struct {
135-
Date githubv4.DateTime
136-
User models.User
137-
}{
138-
Date: githubv4.DateTime{
88+
Author: author{
89+
Email: user.Email,
90+
Date: githubv4.GitTimestamp{
13991
Time: createdAt,
14092
},
14193
User: user.User,
14294
},
143-
Target: struct {
144-
OID string
145-
Commit Commit "graphql:\"... on Commit\""
146-
}{
147-
OID: "",
148-
Commit: commit2,
149-
},
95+
OID: "",
15096
},
15197
}
15298

pkg/github/testdata/tags.golden.jsonc

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
//
33
// Frame[0]
44
// Name: tags
5-
// Dimensions: 9 Fields by 2 Rows
6-
// +----------------+----------------+-----------------+--------------------+--------------------+----------------------+---------------------------------+---------------------------------+---------------------------------+
7-
// | Name: name | Name: id | Name: author | Name: author_login | Name: author_email | Name: author_company | Name: pushed_at | Name: committed_at | Name: commit_pushed_at |
8-
// | Labels: | Labels: | Labels: | Labels: | Labels: | Labels: | Labels: | Labels: | Labels: |
9-
// | Type: []string | Type: []string | Type: []string | Type: []string | Type: []string | Type: []string | Type: []time.Time | Type: []time.Time | Type: []time.Time |
10-
// +----------------+----------------+-----------------+--------------------+--------------------+----------------------+---------------------------------+---------------------------------+---------------------------------+
11-
// | v1.0.0 | | First Committer | firstCommitter | [email protected] | ACME Corp | 2020-08-25 16:21:56 +0000 +0000 | 2020-08-25 16:21:56 +0000 +0000 | 2020-08-25 16:23:56 +0000 +0000 |
12-
// | v1.1.0 | | First Committer | firstCommitter | [email protected] | ACME Corp | 2020-08-25 16:21:56 +0000 +0000 | 2020-08-25 17:21:56 +0000 +0000 | 2020-08-25 18:21:56 +0000 +0000 |
13-
// +----------------+----------------+-----------------+--------------------+--------------------+----------------------+---------------------------------+---------------------------------+---------------------------------+
5+
// Dimensions: 7 Fields by 2 Rows
6+
// +----------------+----------------+-----------------+--------------------+--------------------+----------------------+---------------------------------+
7+
// | Name: name | Name: id | Name: author | Name: author_login | Name: author_email | Name: author_company | Name: date |
8+
// | Labels: | Labels: | Labels: | Labels: | Labels: | Labels: | Labels: |
9+
// | Type: []string | Type: []string | Type: []string | Type: []string | Type: []string | Type: []string | Type: []time.Time |
10+
// +----------------+----------------+-----------------+--------------------+--------------------+----------------------+---------------------------------+
11+
// | v1.0.0 | | First Committer | firstCommitter | [email protected] | ACME Corp | 2020-08-25 16:21:56 +0000 +0000 |
12+
// | v1.1.0 | | First Committer | firstCommitter | [email protected] | ACME Corp | 2020-08-25 16:21:56 +0000 +0000 |
13+
// +----------------+----------------+-----------------+--------------------+--------------------+----------------------+---------------------------------+
1414
//
1515
//
1616
// 🌟 This was machine generated. Do not edit. 🌟
@@ -64,21 +64,7 @@
6464
}
6565
},
6666
{
67-
"name": "pushed_at",
68-
"type": "time",
69-
"typeInfo": {
70-
"frame": "time.Time"
71-
}
72-
},
73-
{
74-
"name": "committed_at",
75-
"type": "time",
76-
"typeInfo": {
77-
"frame": "time.Time"
78-
}
79-
},
80-
{
81-
"name": "commit_pushed_at",
67+
"name": "date",
8268
"type": "time",
8369
"typeInfo": {
8470
"frame": "time.Time"
@@ -115,14 +101,6 @@
115101
[
116102
1598372516000,
117103
1598372516000
118-
],
119-
[
120-
1598372516000,
121-
1598376116000
122-
],
123-
[
124-
1598372636000,
125-
1598379716000
126104
]
127105
]
128106
}

0 commit comments

Comments
 (0)