github.com/google/go-github/v53@v53.2.0/github/issue_import.go (about) 1 // Copyright 2020 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 package github 7 8 import ( 9 "bytes" 10 "context" 11 "encoding/json" 12 "fmt" 13 ) 14 15 // IssueImportService handles communication with the issue import related 16 // methods of the Issue Import GitHub API. 17 type IssueImportService service 18 19 // IssueImportRequest represents a request to create an issue. 20 // 21 // https://gist.github.com/jonmagic/5282384165e0f86ef105#supported-issue-and-comment-fields 22 type IssueImportRequest struct { 23 IssueImport IssueImport `json:"issue"` 24 Comments []*Comment `json:"comments,omitempty"` 25 } 26 27 // IssueImport represents body of issue to import. 28 type IssueImport struct { 29 Title string `json:"title"` 30 Body string `json:"body"` 31 CreatedAt *Timestamp `json:"created_at,omitempty"` 32 ClosedAt *Timestamp `json:"closed_at,omitempty"` 33 UpdatedAt *Timestamp `json:"updated_at,omitempty"` 34 Assignee *string `json:"assignee,omitempty"` 35 Milestone *int `json:"milestone,omitempty"` 36 Closed *bool `json:"closed,omitempty"` 37 Labels []string `json:"labels,omitempty"` 38 } 39 40 // Comment represents comments of issue to import. 41 type Comment struct { 42 CreatedAt *Timestamp `json:"created_at,omitempty"` 43 Body string `json:"body"` 44 } 45 46 // IssueImportResponse represents the response of an issue import create request. 47 // 48 // https://gist.github.com/jonmagic/5282384165e0f86ef105#import-issue-response 49 type IssueImportResponse struct { 50 ID *int `json:"id,omitempty"` 51 Status *string `json:"status,omitempty"` 52 URL *string `json:"url,omitempty"` 53 ImportIssuesURL *string `json:"import_issues_url,omitempty"` 54 RepositoryURL *string `json:"repository_url,omitempty"` 55 CreatedAt *Timestamp `json:"created_at,omitempty"` 56 UpdatedAt *Timestamp `json:"updated_at,omitempty"` 57 Message *string `json:"message,omitempty"` 58 DocumentationURL *string `json:"documentation_url,omitempty"` 59 Errors []*IssueImportError `json:"errors,omitempty"` 60 } 61 62 // IssueImportError represents errors of an issue import create request. 63 type IssueImportError struct { 64 Location *string `json:"location,omitempty"` 65 Resource *string `json:"resource,omitempty"` 66 Field *string `json:"field,omitempty"` 67 Value *string `json:"value,omitempty"` 68 Code *string `json:"code,omitempty"` 69 } 70 71 // Create a new imported issue on the specified repository. 72 // 73 // https://gist.github.com/jonmagic/5282384165e0f86ef105#start-an-issue-import 74 func (s *IssueImportService) Create(ctx context.Context, owner, repo string, issue *IssueImportRequest) (*IssueImportResponse, *Response, error) { 75 u := fmt.Sprintf("repos/%v/%v/import/issues", owner, repo) 76 req, err := s.client.NewRequest("POST", u, issue) 77 if err != nil { 78 return nil, nil, err 79 } 80 81 // TODO: remove custom Accept headers when APIs fully launch. 82 req.Header.Set("Accept", mediaTypeIssueImportAPI) 83 84 i := new(IssueImportResponse) 85 resp, err := s.client.Do(ctx, req, i) 86 if err != nil { 87 aerr, ok := err.(*AcceptedError) 88 if ok { 89 decErr := json.Unmarshal(aerr.Raw, i) 90 if decErr != nil { 91 err = decErr 92 } 93 94 return i, resp, nil 95 } 96 97 return nil, resp, err 98 } 99 100 return i, resp, nil 101 } 102 103 // CheckStatus checks the status of an imported issue. 104 // 105 // https://gist.github.com/jonmagic/5282384165e0f86ef105#import-status-request 106 func (s *IssueImportService) CheckStatus(ctx context.Context, owner, repo string, issueID int64) (*IssueImportResponse, *Response, error) { 107 u := fmt.Sprintf("repos/%v/%v/import/issues/%v", owner, repo, issueID) 108 req, err := s.client.NewRequest("GET", u, nil) 109 if err != nil { 110 return nil, nil, err 111 } 112 113 // TODO: remove custom Accept headers when APIs fully launch. 114 req.Header.Set("Accept", mediaTypeIssueImportAPI) 115 116 i := new(IssueImportResponse) 117 resp, err := s.client.Do(ctx, req, i) 118 if err != nil { 119 return nil, resp, err 120 } 121 122 return i, resp, nil 123 } 124 125 // CheckStatusSince checks the status of multiple imported issues since a given date. 126 // 127 // https://gist.github.com/jonmagic/5282384165e0f86ef105#check-status-of-multiple-issues 128 func (s *IssueImportService) CheckStatusSince(ctx context.Context, owner, repo string, since Timestamp) ([]*IssueImportResponse, *Response, error) { 129 u := fmt.Sprintf("repos/%v/%v/import/issues?since=%v", owner, repo, since.Format("2006-01-02")) 130 req, err := s.client.NewRequest("GET", u, nil) 131 if err != nil { 132 return nil, nil, err 133 } 134 135 // TODO: remove custom Accept headers when APIs fully launch. 136 req.Header.Set("Accept", mediaTypeIssueImportAPI) 137 138 var b bytes.Buffer 139 resp, err := s.client.Do(ctx, req, &b) 140 if err != nil { 141 return nil, resp, err 142 } 143 144 var i []*IssueImportResponse 145 err = json.Unmarshal(b.Bytes(), &i) 146 if err != nil { 147 return nil, resp, err 148 } 149 150 return i, resp, nil 151 }