Skip to content

Commit

Permalink
fix(component,github): fix data structs
Browse files Browse the repository at this point in the history
  • Loading branch information
donch1989 committed Dec 23, 2024
1 parent a2db7de commit 5d57b52
Show file tree
Hide file tree
Showing 13 changed files with 256 additions and 75 deletions.
4 changes: 2 additions & 2 deletions pkg/component/application/github/v0/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ type RepoInfoInterface interface {

// RepoInfo is a struct that contains the owner and repository of a repository
type RepoInfo struct {
Owner string `json:"owner"`
Repository string `json:"repository"`
Owner string `instill:"owner"`
Repository string `instill:"repository"`
}

func (info RepoInfo) getOwner() (string, error) {
Expand Down
18 changes: 9 additions & 9 deletions pkg/component/application/github/v0/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ import (

// Commit is a struct that contains the information of a commit
type Commit struct {
SHA string `json:"sha"`
Message string `json:"message"`
Stats *CommitStats `json:"stats,omitempty"`
Files []CommitFile `json:"files,omitempty"`
SHA string `instill:"sha"`
Message string `instill:"message"`
Stats *CommitStats `instill:"stats"`
Files []CommitFile `instill:"files"`
}

// CommitStats is a struct that contains the statistics of a commit
type CommitStats struct {
Additions int `json:"additions"`
Deletions int `json:"deletions"`
Changes int `json:"changes"`
Additions int `instill:"additions"`
Deletions int `instill:"deletions"`
Changes int `instill:"changes"`
}

// CommitFile is a struct that contains the information of a commit file
type CommitFile struct {
Filename string `json:"filename"`
Patch string `json:"patch"`
Filename string `instill:"filename"`
Patch string `instill:"patch"`
CommitStats
}

Expand Down
8 changes: 3 additions & 5 deletions pkg/component/application/github/v0/io.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package github

import "github.com/google/go-github/v62/github"

type createIssueInput struct {
RepoInfo
Title string `instill:"title"`
Expand All @@ -16,8 +14,8 @@ type createIssueOutput struct {

type createReviewCommentInput struct {
RepoInfo
PRNumber int `instill:"pr-number"`
Comment github.PullRequestComment `instill:"comment"`
PRNumber int `instill:"pr-number"`
Comment PullRequestComment `instill:"comment"`
}

type createReviewCommentOutput struct {
Expand Down Expand Up @@ -95,7 +93,7 @@ type listReviewCommentsInput struct {
PRNumber int `instill:"pr-number,default=0"`
Sort string `instill:"sort,default=created"`
Direction string `instill:"direction,default=desc"`
Since string `instill:"since,default=2021-01-01"`
Since string `instill:"since"`
PageOptions
}

Expand Down
20 changes: 10 additions & 10 deletions pkg/component/application/github/v0/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ type IssuesService interface {
}

type Issue struct {
Number int `json:"number"`
Title string `json:"title"`
State string `json:"state"`
Body string `json:"body"`
Assignee string `json:"assignee"`
Assignees []string `json:"assignees"`
Labels []string `json:"labels"`
IsPullRequest bool `json:"is-pull-request"`
Number int `instill:"number"`
Title string `instill:"title"`
State string `instill:"state"`
Body string `instill:"body"`
Assignee string `instill:"assignee"`
Assignees []string `instill:"assignees"`
Labels []string `instill:"labels"`
IsPullRequest bool `instill:"is-pull-request"`
}

func (client *Client) extractIssue(originalIssue *github.Issue) Issue {
Expand All @@ -30,7 +30,7 @@ func (client *Client) extractIssue(originalIssue *github.Issue) Issue {
Title: originalIssue.GetTitle(),
State: originalIssue.GetState(),
Body: originalIssue.GetBody(),
Assignee: originalIssue.GetAssignee().GetName(),
Assignee: originalIssue.GetAssignee().GetLogin(),
Assignees: extractAssignees(originalIssue.Assignees),
Labels: extractLabels(originalIssue.Labels),
IsPullRequest: originalIssue.IsPullRequest(),
Expand All @@ -40,7 +40,7 @@ func (client *Client) extractIssue(originalIssue *github.Issue) Issue {
func extractAssignees(assignees []*github.User) []string {
assigneeList := make([]string, len(assignees))
for idx, assignee := range assignees {
assigneeList[idx] = assignee.GetName()
assigneeList[idx] = assignee.GetLogin()
}
return assigneeList
}
Expand Down
66 changes: 49 additions & 17 deletions pkg/component/application/github/v0/pull_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,51 @@ type PullRequestService interface {
}

type PullRequest struct {
ID int64 `json:"id"`
Number int `json:"number"`
State string `json:"state"`
Title string `json:"title"`
Body string `json:"body"`
DiffURL string `json:"diff-url,omitempty"`
CommitsURL string `json:"commits-url,omitempty"`
Commits []Commit `json:"commits"`
Head string `json:"head"`
Base string `json:"base"`
CommentsNum int `json:"comments-num"`
CommitsNum int `json:"commits-num"`
ReviewCommentsNum int `json:"review-comments-num"`
ID int64 `instill:"id"`
Number int `instill:"number"`
State string `instill:"state"`
Title string `instill:"title"`
Body string `instill:"body"`
DiffURL string `instill:"diff-url"`
CommitsURL string `instill:"commits-url"`
Commits []Commit `instill:"commits"`
Head string `instill:"head"`
Base string `instill:"base"`
CommentsNum int `instill:"comments-num"`
CommitsNum int `instill:"commits-num"`
ReviewCommentsNum int `instill:"review-comments-num"`
}

type PullRequestComment struct {
ID *int64 `instill:"id"`
NodeID *string `instill:"node-id"`
InReplyTo *int64 `instill:"in-reply-to-id"`
Body *string `instill:"body"`
Path *string `instill:"path"`
DiffHunk *string `instill:"diff-hunk"`
PullRequestReviewID *int64 `instill:"pull-request-review-id"`
Position *int `instill:"position"`
OriginalPosition *int `instill:"original-position"`
StartLine *int `instill:"start-line"`
Line *int `instill:"line"`
OriginalLine *int `instill:"original-line"`
OriginalStartLine *int `instill:"original-start-line"`
Side *string `instill:"side"`
StartSide *string `instill:"start-side"`
CommitID *string `instill:"commit-id"`
OriginalCommitID *string `instill:"original-commit-id"`
User *User `instill:"user"`
Reactions *Reactions `instill:"reactions"`
CreatedAt *Timestamp `instill:"created-at"`
UpdatedAt *Timestamp `instill:"updated-at"`
// AuthorAssociation is the comment author's relationship to the pull request's repository.
// Possible values are "COLLABORATOR", "CONTRIBUTOR", "FIRST_TIMER", "FIRST_TIME_CONTRIBUTOR", "MEMBER", "OWNER", or "NONE".
AuthorAssociation *string `instill:"author-association"`
URL *string `instill:"url"`
HTMLURL *string `instill:"html-url"`
PullRequestURL *string `instill:"pull-request-url"`
// Can be one of: LINE, FILE from https://docs.github.com/rest/pulls/comments#create-a-review-comment-for-a-pull-request
SubjectType *string `instill:"subject-type"`
}

func (client *Client) extractPullRequestInformation(ctx context.Context, owner string, repository string, originalPr *github.PullRequest, needCommitDetails bool) (PullRequest, error) {
Expand Down Expand Up @@ -59,11 +91,11 @@ func (client *Client) extractPullRequestInformation(ctx context.Context, owner s

type ListPullRequestsInput struct {
RepoInfo
State string `json:"state"`
Sort string `json:"sort"`
Direction string `json:"direction"`
State string `instill:"state"`
Sort string `instill:"sort"`
Direction string `instill:"direction"`
PageOptions
}
type ListPullRequestsResp struct {
PullRequests []PullRequest `json:"pull-requests"`
PullRequests []PullRequest `instill:"pull-requests"`
}
44 changes: 44 additions & 0 deletions pkg/component/application/github/v0/review_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,52 @@ import (
"github.com/google/go-github/v62/github"
)

// ReviewComment is originate from github.PullRequestComment, but uses kebab-case field names
// in the instill tags.
type ReviewComment struct {
github.PullRequestComment
ID *int64 `instill:"id"`
NodeID *string `instill:"node-id"`
InReplyTo *int64 `instill:"in-reply-to-id"`
Body *string `instill:"body"`
Path *string `instill:"path"`
DiffHunk *string `instill:"diff-hunk"`
PullRequestReviewID *int64 `instill:"pull-request-review-id"`
Position *int `instill:"position"`
OriginalPosition *int `instill:"original-position"`
StartLine *int `instill:"start-line"`
Line *int `instill:"line"`
OriginalLine *int `instill:"original-line"`
OriginalStartLine *int `instill:"original-start-line"`
Side *string `instill:"side"`
StartSide *string `instill:"start-side"`
CommitID *string `instill:"commit-id"`
OriginalCommitID *string `instill:"original-commit-id"`
User *User `instill:"user"`
Reactions *Reactions `instill:"reactions"`
CreatedAt *Timestamp `instill:"created-at"`
UpdatedAt *Timestamp `instill:"updated-at"`
// AuthorAssociation is the comment author's relationship to the pull request's repository.
// Possible values are "COLLABORATOR", "CONTRIBUTOR", "FIRST_TIMER", "FIRST_TIME_CONTRIBUTOR", "MEMBER", "OWNER", or "NONE".
AuthorAssociation *string `instill:"author-association"`
URL *string `instill:"url"`
HTMLURL *string `instill:"html-url"`
PullRequestURL *string `instill:"pull-request-url"`
// Can be one of: LINE, FILE from https://docs.github.com/rest/pulls/comments#create-a-review-comment-for-a-pull-request
SubjectType *string `instill:"subject-type"`
}

type Reactions struct {
TotalCount *int `instill:"total-count"`
PlusOne *int `instill:"+1"`
MinusOne *int `instill:"-1"`
Laugh *int `instill:"laugh"`
Confused *int `instill:"confused"`
Heart *int `instill:"heart"`
Hooray *int `instill:"hooray"`
Rocket *int `instill:"rocket"`
Eyes *int `instill:"eyes"`
URL *string `instill:"url"`
}

func extractReviewCommentInformation(originalComment *github.PullRequestComment) ReviewComment {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/google/go-github/v62/github"
"github.com/instill-ai/pipeline-backend/pkg/component/base"
)

Expand All @@ -27,7 +28,23 @@ func (client *Client) createReviewComment(ctx context.Context, job *base.Job) er
if *commentReqs.Line == *commentReqs.StartLine {
commentReqs.StartLine = nil // If it's a one line comment, don't send start-line
}
comment, _, err := client.PullRequests.CreateComment(ctx, owner, repository, number, commentReqs)
req := &github.PullRequestComment{
Body: commentReqs.Body,
Path: commentReqs.Path,
Position: commentReqs.Position,
Line: commentReqs.Line,
StartLine: commentReqs.StartLine,
Side: commentReqs.Side,
StartSide: commentReqs.StartSide,
CommitID: commentReqs.CommitID,
OriginalCommitID: commentReqs.OriginalCommitID,
SubjectType: commentReqs.SubjectType,
AuthorAssociation: commentReqs.AuthorAssociation,
URL: commentReqs.URL,
HTMLURL: commentReqs.HTMLURL,
PullRequestURL: commentReqs.PullRequestURL,
}
comment, _, err := client.PullRequests.CreateComment(ctx, owner, repository, number, req)
if err != nil {
return addErrMsgToClientError(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestComponent_CreateReviewCommentTask(t *testing.T) {
Repository: "test_repo",
},
PRNumber: 1,
Comment: github.PullRequestComment{
Comment: PullRequestComment{
Body: github.String("This is a fake comment"),
Line: github.Int(2),
StartLine: github.Int(1),
Expand Down Expand Up @@ -54,7 +54,7 @@ func TestComponent_CreateReviewCommentTask(t *testing.T) {
Repository: "test_repo",
},
PRNumber: 1,
Comment: github.PullRequestComment{
Comment: PullRequestComment{
Body: github.String("This is a fake comment"),
Line: github.Int(1),
StartLine: github.Int(1),
Expand Down Expand Up @@ -86,7 +86,7 @@ func TestComponent_CreateReviewCommentTask(t *testing.T) {
Repository: "test_repo",
},
PRNumber: 1,
Comment: github.PullRequestComment{
Comment: PullRequestComment{
Body: github.String("This is a fake comment"),
Line: github.Int(2),
StartLine: github.Int(1),
Expand All @@ -106,7 +106,7 @@ func TestComponent_CreateReviewCommentTask(t *testing.T) {
Repository: "test_repo",
},
PRNumber: 1,
Comment: github.PullRequestComment{
Comment: PullRequestComment{
Body: github.String("This is a fake comment"),
Line: github.Int(2),
StartLine: github.Int(1),
Expand All @@ -126,7 +126,7 @@ func TestComponent_CreateReviewCommentTask(t *testing.T) {
Repository: "test_repo",
},
PRNumber: 1,
Comment: github.PullRequestComment{
Comment: PullRequestComment{
Body: github.String("This is a fake comment"),
Line: github.Int(2),
StartLine: github.Int(1),
Expand All @@ -146,7 +146,7 @@ func TestComponent_CreateReviewCommentTask(t *testing.T) {
Repository: "test_repo",
},
PRNumber: 1,
Comment: github.PullRequestComment{
Comment: PullRequestComment{
Body: github.String("This is a fake comment"),
Line: github.Int(1),
StartLine: github.Int(2),
Expand Down
17 changes: 10 additions & 7 deletions pkg/component/application/github/v0/task_list_issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,25 @@ func (client *Client) listIssues(ctx context.Context, job *base.Job) error {
if err != nil {
return err
}
// from format like `2006-01-02` parse it into UTC time
// The time will be 2006-01-02 00:00:00 +0000 UTC exactly
sinceTime, err := time.Parse(time.DateOnly, input.Since)
if err != nil {
return fmt.Errorf("parse since time: %w", err)
}

opts := &github.IssueListByRepoOptions{
State: input.State,
Sort: input.Sort,
Direction: input.Direction,
Since: sinceTime,
ListOptions: github.ListOptions{
Page: input.Page,
PerPage: min(input.PerPage, 100), // GitHub API only allows 100 per page
},
}
// from format like `2006-01-02` parse it into UTC time
// The time will be 2006-01-02 00:00:00 +0000 UTC exactly
if input.Since != "" {
sinceTime, err := time.Parse(time.DateOnly, input.Since)
if err != nil {
return fmt.Errorf("parse since time: %w", err)
}
opts.Since = sinceTime
}
if opts.Mentioned == "none" {
opts.Mentioned = ""
}
Expand Down
15 changes: 8 additions & 7 deletions pkg/component/application/github/v0/task_list_review_comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,22 @@ func (client *Client) listReviewComments(ctx context.Context, job *base.Job) err
if err != nil {
return err
}
// from format like `2006-01-02` parse it into UTC time
// The time will be 2006-01-02 00:00:00 +0000 UTC exactly
sinceTime, err := time.Parse(time.DateOnly, input.Since)
if err != nil {
return fmt.Errorf("parse since time: %w", err)
}

opts := &github.PullRequestListCommentsOptions{
Sort: input.Sort,
Direction: input.Direction,
Since: sinceTime,
ListOptions: github.ListOptions{
Page: input.Page,
PerPage: min(input.PerPage, 100), // GitHub API only allows 100 per page
},
}
if input.Since != "" {
sinceTime, err := time.Parse(time.DateOnly, input.Since)
if err != nil {
return fmt.Errorf("parse since time: %w", err)
}
opts.Since = sinceTime
}
number := input.PRNumber
comments, _, err := client.PullRequests.ListComments(ctx, owner, repository, number, opts)
if err != nil {
Expand Down
Loading

0 comments on commit 5d57b52

Please sign in to comment.