Skip to content

Commit

Permalink
perf(request): get issue information in a single request (#30)
Browse files Browse the repository at this point in the history
* perf: economical request for get issue

* refactor: group types with arguments

* fix(var): use camel case
  • Loading branch information
kudoas authored Apr 27, 2024
1 parent 92b2cda commit 2240b79
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 37 deletions.
17 changes: 11 additions & 6 deletions cmd/run/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
)

var (
token = os.Getenv("INPUT_TOKEN")
repository = strings.Split(os.Getenv("INPUT_REPOSITORY"), "/")
owner, repository_name = repository[0], repository[1]
issue, _ = strconv.Atoi(os.Getenv("INPUT_ISSUE"))
token = os.Getenv("INPUT_TOKEN")
repository = strings.Split(os.Getenv("INPUT_REPOSITORY"), "/")
owner, repositoryName = repository[0], repository[1]
issue, _ = strconv.Atoi(os.Getenv("INPUT_ISSUE"))
)

func main() {
Expand All @@ -25,8 +25,13 @@ func main() {
infra.WithContext(ctx),
)

trackedIssueNodeIDs := g.GetTrackedIssueNodeIDs(repository_name, owner, issue)
targetIssueNodeID := g.GetIssueNodeID(repository_name, owner, issue)
q := infra.QueryRequest{
RepositoryOwner: owner,
RepositoryName: repositoryName,
IssueNumber: issue,
}
trackedIssueNodeIDs := g.GetTrackedIssueNodeIDs(&q)
targetIssueNodeID := g.GetIssueNodeID(&q)
parentIssueFields := g.GetIssueFields(&trackedIssueNodeIDs[0])

if err := g.MutateIssue(
Expand Down
66 changes: 35 additions & 31 deletions infra/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,55 +42,59 @@ func WithContext(ctx context.Context) func(*GithubClient) {
}
}

func (g *GithubClient) GetTrackedIssueNodeIDs(repoName string, ownerName string, issueNumber int) []githubv4.ID {
var query struct {
Repository struct {
Issue struct {
ID githubv4.ID
TrackedInIssues struct {
Nodes []struct {
ID githubv4.ID
}
} `graphql:"trackedInIssues(first: 5)"`
} `graphql:"issue(number: $issueNumber)"`
} `graphql:"repository(owner: $repositoryOwner, name: $repositoryName)"`
}
var issueQuery struct {
Repository struct {
Issue struct {
ID githubv4.ID
TrackedInIssues struct {
Nodes []struct {
ID githubv4.ID
}
} `graphql:"trackedInIssues(first: 5)"`
} `graphql:"issue(number: $issueNumber)"`
} `graphql:"repository(owner: $repositoryOwner, name: $repositoryName)"`
}

if err := g.client.Query(g.ctx, &query, map[string]interface{}{
"repositoryOwner": githubv4.String(ownerName),
"repositoryName": githubv4.String(repoName),
"issueNumber": githubv4.Int(issueNumber),
type QueryRequest struct {
RepositoryOwner string
RepositoryName string
IssueNumber int
}

func (g *GithubClient) GetTrackedIssueNodeIDs(q *QueryRequest) []githubv4.ID {
if err := g.client.Query(g.ctx, &issueQuery, map[string]interface{}{
"repositoryOwner": githubv4.String(q.RepositoryOwner),
"repositoryName": githubv4.String(q.RepositoryName),
"issueNumber": githubv4.Int(q.IssueNumber),
}); err != nil {
log.Fatalf("failed to get tracked issue NodeID: %v", err)
}

var ids []githubv4.ID
for _, node := range query.Repository.Issue.TrackedInIssues.Nodes {

for _, node := range issueQuery.Repository.Issue.TrackedInIssues.Nodes {
ids = append(ids, node.ID)
}

return ids
}

// TODO: GetIssueNodeID and GetIssueFields can be combined into one function
func (g *GithubClient) GetIssueNodeID(repoName string, ownerName string, issueNumber int) *githubv4.ID {
var query struct {
Repository struct {
Issue struct {
ID githubv4.ID
} `graphql:"issue(number: $issueNumber)"`
} `graphql:"repository(owner: $repositoryOwner, name: $repositoryName)"`
func (g *GithubClient) GetIssueNodeID(q *QueryRequest) *githubv4.ID {
// If the issue is already tracked, return the issue NodeID directly
// TODO(bug): If QueryResult is not same, it should request to get issue NodeID
if issueQuery.Repository.Issue.ID != "" {
return &issueQuery.Repository.Issue.ID
}

if err := g.client.Query(g.ctx, &query, map[string]interface{}{
"repositoryOwner": githubv4.String(ownerName),
"repositoryName": githubv4.String(repoName),
"issueNumber": githubv4.Int(issueNumber),
if err := g.client.Query(g.ctx, &issueQuery, map[string]interface{}{
"repositoryOwner": githubv4.String(q.RepositoryOwner),
"repositoryName": githubv4.String(q.RepositoryName),
"issueNumber": githubv4.Int(q.IssueNumber),
}); err != nil {
log.Fatalf("failed to get issue NodeID: %v", err)
}

return &query.Repository.Issue.ID
return &issueQuery.Repository.Issue.ID
}

type IssueFields struct {
Expand Down

0 comments on commit 2240b79

Please sign in to comment.