...

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

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

     1  // Copyright 2014 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  	"fmt"
    12  	"net/url"
    13  )
    14  
    15  // MarkdownOptions specifies optional parameters to the Markdown method.
    16  type MarkdownOptions struct {
    17  	// Mode identifies the rendering mode. Possible values are:
    18  	//   markdown - render a document as plain Markdown, just like
    19  	//   README files are rendered.
    20  	//
    21  	//   gfm - to render a document as user-content, e.g. like user
    22  	//   comments or issues are rendered. In GFM mode, hard line breaks are
    23  	//   always taken into account, and issue and user mentions are linked
    24  	//   accordingly.
    25  	//
    26  	// Default is "markdown".
    27  	Mode string
    28  
    29  	// Context identifies the repository context. Only taken into account
    30  	// when rendering as "gfm".
    31  	Context string
    32  }
    33  
    34  type markdownRequest struct {
    35  	Text    *string `json:"text,omitempty"`
    36  	Mode    *string `json:"mode,omitempty"`
    37  	Context *string `json:"context,omitempty"`
    38  }
    39  
    40  // Markdown renders an arbitrary Markdown document.
    41  //
    42  // GitHub API docs: https://docs.github.com/en/rest/markdown/
    43  func (c *Client) Markdown(ctx context.Context, text string, opts *MarkdownOptions) (string, *Response, error) {
    44  	request := &markdownRequest{Text: String(text)}
    45  	if opts != nil {
    46  		if opts.Mode != "" {
    47  			request.Mode = String(opts.Mode)
    48  		}
    49  		if opts.Context != "" {
    50  			request.Context = String(opts.Context)
    51  		}
    52  	}
    53  
    54  	req, err := c.NewRequest("POST", "markdown", request)
    55  	if err != nil {
    56  		return "", nil, err
    57  	}
    58  
    59  	buf := new(bytes.Buffer)
    60  	resp, err := c.Do(ctx, req, buf)
    61  	if err != nil {
    62  		return "", resp, err
    63  	}
    64  
    65  	return buf.String(), resp, nil
    66  }
    67  
    68  // ListEmojis returns the emojis available to use on GitHub.
    69  //
    70  // GitHub API docs: https://docs.github.com/en/rest/emojis/
    71  func (c *Client) ListEmojis(ctx context.Context) (map[string]string, *Response, error) {
    72  	req, err := c.NewRequest("GET", "emojis", nil)
    73  	if err != nil {
    74  		return nil, nil, err
    75  	}
    76  
    77  	var emoji map[string]string
    78  	resp, err := c.Do(ctx, req, &emoji)
    79  	if err != nil {
    80  		return nil, resp, err
    81  	}
    82  
    83  	return emoji, resp, nil
    84  }
    85  
    86  // CodeOfConduct represents a code of conduct.
    87  type CodeOfConduct struct {
    88  	Name *string `json:"name,omitempty"`
    89  	Key  *string `json:"key,omitempty"`
    90  	URL  *string `json:"url,omitempty"`
    91  	Body *string `json:"body,omitempty"`
    92  }
    93  
    94  func (c *CodeOfConduct) String() string {
    95  	return Stringify(c)
    96  }
    97  
    98  // ListCodesOfConduct returns all codes of conduct.
    99  //
   100  // GitHub API docs: https://docs.github.com/en/rest/codes_of_conduct/#list-all-codes-of-conduct
   101  func (c *Client) ListCodesOfConduct(ctx context.Context) ([]*CodeOfConduct, *Response, error) {
   102  	req, err := c.NewRequest("GET", "codes_of_conduct", nil)
   103  	if err != nil {
   104  		return nil, nil, err
   105  	}
   106  
   107  	// TODO: remove custom Accept header when this API fully launches.
   108  	req.Header.Set("Accept", mediaTypeCodesOfConductPreview)
   109  
   110  	var cs []*CodeOfConduct
   111  	resp, err := c.Do(ctx, req, &cs)
   112  	if err != nil {
   113  		return nil, resp, err
   114  	}
   115  
   116  	return cs, resp, nil
   117  }
   118  
   119  // GetCodeOfConduct returns an individual code of conduct.
   120  //
   121  // https://docs.github.com/en/rest/codes_of_conduct/#get-an-individual-code-of-conduct
   122  func (c *Client) GetCodeOfConduct(ctx context.Context, key string) (*CodeOfConduct, *Response, error) {
   123  	u := fmt.Sprintf("codes_of_conduct/%s", key)
   124  	req, err := c.NewRequest("GET", u, nil)
   125  	if err != nil {
   126  		return nil, nil, err
   127  	}
   128  
   129  	// TODO: remove custom Accept header when this API fully launches.
   130  	req.Header.Set("Accept", mediaTypeCodesOfConductPreview)
   131  
   132  	coc := new(CodeOfConduct)
   133  	resp, err := c.Do(ctx, req, coc)
   134  	if err != nil {
   135  		return nil, resp, err
   136  	}
   137  
   138  	return coc, resp, nil
   139  }
   140  
   141  // APIMeta represents metadata about the GitHub API.
   142  type APIMeta struct {
   143  	// An Array of IP addresses in CIDR format specifying the addresses
   144  	// that incoming service hooks will originate from on GitHub.com.
   145  	Hooks []string `json:"hooks,omitempty"`
   146  
   147  	// An Array of IP addresses in CIDR format specifying the Git servers
   148  	// for GitHub.com.
   149  	Git []string `json:"git,omitempty"`
   150  
   151  	// Whether authentication with username and password is supported.
   152  	// (GitHub Enterprise instances using CAS or OAuth for authentication
   153  	// will return false. Features like Basic Authentication with a
   154  	// username and password, sudo mode, and two-factor authentication are
   155  	// not supported on these servers.)
   156  	VerifiablePasswordAuthentication *bool `json:"verifiable_password_authentication,omitempty"`
   157  
   158  	// An array of IP addresses in CIDR format specifying the addresses
   159  	// which serve GitHub Pages websites.
   160  	Pages []string `json:"pages,omitempty"`
   161  
   162  	// An Array of IP addresses specifying the addresses that source imports
   163  	// will originate from on GitHub.com.
   164  	Importer []string `json:"importer,omitempty"`
   165  
   166  	// An array of IP addresses in CIDR format specifying the IP addresses
   167  	// GitHub Actions will originate from.
   168  	Actions []string `json:"actions,omitempty"`
   169  
   170  	// An array of IP addresses in CIDR format specifying the IP addresses
   171  	// Dependabot will originate from.
   172  	Dependabot []string `json:"dependabot,omitempty"`
   173  
   174  	// A map of algorithms to SSH key fingerprints.
   175  	SSHKeyFingerprints map[string]string `json:"ssh_key_fingerprints,omitempty"`
   176  
   177  	// An array of SSH keys.
   178  	SSHKeys []string `json:"ssh_keys,omitempty"`
   179  }
   180  
   181  // APIMeta returns information about GitHub.com, the service. Or, if you access
   182  // this endpoint on your organization’s GitHub Enterprise installation, this
   183  // endpoint provides information about that installation.
   184  //
   185  // GitHub API docs: https://docs.github.com/en/rest/meta#get-github-meta-information
   186  func (c *Client) APIMeta(ctx context.Context) (*APIMeta, *Response, error) {
   187  	req, err := c.NewRequest("GET", "meta", nil)
   188  	if err != nil {
   189  		return nil, nil, err
   190  	}
   191  
   192  	meta := new(APIMeta)
   193  	resp, err := c.Do(ctx, req, meta)
   194  	if err != nil {
   195  		return nil, resp, err
   196  	}
   197  
   198  	return meta, resp, nil
   199  }
   200  
   201  // Octocat returns an ASCII art octocat with the specified message in a speech
   202  // bubble. If message is empty, a random zen phrase is used.
   203  func (c *Client) Octocat(ctx context.Context, message string) (string, *Response, error) {
   204  	u := "octocat"
   205  	if message != "" {
   206  		u = fmt.Sprintf("%s?s=%s", u, url.QueryEscape(message))
   207  	}
   208  
   209  	req, err := c.NewRequest("GET", u, nil)
   210  	if err != nil {
   211  		return "", nil, err
   212  	}
   213  
   214  	buf := new(bytes.Buffer)
   215  	resp, err := c.Do(ctx, req, buf)
   216  	if err != nil {
   217  		return "", resp, err
   218  	}
   219  
   220  	return buf.String(), resp, nil
   221  }
   222  
   223  // Zen returns a random line from The Zen of GitHub.
   224  //
   225  // see also: http://warpspire.com/posts/taste/
   226  func (c *Client) Zen(ctx context.Context) (string, *Response, error) {
   227  	req, err := c.NewRequest("GET", "zen", nil)
   228  	if err != nil {
   229  		return "", nil, err
   230  	}
   231  
   232  	buf := new(bytes.Buffer)
   233  	resp, err := c.Do(ctx, req, buf)
   234  	if err != nil {
   235  		return "", resp, err
   236  	}
   237  
   238  	return buf.String(), resp, nil
   239  }
   240  
   241  // ServiceHook represents a hook that has configuration settings, a list of
   242  // available events, and default events.
   243  type ServiceHook struct {
   244  	Name            *string    `json:"name,omitempty"`
   245  	Events          []string   `json:"events,omitempty"`
   246  	SupportedEvents []string   `json:"supported_events,omitempty"`
   247  	Schema          [][]string `json:"schema,omitempty"`
   248  }
   249  
   250  func (s *ServiceHook) String() string {
   251  	return Stringify(s)
   252  }
   253  
   254  // ListServiceHooks lists all of the available service hooks.
   255  //
   256  // GitHub API docs: https://developer.github.com/webhooks/#services
   257  func (c *Client) ListServiceHooks(ctx context.Context) ([]*ServiceHook, *Response, error) {
   258  	u := "hooks"
   259  	req, err := c.NewRequest("GET", u, nil)
   260  	if err != nil {
   261  		return nil, nil, err
   262  	}
   263  
   264  	var hooks []*ServiceHook
   265  	resp, err := c.Do(ctx, req, &hooks)
   266  	if err != nil {
   267  		return nil, resp, err
   268  	}
   269  
   270  	return hooks, resp, nil
   271  }
   272  

View as plain text