...

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

Documentation: github.com/google/go-github/v45/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  	"time"
    14  )
    15  
    16  // IssueImportService handles communication with the issue import related
    17  // methods of the Issue Import GitHub API.
    18  type IssueImportService service
    19  
    20  // IssueImportRequest represents a request to create an issue.
    21  //
    22  // https://gist.github.com/jonmagic/5282384165e0f86ef105#supported-issue-and-comment-fields
    23  type IssueImportRequest struct {
    24  	IssueImport IssueImport `json:"issue"`
    25  	Comments    []*Comment  `json:"comments,omitempty"`
    26  }
    27  
    28  // IssueImport represents body of issue to import.
    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  // Comment represents comments of issue to import.
    42  type Comment struct {
    43  	CreatedAt *time.Time `json:"created_at,omitempty"`
    44  	Body      string     `json:"body"`
    45  }
    46  
    47  // IssueImportResponse represents the response of an issue import create request.
    48  //
    49  // https://gist.github.com/jonmagic/5282384165e0f86ef105#import-issue-response
    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  // IssueImportError represents errors of an issue import create request.
    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  // Create a new imported issue on the specified repository.
    73  //
    74  // https://gist.github.com/jonmagic/5282384165e0f86ef105#start-an-issue-import
    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  	// TODO: remove custom Accept headers when APIs fully launch.
    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  // CheckStatus checks the status of an imported issue.
   105  //
   106  // https://gist.github.com/jonmagic/5282384165e0f86ef105#import-status-request
   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  // https://gist.github.com/jonmagic/5282384165e0f86ef105#check-status-of-multiple-issues
   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  	// TODO: remove custom Accept headers when APIs fully launch.
   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