| 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 |
// GitHub API docs: https://gist.github.com/jonmagic/5282384165e0f86ef105#start-an-issue-import |
| 74 |
// |
| 75 |
//meta:operation POST /repos/{owner}/{repo}/import/issues |
| 76 |
func (s *IssueImportService) Create(ctx context.Context, owner, repo string, issue *IssueImportRequest) (*IssueImportResponse, *Response, error) { |
| 77 |
u := fmt.Sprintf("repos/%v/%v/import/issues", owner, repo) |
| 78 |
req, err := s.client.NewRequest("POST", u, issue) |
| 79 |
if err != nil { |
| 80 |
return nil, nil, err |
| 81 |
} |
| 82 |
|
| 83 |
// TODO: remove custom Accept headers when APIs fully launch. |
| 84 |
req.Header.Set("Accept", mediaTypeIssueImportAPI) |
| 85 |
|
| 86 |
i := new(IssueImportResponse) |
| 87 |
resp, err := s.client.Do(ctx, req, i) |
| 88 |
if err != nil { |
| 89 |
aerr, ok := err.(*AcceptedError) |
| 90 |
if ok { |
| 91 |
if err := json.Unmarshal(aerr.Raw, i); err != nil { |
| 92 |
return i, resp, err |
| 93 |
} |
| 94 |
return i, resp, err |
| 95 |
} |
| 96 |
return nil, resp, err |
| 97 |
} |
| 98 |
|
| 99 |
return i, resp, nil |
| 100 |
} |
| 101 |
|
| 102 |
// CheckStatus checks the status of an imported issue. |
| 103 |
// |
| 104 |
// GitHub API docs: https://gist.github.com/jonmagic/5282384165e0f86ef105#import-status-request |
| 105 |
// |
| 106 |
//meta:operation GET /repos/{owner}/{repo}/import/issues/{issue_number} |
| 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 |
// TODO: remove custom Accept headers when APIs fully launch. |
| 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 |
// CheckStatusSince checks the status of multiple imported issues since a given date. |
| 127 |
// |
| 128 |
// GitHub API docs: https://gist.github.com/jonmagic/5282384165e0f86ef105#check-status-of-multiple-issues |
| 129 |
// |
| 130 |
//meta:operation GET /repos/{owner}/{repo}/import/issues |
| 131 |
func (s *IssueImportService) CheckStatusSince(ctx context.Context, owner, repo string, since Timestamp) ([]*IssueImportResponse, *Response, error) { |
| 132 |
u := fmt.Sprintf("repos/%v/%v/import/issues?since=%v", owner, repo, since.Format("2006-01-02")) |
| 133 |
req, err := s.client.NewRequest("GET", u, nil) |
| 134 |
if err != nil { |
| 135 |
return nil, nil, err |
| 136 |
} |
| 137 |
|
| 138 |
// TODO: remove custom Accept headers when APIs fully launch. |
| 139 |
req.Header.Set("Accept", mediaTypeIssueImportAPI) |
| 140 |
|
| 141 |
var b bytes.Buffer |
| 142 |
resp, err := s.client.Do(ctx, req, &b) |
| 143 |
if err != nil { |
| 144 |
return nil, resp, err |
| 145 |
} |
| 146 |
|
| 147 |
var i []*IssueImportResponse |
| 148 |
err = json.Unmarshal(b.Bytes(), &i) |
| 149 |
if err != nil { |
| 150 |
return nil, resp, err |
| 151 |
} |
| 152 |
|
| 153 |
return i, resp, nil |
| 154 |
} |