1
2
3
4
5
6 package github
7
8 import (
9 "bytes"
10 "context"
11 "encoding/json"
12 "fmt"
13 "time"
14 )
15
16
17
18 type IssueImportService service
19
20
21
22
23 type IssueImportRequest struct {
24 IssueImport IssueImport `json:"issue"`
25 Comments []*Comment `json:"comments,omitempty"`
26 }
27
28
29 type IssueImport struct {
30 Title string `json:"title"`
31 Body string `json:"body"`
32 CreatedAt *time.Time `json:"created_at,omitempty"`
33 ClosedAt *time.Time `json:"closed_at,omitempty"`
34 UpdatedAt *time.Time `json:"updated_at,omitempty"`
35 Assignee *string `json:"assignee,omitempty"`
36 Milestone *int `json:"milestone,omitempty"`
37 Closed *bool `json:"closed,omitempty"`
38 Labels []string `json:"labels,omitempty"`
39 }
40
41
42 type Comment struct {
43 CreatedAt *time.Time `json:"created_at,omitempty"`
44 Body string `json:"body"`
45 }
46
47
48
49
50 type IssueImportResponse struct {
51 ID *int `json:"id,omitempty"`
52 Status *string `json:"status,omitempty"`
53 URL *string `json:"url,omitempty"`
54 ImportIssuesURL *string `json:"import_issues_url,omitempty"`
55 RepositoryURL *string `json:"repository_url,omitempty"`
56 CreatedAt *time.Time `json:"created_at,omitempty"`
57 UpdatedAt *time.Time `json:"updated_at,omitempty"`
58 Message *string `json:"message,omitempty"`
59 DocumentationURL *string `json:"documentation_url,omitempty"`
60 Errors []*IssueImportError `json:"errors,omitempty"`
61 }
62
63
64 type IssueImportError struct {
65 Location *string `json:"location,omitempty"`
66 Resource *string `json:"resource,omitempty"`
67 Field *string `json:"field,omitempty"`
68 Value *string `json:"value,omitempty"`
69 Code *string `json:"code,omitempty"`
70 }
71
72
73
74
75 func (s *IssueImportService) Create(ctx context.Context, owner, repo string, issue *IssueImportRequest) (*IssueImportResponse, *Response, error) {
76 u := fmt.Sprintf("repos/%v/%v/import/issues", owner, repo)
77 req, err := s.client.NewRequest("POST", u, issue)
78 if err != nil {
79 return nil, nil, err
80 }
81
82
83 req.Header.Set("Accept", mediaTypeIssueImportAPI)
84
85 i := new(IssueImportResponse)
86 resp, err := s.client.Do(ctx, req, i)
87 if err != nil {
88 aerr, ok := err.(*AcceptedError)
89 if ok {
90 decErr := json.Unmarshal(aerr.Raw, i)
91 if decErr != nil {
92 err = decErr
93 }
94
95 return i, resp, nil
96 }
97
98 return nil, resp, err
99 }
100
101 return i, resp, nil
102 }
103
104
105
106
107 func (s *IssueImportService) CheckStatus(ctx context.Context, owner, repo string, issueID int64) (*IssueImportResponse, *Response, error) {
108 u := fmt.Sprintf("repos/%v/%v/import/issues/%v", owner, repo, issueID)
109 req, err := s.client.NewRequest("GET", u, nil)
110 if err != nil {
111 return nil, nil, err
112 }
113
114
115 req.Header.Set("Accept", mediaTypeIssueImportAPI)
116
117 i := new(IssueImportResponse)
118 resp, err := s.client.Do(ctx, req, i)
119 if err != nil {
120 return nil, resp, err
121 }
122
123 return i, resp, nil
124 }
125
126
127
128
129 func (s *IssueImportService) CheckStatusSince(ctx context.Context, owner, repo string, since time.Time) ([]*IssueImportResponse, *Response, error) {
130 u := fmt.Sprintf("repos/%v/%v/import/issues?since=%v", owner, repo, since.Format("2006-01-02"))
131 req, err := s.client.NewRequest("GET", u, nil)
132 if err != nil {
133 return nil, nil, err
134 }
135
136
137 req.Header.Set("Accept", mediaTypeIssueImportAPI)
138
139 var b bytes.Buffer
140 resp, err := s.client.Do(ctx, req, &b)
141 if err != nil {
142 return nil, resp, err
143 }
144
145 var i []*IssueImportResponse
146 err = json.Unmarshal(b.Bytes(), &i)
147 if err != nil {
148 return nil, resp, err
149 }
150
151 return i, resp, nil
152 }
153
View as plain text