github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/velodrome/fetcher/conversion.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"strconv"
    22  	"strings"
    23  	"time"
    24  
    25  	"github.com/google/go-github/github"
    26  	"k8s.io/test-infra/velodrome/sql"
    27  )
    28  
    29  // NewIssue creates a new (orm) Issue from a github Issue
    30  func NewIssue(gIssue *github.Issue, repository string) (*sql.Issue, error) {
    31  	if gIssue.Number == nil ||
    32  		gIssue.Title == nil ||
    33  		gIssue.User == nil ||
    34  		gIssue.User.Login == nil ||
    35  		gIssue.State == nil ||
    36  		gIssue.Comments == nil ||
    37  		gIssue.CreatedAt == nil ||
    38  		gIssue.UpdatedAt == nil {
    39  		return nil, fmt.Errorf("Issue is missing mandatory field: %+v", gIssue)
    40  	}
    41  
    42  	var closedAt *time.Time
    43  	if gIssue.ClosedAt != nil {
    44  		closedAt = gIssue.ClosedAt
    45  	}
    46  	assignees, err := newAssignees(
    47  		*gIssue.Number,
    48  		gIssue.Assignees, repository)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  	var body string
    53  	if gIssue.Body != nil {
    54  		body = *gIssue.Body
    55  	}
    56  	isPR := (gIssue.PullRequestLinks != nil && gIssue.PullRequestLinks.URL != nil)
    57  	labels, err := newLabels(
    58  		*gIssue.Number, gIssue.Labels, repository)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  
    63  	return &sql.Issue{
    64  		ID:             strconv.Itoa(*gIssue.Number),
    65  		Labels:         labels,
    66  		Title:          *gIssue.Title,
    67  		Body:           body,
    68  		User:           *gIssue.User.Login,
    69  		Assignees:      assignees,
    70  		State:          *gIssue.State,
    71  		Comments:       *gIssue.Comments,
    72  		IsPR:           isPR,
    73  		IssueClosedAt:  closedAt,
    74  		IssueCreatedAt: *gIssue.CreatedAt,
    75  		IssueUpdatedAt: *gIssue.UpdatedAt,
    76  		Repository:     strings.ToLower(repository),
    77  	}, nil
    78  }
    79  
    80  // NewIssueEvent creates a new (orm) Issue from a github Issue
    81  func NewIssueEvent(gIssueEvent *github.IssueEvent, issueID int, repository string) (*sql.IssueEvent, error) {
    82  	if gIssueEvent.ID == nil ||
    83  		gIssueEvent.Event == nil ||
    84  		gIssueEvent.CreatedAt == nil {
    85  		return nil, fmt.Errorf("IssueEvent is missing mandatory field: %+v", gIssueEvent)
    86  	}
    87  
    88  	var label *string
    89  	if gIssueEvent.Label != nil {
    90  		label = gIssueEvent.Label.Name
    91  	}
    92  	var assignee *string
    93  	if gIssueEvent.Assignee != nil {
    94  		assignee = gIssueEvent.Assignee.Login
    95  	}
    96  	var actor *string
    97  	if gIssueEvent.Actor != nil {
    98  		actor = gIssueEvent.Actor.Login
    99  	}
   100  
   101  	return &sql.IssueEvent{
   102  		ID:             strconv.Itoa(*gIssueEvent.ID),
   103  		Label:          label,
   104  		Event:          *gIssueEvent.Event,
   105  		EventCreatedAt: *gIssueEvent.CreatedAt,
   106  		IssueId:        strconv.Itoa(issueID),
   107  		Assignee:       assignee,
   108  		Actor:          actor,
   109  		Repository:     strings.ToLower(repository),
   110  	}, nil
   111  }
   112  
   113  // newLabels creates a new Label for each label in the issue
   114  func newLabels(issueId int, gLabels []github.Label, repository string) ([]sql.Label, error) {
   115  	labels := []sql.Label{}
   116  	repository = strings.ToLower(repository)
   117  
   118  	for _, label := range gLabels {
   119  		if label.Name == nil {
   120  			return nil, fmt.Errorf("Label is missing name field")
   121  		}
   122  		labels = append(labels, sql.Label{
   123  			IssueID:    strconv.Itoa(issueId),
   124  			Name:       *label.Name,
   125  			Repository: repository,
   126  		})
   127  	}
   128  
   129  	return labels, nil
   130  }
   131  
   132  // newAssignees creates a new Label for each label in the issue
   133  func newAssignees(issueId int, gAssignees []*github.User, repository string) ([]sql.Assignee, error) {
   134  	assignees := []sql.Assignee{}
   135  	repository = strings.ToLower(repository)
   136  
   137  	for _, assignee := range gAssignees {
   138  		if assignee != nil && assignee.Login == nil {
   139  			return nil, fmt.Errorf("Assignee is missing Login field")
   140  		}
   141  		assignees = append(assignees, sql.Assignee{
   142  			IssueID:    strconv.Itoa(issueId),
   143  			Name:       *assignee.Login,
   144  			Repository: repository,
   145  		})
   146  	}
   147  
   148  	return assignees, nil
   149  }
   150  
   151  // NewIssueComment creates a Comment from a github.IssueComment
   152  func NewIssueComment(issueId int, gComment *github.IssueComment, repository string) (*sql.Comment, error) {
   153  	if gComment.ID == nil ||
   154  		gComment.Body == nil ||
   155  		gComment.CreatedAt == nil ||
   156  		gComment.UpdatedAt == nil {
   157  		return nil, fmt.Errorf("IssueComment is missing mandatory field: %s", gComment)
   158  	}
   159  
   160  	var login string
   161  	if gComment.User != nil && gComment.User.Login != nil {
   162  		login = *gComment.User.Login
   163  	}
   164  
   165  	return &sql.Comment{
   166  		ID:               strconv.Itoa(*gComment.ID),
   167  		IssueID:          strconv.Itoa(issueId),
   168  		Body:             *gComment.Body,
   169  		User:             login,
   170  		CommentCreatedAt: *gComment.CreatedAt,
   171  		CommentUpdatedAt: *gComment.UpdatedAt,
   172  		PullRequest:      false,
   173  		Repository:       strings.ToLower(repository),
   174  	}, nil
   175  }
   176  
   177  // NewPullComment creates a Comment from a github.PullRequestComment
   178  func NewPullComment(issueId int, gComment *github.PullRequestComment, repository string) (*sql.Comment, error) {
   179  	if gComment.ID == nil ||
   180  		gComment.Body == nil ||
   181  		gComment.CreatedAt == nil ||
   182  		gComment.UpdatedAt == nil {
   183  		return nil, fmt.Errorf("PullComment is missing mandatory field: %s", gComment)
   184  	}
   185  
   186  	var login string
   187  	if gComment.User != nil && gComment.User.Login != nil {
   188  		login = *gComment.User.Login
   189  	}
   190  	return &sql.Comment{
   191  		ID:               strconv.Itoa(*gComment.ID),
   192  		IssueID:          strconv.Itoa(issueId),
   193  		Body:             *gComment.Body,
   194  		User:             login,
   195  		CommentCreatedAt: *gComment.CreatedAt,
   196  		CommentUpdatedAt: *gComment.UpdatedAt,
   197  		PullRequest:      true,
   198  		Repository:       strings.ToLower(repository),
   199  	}, nil
   200  }