Skip to content

Commit

Permalink
Tag: Fix tag queries to return commits as well (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
zoltanbedi authored Jan 4, 2024
1 parent 1f74b37 commit d4b4fa0
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 148 deletions.
1 change: 1 addition & 0 deletions pkg/github/contributors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type GitActor struct {
Name string
Email string
User models.User
Date githubv4.GitTimestamp
}

// GitActors is a list of GitHub users
Expand Down
138 changes: 88 additions & 50 deletions pkg/github/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,35 @@ import (
"github.com/shurcooL/githubv4"
)

// Tag is a GitHub tag. Every tag has an associated commit
type Tag struct {
type tagDTO struct {
Name string
Tagger struct {
Date githubv4.DateTime
User models.User
}
Target struct {
OID string
Commit Commit `graphql:"... on Commit"`
}
Author author
OID string
}

type user struct {
Login string
Name string
Company string
}

type author struct {
Email string
User user
Date githubv4.GitTimestamp
}
type commit struct {
OID string
Author author
}

type tag struct {
OID string
Tagger author
}

// Tags is a list of GitHub tags
type Tags []Tag
type Tags []tagDTO

// Frames converts the list of tags to a Grafana DataFrame
func (t Tags) Frames() data.Frames {
Expand All @@ -35,22 +49,18 @@ func (t Tags) Frames() data.Frames {
data.NewField("author_login", nil, []string{}),
data.NewField("author_email", nil, []string{}),
data.NewField("author_company", nil, []string{}),
data.NewField("pushed_at", nil, []time.Time{}),
data.NewField("committed_at", nil, []time.Time{}),
data.NewField("commit_pushed_at", nil, []time.Time{}),
data.NewField("date", nil, []time.Time{}), // The timestamp of the Git action (authoring or committing).
)

for _, v := range t {
frame.AppendRow(
v.Name,
v.Target.Commit.OID,
v.Tagger.User.Name,
v.Tagger.User.Login,
v.Target.Commit.Author.Email,
v.Target.Commit.Author.User.Company,
v.Tagger.Date.Time,
v.Target.Commit.CommittedDate.Time,
v.Target.Commit.PushedDate.Time,
v.OID,
v.Author.User.Name,
v.Author.User.Login,
v.Author.Email,
v.Author.User.Company,
v.Author.Date.Time,
)
}

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

// QueryListTags is the GraphQL query for listing GitHub tags in a repository
//
// repository(name: "grafana", owner: "grafana") {
// refs(refPrefix: "refs/tags/", orderBy: {field: TAG_COMMIT_DATE, direction: DESC}, first: 10, query: "") {
// nodes {
// target {
// oid
// ... on Tag {
// name
// tagger {
// date
// }
// target {
// oid
// ... on Commit {
// committedDate
// pushedDate
// }
// }
// }
// }
// }
// }
// {
// repository(name: "grafana", owner: "grafana") {
// refs(
// refPrefix: "refs/tags/"
// orderBy: {field: TAG_COMMIT_DATE, direction: DESC}
// first: 30
// query: ""
// ) {
// nodes {
// name
// target {
// __typename
// ... on Commit {
// oid
// author {
// date
// email
// user {
// login
// name
// company
// }
// }
// }
// ... on Tag {
// oid
// tagger {
// date
// email
// user {
// login
// name
// company
// }
// }
// }
// }
// }
// }
// }
// }
// }
type QueryListTags struct {
Repository struct {
Refs struct {
Nodes []struct {
Name string
Target struct {
Tag Tag `graphql:"... on Tag"`
TypeName string `graphql:"__typename"`
Tag tag `graphql:"... on Tag"`
Commit commit `graphql:"... on Commit"`
}
}
PageInfo models.PageInfo
Expand All @@ -104,17 +135,24 @@ func GetAllTags(ctx context.Context, client models.Client, opts models.ListTagsO
"name": githubv4.String(opts.Repository),
}

tags = []Tag{}
tags = []tagDTO{}
)

for {
q := &QueryListTags{}
if err := client.Query(ctx, q, variables); err != nil {
return nil, err
}
t := make([]Tag, len(q.Repository.Refs.Nodes))
t := make([]tagDTO, len(q.Repository.Refs.Nodes))
for i, v := range q.Repository.Refs.Nodes {
t[i] = v.Target.Tag
t[i].Name = v.Name
if v.Target.TypeName == "Commit" {
t[i].Author = v.Target.Commit.Author
t[i].OID = v.Target.Commit.OID
} else if v.Target.TypeName == "Tag" {
t[i].Author = v.Target.Tag.Tagger
t[i].OID = v.Target.Tag.OID
}
}

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

filtered := []Tag{}
filtered := []tagDTO{}

for i, v := range tags {
if v.Tagger.Date.After(from) && v.Tagger.Date.Before(to) {
if v.Author.Date.After(from) && v.Author.Date.Before(to) {
filtered = append(filtered, tags[i])
}
}
Expand Down
78 changes: 12 additions & 66 deletions pkg/github/tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,96 +57,42 @@ func TestListTags(t *testing.T) {
}

func TestTagsDataFrames(t *testing.T) {
committedAt, err := time.Parse(time.RFC3339, "2020-08-25T16:21:56+00:00")
if err != nil {
t.Fatal(err)
}

createdAt, err := time.Parse(time.RFC3339, "2020-08-25T16:21:56+00:00")
if err != nil {
t.Fatal(err)
}

user := GitActor{
Name: "firstCommitter",
user := author{
Email: "first@example.com",
User: models.User{
ID: "1",
User: user{
Login: "firstCommitter",
Name: "First Committer",
Company: "ACME Corp",
Email: "first@example.com",
},
}

commit1 := Commit{
OID: "",
PushedDate: githubv4.DateTime{
Time: committedAt.Add(time.Minute * 2),
},
AuthoredDate: githubv4.DateTime{
Time: committedAt,
},
CommittedDate: githubv4.DateTime{
Time: committedAt,
},
Message: "commit #1",
Author: user,
}

commit2 := Commit{
OID: "",
PushedDate: githubv4.DateTime{
Time: committedAt.Add(time.Hour * 2),
},
AuthoredDate: githubv4.DateTime{
Time: committedAt.Add(time.Hour),
},
CommittedDate: githubv4.DateTime{
Time: committedAt.Add(time.Hour),
},
Message: "commit #2",
Author: user,
}

tags := Tags{
Tag{
tagDTO{
Name: "v1.0.0",
Tagger: struct {
Date githubv4.DateTime
User models.User
}{
Date: githubv4.DateTime{
OID: "",
Author: author{
Email: user.Email,
Date: githubv4.GitTimestamp{
Time: createdAt,
},
User: user.User,
},
Target: struct {
OID string
Commit Commit "graphql:\"... on Commit\""
}{
OID: "",
Commit: commit1,
},
},
Tag{
tagDTO{
Name: "v1.1.0",
Tagger: struct {
Date githubv4.DateTime
User models.User
}{
Date: githubv4.DateTime{
Author: author{
Email: user.Email,
Date: githubv4.GitTimestamp{
Time: createdAt,
},
User: user.User,
},
Target: struct {
OID string
Commit Commit "graphql:\"... on Commit\""
}{
OID: "",
Commit: commit2,
},
OID: "",
},
}

Expand Down
42 changes: 10 additions & 32 deletions pkg/github/testdata/tags.golden.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
//
// Frame[0]
// Name: tags
// Dimensions: 9 Fields by 2 Rows
// +----------------+----------------+-----------------+--------------------+--------------------+----------------------+---------------------------------+---------------------------------+---------------------------------+
// | 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 |
// | Labels: | Labels: | Labels: | Labels: | Labels: | Labels: | Labels: | Labels: | Labels: |
// | Type: []string | Type: []string | Type: []string | Type: []string | Type: []string | Type: []string | Type: []time.Time | Type: []time.Time | Type: []time.Time |
// +----------------+----------------+-----------------+--------------------+--------------------+----------------------+---------------------------------+---------------------------------+---------------------------------+
// | v1.0.0 | | First Committer | firstCommitter | first@example.com | 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 |
// | v1.1.0 | | First Committer | firstCommitter | first@example.com | 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 |
// +----------------+----------------+-----------------+--------------------+--------------------+----------------------+---------------------------------+---------------------------------+---------------------------------+
// Dimensions: 7 Fields by 2 Rows
// +----------------+----------------+-----------------+--------------------+--------------------+----------------------+---------------------------------+
// | Name: name | Name: id | Name: author | Name: author_login | Name: author_email | Name: author_company | Name: date |
// | Labels: | Labels: | Labels: | Labels: | Labels: | Labels: | Labels: |
// | Type: []string | Type: []string | Type: []string | Type: []string | Type: []string | Type: []string | Type: []time.Time |
// +----------------+----------------+-----------------+--------------------+--------------------+----------------------+---------------------------------+
// | v1.0.0 | | First Committer | firstCommitter | first@example.com | ACME Corp | 2020-08-25 16:21:56 +0000 +0000 |
// | v1.1.0 | | First Committer | firstCommitter | first@example.com | ACME Corp | 2020-08-25 16:21:56 +0000 +0000 |
// +----------------+----------------+-----------------+--------------------+--------------------+----------------------+---------------------------------+
//
//
// 🌟 This was machine generated. Do not edit. 🌟
Expand Down Expand Up @@ -64,21 +64,7 @@
}
},
{
"name": "pushed_at",
"type": "time",
"typeInfo": {
"frame": "time.Time"
}
},
{
"name": "committed_at",
"type": "time",
"typeInfo": {
"frame": "time.Time"
}
},
{
"name": "commit_pushed_at",
"name": "date",
"type": "time",
"typeInfo": {
"frame": "time.Time"
Expand Down Expand Up @@ -115,14 +101,6 @@
[
1598372516000,
1598372516000
],
[
1598372516000,
1598376116000
],
[
1598372636000,
1598379716000
]
]
}
Expand Down

0 comments on commit d4b4fa0

Please sign in to comment.