...

Source file src/github.com/google/go-github/v55/github/issue_import.go

Documentation: github.com/google/go-github/v55/github

     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  			if err := json.Unmarshal(aerr.Raw, i); err != nil {
    90  				return i, resp, err
    91  			}
    92  			return i, resp, err
    93  		}
    94  		return nil, resp, err
    95  	}
    96  
    97  	return i, resp, nil
    98  }
    99  
   100  // CheckStatus checks the status of an imported issue.
   101  //
   102  // https://gist.github.com/jonmagic/5282384165e0f86ef105#import-status-request
   103  func (s *IssueImportService) CheckStatus(ctx context.Context, owner, repo string, issueID int64) (*IssueImportResponse, *Response, error) {
   104  	u := fmt.Sprintf("repos/%v/%v/import/issues/%v", owner, repo, issueID)
   105  	req, err := s.client.NewRequest("GET", u, nil)
   106  	if err != nil {
   107  		return nil, nil, err
   108  	}
   109  
   110  	// TODO: remove custom Accept headers when APIs fully launch.
   111  	req.Header.Set("Accept", mediaTypeIssueImportAPI)
   112  
   113  	i := new(IssueImportResponse)
   114  	resp, err := s.client.Do(ctx, req, i)
   115  	if err != nil {
   116  		return nil, resp, err
   117  	}
   118  
   119  	return i, resp, nil
   120  }
   121  
   122  // CheckStatusSince checks the status of multiple imported issues since a given date.
   123  //
   124  // https://gist.github.com/jonmagic/5282384165e0f86ef105#check-status-of-multiple-issues
   125  func (s *IssueImportService) CheckStatusSince(ctx context.Context, owner, repo string, since Timestamp) ([]*IssueImportResponse, *Response, error) {
   126  	u := fmt.Sprintf("repos/%v/%v/import/issues?since=%v", owner, repo, since.Format("2006-01-02"))
   127  	req, err := s.client.NewRequest("GET", u, nil)
   128  	if err != nil {
   129  		return nil, nil, err
   130  	}
   131  
   132  	// TODO: remove custom Accept headers when APIs fully launch.
   133  	req.Header.Set("Accept", mediaTypeIssueImportAPI)
   134  
   135  	var b bytes.Buffer
   136  	resp, err := s.client.Do(ctx, req, &b)
   137  	if err != nil {
   138  		return nil, resp, err
   139  	}
   140  
   141  	var i []*IssueImportResponse
   142  	err = json.Unmarshal(b.Bytes(), &i)
   143  	if err != nil {
   144  		return nil, resp, err
   145  	}
   146  
   147  	return i, resp, nil
   148  }
   149  

View as plain text