Update deps

This commit is contained in:
Jeff Mitchell 2017-02-24 14:36:54 -05:00
parent f0bb6de9fb
commit 8836da35a6
146 changed files with 2195 additions and 4457 deletions

View file

@ -22,9 +22,9 @@ type RequestRetryer interface{}
//
// // Create Session with MaxRetry configuration to be shared by multiple
// // service clients.
// sess, err := session.NewSession(&aws.Config{
// sess := session.Must(session.NewSession(&aws.Config{
// MaxRetries: aws.Int(3),
// })
// }))
//
// // Create S3 service client with a specific Region.
// svc := s3.New(sess, &aws.Config{
@ -154,7 +154,8 @@ type Config struct {
// the EC2Metadata overriding the timeout for default credentials chain.
//
// Example:
// sess, err := session.NewSession(aws.NewConfig().WithEC2MetadataDiableTimeoutOverride(true))
// sess := session.Must(session.NewSession(aws.NewConfig()
// .WithEC2MetadataDiableTimeoutOverride(true)))
//
// svc := s3.New(sess)
//
@ -174,7 +175,7 @@ type Config struct {
//
// Only supported with.
//
// sess, err := session.NewSession()
// sess := session.Must(session.NewSession())
//
// svc := s3.New(sess, &aws.Config{
// UseDualStack: aws.Bool(true),
@ -192,7 +193,9 @@ type Config struct {
// Will default to false. This would only be used for empty directory names in s3 requests.
//
// Example:
// sess, err := session.NewSession(&aws.Config{DisableRestProtocolURICleaning: aws.Bool(true))
// sess := session.Must(session.NewSession(&aws.Config{
// DisableRestProtocolURICleaning: aws.Bool(true),
// }))
//
// svc := s3.New(sess)
// out, err := svc.GetObject(&s3.GetObjectInput {
@ -207,9 +210,9 @@ type Config struct {
//
// // Create Session with MaxRetry configuration to be shared by multiple
// // service clients.
// sess, err := session.NewSession(aws.NewConfig().
// sess := session.Must(session.NewSession(aws.NewConfig().
// WithMaxRetries(3),
// )
// ))
//
// // Create S3 service client with a specific Region.
// svc := s3.New(sess, aws.NewConfig().

View file

@ -1,7 +1,81 @@
// Package stscreds are credential Providers to retrieve STS AWS credentials.
//
// STS provides multiple ways to retrieve credentials which can be used when making
// future AWS service API operation calls.
/*
Package stscreds are credential Providers to retrieve STS AWS credentials.
STS provides multiple ways to retrieve credentials which can be used when making
future AWS service API operation calls.
The SDK will ensure that per instance of credentials.Credentials all requests
to refresh the credentials will be synchronized. But, the SDK is unable to
ensure synchronous usage of the AssumeRoleProvider if the value is shared
between multiple Credentials, Sessions or service clients.
Assume Role
To assume an IAM role using STS with the SDK you can create a new Credentials
with the SDKs's stscreds package.
// Initial credentials loaded from SDK's default credential chain. Such as
// the environment, shared credentials (~/.aws/credentials), or EC2 Instance
// Role. These credentials will be used to to make the STS Assume Role API.
sess := session.Must(session.NewSession())
// Create the credentials from AssumeRoleProvider to assume the role
// referenced by the "myRoleARN" ARN.
creds := stscreds.NewCredentials(sess, "myRoleArn")
// Create service client value configured for credentials
// from assumed role.
svc := s3.New(sess, &aws.Config{Credentials: creds})
Assume Role with static MFA Token
To assume an IAM role with a MFA token you can either specify a MFA token code
directly or provide a function to prompt the user each time the credentials
need to refresh the role's credentials. Specifying the TokenCode should be used
for short lived operations that will not need to be refreshed, and when you do
not want to have direct control over the user provides their MFA token.
With TokenCode the AssumeRoleProvider will be not be able to refresh the role's
credentials.
// Create the credentials from AssumeRoleProvider to assume the role
// referenced by the "myRoleARN" ARN using the MFA token code provided.
creds := stscreds.NewCredentials(sess, "myRoleArn", func(p *stscreds.AssumeRoleProvider) {
p.SerialNumber = aws.String("myTokenSerialNumber")
p.TokenCode = aws.String("00000000")
})
// Create service client value configured for credentials
// from assumed role.
svc := s3.New(sess, &aws.Config{Credentials: creds})
Assume Role with MFA Token Provider
To assume an IAM role with MFA for longer running tasks where the credentials
may need to be refreshed setting the TokenProvider field of AssumeRoleProvider
will allow the credential provider to prompt for new MFA token code when the
role's credentials need to be refreshed.
The StdinTokenProvider function is available to prompt on stdin to retrieve
the MFA token code from the user. You can also implement custom prompts by
satisfing the TokenProvider function signature.
Using StdinTokenProvider with multiple AssumeRoleProviders, or Credentials will
have undesirable results as the StdinTokenProvider will not be synchronized. A
single Credentials with an AssumeRoleProvider can be shared safely.
// Create the credentials from AssumeRoleProvider to assume the role
// referenced by the "myRoleARN" ARN. Prompting for MFA token from stdin.
creds := stscreds.NewCredentials(sess, "myRoleArn", func(p *stscreds.AssumeRoleProvider) {
p.SerialNumber = aws.String("myTokenSerialNumber")
p.TokenProvider = stscreds.StdinTokenProvider
})
// Create service client value configured for credentials
// from assumed role.
svc := s3.New(sess, &aws.Config{Credentials: creds})
*/
package stscreds
import (
@ -9,11 +83,31 @@ import (
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/client"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/service/sts"
)
// StdinTokenProvider will prompt on stdout and read from stdin for a string value.
// An error is returned if reading from stdin fails.
//
// Use this function go read MFA tokens from stdin. The function makes no attempt
// to make atomic prompts from stdin across multiple gorouties.
//
// Using StdinTokenProvider with multiple AssumeRoleProviders, or Credentials will
// have undesirable results as the StdinTokenProvider will not be synchronized. A
// single Credentials with an AssumeRoleProvider can be shared safely
//
// Will wait forever until something is provided on the stdin.
func StdinTokenProvider() (string, error) {
var v string
fmt.Printf("Assume Role MFA token code: ")
_, err := fmt.Scanln(&v)
return v, err
}
// ProviderName provides a name of AssumeRole provider
const ProviderName = "AssumeRoleProvider"
@ -27,8 +121,15 @@ type AssumeRoler interface {
var DefaultDuration = time.Duration(15) * time.Minute
// AssumeRoleProvider retrieves temporary credentials from the STS service, and
// keeps track of their expiration time. This provider must be used explicitly,
// as it is not included in the credentials chain.
// keeps track of their expiration time.
//
// This credential provider will be used by the SDKs default credential change
// when shared configuration is enabled, and the shared config or shared credentials
// file configure assume role. See Session docs for how to do this.
//
// AssumeRoleProvider does not provide any synchronization and it is not safe
// to share this value across multiple Credentials, Sessions, or service clients
// without also sharing the same Credentials instance.
type AssumeRoleProvider struct {
credentials.Expiry
@ -65,8 +166,23 @@ type AssumeRoleProvider struct {
// assumed requires MFA (that is, if the policy includes a condition that tests
// for MFA). If the role being assumed requires MFA and if the TokenCode value
// is missing or expired, the AssumeRole call returns an "access denied" error.
//
// If SerialNumber is set and neither TokenCode nor TokenProvider are also
// set an error will be returned.
TokenCode *string
// Async method of providing MFA token code for assuming an IAM role with MFA.
// The value returned by the function will be used as the TokenCode in the Retrieve
// call. See StdinTokenProvider for a provider that prompts and reads from stdin.
//
// This token provider will be called when ever the assumed role's
// credentials need to be refreshed when SerialNumber is also set and
// TokenCode is not set.
//
// If both TokenCode and TokenProvider is set, TokenProvider will be used and
// TokenCode is ignored.
TokenProvider func() (string, error)
// ExpiryWindow will allow the credentials to trigger refreshing prior to
// the credentials actually expiring. This is beneficial so race conditions
// with expiring credentials do not cause request to fail unexpectedly
@ -85,6 +201,10 @@ type AssumeRoleProvider struct {
//
// Takes a Config provider to create the STS client. The ConfigProvider is
// satisfied by the session.Session type.
//
// It is safe to share the returned Credentials with multiple Sessions and
// service clients. All access to the credentials and refreshing them
// will be synchronized.
func NewCredentials(c client.ConfigProvider, roleARN string, options ...func(*AssumeRoleProvider)) *credentials.Credentials {
p := &AssumeRoleProvider{
Client: sts.New(c),
@ -103,7 +223,11 @@ func NewCredentials(c client.ConfigProvider, roleARN string, options ...func(*As
// AssumeRoleProvider. The credentials will expire every 15 minutes and the
// role will be named after a nanosecond timestamp of this operation.
//
// Takes an AssumeRoler which can be satisfiede by the STS client.
// Takes an AssumeRoler which can be satisfied by the STS client.
//
// It is safe to share the returned Credentials with multiple Sessions and
// service clients. All access to the credentials and refreshing them
// will be synchronized.
func NewCredentialsWithClient(svc AssumeRoler, roleARN string, options ...func(*AssumeRoleProvider)) *credentials.Credentials {
p := &AssumeRoleProvider{
Client: svc,
@ -139,12 +263,25 @@ func (p *AssumeRoleProvider) Retrieve() (credentials.Value, error) {
if p.Policy != nil {
input.Policy = p.Policy
}
if p.SerialNumber != nil && p.TokenCode != nil {
input.SerialNumber = p.SerialNumber
input.TokenCode = p.TokenCode
if p.SerialNumber != nil {
if p.TokenCode != nil {
input.SerialNumber = p.SerialNumber
input.TokenCode = p.TokenCode
} else if p.TokenProvider != nil {
input.SerialNumber = p.SerialNumber
code, err := p.TokenProvider()
if err != nil {
return credentials.Value{ProviderName: ProviderName}, err
}
input.TokenCode = aws.String(code)
} else {
return credentials.Value{ProviderName: ProviderName},
awserr.New("AssumeRoleTokenNotAvailable",
"assume role with MFA enabled, but neither TokenCode nor TokenProvider are set", nil)
}
}
roleOutput, err := p.Client.AssumeRole(input)
roleOutput, err := p.Client.AssumeRole(input)
if err != nil {
return credentials.Value{ProviderName: ProviderName}, err
}

View file

@ -45,16 +45,16 @@ region, and profile loaded from the environment and shared config automatically.
Requires the AWS_PROFILE to be set, or "default" is used.
// Create Session
sess, err := session.NewSession()
sess := session.Must(session.NewSession())
// Create a Session with a custom region
sess, err := session.NewSession(&aws.Config{Region: aws.String("us-east-1")})
sess := session.Must(session.NewSession(&aws.Config{
Region: aws.String("us-east-1"),
}))
// Create a S3 client instance from a session
sess, err := session.NewSession()
if err != nil {
// Handle Session creation error
}
sess := session.Must(session.NewSession())
svc := s3.New(sess)
Create Session With Option Overrides
@ -67,23 +67,25 @@ Use NewSessionWithOptions when you want to provide the config profile, or
override the shared config state (AWS_SDK_LOAD_CONFIG).
// Equivalent to session.NewSession()
sess, err := session.NewSessionWithOptions(session.Options{})
sess := session.Must(session.NewSessionWithOptions(session.Options{
// Options
}))
// Specify profile to load for the session's config
sess, err := session.NewSessionWithOptions(session.Options{
sess := session.Must(session.NewSessionWithOptions(session.Options{
Profile: "profile_name",
})
}))
// Specify profile for config and region for requests
sess, err := session.NewSessionWithOptions(session.Options{
sess := session.Must(session.NewSessionWithOptions(session.Options{
Config: aws.Config{Region: aws.String("us-east-1")},
Profile: "profile_name",
})
}))
// Force enable Shared Config support
sess, err := session.NewSessionWithOptions(session.Options{
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: SharedConfigEnable,
})
}))
Adding Handlers
@ -93,7 +95,8 @@ handler logs every request and its payload made by a service client:
// Create a session, and add additional handlers for all service
// clients created with the Session to inherit. Adds logging handler.
sess, err := session.NewSession()
sess := session.Must(session.NewSession())
sess.Handlers.Send.PushFront(func(r *request.Request) {
// Log every request made and its payload
logger.Println("Request: %s/%s, Payload: %s",
@ -138,15 +141,14 @@ the other two fields are also provided.
Assume Role values allow you to configure the SDK to assume an IAM role using
a set of credentials provided in a config file via the source_profile field.
Both "role_arn" and "source_profile" are required. The SDK does not support
assuming a role with MFA token Via the Session's constructor. You can use the
stscreds.AssumeRoleProvider credentials provider to specify custom
configuration and support for MFA.
Both "role_arn" and "source_profile" are required. The SDK supports assuming
a role with MFA token if the session option AssumeRoleTokenProvider
is set.
role_arn = arn:aws:iam::<account_number>:role/<role_name>
source_profile = profile_with_creds
external_id = 1234
mfa_serial = not supported!
mfa_serial = <serial or mfa arn>
role_session_name = session_name
Region is the region the SDK should use for looking up AWS service endpoints
@ -154,6 +156,37 @@ and signing requests.
region = us-east-1
Assume Role with MFA token
To create a session with support for assuming an IAM role with MFA set the
session option AssumeRoleTokenProvider to a function that will prompt for the
MFA token code when the SDK assumes the role and refreshes the role's credentials.
This allows you to configure the SDK via the shared config to assumea role
with MFA tokens.
In order for the SDK to assume a role with MFA the SharedConfigState
session option must be set to SharedConfigEnable, or AWS_SDK_LOAD_CONFIG
environment variable set.
The shared configuration instructs the SDK to assume an IAM role with MFA
when the mfa_serial configuration field is set in the shared config
(~/.aws/config) or shared credentials (~/.aws/credentials) file.
If mfa_serial is set in the configuration, the SDK will assume the role, and
the AssumeRoleTokenProvider session option is not set an an error will
be returned when creating the session.
sess := session.Must(session.NewSessionWithOptions(session.Options{
AssumeRoleTokenProvider: stscreds.StdinTokenProvider,
}))
// Create service client value configured for credentials
// from assumed role.
svc := s3.New(sess)
To setup assume role outside of a session see the stscrds.AssumeRoleProvider
documentation.
Environment Variables
When a Session is created several environment variables can be set to adjust

View file

@ -52,7 +52,7 @@ func New(cfgs ...*aws.Config) *Session {
envCfg := loadEnvConfig()
if envCfg.EnableSharedConfig {
s, err := newSession(envCfg, cfgs...)
s, err := newSession(Options{}, envCfg, cfgs...)
if err != nil {
// Old session.New expected all errors to be discovered when
// a request is made, and would report the errors then. This
@ -73,7 +73,7 @@ func New(cfgs ...*aws.Config) *Session {
return s
}
return oldNewSession(cfgs...)
return deprecatedNewSession(cfgs...)
}
// NewSession returns a new Session created from SDK defaults, config files,
@ -94,7 +94,7 @@ func New(cfgs ...*aws.Config) *Session {
func NewSession(cfgs ...*aws.Config) (*Session, error) {
envCfg := loadEnvConfig()
return newSession(envCfg, cfgs...)
return newSession(Options{}, envCfg, cfgs...)
}
// SharedConfigState provides the ability to optionally override the state
@ -147,6 +147,26 @@ type Options struct {
// will allow you to override the AWS_SDK_LOAD_CONFIG environment variable
// and enable or disable the shared config functionality.
SharedConfigState SharedConfigState
// When the SDK's shared config is configured to assume a role with MFA
// this option is required in order to provide the mechanism that will
// retrieve the MFA token. There is no default value for this field. If
// it is not set an error will be returned when creating the session.
//
// This token provider will be called when ever the assumed role's
// credentials need to be refreshed. Within the context of service clients
// all sharing the same session the SDK will ensure calls to the token
// provider are atomic. When sharing a token provider across multiple
// sessions additional synchronization logic is needed to ensure the
// token providers do not introduce race conditions. It is recommend to
// share the session where possible.
//
// stscreds.StdinTokenProvider is a basic implementation that will prompt
// from stdin for the MFA token code.
//
// This field is only used if the shared configuration is enabled, and
// the config enables assume role wit MFA via the mfa_serial field.
AssumeRoleTokenProvider func() (string, error)
}
// NewSessionWithOptions returns a new Session created from SDK defaults, config files,
@ -161,23 +181,23 @@ type Options struct {
// to be built with retrieving credentials with AssumeRole set in the config.
//
// // Equivalent to session.New
// sess, err := session.NewSessionWithOptions(session.Options{})
// sess := session.Must(session.NewSessionWithOptions(session.Options{}))
//
// // Specify profile to load for the session's config
// sess, err := session.NewSessionWithOptions(session.Options{
// sess := session.Must(session.NewSessionWithOptions(session.Options{
// Profile: "profile_name",
// })
// }))
//
// // Specify profile for config and region for requests
// sess, err := session.NewSessionWithOptions(session.Options{
// sess := session.Must(session.NewSessionWithOptions(session.Options{
// Config: aws.Config{Region: aws.String("us-east-1")},
// Profile: "profile_name",
// })
// }))
//
// // Force enable Shared Config support
// sess, err := session.NewSessionWithOptions(session.Options{
// sess := session.Must(session.NewSessionWithOptions(session.Options{
// SharedConfigState: SharedConfigEnable,
// })
// }))
func NewSessionWithOptions(opts Options) (*Session, error) {
var envCfg envConfig
if opts.SharedConfigState == SharedConfigEnable {
@ -197,7 +217,7 @@ func NewSessionWithOptions(opts Options) (*Session, error) {
envCfg.EnableSharedConfig = true
}
return newSession(envCfg, &opts.Config)
return newSession(opts, envCfg, &opts.Config)
}
// Must is a helper function to ensure the Session is valid and there was no
@ -215,7 +235,7 @@ func Must(sess *Session, err error) *Session {
return sess
}
func oldNewSession(cfgs ...*aws.Config) *Session {
func deprecatedNewSession(cfgs ...*aws.Config) *Session {
cfg := defaults.Config()
handlers := defaults.Handlers()
@ -242,7 +262,7 @@ func oldNewSession(cfgs ...*aws.Config) *Session {
return s
}
func newSession(envCfg envConfig, cfgs ...*aws.Config) (*Session, error) {
func newSession(opts Options, envCfg envConfig, cfgs ...*aws.Config) (*Session, error) {
cfg := defaults.Config()
handlers := defaults.Handlers()
@ -266,7 +286,9 @@ func newSession(envCfg envConfig, cfgs ...*aws.Config) (*Session, error) {
return nil, err
}
mergeConfigSrcs(cfg, userCfg, envCfg, sharedCfg, handlers)
if err := mergeConfigSrcs(cfg, userCfg, envCfg, sharedCfg, handlers, opts); err != nil {
return nil, err
}
s := &Session{
Config: cfg,
@ -278,7 +300,7 @@ func newSession(envCfg envConfig, cfgs ...*aws.Config) (*Session, error) {
return s, nil
}
func mergeConfigSrcs(cfg, userCfg *aws.Config, envCfg envConfig, sharedCfg sharedConfig, handlers request.Handlers) {
func mergeConfigSrcs(cfg, userCfg *aws.Config, envCfg envConfig, sharedCfg sharedConfig, handlers request.Handlers, sessOpts Options) error {
// Merge in user provided configuration
cfg.MergeIn(userCfg)
@ -302,6 +324,11 @@ func mergeConfigSrcs(cfg, userCfg *aws.Config, envCfg envConfig, sharedCfg share
cfgCp.Credentials = credentials.NewStaticCredentialsFromCreds(
sharedCfg.AssumeRoleSource.Creds,
)
if len(sharedCfg.AssumeRole.MFASerial) > 0 && sessOpts.AssumeRoleTokenProvider == nil {
// AssumeRole Token provider is required if doing Assume Role
// with MFA.
return AssumeRoleTokenProviderNotSetError{}
}
cfg.Credentials = stscreds.NewCredentials(
&Session{
Config: &cfgCp,
@ -311,11 +338,16 @@ func mergeConfigSrcs(cfg, userCfg *aws.Config, envCfg envConfig, sharedCfg share
func(opt *stscreds.AssumeRoleProvider) {
opt.RoleSessionName = sharedCfg.AssumeRole.RoleSessionName
// Assume role with external ID
if len(sharedCfg.AssumeRole.ExternalID) > 0 {
opt.ExternalID = aws.String(sharedCfg.AssumeRole.ExternalID)
}
// MFA not supported
// Assume role with MFA
if len(sharedCfg.AssumeRole.MFASerial) > 0 {
opt.SerialNumber = aws.String(sharedCfg.AssumeRole.MFASerial)
opt.TokenProvider = sessOpts.AssumeRoleTokenProvider
}
},
)
} else if len(sharedCfg.Creds.AccessKeyID) > 0 {
@ -336,6 +368,33 @@ func mergeConfigSrcs(cfg, userCfg *aws.Config, envCfg envConfig, sharedCfg share
})
}
}
return nil
}
// AssumeRoleTokenProviderNotSetError is an error returned when creating a session when the
// MFAToken option is not set when shared config is configured load assume a
// role with an MFA token.
type AssumeRoleTokenProviderNotSetError struct{}
// Code is the short id of the error.
func (e AssumeRoleTokenProviderNotSetError) Code() string {
return "AssumeRoleTokenProviderNotSetError"
}
// Message is the description of the error
func (e AssumeRoleTokenProviderNotSetError) Message() string {
return fmt.Sprintf("assume role with MFA enabled, but AssumeRoleTokenProvider session option not set.")
}
// OrigErr is the underlying error that caused the failure.
func (e AssumeRoleTokenProviderNotSetError) OrigErr() error {
return nil
}
// Error satisfies the error interface.
func (e AssumeRoleTokenProviderNotSetError) Error() string {
return awserr.SprintError(e.Code(), e.Message(), "", nil)
}
type credProviderError struct {

View file

@ -5,4 +5,4 @@ package aws
const SDKName = "aws-sdk-go"
// SDKVersion is the version of this SDK
const SDKVersion = "1.6.24"
const SDKVersion = "1.7.0"

View file

@ -31,11 +31,7 @@
//
// Marshal Go value type for DynamoDB.PutItem:
//
// sess, err := session.NewSession()
// if err != nil {
// fmt.Println("Failed create session", err)
// return
// }
// sess := session.Must(session.NewSession())
//
// svc := dynamodb.New(sess)
// item, err := dynamodbattribute.MarshalMap(r)

View file

@ -9921,7 +9921,7 @@ func (c *EC2) DescribeVolumesModificationsRequest(input *DescribeVolumesModifica
//
// You can also use CloudWatch Events to check the status of a modification
// to an EBS volume. For information about CloudWatch Events, see the Amazon
// CloudWatch Events User Guide (http://docs.aws.amazon.com/http:/docs.aws.amazon.com/AmazonCloudWatch/latest/events/WhatIsCloudWatchEvents.html).
// CloudWatch Events User Guide (http://docs.aws.amazon.com/AmazonCloudWatch/latest/events/).
// For more information, see Monitoring Volume Modifications" (http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-expand-volume.html#monitoring_mods).
//
// Returns awserr.Error for service API and SDK errors. Use runtime type assertions
@ -13080,30 +13080,29 @@ func (c *EC2) ModifyVolumeRequest(input *ModifyVolumeInput) (req *request.Reques
// without stopping the instance or detaching the volume from it. For more information
// about modifying an EBS volume running Linux, see Modifying the Size, IOPS,
// or Type of an EBS Volume on Linux (http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-expand-volume.html).
// For more information about modifying an EBS volume running Windows, see Expanding
// the Storage Space of an EBS Volume on Windows (http://docs.aws.amazon.com/docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ebs-expand-volume.html).
// For more information about modifying an EBS volume running Windows, see Modifying
// the Size, IOPS, or Type of an EBS Volume on Windows (http://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ebs-expand-volume.html).
//
// When you complete a resize operation on your volume, you need to extend the
// volume's file-system size to take advantage of the new storage capacity.
// For information about extending a Linux file system, see Extending a Linux
// File System (http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-expand-volume.html#recognize-expanded-volume-linux).
// For information about extending a Windows file system, see Extending a Windows
// File System (http://docs.aws.amazon.com/http:/docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ebs-expand-volume.html#recognize-expanded-volume-windows).
// File System (http://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ebs-expand-volume.html#recognize-expanded-volume-windows).
//
// You can use CloudWatch Events to check the status of a modification to an
// EBS volume. For information about CloudWatch Events, see the Amazon CloudWatch
// Events User Guide (http://docs.aws.amazon.com/http:/docs.aws.amazon.com/AmazonCloudWatch/latest/events/WhatIsCloudWatchEvents.html).
// Events User Guide (http://docs.aws.amazon.com/AmazonCloudWatch/latest/events/).
// You can also track the status of a modification using the DescribeVolumesModifications
// (http://docs.aws.amazon.com/http:/docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeVolumesModifications.html)
// API. For information about tracking status changes using either method, see
// Monitoring Volume Modifications" (http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-expand-volume.html#monitoring_mods).
// Monitoring Volume Modifications (http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-expand-volume.html#monitoring_mods).
//
// With previous-generation volumes and instance types, resizing an EBS volume
// may require detaching and reattaching the volume or stopping and restarting
// the instance. For more information about modifying an EBS volume running
// Linux, see Modifying the Size, IOPS, or Type of an EBS Volume on Linux (http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-expand-volume.html).
// With previous-generation instance types, resizing an EBS volume may require
// detaching and reattaching the volume or stopping and restarting the instance.
// For more information about modifying an EBS volume running Linux, see Modifying
// the Size, IOPS, or Type of an EBS Volume on Linux (http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ebs-expand-volume.html).
// For more information about modifying an EBS volume running Windows, see Modifying
// the Size, IOPS, or Type of an EBS Volume on Windows (http://docs.aws.amazon.com/docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ebs-expand-volume.html).
// the Size, IOPS, or Type of an EBS Volume on Windows (http://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ebs-expand-volume.html).
//
// If you reach the maximum volume modification rate per volume limit, you will
// need to wait at least six hours before applying further modifications to
@ -13854,31 +13853,27 @@ func (c *EC2) RegisterImageRequest(input *RegisterImageInput) (req *request.Requ
// in a single request, so you don't have to register the AMI yourself.
//
// You can also use RegisterImage to create an Amazon EBS-backed Linux AMI from
// a snapshot of a root device volume. For more information, see Launching an
// Instance from a Snapshot (http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_LaunchingInstanceFromSnapshot.html)
// a snapshot of a root device volume. You specify the snapshot using the block
// device mapping. For more information, see Launching an Instance from a Snapshot
// (http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_LaunchingInstanceFromSnapshot.html)
// in the Amazon Elastic Compute Cloud User Guide.
//
// You can't register an image where a secondary (non-root) snapshot has AWS
// Marketplace product codes.
//
// Some Linux distributions, such as Red Hat Enterprise Linux (RHEL) and SUSE
// Linux Enterprise Server (SLES), use the EC2 billingProduct code associated
// with an AMI to verify subscription status for package updates. Creating an
// AMI from an EBS snapshot does not maintain this billing code, and subsequent
// Linux Enterprise Server (SLES), use the EC2 billing product code associated
// with an AMI to verify the subscription status for package updates. Creating
// an AMI from an EBS snapshot does not maintain this billing code, and subsequent
// instances launched from such an AMI will not be able to connect to package
// update infrastructure.
//
// Similarly, although you can create a Windows AMI from a snapshot, you can't
// successfully launch an instance from the AMI.
//
// To create Windows AMIs or to create AMIs for Linux operating systems that
// must retain AMI billing codes to work properly, see CreateImage.
// update infrastructure. To create an AMI that must retain billing codes, see
// CreateImage.
//
// If needed, you can deregister an AMI at any time. Any modifications you make
// to an AMI backed by an instance store volume invalidates its registration.
// If you make changes to an image, deregister the previous image and register
// the new image.
//
// You can't register an image where a secondary (non-root) snapshot has AWS
// Marketplace product codes.
//
// Returns awserr.Error for service API and SDK errors. Use runtime type assertions
// with awserr.Error's Code and Message methods to get detailed information about
// the error.
@ -40467,6 +40462,8 @@ type ModifyVolumeInput struct {
// Only valid for Provisioned IOPS SSD (io1) volumes. For more information about
// io1 IOPS configuration, see http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSVolumeTypes.html#EBSVolumeTypes_piops
// (http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSVolumeTypes.html#EBSVolumeTypes_piops).
//
// Default: If no IOPS value is specified, the existing value is retained.
Iops *int64 `type:"integer"`
// Target size in GiB of the volume to be modified. Target volume size must
@ -40482,10 +40479,10 @@ type ModifyVolumeInput struct {
// Target EBS volume type of the volume to be modified
//
// Valid values are io1 | gp2 | sc1 | st1
//
// The API does not support modifications for volume type standard. You also
// cannot change the type of a volume to standard.
//
// Default: If no type is specified, the existing type is retained.
VolumeType *string `type:"string" enum:"VolumeType"`
}
@ -43248,6 +43245,9 @@ type RegisterImageInput struct {
// the architecture specified in the manifest file.
Architecture *string `locationName:"architecture" type:"string" enum:"ArchitectureValues"`
// The billing product codes.
BillingProducts []*string `locationName:"BillingProduct" locationNameList:"item" type:"list"`
// One or more block device mapping entries.
BlockDeviceMappings []*BlockDeviceMapping `locationName:"BlockDeviceMapping" locationNameList:"BlockDeviceMapping" type:"list"`
@ -43333,6 +43333,12 @@ func (s *RegisterImageInput) SetArchitecture(v string) *RegisterImageInput {
return s
}
// SetBillingProducts sets the BillingProducts field's value.
func (s *RegisterImageInput) SetBillingProducts(v []*string) *RegisterImageInput {
s.BillingProducts = v
return s
}
// SetBlockDeviceMappings sets the BlockDeviceMappings field's value.
func (s *RegisterImageInput) SetBlockDeviceMappings(v []*BlockDeviceMapping) *RegisterImageInput {
s.BlockDeviceMappings = v
@ -51527,8 +51533,8 @@ type VolumeModification struct {
// Modification completion or failure time.
EndTime *time.Time `locationName:"endTime" type:"timestamp" timestampFormat:"iso8601"`
// Current state of modification. Possible values are modifying | optimizing
// | complete | failed. Modification state is null for unmodified volumes.
// Current state of modification. Modification state is null for unmodified
// volumes.
ModificationState *string `locationName:"modificationState" type:"string" enum:"VolumeModificationState"`
// Original IOPS rate of the volume being modified.
@ -53314,6 +53320,24 @@ const (
// InstanceTypeI28xlarge is a InstanceType enum value
InstanceTypeI28xlarge = "i2.8xlarge"
// InstanceTypeI3Large is a InstanceType enum value
InstanceTypeI3Large = "i3.large"
// InstanceTypeI3Xlarge is a InstanceType enum value
InstanceTypeI3Xlarge = "i3.xlarge"
// InstanceTypeI32xlarge is a InstanceType enum value
InstanceTypeI32xlarge = "i3.2xlarge"
// InstanceTypeI34xlarge is a InstanceType enum value
InstanceTypeI34xlarge = "i3.4xlarge"
// InstanceTypeI38xlarge is a InstanceType enum value
InstanceTypeI38xlarge = "i3.8xlarge"
// InstanceTypeI316xlarge is a InstanceType enum value
InstanceTypeI316xlarge = "i3.16xlarge"
// InstanceTypeHi14xlarge is a InstanceType enum value
InstanceTypeHi14xlarge = "hi1.4xlarge"

View file

@ -251,7 +251,7 @@ func (a *API) apiCall(reqMethod string, reqPath string, data []byte) ([]byte, er
reqURL += "/"
}
if len(reqPath) >= 3 && reqPath[:3] == "/v2" {
reqURL += reqPath[3:len(reqPath)]
reqURL += reqPath[3:]
} else {
reqURL += reqPath
}

View file

@ -1,390 +0,0 @@
{
"_active": true,
"_cid": "/dashboard/1234",
"_created": 1483193930,
"_created_by": "/user/1234",
"_dashboard_uuid": "01234567-89ab-cdef-0123-456789abcdef",
"_last_modified": 1483450351,
"account_default": false,
"grid_layout": {
"height": 4,
"width": 4
},
"options": {
"access_configs": [
],
"fullscreen_hide_title": false,
"hide_grid": false,
"linkages": [
],
"scale_text": true,
"text_size": 16
},
"shared": false,
"title": "foo bar baz",
"widgets": [
{
"active": true,
"height": 1,
"name": "Cluster",
"origin": "d0",
"settings": {
"account_id": "1234",
"algorithm": "cor",
"cluster_id": 1234,
"cluster_name": "test",
"layout": "compact",
"size": "medium",
"threshold": 0.7
},
"type": "cluster",
"widget_id": "w4",
"width": 1
},
{
"active": true,
"height": 1,
"name": "HTML",
"origin": "d1",
"settings": {
"markup": "<h1>foo</h1>",
"title": "html"
},
"type": "html",
"widget_id": "w9",
"width": 1
},
{
"active": true,
"height": 1,
"name": "Chart",
"origin": "c0",
"settings": {
"chart_type": "bar",
"datapoints": [
{
"_check_id": 1234,
"_metric_type": "numeric",
"account_id": "1234",
"label": "Used",
"metric": "01234567-89ab-cdef-0123-456789abcdef:vm`memory`used"
},
{
"_check_id": 1234,
"_metric_type": "numeric",
"account_id": "1234",
"label": "Free",
"metric": "01234567-89ab-cdef-0123-456789abcdef:vm`memory`free"
}
],
"definition": {
"datasource": "realtime",
"derive": "gauge",
"disable_autoformat": false,
"formula": "",
"legend": {
"show": false,
"type": "html"
},
"period": 0,
"pop_onhover": false,
"wedge_labels": {
"on_chart": true,
"tooltips": false
},
"wedge_values": {
"angle": "0",
"color": "background",
"show": true
}
},
"title": "chart graph"
},
"type": "chart",
"widget_id": "w5",
"width": 1
},
{
"active": true,
"height": 1,
"name": "Alerts",
"origin": "a0",
"settings": {
"account_id": "1234",
"acknowledged": "all",
"cleared": "all",
"contact_groups": [
],
"dependents": "all",
"display": "list",
"maintenance": "all",
"min_age": "0",
"off_hours": [
17,
9
],
"search": "",
"severity": "12345",
"tag_filter_set": [
],
"time_window": "30M",
"title": "alerts",
"week_days": [
"sun",
"mon",
"tue",
"wed",
"thu",
"fri",
"sat"
]
},
"type": "alerts",
"widget_id": "w2",
"width": 1
},
{
"active": true,
"height": 1,
"name": "Graph",
"origin": "c1",
"settings": {
"_graph_title": "foo bar / %Used",
"account_id": "1234",
"date_window": "2w",
"graph_id": "01234567-89ab-cdef-0123-456789abcdef",
"hide_xaxis": false,
"hide_yaxis": false,
"key_inline": false,
"key_loc": "noop",
"key_size": "1",
"key_wrap": false,
"label": "",
"overlay_set_id": "",
"period": "2000",
"previous_graph_id": "null",
"realtime": false,
"show_flags": false
},
"type": "graph",
"widget_id": "w8",
"width": 1
},
{
"active": true,
"height": 1,
"name": "List",
"origin": "a2",
"settings": {
"account_id": "1234",
"limit": "10",
"search": "",
"type": "graph"
},
"type": "list",
"widget_id": "w10",
"width": 1
},
{
"active": true,
"height": 1,
"name": "Status",
"origin": "b2",
"settings": {
"account_id": "1234",
"agent_status_settings": {
"search": "",
"show_agent_types": "both",
"show_contact": false,
"show_feeds": true,
"show_setup": false,
"show_skew": true,
"show_updates": true
},
"content_type": "agent_status",
"host_status_settings": {
"layout_style": "grid",
"search": "",
"sort_by": "alerts",
"tag_filter_set": [
]
}
},
"type": "status",
"widget_id": "w11",
"width": 1
},
{
"active": true,
"height": 1,
"name": "Text",
"origin": "d2",
"settings": {
"autoformat": false,
"body_format": "<p>{metric_name} ({value_type})<br /><strong>{metric_value}</strong><br /><span class=\"date\">{value_date}</span></p>",
"datapoints": [
{
"_cluster_title": "test",
"_label": "Cluster: test",
"account_id": "1234",
"cluster_id": 1234,
"numeric_only": false
}
],
"period": 0,
"title_format": "Metric Status",
"use_default": true,
"value_type": "gauge"
},
"type": "text",
"widget_id": "w13",
"width": 1
},
{
"active": true,
"height": 1,
"name": "Chart",
"origin": "b0",
"settings": {
"chart_type": "bar",
"datapoints": [
{
"_cluster_title": "test",
"_label": "Cluster: test",
"account_id": "1234",
"cluster_id": 1234,
"numeric_only": true
}
],
"definition": {
"datasource": "realtime",
"derive": "gauge",
"disable_autoformat": false,
"formula": "",
"legend": {
"show": false,
"type": "html"
},
"period": 0,
"pop_onhover": false,
"wedge_labels": {
"on_chart": true,
"tooltips": false
},
"wedge_values": {
"angle": "0",
"color": "background",
"show": true
}
},
"title": "chart metric cluster"
},
"type": "chart",
"widget_id": "w3",
"width": 1
},
{
"active": true,
"height": 1,
"name": "Gauge",
"origin": "b1",
"settings": {
"_check_id": 1234,
"account_id": "1234",
"check_uuid": "01234567-89ab-cdef-0123-456789abcdef",
"disable_autoformat": false,
"formula": "",
"metric_display_name": "%Used",
"metric_name": "fs`/foo`df_used_percent",
"period": 0,
"range_high": 100,
"range_low": 0,
"thresholds": {
"colors": [
"#008000",
"#ffcc00",
"#ee0000"
],
"flip": false,
"values": [
"75%",
"87.5%"
]
},
"title": "Metric Gauge",
"type": "bar",
"value_type": "gauge"
},
"type": "gauge",
"widget_id": "w7",
"width": 1
},
{
"active": true,
"height": 1,
"name": "Text",
"origin": "c2",
"settings": {
"autoformat": false,
"body_format": "<p>{metric_name} ({value_type})<br /><strong>{metric_value}</strong><br /><span class=\"date\">{value_date}</span></p>",
"datapoints": [
{
"_check_id": 1234,
"_metric_type": "numeric",
"account_id": "1234",
"label": "cache entries",
"metric": "01234567-89ab-cdef-0123-456789abcdef:foo`cache_entries"
},
{
"_check_id": 1234,
"_metric_type": "numeric",
"account_id": "1234",
"label": "cache capacity",
"metric": "01234567-89ab-cdef-0123-456789abcdef:foo`cache_capacity"
},
{
"_check_id": 1234,
"_metric_type": "numeric",
"account_id": "1234",
"label": "cache size",
"metric": "01234567-89ab-cdef-0123-456789abcdef:foo`cache_size"
}
],
"period": 0,
"title_format": "Metric Status",
"use_default": true,
"value_type": "gauge"
},
"type": "text",
"widget_id": "w12",
"width": 1
},
{
"active": true,
"height": 1,
"name": "Forecast",
"origin": "a1",
"settings": {
"format": "standard",
"resource_limit": "0",
"resource_usage": "metric:average(\"01234567-89ab-cdef-0123-456789abcdef\",p\"fs%60/foo%60df_used_percent\")",
"thresholds": {
"colors": [
"#008000",
"#ffcc00",
"#ee0000"
],
"values": [
"1d",
"1h"
]
},
"title": "Resource Forecast",
"trend": "auto"
},
"type": "forecast",
"widget_id": "w6",
"width": 1
}
]
}

View file

@ -144,11 +144,11 @@ type DashboardWidgetSettings struct {
HostStatusSettings StatusWidgetHostStatusSettings `json:"host_status_settings,omitempty"` // status
KeyInline bool `json:"key_inline,omitempty"` // graphs
KeyLoc string `json:"key_loc,omitempty"` // graphs
KeySize string `json:"key_size,omitempty"` // graphs
KeySize uint `json:"key_size,omitempty"` // graphs
KeyWrap bool `json:"key_wrap,omitempty"` // graphs
Label string `json:"label,omitempty"` // graphs
Layout string `json:"layout,omitempty"` // clusters
Limit string `json:"limit,omitempty"` // lists
Limit uint `json:"limit,omitempty"` // lists
Maintenance string `json:"maintenance,omitempty"` // alerts
Markup string `json:"markup,omitempty"` // html
MetricDisplayName string `json:"metric_display_name,omitempty"` // gauges
@ -156,7 +156,7 @@ type DashboardWidgetSettings struct {
MinAge string `json:"min_age,omitempty"` // alerts
OffHours []uint `json:"off_hours,omitempty"` // alerts
OverlaySetID string `json:"overlay_set_id,omitempty"` // graphs
Period interface{} `json:"period,omitempty"` // BUG type switching between widgets (doc: string; gauges, text: uint; graphs: string)
Period uint `json:"period,omitempty"` // gauges, text, graphs
RangeHigh int `json:"range_high,omitempty"` // gauges
RangeLow int `json:"range_low,omitempty"` // gauges
Realtime bool `json:"realtime,omitempty"` // graphs

View file

@ -26,7 +26,7 @@ type Metric struct {
CheckTags []string `json:"_check_tags,omitempty"` // [] len >= 0
CheckUUID string `json:"_check_uuid,omitempty"` // string
CID string `json:"_cid,omitempty"` // string
Histogram bool `json:"_histogram,omitempty"` // boolean
Histogram string `json:"_histogram,omitempty"` // string
Link *string `json:"link,omitempty"` // string or null
MetricName string `json:"_metric_name,omitempty"` // string
MetricType string `json:"_metric_type,omitempty"` // string

View file

@ -42,17 +42,22 @@ type CACert struct {
}
// loadCACert loads the CA cert for the broker designated by the submission url
func (cm *CheckManager) loadCACert() {
func (cm *CheckManager) loadCACert() error {
if cm.certPool != nil {
return
return nil
}
cm.certPool = x509.NewCertPool()
cert, err := cm.fetchCert()
if err != nil {
if cm.Debug {
cm.Log.Printf("[DEBUG] Unable to fetch ca.crt, using default. %+v\n", err)
var cert []byte
var err error
if cm.enabled {
// only attempt to retrieve broker CA cert if
// the check is being managed.
cert, err = cm.fetchCert()
if err != nil {
return err
}
}
@ -61,6 +66,8 @@ func (cm *CheckManager) loadCACert() {
}
cm.certPool.AppendCertsFromPEM(cert)
return nil
}
// fetchCert fetches CA certificate using Circonus API

View file

@ -243,6 +243,18 @@ func (cm *CheckManager) initializeTrapURL() error {
}
cm.trapCN = BrokerCNType(cn)
if cm.enabled {
u, err := url.Parse(string(cm.trapURL))
if err != nil {
return err
}
if u.Scheme == "https" {
if err := cm.loadCACert(); err != nil {
return err
}
}
}
cm.trapLastUpdate = time.Now()
return nil

View file

@ -385,7 +385,9 @@ func (cm *CheckManager) GetSubmissionURL() (*Trap, error) {
if u.Scheme == "https" {
if cm.certPool == nil {
cm.loadCACert()
if err := cm.loadCACert(); err != nil {
return nil, err
}
}
t := &tls.Config{
RootCAs: cm.certPool,

View file

@ -205,7 +205,7 @@ func New(cfg *Config) (*CirconusMetrics, error) {
// note: submit will jettison metrics until initialization has completed.
if cm.flushInterval > time.Duration(0) {
go func() {
for _ = range time.NewTicker(cm.flushInterval).C {
for range time.NewTicker(cm.flushInterval).C {
cm.Flush()
}
}()

View file

@ -32,7 +32,7 @@ pass `context.WithTimeout` to APIs:
```go
ctx, cancel := context.WithTimeout(context.Background(), timeout)
resp, err := kvc.Put(ctx, "sample_key", "sample_value")
resp, err := cli.Put(ctx, "sample_key", "sample_value")
cancel()
if err != nil {
// handle error!
@ -57,7 +57,7 @@ etcd client returns 2 types of errors:
Here is the example code to handle client errors:
```go
resp, err := kvc.Put(ctx, "", "")
resp, err := cli.Put(ctx, "", "")
if err != nil {
switch err {
case context.Canceled:

View file

@ -20,6 +20,7 @@ import (
"fmt"
"net"
"net/url"
"strconv"
"strings"
"sync"
"time"
@ -35,6 +36,7 @@ import (
var (
ErrNoAvailableEndpoints = errors.New("etcdclient: no available endpoints")
ErrOldCluster = errors.New("etcdclient: old cluster version")
)
// Client provides and manages an etcd v3 client session.
@ -272,7 +274,7 @@ func (c *Client) dial(endpoint string, dopts ...grpc.DialOption) (*grpc.ClientCo
tokenMu: &sync.RWMutex{},
}
err := c.getToken(context.TODO())
err := c.getToken(c.ctx)
if err != nil {
return nil, err
}
@ -307,7 +309,12 @@ func newClient(cfg *Config) (*Client, error) {
}
// use a temporary skeleton client to bootstrap first connection
ctx, cancel := context.WithCancel(context.TODO())
baseCtx := context.TODO()
if cfg.Context != nil {
baseCtx = cfg.Context
}
ctx, cancel := context.WithCancel(baseCtx)
client := &Client{
conn: nil,
cfg: *cfg,
@ -353,10 +360,57 @@ func newClient(cfg *Config) (*Client, error) {
client.Auth = NewAuth(client)
client.Maintenance = NewMaintenance(client)
if cfg.RejectOldCluster {
if err := client.checkVersion(); err != nil {
client.Close()
return nil, err
}
}
go client.autoSync()
return client, nil
}
func (c *Client) checkVersion() (err error) {
var wg sync.WaitGroup
errc := make(chan error, len(c.cfg.Endpoints))
ctx, cancel := context.WithCancel(c.ctx)
if c.cfg.DialTimeout > 0 {
ctx, _ = context.WithTimeout(ctx, c.cfg.DialTimeout)
}
wg.Add(len(c.cfg.Endpoints))
for _, ep := range c.cfg.Endpoints {
// if cluster is current, any endpoint gives a recent version
go func(e string) {
defer wg.Done()
resp, rerr := c.Status(ctx, e)
if rerr != nil {
errc <- rerr
return
}
vs := strings.Split(resp.Version, ".")
maj, min := 0, 0
if len(vs) >= 2 {
maj, rerr = strconv.Atoi(vs[0])
min, rerr = strconv.Atoi(vs[1])
}
if maj < 3 || (maj == 3 && min < 2) {
rerr = ErrOldCluster
}
errc <- rerr
}(ep)
}
// wait for success
for i := 0; i < len(c.cfg.Endpoints); i++ {
if err = <-errc; err == nil {
break
}
}
cancel()
wg.Wait()
return err
}
// ActiveConnection returns the current in-use connection
func (c *Client) ActiveConnection() *grpc.ClientConn { return c.conn }

View file

@ -18,6 +18,7 @@ import (
"crypto/tls"
"time"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
@ -41,6 +42,13 @@ type Config struct {
// Password is a password for authentication.
Password string `json:"password"`
// RejectOldCluster when set will refuse to create a client against an outdated cluster.
RejectOldCluster bool `json:"reject-old-cluster"`
// DialOptions is a list of dial options for the grpc client (e.g., for interceptors).
DialOptions []grpc.DialOption
// Context is the default client context; it can be used to cancel grpc dial out and
// other operations that do not have an explicit context.
Context context.Context
}

View file

@ -410,7 +410,7 @@ func (l *lessor) recvKeepAlive(resp *pb.LeaseKeepAliveResponse) {
}
// send update to all channels
nextKeepAlive := time.Now().Add(time.Duration(karesp.TTL+2) / 3 * time.Second)
nextKeepAlive := time.Now().Add((time.Duration(karesp.TTL) * time.Second) / 3.0)
ka.deadline = time.Now().Add(time.Duration(karesp.TTL) * time.Second)
for _, ch := range ka.chs {
select {

View file

@ -1,22 +0,0 @@
// Copyright 2016 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build !windows
package fileutil
import "os"
// OpenDir opens a directory for syncing.
func OpenDir(path string) (*os.File, error) { return os.Open(path) }

View file

@ -1,46 +0,0 @@
// Copyright 2016 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build windows
package fileutil
import (
"os"
"syscall"
)
// OpenDir opens a directory in windows with write access for syncing.
func OpenDir(path string) (*os.File, error) {
fd, err := openDir(path)
if err != nil {
return nil, err
}
return os.NewFile(uintptr(fd), path), nil
}
func openDir(path string) (fd syscall.Handle, err error) {
if len(path) == 0 {
return syscall.InvalidHandle, syscall.ERROR_FILE_NOT_FOUND
}
pathp, err := syscall.UTF16PtrFromString(path)
if err != nil {
return syscall.InvalidHandle, err
}
access := uint32(syscall.GENERIC_READ | syscall.GENERIC_WRITE)
sharemode := uint32(syscall.FILE_SHARE_READ | syscall.FILE_SHARE_WRITE)
createmode := uint32(syscall.OPEN_EXISTING)
fl := uint32(syscall.FILE_FLAG_BACKUP_SEMANTICS)
return syscall.CreateFile(pathp, access, sharemode, nil, createmode, fl, 0)
}

View file

@ -1,121 +0,0 @@
// Copyright 2015 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package fileutil implements utility functions related to files and paths.
package fileutil
import (
"fmt"
"io/ioutil"
"os"
"path"
"sort"
"github.com/coreos/pkg/capnslog"
)
const (
// PrivateFileMode grants owner to read/write a file.
PrivateFileMode = 0600
// PrivateDirMode grants owner to make/remove files inside the directory.
PrivateDirMode = 0700
)
var (
plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "pkg/fileutil")
)
// IsDirWriteable checks if dir is writable by writing and removing a file
// to dir. It returns nil if dir is writable.
func IsDirWriteable(dir string) error {
f := path.Join(dir, ".touch")
if err := ioutil.WriteFile(f, []byte(""), PrivateFileMode); err != nil {
return err
}
return os.Remove(f)
}
// ReadDir returns the filenames in the given directory in sorted order.
func ReadDir(dirpath string) ([]string, error) {
dir, err := os.Open(dirpath)
if err != nil {
return nil, err
}
defer dir.Close()
names, err := dir.Readdirnames(-1)
if err != nil {
return nil, err
}
sort.Strings(names)
return names, nil
}
// TouchDirAll is similar to os.MkdirAll. It creates directories with 0700 permission if any directory
// does not exists. TouchDirAll also ensures the given directory is writable.
func TouchDirAll(dir string) error {
// If path is already a directory, MkdirAll does nothing
// and returns nil.
err := os.MkdirAll(dir, PrivateDirMode)
if err != nil {
// if mkdirAll("a/text") and "text" is not
// a directory, this will return syscall.ENOTDIR
return err
}
return IsDirWriteable(dir)
}
// CreateDirAll is similar to TouchDirAll but returns error
// if the deepest directory was not empty.
func CreateDirAll(dir string) error {
err := TouchDirAll(dir)
if err == nil {
var ns []string
ns, err = ReadDir(dir)
if err != nil {
return err
}
if len(ns) != 0 {
err = fmt.Errorf("expected %q to be empty, got %q", dir, ns)
}
}
return err
}
func Exist(name string) bool {
_, err := os.Stat(name)
return err == nil
}
// ZeroToEnd zeros a file starting from SEEK_CUR to its SEEK_END. May temporarily
// shorten the length of the file.
func ZeroToEnd(f *os.File) error {
// TODO: support FALLOC_FL_ZERO_RANGE
off, err := f.Seek(0, os.SEEK_CUR)
if err != nil {
return err
}
lenf, lerr := f.Seek(0, os.SEEK_END)
if lerr != nil {
return lerr
}
if err = f.Truncate(off); err != nil {
return err
}
// make sure blocks remain allocated
if err = Preallocate(f, lenf, true); err != nil {
return err
}
_, err = f.Seek(off, os.SEEK_SET)
return err
}

View file

@ -1,26 +0,0 @@
// Copyright 2016 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package fileutil
import (
"errors"
"os"
)
var (
ErrLocked = errors.New("fileutil: file already locked")
)
type LockedFile struct{ *os.File }

View file

@ -1,49 +0,0 @@
// Copyright 2016 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build !windows,!plan9,!solaris
package fileutil
import (
"os"
"syscall"
)
func flockTryLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
f, err := os.OpenFile(path, flag, perm)
if err != nil {
return nil, err
}
if err = syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil {
f.Close()
if err == syscall.EWOULDBLOCK {
err = ErrLocked
}
return nil, err
}
return &LockedFile{f}, nil
}
func flockLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
f, err := os.OpenFile(path, flag, perm)
if err != nil {
return nil, err
}
if err = syscall.Flock(int(f.Fd()), syscall.LOCK_EX); err != nil {
f.Close()
return nil, err
}
return &LockedFile{f}, err
}

View file

@ -1,96 +0,0 @@
// Copyright 2016 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build linux
package fileutil
import (
"os"
"syscall"
)
// This used to call syscall.Flock() but that call fails with EBADF on NFS.
// An alternative is lockf() which works on NFS but that call lets a process lock
// the same file twice. Instead, use Linux's non-standard open file descriptor
// locks which will block if the process already holds the file lock.
//
// constants from /usr/include/bits/fcntl-linux.h
const (
F_OFD_GETLK = 37
F_OFD_SETLK = 37
F_OFD_SETLKW = 38
)
var (
wrlck = syscall.Flock_t{
Type: syscall.F_WRLCK,
Whence: int16(os.SEEK_SET),
Start: 0,
Len: 0,
}
linuxTryLockFile = flockTryLockFile
linuxLockFile = flockLockFile
)
func init() {
// use open file descriptor locks if the system supports it
getlk := syscall.Flock_t{Type: syscall.F_RDLCK}
if err := syscall.FcntlFlock(0, F_OFD_GETLK, &getlk); err == nil {
linuxTryLockFile = ofdTryLockFile
linuxLockFile = ofdLockFile
}
}
func TryLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
return linuxTryLockFile(path, flag, perm)
}
func ofdTryLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
f, err := os.OpenFile(path, flag, perm)
if err != nil {
return nil, err
}
flock := wrlck
if err = syscall.FcntlFlock(f.Fd(), F_OFD_SETLK, &flock); err != nil {
f.Close()
if err == syscall.EWOULDBLOCK {
err = ErrLocked
}
return nil, err
}
return &LockedFile{f}, nil
}
func LockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
return linuxLockFile(path, flag, perm)
}
func ofdLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
f, err := os.OpenFile(path, flag, perm)
if err != nil {
return nil, err
}
flock := wrlck
err = syscall.FcntlFlock(f.Fd(), F_OFD_SETLKW, &flock)
if err != nil {
f.Close()
return nil, err
}
return &LockedFile{f}, err
}

View file

@ -1,45 +0,0 @@
// Copyright 2015 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package fileutil
import (
"os"
"syscall"
"time"
)
func TryLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
if err := os.Chmod(path, syscall.DMEXCL|PrivateFileMode); err != nil {
return nil, err
}
f, err := os.Open(path, flag, perm)
if err != nil {
return nil, ErrLocked
}
return &LockedFile{f}, nil
}
func LockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
if err := os.Chmod(path, syscall.DMEXCL|PrivateFileMode); err != nil {
return nil, err
}
for {
f, err := os.OpenFile(path, flag, perm)
if err == nil {
return &LockedFile{f}, nil
}
time.Sleep(10 * time.Millisecond)
}
}

View file

@ -1,62 +0,0 @@
// Copyright 2015 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build solaris
package fileutil
import (
"os"
"syscall"
)
func TryLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
var lock syscall.Flock_t
lock.Start = 0
lock.Len = 0
lock.Pid = 0
lock.Type = syscall.F_WRLCK
lock.Whence = 0
lock.Pid = 0
f, err := os.OpenFile(path, flag, perm)
if err != nil {
return nil, err
}
if err := syscall.FcntlFlock(f.Fd(), syscall.F_SETLK, &lock); err != nil {
f.Close()
if err == syscall.EAGAIN {
err = ErrLocked
}
return nil, err
}
return &LockedFile{f}, nil
}
func LockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
var lock syscall.Flock_t
lock.Start = 0
lock.Len = 0
lock.Pid = 0
lock.Type = syscall.F_WRLCK
lock.Whence = 0
f, err := os.OpenFile(path, flag, perm)
if err != nil {
return nil, err
}
if err = syscall.FcntlFlock(f.Fd(), syscall.F_SETLKW, &lock); err != nil {
f.Close()
return nil, err
}
return &LockedFile{f}, nil
}

View file

@ -1,29 +0,0 @@
// Copyright 2015 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build !windows,!plan9,!solaris,!linux
package fileutil
import (
"os"
)
func TryLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
return flockTryLockFile(path, flag, perm)
}
func LockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
return flockLockFile(path, flag, perm)
}

View file

@ -1,125 +0,0 @@
// Copyright 2015 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build windows
package fileutil
import (
"errors"
"fmt"
"os"
"syscall"
"unsafe"
)
var (
modkernel32 = syscall.NewLazyDLL("kernel32.dll")
procLockFileEx = modkernel32.NewProc("LockFileEx")
errLocked = errors.New("The process cannot access the file because another process has locked a portion of the file.")
)
const (
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365203(v=vs.85).aspx
LOCKFILE_EXCLUSIVE_LOCK = 2
LOCKFILE_FAIL_IMMEDIATELY = 1
// see https://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx
errLockViolation syscall.Errno = 0x21
)
func TryLockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
f, err := open(path, flag, perm)
if err != nil {
return nil, err
}
if err := lockFile(syscall.Handle(f.Fd()), LOCKFILE_FAIL_IMMEDIATELY); err != nil {
f.Close()
return nil, err
}
return &LockedFile{f}, nil
}
func LockFile(path string, flag int, perm os.FileMode) (*LockedFile, error) {
f, err := open(path, flag, perm)
if err != nil {
return nil, err
}
if err := lockFile(syscall.Handle(f.Fd()), 0); err != nil {
f.Close()
return nil, err
}
return &LockedFile{f}, nil
}
func open(path string, flag int, perm os.FileMode) (*os.File, error) {
if path == "" {
return nil, fmt.Errorf("cannot open empty filename")
}
var access uint32
switch flag {
case syscall.O_RDONLY:
access = syscall.GENERIC_READ
case syscall.O_WRONLY:
access = syscall.GENERIC_WRITE
case syscall.O_RDWR:
access = syscall.GENERIC_READ | syscall.GENERIC_WRITE
case syscall.O_WRONLY | syscall.O_CREAT:
access = syscall.GENERIC_ALL
default:
panic(fmt.Errorf("flag %v is not supported", flag))
}
fd, err := syscall.CreateFile(&(syscall.StringToUTF16(path)[0]),
access,
syscall.FILE_SHARE_READ|syscall.FILE_SHARE_WRITE|syscall.FILE_SHARE_DELETE,
nil,
syscall.OPEN_ALWAYS,
syscall.FILE_ATTRIBUTE_NORMAL,
0)
if err != nil {
return nil, err
}
return os.NewFile(uintptr(fd), path), nil
}
func lockFile(fd syscall.Handle, flags uint32) error {
var flag uint32 = LOCKFILE_EXCLUSIVE_LOCK
flag |= flags
if fd == syscall.InvalidHandle {
return nil
}
err := lockFileEx(fd, flag, 1, 0, &syscall.Overlapped{})
if err == nil {
return nil
} else if err.Error() == errLocked.Error() {
return ErrLocked
} else if err != errLockViolation {
return err
}
return nil
}
func lockFileEx(h syscall.Handle, flags, locklow, lockhigh uint32, ol *syscall.Overlapped) (err error) {
var reserved uint32 = 0
r1, _, e1 := syscall.Syscall6(procLockFileEx.Addr(), 6, uintptr(h), uintptr(flags), uintptr(reserved), uintptr(locklow), uintptr(lockhigh), uintptr(unsafe.Pointer(ol)))
if r1 == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}

View file

@ -1,47 +0,0 @@
// Copyright 2015 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package fileutil
import "os"
// Preallocate tries to allocate the space for given
// file. This operation is only supported on linux by a
// few filesystems (btrfs, ext4, etc.).
// If the operation is unsupported, no error will be returned.
// Otherwise, the error encountered will be returned.
func Preallocate(f *os.File, sizeInBytes int64, extendFile bool) error {
if extendFile {
return preallocExtend(f, sizeInBytes)
}
return preallocFixed(f, sizeInBytes)
}
func preallocExtendTrunc(f *os.File, sizeInBytes int64) error {
curOff, err := f.Seek(0, os.SEEK_CUR)
if err != nil {
return err
}
size, err := f.Seek(sizeInBytes, os.SEEK_END)
if err != nil {
return err
}
if _, err = f.Seek(curOff, os.SEEK_SET); err != nil {
return err
}
if sizeInBytes > size {
return nil
}
return f.Truncate(sizeInBytes)
}

View file

@ -1,43 +0,0 @@
// Copyright 2016 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build darwin
package fileutil
import (
"os"
"syscall"
"unsafe"
)
func preallocExtend(f *os.File, sizeInBytes int64) error {
if err := preallocFixed(f, sizeInBytes); err != nil {
return err
}
return preallocExtendTrunc(f, sizeInBytes)
}
func preallocFixed(f *os.File, sizeInBytes int64) error {
fstore := &syscall.Fstore_t{
Flags: syscall.F_ALLOCATEALL,
Posmode: syscall.F_PEOFPOSMODE,
Length: sizeInBytes}
p := unsafe.Pointer(fstore)
_, _, errno := syscall.Syscall(syscall.SYS_FCNTL, f.Fd(), uintptr(syscall.F_PREALLOCATE), uintptr(p))
if errno == 0 || errno == syscall.ENOTSUP {
return nil
}
return errno
}

View file

@ -1,49 +0,0 @@
// Copyright 2016 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build linux
package fileutil
import (
"os"
"syscall"
)
func preallocExtend(f *os.File, sizeInBytes int64) error {
// use mode = 0 to change size
err := syscall.Fallocate(int(f.Fd()), 0, 0, sizeInBytes)
if err != nil {
errno, ok := err.(syscall.Errno)
// not supported; fallback
// fallocate EINTRs frequently in some environments; fallback
if ok && (errno == syscall.ENOTSUP || errno == syscall.EINTR) {
return preallocExtendTrunc(f, sizeInBytes)
}
}
return err
}
func preallocFixed(f *os.File, sizeInBytes int64) error {
// use mode = 1 to keep size; see FALLOC_FL_KEEP_SIZE
err := syscall.Fallocate(int(f.Fd()), 1, 0, sizeInBytes)
if err != nil {
errno, ok := err.(syscall.Errno)
// treat not supported as nil error
if ok && errno == syscall.ENOTSUP {
return nil
}
}
return err
}

View file

@ -1,25 +0,0 @@
// Copyright 2015 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build !linux,!darwin
package fileutil
import "os"
func preallocExtend(f *os.File, sizeInBytes int64) error {
return preallocExtendTrunc(f, sizeInBytes)
}
func preallocFixed(f *os.File, sizeInBytes int64) error { return nil }

View file

@ -1,78 +0,0 @@
// Copyright 2015 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package fileutil
import (
"os"
"path"
"sort"
"strings"
"time"
)
func PurgeFile(dirname string, suffix string, max uint, interval time.Duration, stop <-chan struct{}) <-chan error {
return purgeFile(dirname, suffix, max, interval, stop, nil)
}
// purgeFile is the internal implementation for PurgeFile which can post purged files to purgec if non-nil.
func purgeFile(dirname string, suffix string, max uint, interval time.Duration, stop <-chan struct{}, purgec chan<- string) <-chan error {
errC := make(chan error, 1)
go func() {
for {
fnames, err := ReadDir(dirname)
if err != nil {
errC <- err
return
}
newfnames := make([]string, 0)
for _, fname := range fnames {
if strings.HasSuffix(fname, suffix) {
newfnames = append(newfnames, fname)
}
}
sort.Strings(newfnames)
fnames = newfnames
for len(newfnames) > int(max) {
f := path.Join(dirname, newfnames[0])
l, err := TryLockFile(f, os.O_WRONLY, PrivateFileMode)
if err != nil {
break
}
if err = os.Remove(f); err != nil {
errC <- err
return
}
if err = l.Close(); err != nil {
plog.Errorf("error unlocking %s when purging file (%v)", l.Name(), err)
errC <- err
return
}
plog.Infof("purged file %s successfully", f)
newfnames = newfnames[1:]
}
if purgec != nil {
for i := 0; i < len(fnames)-len(newfnames); i++ {
purgec <- fnames[i]
}
}
select {
case <-time.After(interval):
case <-stop:
return
}
}
}()
return errC
}

View file

@ -1,29 +0,0 @@
// Copyright 2016 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build !linux,!darwin
package fileutil
import "os"
// Fsync is a wrapper around file.Sync(). Special handling is needed on darwin platform.
func Fsync(f *os.File) error {
return f.Sync()
}
// Fdatasync is a wrapper around file.Sync(). Special handling is needed on linux platform.
func Fdatasync(f *os.File) error {
return f.Sync()
}

View file

@ -1,40 +0,0 @@
// Copyright 2016 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build darwin
package fileutil
import (
"os"
"syscall"
)
// Fsync on HFS/OSX flushes the data on to the physical drive but the drive
// may not write it to the persistent media for quite sometime and it may be
// written in out-of-order sequence. Using F_FULLFSYNC ensures that the
// physical drive's buffer will also get flushed to the media.
func Fsync(f *os.File) error {
_, _, errno := syscall.Syscall(syscall.SYS_FCNTL, f.Fd(), uintptr(syscall.F_FULLFSYNC), uintptr(0))
if errno == 0 {
return nil
}
return errno
}
// Fdatasync on darwin platform invokes fcntl(F_FULLFSYNC) for actual persistence
// on physical drive media.
func Fdatasync(f *os.File) error {
return Fsync(f)
}

View file

@ -1,34 +0,0 @@
// Copyright 2016 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// +build linux
package fileutil
import (
"os"
"syscall"
)
// Fsync is a wrapper around file.Sync(). Special handling is needed on darwin platform.
func Fsync(f *os.File) error {
return f.Sync()
}
// Fdatasync is similar to fsync(), but does not flush modified metadata
// unless that metadata is needed in order to allow a subsequent data retrieval
// to be correctly handled.
func Fdatasync(f *os.File) error {
return syscall.Fdatasync(int(f.Fd()))
}

View file

@ -30,7 +30,6 @@ import (
"strings"
"time"
"github.com/coreos/etcd/pkg/fileutil"
"github.com/coreos/etcd/pkg/tlsutil"
)
@ -86,7 +85,7 @@ func (info TLSInfo) Empty() bool {
}
func SelfCert(dirpath string, hosts []string) (info TLSInfo, err error) {
if err = fileutil.TouchDirAll(dirpath); err != nil {
if err = os.MkdirAll(dirpath, 0700); err != nil {
return
}

View file

@ -1,191 +0,0 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction, and
distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by the copyright
owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all other entities
that control, are controlled by, or are under common control with that entity.
For the purposes of this definition, "control" means (i) the power, direct or
indirect, to cause the direction or management of such entity, whether by
contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity exercising
permissions granted by this License.
"Source" form shall mean the preferred form for making modifications, including
but not limited to software source code, documentation source, and configuration
files.
"Object" form shall mean any form resulting from mechanical transformation or
translation of a Source form, including but not limited to compiled object code,
generated documentation, and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or Object form, made
available under the License, as indicated by a copyright notice that is included
in or attached to the work (an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object form, that
is based on (or derived from) the Work and for which the editorial revisions,
annotations, elaborations, or other modifications represent, as a whole, an
original work of authorship. For the purposes of this License, Derivative Works
shall not include works that remain separable from, or merely link (or bind by
name) to the interfaces of, the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including the original version
of the Work and any modifications or additions to that Work or Derivative Works
thereof, that is intentionally submitted to Licensor for inclusion in the Work
by the copyright owner or by an individual or Legal Entity authorized to submit
on behalf of the copyright owner. For the purposes of this definition,
"submitted" means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems, and
issue tracking systems that are managed by, or on behalf of, the Licensor for
the purpose of discussing and improving the Work, but excluding communication
that is conspicuously marked or otherwise designated in writing by the copyright
owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
of whom a Contribution has been received by Licensor and subsequently
incorporated within the Work.
2. Grant of Copyright License.
Subject to the terms and conditions of this License, each Contributor hereby
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
irrevocable copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the Work and such
Derivative Works in Source or Object form.
3. Grant of Patent License.
Subject to the terms and conditions of this License, each Contributor hereby
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
irrevocable (except as stated in this section) patent license to make, have
made, use, offer to sell, sell, import, and otherwise transfer the Work, where
such license applies only to those patent claims licensable by such Contributor
that are necessarily infringed by their Contribution(s) alone or by combination
of their Contribution(s) with the Work to which such Contribution(s) was
submitted. If You institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work or a
Contribution incorporated within the Work constitutes direct or contributory
patent infringement, then any patent licenses granted to You under this License
for that Work shall terminate as of the date such litigation is filed.
4. Redistribution.
You may reproduce and distribute copies of the Work or Derivative Works thereof
in any medium, with or without modifications, and in Source or Object form,
provided that You meet the following conditions:
You must give any other recipients of the Work or Derivative Works a copy of
this License; and
You must cause any modified files to carry prominent notices stating that You
changed the files; and
You must retain, in the Source form of any Derivative Works that You distribute,
all copyright, patent, trademark, and attribution notices from the Source form
of the Work, excluding those notices that do not pertain to any part of the
Derivative Works; and
If the Work includes a "NOTICE" text file as part of its distribution, then any
Derivative Works that You distribute must include a readable copy of the
attribution notices contained within such NOTICE file, excluding those notices
that do not pertain to any part of the Derivative Works, in at least one of the
following places: within a NOTICE text file distributed as part of the
Derivative Works; within the Source form or documentation, if provided along
with the Derivative Works; or, within a display generated by the Derivative
Works, if and wherever such third-party notices normally appear. The contents of
the NOTICE file are for informational purposes only and do not modify the
License. You may add Your own attribution notices within Derivative Works that
You distribute, alongside or as an addendum to the NOTICE text from the Work,
provided that such additional attribution notices cannot be construed as
modifying the License.
You may add Your own copyright statement to Your modifications and may provide
additional or different license terms and conditions for use, reproduction, or
distribution of Your modifications, or for any such Derivative Works as a whole,
provided Your use, reproduction, and distribution of the Work otherwise complies
with the conditions stated in this License.
5. Submission of Contributions.
Unless You explicitly state otherwise, any Contribution intentionally submitted
for inclusion in the Work by You to the Licensor shall be under the terms and
conditions of this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify the terms of
any separate license agreement you may have executed with Licensor regarding
such Contributions.
6. Trademarks.
This License does not grant permission to use the trade names, trademarks,
service marks, or product names of the Licensor, except as required for
reasonable and customary use in describing the origin of the Work and
reproducing the content of the NOTICE file.
7. Disclaimer of Warranty.
Unless required by applicable law or agreed to in writing, Licensor provides the
Work (and each Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
including, without limitation, any warranties or conditions of TITLE,
NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are
solely responsible for determining the appropriateness of using or
redistributing the Work and assume any risks associated with Your exercise of
permissions under this License.
8. Limitation of Liability.
In no event and under no legal theory, whether in tort (including negligence),
contract, or otherwise, unless required by applicable law (such as deliberate
and grossly negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special, incidental,
or consequential damages of any character arising as a result of this License or
out of the use or inability to use the Work (including but not limited to
damages for loss of goodwill, work stoppage, computer failure or malfunction, or
any and all other commercial damages or losses), even if such Contributor has
been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability.
While redistributing the Work or Derivative Works thereof, You may choose to
offer, and charge a fee for, acceptance of support, warranty, indemnity, or
other liability obligations and/or rights consistent with this License. However,
in accepting such obligations, You may act only on Your own behalf and on Your
sole responsibility, not on behalf of any other Contributor, and only if You
agree to indemnify, defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason of your
accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work
To apply the Apache License to your work, attach the following boilerplate
notice, with the fields enclosed by brackets "[]" replaced with your own
identifying information. (Don't include the brackets!) The text should be
enclosed in the appropriate comment syntax for the file format. We also
recommend that a file or class name and description of purpose be included on
the same "printed page" as the copyright notice for easier identification within
third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View file

@ -1,179 +0,0 @@
// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package journal provides write bindings to the local systemd journal.
// It is implemented in pure Go and connects to the journal directly over its
// unix socket.
//
// To read from the journal, see the "sdjournal" package, which wraps the
// sd-journal a C API.
//
// http://www.freedesktop.org/software/systemd/man/systemd-journald.service.html
package journal
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"strconv"
"strings"
"syscall"
)
// Priority of a journal message
type Priority int
const (
PriEmerg Priority = iota
PriAlert
PriCrit
PriErr
PriWarning
PriNotice
PriInfo
PriDebug
)
var conn net.Conn
func init() {
var err error
conn, err = net.Dial("unixgram", "/run/systemd/journal/socket")
if err != nil {
conn = nil
}
}
// Enabled returns true if the local systemd journal is available for logging
func Enabled() bool {
return conn != nil
}
// Send a message to the local systemd journal. vars is a map of journald
// fields to values. Fields must be composed of uppercase letters, numbers,
// and underscores, but must not start with an underscore. Within these
// restrictions, any arbitrary field name may be used. Some names have special
// significance: see the journalctl documentation
// (http://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html)
// for more details. vars may be nil.
func Send(message string, priority Priority, vars map[string]string) error {
if conn == nil {
return journalError("could not connect to journald socket")
}
data := new(bytes.Buffer)
appendVariable(data, "PRIORITY", strconv.Itoa(int(priority)))
appendVariable(data, "MESSAGE", message)
for k, v := range vars {
appendVariable(data, k, v)
}
_, err := io.Copy(conn, data)
if err != nil && isSocketSpaceError(err) {
file, err := tempFd()
if err != nil {
return journalError(err.Error())
}
defer file.Close()
_, err = io.Copy(file, data)
if err != nil {
return journalError(err.Error())
}
rights := syscall.UnixRights(int(file.Fd()))
/* this connection should always be a UnixConn, but better safe than sorry */
unixConn, ok := conn.(*net.UnixConn)
if !ok {
return journalError("can't send file through non-Unix connection")
}
unixConn.WriteMsgUnix([]byte{}, rights, nil)
} else if err != nil {
return journalError(err.Error())
}
return nil
}
// Print prints a message to the local systemd journal using Send().
func Print(priority Priority, format string, a ...interface{}) error {
return Send(fmt.Sprintf(format, a...), priority, nil)
}
func appendVariable(w io.Writer, name, value string) {
if !validVarName(name) {
journalError("variable name contains invalid character, ignoring")
}
if strings.ContainsRune(value, '\n') {
/* When the value contains a newline, we write:
* - the variable name, followed by a newline
* - the size (in 64bit little endian format)
* - the data, followed by a newline
*/
fmt.Fprintln(w, name)
binary.Write(w, binary.LittleEndian, uint64(len(value)))
fmt.Fprintln(w, value)
} else {
/* just write the variable and value all on one line */
fmt.Fprintf(w, "%s=%s\n", name, value)
}
}
func validVarName(name string) bool {
/* The variable name must be in uppercase and consist only of characters,
* numbers and underscores, and may not begin with an underscore. (from the docs)
*/
valid := name[0] != '_'
for _, c := range name {
valid = valid && ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') || c == '_'
}
return valid
}
func isSocketSpaceError(err error) bool {
opErr, ok := err.(*net.OpError)
if !ok {
return false
}
sysErr, ok := opErr.Err.(syscall.Errno)
if !ok {
return false
}
return sysErr == syscall.EMSGSIZE || sysErr == syscall.ENOBUFS
}
func tempFd() (*os.File, error) {
file, err := ioutil.TempFile("/dev/shm/", "journal.XXXXX")
if err != nil {
return nil, err
}
syscall.Unlink(file.Name())
if err != nil {
return nil, err
}
return file, nil
}
func journalError(s string) error {
s = "journal error: " + s
fmt.Fprintln(os.Stderr, s)
return errors.New(s)
}

202
vendor/github.com/coreos/pkg/LICENSE generated vendored
View file

@ -1,202 +0,0 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "{}"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright {yyyy} {name of copyright owner}
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View file

@ -1,5 +0,0 @@
CoreOS Project
Copyright 2014 CoreOS, Inc
This product includes software developed at CoreOS, Inc.
(http://www.coreos.com/).

View file

@ -1,39 +0,0 @@
# capnslog, the CoreOS logging package
There are far too many logging packages out there, with varying degrees of licenses, far too many features (colorization, all sorts of log frameworks) or are just a pain to use (lack of `Fatalln()`?).
capnslog provides a simple but consistent logging interface suitable for all kinds of projects.
### Design Principles
##### `package main` is the place where logging gets turned on and routed
A library should not touch log options, only generate log entries. Libraries are silent until main lets them speak.
##### All log options are runtime-configurable.
Still the job of `main` to expose these configurations. `main` may delegate this to, say, a configuration webhook, but does so explicitly.
##### There is one log object per package. It is registered under its repository and package name.
`main` activates logging for its repository and any dependency repositories it would also like to have output in its logstream. `main` also dictates at which level each subpackage logs.
##### There is *one* output stream, and it is an `io.Writer` composed with a formatter.
Splitting streams is probably not the job of your program, but rather, your log aggregation framework. If you must split output streams, again, `main` configures this and you can write a very simple two-output struct that satisfies io.Writer.
Fancy colorful formatting and JSON output are beyond the scope of a basic logging framework -- they're application/log-collector dependant. These are, at best, provided as options, but more likely, provided by your application.
##### Log objects are an interface
An object knows best how to print itself. Log objects can collect more interesting metadata if they wish, however, because text isn't going away anytime soon, they must all be marshalable to text. The simplest log object is a string, which returns itself. If you wish to do more fancy tricks for printing your log objects, see also JSON output -- introspect and write a formatter which can handle your advanced log interface. Making strings is the only thing guaranteed.
##### Log levels have specific meanings:
* Critical: Unrecoverable. Must fail.
* Error: Data has been lost, a request has failed for a bad reason, or a required resource has been lost
* Warning: (Hopefully) Temporary conditions that may cause errors, but may work fine. A replica disappearing (that may reconnect) is a warning.
* Notice: Normal, but important (uncommon) log information.
* Info: Normal, working log information, everything is fine, but helpful notices for auditing or common operations.
* Debug: Everything is still fine, but even common operations may be logged, and less helpful but more quantity of notices.
* Trace: Anything goes, from logging every function call as part of a common operation, to tracing execution of a query.

View file

@ -1,157 +0,0 @@
// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package capnslog
import (
"bufio"
"fmt"
"io"
"log"
"runtime"
"strings"
"time"
)
type Formatter interface {
Format(pkg string, level LogLevel, depth int, entries ...interface{})
Flush()
}
func NewStringFormatter(w io.Writer) Formatter {
return &StringFormatter{
w: bufio.NewWriter(w),
}
}
type StringFormatter struct {
w *bufio.Writer
}
func (s *StringFormatter) Format(pkg string, l LogLevel, i int, entries ...interface{}) {
now := time.Now().UTC()
s.w.WriteString(now.Format(time.RFC3339))
s.w.WriteByte(' ')
writeEntries(s.w, pkg, l, i, entries...)
s.Flush()
}
func writeEntries(w *bufio.Writer, pkg string, _ LogLevel, _ int, entries ...interface{}) {
if pkg != "" {
w.WriteString(pkg + ": ")
}
str := fmt.Sprint(entries...)
endsInNL := strings.HasSuffix(str, "\n")
w.WriteString(str)
if !endsInNL {
w.WriteString("\n")
}
}
func (s *StringFormatter) Flush() {
s.w.Flush()
}
func NewPrettyFormatter(w io.Writer, debug bool) Formatter {
return &PrettyFormatter{
w: bufio.NewWriter(w),
debug: debug,
}
}
type PrettyFormatter struct {
w *bufio.Writer
debug bool
}
func (c *PrettyFormatter) Format(pkg string, l LogLevel, depth int, entries ...interface{}) {
now := time.Now()
ts := now.Format("2006-01-02 15:04:05")
c.w.WriteString(ts)
ms := now.Nanosecond() / 1000
c.w.WriteString(fmt.Sprintf(".%06d", ms))
if c.debug {
_, file, line, ok := runtime.Caller(depth) // It's always the same number of frames to the user's call.
if !ok {
file = "???"
line = 1
} else {
slash := strings.LastIndex(file, "/")
if slash >= 0 {
file = file[slash+1:]
}
}
if line < 0 {
line = 0 // not a real line number
}
c.w.WriteString(fmt.Sprintf(" [%s:%d]", file, line))
}
c.w.WriteString(fmt.Sprint(" ", l.Char(), " | "))
writeEntries(c.w, pkg, l, depth, entries...)
c.Flush()
}
func (c *PrettyFormatter) Flush() {
c.w.Flush()
}
// LogFormatter emulates the form of the traditional built-in logger.
type LogFormatter struct {
logger *log.Logger
prefix string
}
// NewLogFormatter is a helper to produce a new LogFormatter struct. It uses the
// golang log package to actually do the logging work so that logs look similar.
func NewLogFormatter(w io.Writer, prefix string, flag int) Formatter {
return &LogFormatter{
logger: log.New(w, "", flag), // don't use prefix here
prefix: prefix, // save it instead
}
}
// Format builds a log message for the LogFormatter. The LogLevel is ignored.
func (lf *LogFormatter) Format(pkg string, _ LogLevel, _ int, entries ...interface{}) {
str := fmt.Sprint(entries...)
prefix := lf.prefix
if pkg != "" {
prefix = fmt.Sprintf("%s%s: ", prefix, pkg)
}
lf.logger.Output(5, fmt.Sprintf("%s%v", prefix, str)) // call depth is 5
}
// Flush is included so that the interface is complete, but is a no-op.
func (lf *LogFormatter) Flush() {
// noop
}
// NilFormatter is a no-op log formatter that does nothing.
type NilFormatter struct {
}
// NewNilFormatter is a helper to produce a new LogFormatter struct. It logs no
// messages so that you can cause part of your logging to be silent.
func NewNilFormatter() Formatter {
return &NilFormatter{}
}
// Format does nothing.
func (_ *NilFormatter) Format(_ string, _ LogLevel, _ int, _ ...interface{}) {
// noop
}
// Flush is included so that the interface is complete, but is a no-op.
func (_ *NilFormatter) Flush() {
// noop
}

View file

@ -1,96 +0,0 @@
// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package capnslog
import (
"bufio"
"bytes"
"io"
"os"
"runtime"
"strconv"
"strings"
"time"
)
var pid = os.Getpid()
type GlogFormatter struct {
StringFormatter
}
func NewGlogFormatter(w io.Writer) *GlogFormatter {
g := &GlogFormatter{}
g.w = bufio.NewWriter(w)
return g
}
func (g GlogFormatter) Format(pkg string, level LogLevel, depth int, entries ...interface{}) {
g.w.Write(GlogHeader(level, depth+1))
g.StringFormatter.Format(pkg, level, depth+1, entries...)
}
func GlogHeader(level LogLevel, depth int) []byte {
// Lmmdd hh:mm:ss.uuuuuu threadid file:line]
now := time.Now().UTC()
_, file, line, ok := runtime.Caller(depth) // It's always the same number of frames to the user's call.
if !ok {
file = "???"
line = 1
} else {
slash := strings.LastIndex(file, "/")
if slash >= 0 {
file = file[slash+1:]
}
}
if line < 0 {
line = 0 // not a real line number
}
buf := &bytes.Buffer{}
buf.Grow(30)
_, month, day := now.Date()
hour, minute, second := now.Clock()
buf.WriteString(level.Char())
twoDigits(buf, int(month))
twoDigits(buf, day)
buf.WriteByte(' ')
twoDigits(buf, hour)
buf.WriteByte(':')
twoDigits(buf, minute)
buf.WriteByte(':')
twoDigits(buf, second)
buf.WriteByte('.')
buf.WriteString(strconv.Itoa(now.Nanosecond() / 1000))
buf.WriteByte('Z')
buf.WriteByte(' ')
buf.WriteString(strconv.Itoa(pid))
buf.WriteByte(' ')
buf.WriteString(file)
buf.WriteByte(':')
buf.WriteString(strconv.Itoa(line))
buf.WriteByte(']')
buf.WriteByte(' ')
return buf.Bytes()
}
const digits = "0123456789"
func twoDigits(b *bytes.Buffer, d int) {
c2 := digits[d%10]
d /= 10
c1 := digits[d%10]
b.WriteByte(c1)
b.WriteByte(c2)
}

View file

@ -1,49 +0,0 @@
// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// +build !windows
package capnslog
import (
"io"
"os"
"syscall"
)
// Here's where the opinionation comes in. We need some sensible defaults,
// especially after taking over the log package. Your project (whatever it may
// be) may see things differently. That's okay; there should be no defaults in
// the main package that cannot be controlled or overridden programatically,
// otherwise it's a bug. Doing so is creating your own init_log.go file much
// like this one.
func init() {
initHijack()
// Go `log` pacakge uses os.Stderr.
SetFormatter(NewDefaultFormatter(os.Stderr))
SetGlobalLogLevel(INFO)
}
func NewDefaultFormatter(out io.Writer) Formatter {
if syscall.Getppid() == 1 {
// We're running under init, which may be systemd.
f, err := NewJournaldFormatter()
if err == nil {
return f
}
}
return NewPrettyFormatter(out, false)
}

View file

@ -1,25 +0,0 @@
// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package capnslog
import "os"
func init() {
initHijack()
// Go `log` package uses os.Stderr.
SetFormatter(NewPrettyFormatter(os.Stderr, false))
SetGlobalLogLevel(INFO)
}

View file

@ -1,68 +0,0 @@
// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// +build !windows
package capnslog
import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/coreos/go-systemd/journal"
)
func NewJournaldFormatter() (Formatter, error) {
if !journal.Enabled() {
return nil, errors.New("No systemd detected")
}
return &journaldFormatter{}, nil
}
type journaldFormatter struct{}
func (j *journaldFormatter) Format(pkg string, l LogLevel, _ int, entries ...interface{}) {
var pri journal.Priority
switch l {
case CRITICAL:
pri = journal.PriCrit
case ERROR:
pri = journal.PriErr
case WARNING:
pri = journal.PriWarning
case NOTICE:
pri = journal.PriNotice
case INFO:
pri = journal.PriInfo
case DEBUG:
pri = journal.PriDebug
case TRACE:
pri = journal.PriDebug
default:
panic("Unhandled loglevel")
}
msg := fmt.Sprint(entries...)
tags := map[string]string{
"PACKAGE": pkg,
"SYSLOG_IDENTIFIER": filepath.Base(os.Args[0]),
}
err := journal.Send(msg, pri, tags)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
func (j *journaldFormatter) Flush() {}

View file

@ -1,39 +0,0 @@
// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package capnslog
import (
"log"
)
func initHijack() {
pkg := NewPackageLogger("log", "")
w := packageWriter{pkg}
log.SetFlags(0)
log.SetPrefix("")
log.SetOutput(w)
}
type packageWriter struct {
pl *PackageLogger
}
func (p packageWriter) Write(b []byte) (int, error) {
if p.pl.level < INFO {
return 0, nil
}
p.pl.internalLog(calldepth+2, INFO, string(b))
return len(b), nil
}

View file

@ -1,245 +0,0 @@
// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package capnslog
import (
"errors"
"strings"
"sync"
)
// LogLevel is the set of all log levels.
type LogLevel int8
const (
// CRITICAL is the lowest log level; only errors which will end the program will be propagated.
CRITICAL LogLevel = iota - 1
// ERROR is for errors that are not fatal but lead to troubling behavior.
ERROR
// WARNING is for errors which are not fatal and not errors, but are unusual. Often sourced from misconfigurations.
WARNING
// NOTICE is for normal but significant conditions.
NOTICE
// INFO is a log level for common, everyday log updates.
INFO
// DEBUG is the default hidden level for more verbose updates about internal processes.
DEBUG
// TRACE is for (potentially) call by call tracing of programs.
TRACE
)
// Char returns a single-character representation of the log level.
func (l LogLevel) Char() string {
switch l {
case CRITICAL:
return "C"
case ERROR:
return "E"
case WARNING:
return "W"
case NOTICE:
return "N"
case INFO:
return "I"
case DEBUG:
return "D"
case TRACE:
return "T"
default:
panic("Unhandled loglevel")
}
}
// String returns a multi-character representation of the log level.
func (l LogLevel) String() string {
switch l {
case CRITICAL:
return "CRITICAL"
case ERROR:
return "ERROR"
case WARNING:
return "WARNING"
case NOTICE:
return "NOTICE"
case INFO:
return "INFO"
case DEBUG:
return "DEBUG"
case TRACE:
return "TRACE"
default:
panic("Unhandled loglevel")
}
}
// Update using the given string value. Fulfills the flag.Value interface.
func (l *LogLevel) Set(s string) error {
value, err := ParseLevel(s)
if err != nil {
return err
}
*l = value
return nil
}
// Returns an empty string, only here to fulfill the pflag.Value interface.
func (l *LogLevel) Type() string {
return ""
}
// ParseLevel translates some potential loglevel strings into their corresponding levels.
func ParseLevel(s string) (LogLevel, error) {
switch s {
case "CRITICAL", "C":
return CRITICAL, nil
case "ERROR", "0", "E":
return ERROR, nil
case "WARNING", "1", "W":
return WARNING, nil
case "NOTICE", "2", "N":
return NOTICE, nil
case "INFO", "3", "I":
return INFO, nil
case "DEBUG", "4", "D":
return DEBUG, nil
case "TRACE", "5", "T":
return TRACE, nil
}
return CRITICAL, errors.New("couldn't parse log level " + s)
}
type RepoLogger map[string]*PackageLogger
type loggerStruct struct {
sync.Mutex
repoMap map[string]RepoLogger
formatter Formatter
}
// logger is the global logger
var logger = new(loggerStruct)
// SetGlobalLogLevel sets the log level for all packages in all repositories
// registered with capnslog.
func SetGlobalLogLevel(l LogLevel) {
logger.Lock()
defer logger.Unlock()
for _, r := range logger.repoMap {
r.setRepoLogLevelInternal(l)
}
}
// GetRepoLogger may return the handle to the repository's set of packages' loggers.
func GetRepoLogger(repo string) (RepoLogger, error) {
logger.Lock()
defer logger.Unlock()
r, ok := logger.repoMap[repo]
if !ok {
return nil, errors.New("no packages registered for repo " + repo)
}
return r, nil
}
// MustRepoLogger returns the handle to the repository's packages' loggers.
func MustRepoLogger(repo string) RepoLogger {
r, err := GetRepoLogger(repo)
if err != nil {
panic(err)
}
return r
}
// SetRepoLogLevel sets the log level for all packages in the repository.
func (r RepoLogger) SetRepoLogLevel(l LogLevel) {
logger.Lock()
defer logger.Unlock()
r.setRepoLogLevelInternal(l)
}
func (r RepoLogger) setRepoLogLevelInternal(l LogLevel) {
for _, v := range r {
v.level = l
}
}
// ParseLogLevelConfig parses a comma-separated string of "package=loglevel", in
// order, and returns a map of the results, for use in SetLogLevel.
func (r RepoLogger) ParseLogLevelConfig(conf string) (map[string]LogLevel, error) {
setlist := strings.Split(conf, ",")
out := make(map[string]LogLevel)
for _, setstring := range setlist {
setting := strings.Split(setstring, "=")
if len(setting) != 2 {
return nil, errors.New("oddly structured `pkg=level` option: " + setstring)
}
l, err := ParseLevel(setting[1])
if err != nil {
return nil, err
}
out[setting[0]] = l
}
return out, nil
}
// SetLogLevel takes a map of package names within a repository to their desired
// loglevel, and sets the levels appropriately. Unknown packages are ignored.
// "*" is a special package name that corresponds to all packages, and will be
// processed first.
func (r RepoLogger) SetLogLevel(m map[string]LogLevel) {
logger.Lock()
defer logger.Unlock()
if l, ok := m["*"]; ok {
r.setRepoLogLevelInternal(l)
}
for k, v := range m {
l, ok := r[k]
if !ok {
continue
}
l.level = v
}
}
// SetFormatter sets the formatting function for all logs.
func SetFormatter(f Formatter) {
logger.Lock()
defer logger.Unlock()
logger.formatter = f
}
// NewPackageLogger creates a package logger object.
// This should be defined as a global var in your package, referencing your repo.
func NewPackageLogger(repo string, pkg string) (p *PackageLogger) {
logger.Lock()
defer logger.Unlock()
if logger.repoMap == nil {
logger.repoMap = make(map[string]RepoLogger)
}
r, rok := logger.repoMap[repo]
if !rok {
logger.repoMap[repo] = make(RepoLogger)
r = logger.repoMap[repo]
}
p, pok := r[pkg]
if !pok {
r[pkg] = &PackageLogger{
pkg: pkg,
level: INFO,
}
p = r[pkg]
}
return
}

View file

@ -1,177 +0,0 @@
// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package capnslog
import (
"fmt"
"os"
)
type PackageLogger struct {
pkg string
level LogLevel
}
const calldepth = 2
func (p *PackageLogger) internalLog(depth int, inLevel LogLevel, entries ...interface{}) {
logger.Lock()
defer logger.Unlock()
if inLevel != CRITICAL && p.level < inLevel {
return
}
if logger.formatter != nil {
logger.formatter.Format(p.pkg, inLevel, depth+1, entries...)
}
}
func (p *PackageLogger) LevelAt(l LogLevel) bool {
logger.Lock()
defer logger.Unlock()
return p.level >= l
}
// Log a formatted string at any level between ERROR and TRACE
func (p *PackageLogger) Logf(l LogLevel, format string, args ...interface{}) {
p.internalLog(calldepth, l, fmt.Sprintf(format, args...))
}
// Log a message at any level between ERROR and TRACE
func (p *PackageLogger) Log(l LogLevel, args ...interface{}) {
p.internalLog(calldepth, l, fmt.Sprint(args...))
}
// log stdlib compatibility
func (p *PackageLogger) Println(args ...interface{}) {
p.internalLog(calldepth, INFO, fmt.Sprintln(args...))
}
func (p *PackageLogger) Printf(format string, args ...interface{}) {
p.Logf(INFO, format, args...)
}
func (p *PackageLogger) Print(args ...interface{}) {
p.internalLog(calldepth, INFO, fmt.Sprint(args...))
}
// Panic and fatal
func (p *PackageLogger) Panicf(format string, args ...interface{}) {
s := fmt.Sprintf(format, args...)
p.internalLog(calldepth, CRITICAL, s)
panic(s)
}
func (p *PackageLogger) Panic(args ...interface{}) {
s := fmt.Sprint(args...)
p.internalLog(calldepth, CRITICAL, s)
panic(s)
}
func (p *PackageLogger) Fatalf(format string, args ...interface{}) {
p.Logf(CRITICAL, format, args...)
os.Exit(1)
}
func (p *PackageLogger) Fatal(args ...interface{}) {
s := fmt.Sprint(args...)
p.internalLog(calldepth, CRITICAL, s)
os.Exit(1)
}
func (p *PackageLogger) Fatalln(args ...interface{}) {
s := fmt.Sprintln(args...)
p.internalLog(calldepth, CRITICAL, s)
os.Exit(1)
}
// Error Functions
func (p *PackageLogger) Errorf(format string, args ...interface{}) {
p.Logf(ERROR, format, args...)
}
func (p *PackageLogger) Error(entries ...interface{}) {
p.internalLog(calldepth, ERROR, entries...)
}
// Warning Functions
func (p *PackageLogger) Warningf(format string, args ...interface{}) {
p.Logf(WARNING, format, args...)
}
func (p *PackageLogger) Warning(entries ...interface{}) {
p.internalLog(calldepth, WARNING, entries...)
}
// Notice Functions
func (p *PackageLogger) Noticef(format string, args ...interface{}) {
p.Logf(NOTICE, format, args...)
}
func (p *PackageLogger) Notice(entries ...interface{}) {
p.internalLog(calldepth, NOTICE, entries...)
}
// Info Functions
func (p *PackageLogger) Infof(format string, args ...interface{}) {
p.Logf(INFO, format, args...)
}
func (p *PackageLogger) Info(entries ...interface{}) {
p.internalLog(calldepth, INFO, entries...)
}
// Debug Functions
func (p *PackageLogger) Debugf(format string, args ...interface{}) {
if p.level < DEBUG {
return
}
p.Logf(DEBUG, format, args...)
}
func (p *PackageLogger) Debug(entries ...interface{}) {
if p.level < DEBUG {
return
}
p.internalLog(calldepth, DEBUG, entries...)
}
// Trace Functions
func (p *PackageLogger) Tracef(format string, args ...interface{}) {
if p.level < TRACE {
return
}
p.Logf(TRACE, format, args...)
}
func (p *PackageLogger) Trace(entries ...interface{}) {
if p.level < TRACE {
return
}
p.internalLog(calldepth, TRACE, entries...)
}
func (p *PackageLogger) Flush() {
logger.Lock()
defer logger.Unlock()
logger.formatter.Flush()
}

View file

@ -1,65 +0,0 @@
// Copyright 2015 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// +build !windows
package capnslog
import (
"fmt"
"log/syslog"
)
func NewSyslogFormatter(w *syslog.Writer) Formatter {
return &syslogFormatter{w}
}
func NewDefaultSyslogFormatter(tag string) (Formatter, error) {
w, err := syslog.New(syslog.LOG_DEBUG, tag)
if err != nil {
return nil, err
}
return NewSyslogFormatter(w), nil
}
type syslogFormatter struct {
w *syslog.Writer
}
func (s *syslogFormatter) Format(pkg string, l LogLevel, _ int, entries ...interface{}) {
for _, entry := range entries {
str := fmt.Sprint(entry)
switch l {
case CRITICAL:
s.w.Crit(str)
case ERROR:
s.w.Err(str)
case WARNING:
s.w.Warning(str)
case NOTICE:
s.w.Notice(str)
case INFO:
s.w.Info(str)
case DEBUG:
s.w.Debug(str)
case TRACE:
s.w.Debug(str)
default:
panic("Unhandled loglevel")
}
}
}
func (s *syslogFormatter) Flush() {
}

55
vendor/github.com/go-ini/ini/ini.go generated vendored
View file

@ -37,7 +37,7 @@ const (
// Maximum allowed depth when recursively substituing variable names.
_DEPTH_VALUES = 99
_VERSION = "1.24.0"
_VERSION = "1.25.2"
)
// Version returns current package version literal.
@ -176,6 +176,8 @@ type LoadOptions struct {
// AllowBooleanKeys indicates whether to allow boolean type keys or treat as value is missing.
// This type of keys are mostly used in my.cnf.
AllowBooleanKeys bool
// AllowShadows indicates whether to keep track of keys with same name under same section.
AllowShadows bool
// Some INI formats allow group blocks that store a block of raw content that doesn't otherwise
// conform to key/value pairs. Specify the names of those blocks here.
UnparseableSections []string
@ -219,6 +221,12 @@ func InsensitiveLoad(source interface{}, others ...interface{}) (*File, error) {
return LoadSources(LoadOptions{Insensitive: true}, source, others...)
}
// InsensitiveLoad has exactly same functionality as Load function
// except it allows have shadow keys.
func ShadowLoad(source interface{}, others ...interface{}) (*File, error) {
return LoadSources(LoadOptions{AllowShadows: true}, source, others...)
}
// Empty returns an empty file object.
func Empty() *File {
// Ignore error here, we sure our data is good.
@ -441,6 +449,7 @@ func (f *File) WriteToIndent(w io.Writer, indent string) (n int64, err error) {
}
alignSpaces := bytes.Repeat([]byte(" "), alignLength)
KEY_LIST:
for _, kname := range sec.keyList {
key := sec.Key(kname)
if len(key.Comment) > 0 {
@ -467,31 +476,33 @@ func (f *File) WriteToIndent(w io.Writer, indent string) (n int64, err error) {
case strings.Contains(kname, "`"):
kname = `"""` + kname + `"""`
}
if _, err = buf.WriteString(kname); err != nil {
return 0, err
}
if key.isBooleanType {
if kname != sec.keyList[len(sec.keyList)-1] {
buf.WriteString(LineBreak)
for _, val := range key.ValueWithShadows() {
if _, err = buf.WriteString(kname); err != nil {
return 0, err
}
continue
}
// Write out alignment spaces before "=" sign
if PrettyFormat {
buf.Write(alignSpaces[:alignLength-len(kname)])
}
if key.isBooleanType {
if kname != sec.keyList[len(sec.keyList)-1] {
buf.WriteString(LineBreak)
}
continue KEY_LIST
}
val := key.value
// In case key value contains "\n", "`", "\"", "#" or ";"
if strings.ContainsAny(val, "\n`") {
val = `"""` + val + `"""`
} else if strings.ContainsAny(val, "#;") {
val = "`" + val + "`"
}
if _, err = buf.WriteString(equalSign + val + LineBreak); err != nil {
return 0, err
// Write out alignment spaces before "=" sign
if PrettyFormat {
buf.Write(alignSpaces[:alignLength-len(kname)])
}
// In case key value contains "\n", "`", "\"", "#" or ";"
if strings.ContainsAny(val, "\n`") {
val = `"""` + val + `"""`
} else if strings.ContainsAny(val, "#;") {
val = "`" + val + "`"
}
if _, err = buf.WriteString(equalSign + val + LineBreak); err != nil {
return 0, err
}
}
}

47
vendor/github.com/go-ini/ini/key.go generated vendored
View file

@ -15,6 +15,7 @@
package ini
import (
"errors"
"fmt"
"strconv"
"strings"
@ -29,9 +30,42 @@ type Key struct {
isAutoIncrement bool
isBooleanType bool
isShadow bool
shadows []*Key
Comment string
}
// newKey simply return a key object with given values.
func newKey(s *Section, name, val string) *Key {
return &Key{
s: s,
name: name,
value: val,
}
}
func (k *Key) addShadow(val string) error {
if k.isShadow {
return errors.New("cannot add shadow to another shadow key")
} else if k.isAutoIncrement || k.isBooleanType {
return errors.New("cannot add shadow to auto-increment or boolean key")
}
shadow := newKey(k.s, k.name, val)
shadow.isShadow = true
k.shadows = append(k.shadows, shadow)
return nil
}
// AddShadow adds a new shadow key to itself.
func (k *Key) AddShadow(val string) error {
if !k.s.f.options.AllowShadows {
return errors.New("shadow key is not allowed")
}
return k.addShadow(val)
}
// ValueMapper represents a mapping function for values, e.g. os.ExpandEnv
type ValueMapper func(string) string
@ -45,6 +79,19 @@ func (k *Key) Value() string {
return k.value
}
// ValueWithShadows returns raw values of key and its shadows if any.
func (k *Key) ValueWithShadows() []string {
if len(k.shadows) == 0 {
return []string{k.value}
}
vals := make([]string, len(k.shadows)+1)
vals[0] = k.value
for i := range k.shadows {
vals[i+1] = k.shadows[i].value
}
return vals
}
// String returns string representation of value.
func (k *Key) String() string {
val := k.value

View file

@ -341,17 +341,16 @@ func (f *File) parse(reader io.Reader) (err error) {
p.count++
}
key, err := section.NewKey(kname, "")
if err != nil {
return err
}
key.isAutoIncrement = isAutoIncr
value, err := p.readValue(line[offset:], f.options.IgnoreContinuation)
if err != nil {
return err
}
key.SetValue(value)
key, err := section.NewKey(kname, value)
if err != nil {
return err
}
key.isAutoIncrement = isAutoIncr
key.Comment = strings.TrimSpace(p.comment.String())
p.comment.Reset()
}

View file

@ -68,16 +68,18 @@ func (s *Section) NewKey(name, val string) (*Key, error) {
}
if inSlice(name, s.keyList) {
s.keys[name].value = val
if s.f.options.AllowShadows {
if err := s.keys[name].addShadow(val); err != nil {
return nil, err
}
} else {
s.keys[name].value = val
}
return s.keys[name], nil
}
s.keyList = append(s.keyList, name)
s.keys[name] = &Key{
s: s,
name: name,
value: val,
}
s.keys[name] = newKey(s, name, val)
s.keysHash[name] = val
return s.keys[name], nil
}

View file

@ -29,7 +29,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------
Some documentation is taken from the GitHub Developer site
<http://developer.github.com/>, which is available under the following Creative
<https://developer.github.com/>, which is available under the following Creative
Commons Attribution 3.0 License. This applies only to the go-github source
code and would not apply to any compiled binaries.

View file

@ -5,10 +5,12 @@
package github
import "context"
// ActivityService handles communication with the activity related
// methods of the GitHub API.
//
// GitHub API docs: http://developer.github.com/v3/activity/
// GitHub API docs: https://developer.github.com/v3/activity/
type ActivityService service
// FeedLink represents a link to a related resource.
@ -51,14 +53,14 @@ type Feeds struct {
//
// Note: Private feeds are only returned when authenticating via Basic Auth
// since current feed URIs use the older, non revocable auth tokens.
func (s *ActivityService) ListFeeds() (*Feeds, *Response, error) {
func (s *ActivityService) ListFeeds(ctx context.Context) (*Feeds, *Response, error) {
req, err := s.client.NewRequest("GET", "feeds", nil)
if err != nil {
return nil, nil, err
}
f := &Feeds{}
resp, err := s.client.Do(req, f)
resp, err := s.client.Do(ctx, req, f)
if err != nil {
return nil, resp, err
}

View file

@ -6,6 +6,7 @@
package github
import (
"context"
"encoding/json"
"fmt"
"time"
@ -49,8 +50,6 @@ func (e *Event) Payload() (payload interface{}) {
payload = &IntegrationInstallationEvent{}
case "IntegrationInstallationRepositoriesEvent":
payload = &IntegrationInstallationRepositoriesEvent{}
case "IssueActivityEvent":
payload = &IssueActivityEvent{}
case "IssueCommentEvent":
payload = &IssueCommentEvent{}
case "IssuesEvent":
@ -98,8 +97,8 @@ func (e *Event) Payload() (payload interface{}) {
// ListEvents drinks from the firehose of all public events across GitHub.
//
// GitHub API docs: http://developer.github.com/v3/activity/events/#list-public-events
func (s *ActivityService) ListEvents(opt *ListOptions) ([]*Event, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/activity/events/#list-public-events
func (s *ActivityService) ListEvents(ctx context.Context, opt *ListOptions) ([]*Event, *Response, error) {
u, err := addOptions("events", opt)
if err != nil {
return nil, nil, err
@ -111,7 +110,7 @@ func (s *ActivityService) ListEvents(opt *ListOptions) ([]*Event, *Response, err
}
var events []*Event
resp, err := s.client.Do(req, &events)
resp, err := s.client.Do(ctx, req, &events)
if err != nil {
return nil, resp, err
}
@ -121,8 +120,8 @@ func (s *ActivityService) ListEvents(opt *ListOptions) ([]*Event, *Response, err
// ListRepositoryEvents lists events for a repository.
//
// GitHub API docs: http://developer.github.com/v3/activity/events/#list-repository-events
func (s *ActivityService) ListRepositoryEvents(owner, repo string, opt *ListOptions) ([]*Event, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/activity/events/#list-repository-events
func (s *ActivityService) ListRepositoryEvents(ctx context.Context, owner, repo string, opt *ListOptions) ([]*Event, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/events", owner, repo)
u, err := addOptions(u, opt)
if err != nil {
@ -135,7 +134,7 @@ func (s *ActivityService) ListRepositoryEvents(owner, repo string, opt *ListOpti
}
var events []*Event
resp, err := s.client.Do(req, &events)
resp, err := s.client.Do(ctx, req, &events)
if err != nil {
return nil, resp, err
}
@ -145,8 +144,8 @@ func (s *ActivityService) ListRepositoryEvents(owner, repo string, opt *ListOpti
// ListIssueEventsForRepository lists issue events for a repository.
//
// GitHub API docs: http://developer.github.com/v3/activity/events/#list-issue-events-for-a-repository
func (s *ActivityService) ListIssueEventsForRepository(owner, repo string, opt *ListOptions) ([]*IssueEvent, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/activity/events/#list-issue-events-for-a-repository
func (s *ActivityService) ListIssueEventsForRepository(ctx context.Context, owner, repo string, opt *ListOptions) ([]*IssueEvent, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/issues/events", owner, repo)
u, err := addOptions(u, opt)
if err != nil {
@ -159,7 +158,7 @@ func (s *ActivityService) ListIssueEventsForRepository(owner, repo string, opt *
}
var events []*IssueEvent
resp, err := s.client.Do(req, &events)
resp, err := s.client.Do(ctx, req, &events)
if err != nil {
return nil, resp, err
}
@ -169,8 +168,8 @@ func (s *ActivityService) ListIssueEventsForRepository(owner, repo string, opt *
// ListEventsForRepoNetwork lists public events for a network of repositories.
//
// GitHub API docs: http://developer.github.com/v3/activity/events/#list-public-events-for-a-network-of-repositories
func (s *ActivityService) ListEventsForRepoNetwork(owner, repo string, opt *ListOptions) ([]*Event, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/activity/events/#list-public-events-for-a-network-of-repositories
func (s *ActivityService) ListEventsForRepoNetwork(ctx context.Context, owner, repo string, opt *ListOptions) ([]*Event, *Response, error) {
u := fmt.Sprintf("networks/%v/%v/events", owner, repo)
u, err := addOptions(u, opt)
if err != nil {
@ -183,7 +182,7 @@ func (s *ActivityService) ListEventsForRepoNetwork(owner, repo string, opt *List
}
var events []*Event
resp, err := s.client.Do(req, &events)
resp, err := s.client.Do(ctx, req, &events)
if err != nil {
return nil, resp, err
}
@ -193,8 +192,8 @@ func (s *ActivityService) ListEventsForRepoNetwork(owner, repo string, opt *List
// ListEventsForOrganization lists public events for an organization.
//
// GitHub API docs: http://developer.github.com/v3/activity/events/#list-public-events-for-an-organization
func (s *ActivityService) ListEventsForOrganization(org string, opt *ListOptions) ([]*Event, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/activity/events/#list-public-events-for-an-organization
func (s *ActivityService) ListEventsForOrganization(ctx context.Context, org string, opt *ListOptions) ([]*Event, *Response, error) {
u := fmt.Sprintf("orgs/%v/events", org)
u, err := addOptions(u, opt)
if err != nil {
@ -207,7 +206,7 @@ func (s *ActivityService) ListEventsForOrganization(org string, opt *ListOptions
}
var events []*Event
resp, err := s.client.Do(req, &events)
resp, err := s.client.Do(ctx, req, &events)
if err != nil {
return nil, resp, err
}
@ -218,8 +217,8 @@ func (s *ActivityService) ListEventsForOrganization(org string, opt *ListOptions
// ListEventsPerformedByUser lists the events performed by a user. If publicOnly is
// true, only public events will be returned.
//
// GitHub API docs: http://developer.github.com/v3/activity/events/#list-events-performed-by-a-user
func (s *ActivityService) ListEventsPerformedByUser(user string, publicOnly bool, opt *ListOptions) ([]*Event, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/activity/events/#list-events-performed-by-a-user
func (s *ActivityService) ListEventsPerformedByUser(ctx context.Context, user string, publicOnly bool, opt *ListOptions) ([]*Event, *Response, error) {
var u string
if publicOnly {
u = fmt.Sprintf("users/%v/events/public", user)
@ -237,7 +236,7 @@ func (s *ActivityService) ListEventsPerformedByUser(user string, publicOnly bool
}
var events []*Event
resp, err := s.client.Do(req, &events)
resp, err := s.client.Do(ctx, req, &events)
if err != nil {
return nil, resp, err
}
@ -248,8 +247,8 @@ func (s *ActivityService) ListEventsPerformedByUser(user string, publicOnly bool
// ListEventsReceivedByUser lists the events received by a user. If publicOnly is
// true, only public events will be returned.
//
// GitHub API docs: http://developer.github.com/v3/activity/events/#list-events-that-a-user-has-received
func (s *ActivityService) ListEventsReceivedByUser(user string, publicOnly bool, opt *ListOptions) ([]*Event, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/activity/events/#list-events-that-a-user-has-received
func (s *ActivityService) ListEventsReceivedByUser(ctx context.Context, user string, publicOnly bool, opt *ListOptions) ([]*Event, *Response, error) {
var u string
if publicOnly {
u = fmt.Sprintf("users/%v/received_events/public", user)
@ -267,7 +266,7 @@ func (s *ActivityService) ListEventsReceivedByUser(user string, publicOnly bool,
}
var events []*Event
resp, err := s.client.Do(req, &events)
resp, err := s.client.Do(ctx, req, &events)
if err != nil {
return nil, resp, err
}
@ -278,8 +277,8 @@ func (s *ActivityService) ListEventsReceivedByUser(user string, publicOnly bool,
// ListUserEventsForOrganization provides the users organization dashboard. You
// must be authenticated as the user to view this.
//
// GitHub API docs: http://developer.github.com/v3/activity/events/#list-events-for-an-organization
func (s *ActivityService) ListUserEventsForOrganization(org, user string, opt *ListOptions) ([]*Event, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/activity/events/#list-events-for-an-organization
func (s *ActivityService) ListUserEventsForOrganization(ctx context.Context, org, user string, opt *ListOptions) ([]*Event, *Response, error) {
u := fmt.Sprintf("users/%v/events/orgs/%v", user, org)
u, err := addOptions(u, opt)
if err != nil {
@ -292,7 +291,7 @@ func (s *ActivityService) ListUserEventsForOrganization(org, user string, opt *L
}
var events []*Event
resp, err := s.client.Do(req, &events)
resp, err := s.client.Do(ctx, req, &events)
if err != nil {
return nil, resp, err
}

View file

@ -6,6 +6,7 @@
package github
import (
"context"
"fmt"
"time"
)
@ -18,7 +19,7 @@ type Notification struct {
// Reason identifies the event that triggered the notification.
//
// GitHub API Docs: https://developer.github.com/v3/activity/notifications/#notification-reasons
// GitHub API docs: https://developer.github.com/v3/activity/notifications/#notification-reasons
Reason *string `json:"reason,omitempty"`
Unread *bool `json:"unread,omitempty"`
@ -48,8 +49,8 @@ type NotificationListOptions struct {
// ListNotifications lists all notifications for the authenticated user.
//
// GitHub API Docs: https://developer.github.com/v3/activity/notifications/#list-your-notifications
func (s *ActivityService) ListNotifications(opt *NotificationListOptions) ([]*Notification, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/activity/notifications/#list-your-notifications
func (s *ActivityService) ListNotifications(ctx context.Context, opt *NotificationListOptions) ([]*Notification, *Response, error) {
u := fmt.Sprintf("notifications")
u, err := addOptions(u, opt)
if err != nil {
@ -62,7 +63,7 @@ func (s *ActivityService) ListNotifications(opt *NotificationListOptions) ([]*No
}
var notifications []*Notification
resp, err := s.client.Do(req, &notifications)
resp, err := s.client.Do(ctx, req, &notifications)
if err != nil {
return nil, resp, err
}
@ -73,8 +74,8 @@ func (s *ActivityService) ListNotifications(opt *NotificationListOptions) ([]*No
// ListRepositoryNotifications lists all notifications in a given repository
// for the authenticated user.
//
// GitHub API Docs: https://developer.github.com/v3/activity/notifications/#list-your-notifications-in-a-repository
func (s *ActivityService) ListRepositoryNotifications(owner, repo string, opt *NotificationListOptions) ([]*Notification, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/activity/notifications/#list-your-notifications-in-a-repository
func (s *ActivityService) ListRepositoryNotifications(ctx context.Context, owner, repo string, opt *NotificationListOptions) ([]*Notification, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/notifications", owner, repo)
u, err := addOptions(u, opt)
if err != nil {
@ -87,7 +88,7 @@ func (s *ActivityService) ListRepositoryNotifications(owner, repo string, opt *N
}
var notifications []*Notification
resp, err := s.client.Do(req, &notifications)
resp, err := s.client.Do(ctx, req, &notifications)
if err != nil {
return nil, resp, err
}
@ -101,8 +102,8 @@ type markReadOptions struct {
// MarkNotificationsRead marks all notifications up to lastRead as read.
//
// GitHub API Docs: https://developer.github.com/v3/activity/notifications/#mark-as-read
func (s *ActivityService) MarkNotificationsRead(lastRead time.Time) (*Response, error) {
// GitHub API docs: https://developer.github.com/v3/activity/notifications/#mark-as-read
func (s *ActivityService) MarkNotificationsRead(ctx context.Context, lastRead time.Time) (*Response, error) {
opts := &markReadOptions{
LastReadAt: lastRead,
}
@ -111,14 +112,14 @@ func (s *ActivityService) MarkNotificationsRead(lastRead time.Time) (*Response,
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}
// MarkRepositoryNotificationsRead marks all notifications up to lastRead in
// the specified repository as read.
//
// GitHub API Docs: https://developer.github.com/v3/activity/notifications/#mark-notifications-as-read-in-a-repository
func (s *ActivityService) MarkRepositoryNotificationsRead(owner, repo string, lastRead time.Time) (*Response, error) {
// GitHub API docs: https://developer.github.com/v3/activity/notifications/#mark-notifications-as-read-in-a-repository
func (s *ActivityService) MarkRepositoryNotificationsRead(ctx context.Context, owner, repo string, lastRead time.Time) (*Response, error) {
opts := &markReadOptions{
LastReadAt: lastRead,
}
@ -128,13 +129,13 @@ func (s *ActivityService) MarkRepositoryNotificationsRead(owner, repo string, la
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}
// GetThread gets the specified notification thread.
//
// GitHub API Docs: https://developer.github.com/v3/activity/notifications/#view-a-single-thread
func (s *ActivityService) GetThread(id string) (*Notification, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/activity/notifications/#view-a-single-thread
func (s *ActivityService) GetThread(ctx context.Context, id string) (*Notification, *Response, error) {
u := fmt.Sprintf("notifications/threads/%v", id)
req, err := s.client.NewRequest("GET", u, nil)
@ -143,7 +144,7 @@ func (s *ActivityService) GetThread(id string) (*Notification, *Response, error)
}
notification := new(Notification)
resp, err := s.client.Do(req, notification)
resp, err := s.client.Do(ctx, req, notification)
if err != nil {
return nil, resp, err
}
@ -153,8 +154,8 @@ func (s *ActivityService) GetThread(id string) (*Notification, *Response, error)
// MarkThreadRead marks the specified thread as read.
//
// GitHub API Docs: https://developer.github.com/v3/activity/notifications/#mark-a-thread-as-read
func (s *ActivityService) MarkThreadRead(id string) (*Response, error) {
// GitHub API docs: https://developer.github.com/v3/activity/notifications/#mark-a-thread-as-read
func (s *ActivityService) MarkThreadRead(ctx context.Context, id string) (*Response, error) {
u := fmt.Sprintf("notifications/threads/%v", id)
req, err := s.client.NewRequest("PATCH", u, nil)
@ -162,14 +163,14 @@ func (s *ActivityService) MarkThreadRead(id string) (*Response, error) {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}
// GetThreadSubscription checks to see if the authenticated user is subscribed
// to a thread.
//
// GitHub API Docs: https://developer.github.com/v3/activity/notifications/#get-a-thread-subscription
func (s *ActivityService) GetThreadSubscription(id string) (*Subscription, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/activity/notifications/#get-a-thread-subscription
func (s *ActivityService) GetThreadSubscription(ctx context.Context, id string) (*Subscription, *Response, error) {
u := fmt.Sprintf("notifications/threads/%v/subscription", id)
req, err := s.client.NewRequest("GET", u, nil)
@ -178,7 +179,7 @@ func (s *ActivityService) GetThreadSubscription(id string) (*Subscription, *Resp
}
sub := new(Subscription)
resp, err := s.client.Do(req, sub)
resp, err := s.client.Do(ctx, req, sub)
if err != nil {
return nil, resp, err
}
@ -189,8 +190,8 @@ func (s *ActivityService) GetThreadSubscription(id string) (*Subscription, *Resp
// SetThreadSubscription sets the subscription for the specified thread for the
// authenticated user.
//
// GitHub API Docs: https://developer.github.com/v3/activity/notifications/#set-a-thread-subscription
func (s *ActivityService) SetThreadSubscription(id string, subscription *Subscription) (*Subscription, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/activity/notifications/#set-a-thread-subscription
func (s *ActivityService) SetThreadSubscription(ctx context.Context, id string, subscription *Subscription) (*Subscription, *Response, error) {
u := fmt.Sprintf("notifications/threads/%v/subscription", id)
req, err := s.client.NewRequest("PUT", u, subscription)
@ -199,7 +200,7 @@ func (s *ActivityService) SetThreadSubscription(id string, subscription *Subscri
}
sub := new(Subscription)
resp, err := s.client.Do(req, sub)
resp, err := s.client.Do(ctx, req, sub)
if err != nil {
return nil, resp, err
}
@ -210,13 +211,13 @@ func (s *ActivityService) SetThreadSubscription(id string, subscription *Subscri
// DeleteThreadSubscription deletes the subscription for the specified thread
// for the authenticated user.
//
// GitHub API Docs: https://developer.github.com/v3/activity/notifications/#delete-a-thread-subscription
func (s *ActivityService) DeleteThreadSubscription(id string) (*Response, error) {
// GitHub API docs: https://developer.github.com/v3/activity/notifications/#delete-a-thread-subscription
func (s *ActivityService) DeleteThreadSubscription(ctx context.Context, id string) (*Response, error) {
u := fmt.Sprintf("notifications/threads/%v/subscription", id)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}

View file

@ -5,7 +5,10 @@
package github
import "fmt"
import (
"context"
"fmt"
)
// StarredRepository is returned by ListStarred.
type StarredRepository struct {
@ -21,8 +24,8 @@ type Stargazer struct {
// ListStargazers lists people who have starred the specified repo.
//
// GitHub API Docs: https://developer.github.com/v3/activity/starring/#list-stargazers
func (s *ActivityService) ListStargazers(owner, repo string, opt *ListOptions) ([]*Stargazer, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/activity/starring/#list-stargazers
func (s *ActivityService) ListStargazers(ctx context.Context, owner, repo string, opt *ListOptions) ([]*Stargazer, *Response, error) {
u := fmt.Sprintf("repos/%s/%s/stargazers", owner, repo)
u, err := addOptions(u, opt)
if err != nil {
@ -38,7 +41,7 @@ func (s *ActivityService) ListStargazers(owner, repo string, opt *ListOptions) (
req.Header.Set("Accept", mediaTypeStarringPreview)
var stargazers []*Stargazer
resp, err := s.client.Do(req, &stargazers)
resp, err := s.client.Do(ctx, req, &stargazers)
if err != nil {
return nil, resp, err
}
@ -63,8 +66,8 @@ type ActivityListStarredOptions struct {
// ListStarred lists all the repos starred by a user. Passing the empty string
// will list the starred repositories for the authenticated user.
//
// GitHub API docs: http://developer.github.com/v3/activity/starring/#list-repositories-being-starred
func (s *ActivityService) ListStarred(user string, opt *ActivityListStarredOptions) ([]*StarredRepository, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/activity/starring/#list-repositories-being-starred
func (s *ActivityService) ListStarred(ctx context.Context, user string, opt *ActivityListStarredOptions) ([]*StarredRepository, *Response, error) {
var u string
if user != "" {
u = fmt.Sprintf("users/%v/starred", user)
@ -85,7 +88,7 @@ func (s *ActivityService) ListStarred(user string, opt *ActivityListStarredOptio
req.Header.Set("Accept", mediaTypeStarringPreview)
var repos []*StarredRepository
resp, err := s.client.Do(req, &repos)
resp, err := s.client.Do(ctx, req, &repos)
if err != nil {
return nil, resp, err
}
@ -96,13 +99,13 @@ func (s *ActivityService) ListStarred(user string, opt *ActivityListStarredOptio
// IsStarred checks if a repository is starred by authenticated user.
//
// GitHub API docs: https://developer.github.com/v3/activity/starring/#check-if-you-are-starring-a-repository
func (s *ActivityService) IsStarred(owner, repo string) (bool, *Response, error) {
func (s *ActivityService) IsStarred(ctx context.Context, owner, repo string) (bool, *Response, error) {
u := fmt.Sprintf("user/starred/%v/%v", owner, repo)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return false, nil, err
}
resp, err := s.client.Do(req, nil)
resp, err := s.client.Do(ctx, req, nil)
starred, err := parseBoolResponse(err)
return starred, resp, err
}
@ -110,23 +113,23 @@ func (s *ActivityService) IsStarred(owner, repo string) (bool, *Response, error)
// Star a repository as the authenticated user.
//
// GitHub API docs: https://developer.github.com/v3/activity/starring/#star-a-repository
func (s *ActivityService) Star(owner, repo string) (*Response, error) {
func (s *ActivityService) Star(ctx context.Context, owner, repo string) (*Response, error) {
u := fmt.Sprintf("user/starred/%v/%v", owner, repo)
req, err := s.client.NewRequest("PUT", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}
// Unstar a repository as the authenticated user.
//
// GitHub API docs: https://developer.github.com/v3/activity/starring/#unstar-a-repository
func (s *ActivityService) Unstar(owner, repo string) (*Response, error) {
func (s *ActivityService) Unstar(ctx context.Context, owner, repo string) (*Response, error) {
u := fmt.Sprintf("user/starred/%v/%v", owner, repo)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}

View file

@ -5,7 +5,10 @@
package github
import "fmt"
import (
"context"
"fmt"
)
// Subscription identifies a repository or thread subscription.
type Subscription struct {
@ -24,8 +27,8 @@ type Subscription struct {
// ListWatchers lists watchers of a particular repo.
//
// GitHub API Docs: http://developer.github.com/v3/activity/watching/#list-watchers
func (s *ActivityService) ListWatchers(owner, repo string, opt *ListOptions) ([]*User, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/activity/watching/#list-watchers
func (s *ActivityService) ListWatchers(ctx context.Context, owner, repo string, opt *ListOptions) ([]*User, *Response, error) {
u := fmt.Sprintf("repos/%s/%s/subscribers", owner, repo)
u, err := addOptions(u, opt)
if err != nil {
@ -38,7 +41,7 @@ func (s *ActivityService) ListWatchers(owner, repo string, opt *ListOptions) ([]
}
var watchers []*User
resp, err := s.client.Do(req, &watchers)
resp, err := s.client.Do(ctx, req, &watchers)
if err != nil {
return nil, resp, err
}
@ -49,8 +52,8 @@ func (s *ActivityService) ListWatchers(owner, repo string, opt *ListOptions) ([]
// ListWatched lists the repositories the specified user is watching. Passing
// the empty string will fetch watched repos for the authenticated user.
//
// GitHub API Docs: https://developer.github.com/v3/activity/watching/#list-repositories-being-watched
func (s *ActivityService) ListWatched(user string, opt *ListOptions) ([]*Repository, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/activity/watching/#list-repositories-being-watched
func (s *ActivityService) ListWatched(ctx context.Context, user string, opt *ListOptions) ([]*Repository, *Response, error) {
var u string
if user != "" {
u = fmt.Sprintf("users/%v/subscriptions", user)
@ -68,7 +71,7 @@ func (s *ActivityService) ListWatched(user string, opt *ListOptions) ([]*Reposit
}
var watched []*Repository
resp, err := s.client.Do(req, &watched)
resp, err := s.client.Do(ctx, req, &watched)
if err != nil {
return nil, resp, err
}
@ -80,8 +83,8 @@ func (s *ActivityService) ListWatched(user string, opt *ListOptions) ([]*Reposit
// repository for the authenticated user. If the authenticated user is not
// watching the repository, a nil Subscription is returned.
//
// GitHub API Docs: https://developer.github.com/v3/activity/watching/#get-a-repository-subscription
func (s *ActivityService) GetRepositorySubscription(owner, repo string) (*Subscription, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/activity/watching/#get-a-repository-subscription
func (s *ActivityService) GetRepositorySubscription(ctx context.Context, owner, repo string) (*Subscription, *Response, error) {
u := fmt.Sprintf("repos/%s/%s/subscription", owner, repo)
req, err := s.client.NewRequest("GET", u, nil)
@ -90,7 +93,7 @@ func (s *ActivityService) GetRepositorySubscription(owner, repo string) (*Subscr
}
sub := new(Subscription)
resp, err := s.client.Do(req, sub)
resp, err := s.client.Do(ctx, req, sub)
if err != nil {
// if it's just a 404, don't return that as an error
_, err = parseBoolResponse(err)
@ -107,8 +110,8 @@ func (s *ActivityService) GetRepositorySubscription(owner, repo string) (*Subscr
// To ignore notifications made within a repository, set subscription.Ignored to true.
// To stop watching a repository, use DeleteRepositorySubscription.
//
// GitHub API Docs: https://developer.github.com/v3/activity/watching/#set-a-repository-subscription
func (s *ActivityService) SetRepositorySubscription(owner, repo string, subscription *Subscription) (*Subscription, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/activity/watching/#set-a-repository-subscription
func (s *ActivityService) SetRepositorySubscription(ctx context.Context, owner, repo string, subscription *Subscription) (*Subscription, *Response, error) {
u := fmt.Sprintf("repos/%s/%s/subscription", owner, repo)
req, err := s.client.NewRequest("PUT", u, subscription)
@ -117,7 +120,7 @@ func (s *ActivityService) SetRepositorySubscription(owner, repo string, subscrip
}
sub := new(Subscription)
resp, err := s.client.Do(req, sub)
resp, err := s.client.Do(ctx, req, sub)
if err != nil {
return nil, resp, err
}
@ -131,13 +134,13 @@ func (s *ActivityService) SetRepositorySubscription(owner, repo string, subscrip
// This is used to stop watching a repository. To control whether or not to
// receive notifications from a repository, use SetRepositorySubscription.
//
// GitHub API Docs: https://developer.github.com/v3/activity/watching/#delete-a-repository-subscription
func (s *ActivityService) DeleteRepositorySubscription(owner, repo string) (*Response, error) {
// GitHub API docs: https://developer.github.com/v3/activity/watching/#delete-a-repository-subscription
func (s *ActivityService) DeleteRepositorySubscription(ctx context.Context, owner, repo string) (*Response, error) {
u := fmt.Sprintf("repos/%s/%s/subscription", owner, repo)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}

View file

@ -5,7 +5,10 @@
package github
import "fmt"
import (
"context"
"fmt"
)
// AdminService handles communication with the admin related methods of the
// GitHub API. These API routes are normally only accessible for GitHub
@ -62,7 +65,7 @@ func (m UserLDAPMapping) String() string {
// UpdateUserLDAPMapping updates the mapping between a GitHub user and an LDAP user.
//
// GitHub API docs: https://developer.github.com/v3/enterprise/ldap/#update-ldap-mapping-for-a-user
func (s *AdminService) UpdateUserLDAPMapping(user string, mapping *UserLDAPMapping) (*UserLDAPMapping, *Response, error) {
func (s *AdminService) UpdateUserLDAPMapping(ctx context.Context, user string, mapping *UserLDAPMapping) (*UserLDAPMapping, *Response, error) {
u := fmt.Sprintf("admin/ldap/users/%v/mapping", user)
req, err := s.client.NewRequest("PATCH", u, mapping)
if err != nil {
@ -70,7 +73,7 @@ func (s *AdminService) UpdateUserLDAPMapping(user string, mapping *UserLDAPMappi
}
m := new(UserLDAPMapping)
resp, err := s.client.Do(req, m)
resp, err := s.client.Do(ctx, req, m)
if err != nil {
return nil, resp, err
}
@ -81,7 +84,7 @@ func (s *AdminService) UpdateUserLDAPMapping(user string, mapping *UserLDAPMappi
// UpdateTeamLDAPMapping updates the mapping between a GitHub team and an LDAP group.
//
// GitHub API docs: https://developer.github.com/v3/enterprise/ldap/#update-ldap-mapping-for-a-team
func (s *AdminService) UpdateTeamLDAPMapping(team int, mapping *TeamLDAPMapping) (*TeamLDAPMapping, *Response, error) {
func (s *AdminService) UpdateTeamLDAPMapping(ctx context.Context, team int, mapping *TeamLDAPMapping) (*TeamLDAPMapping, *Response, error) {
u := fmt.Sprintf("admin/ldap/teams/%v/mapping", team)
req, err := s.client.NewRequest("PATCH", u, mapping)
if err != nil {
@ -89,7 +92,7 @@ func (s *AdminService) UpdateTeamLDAPMapping(team int, mapping *TeamLDAPMapping)
}
m := new(TeamLDAPMapping)
resp, err := s.client.Do(req, m)
resp, err := s.client.Do(ctx, req, m)
if err != nil {
return nil, resp, err
}

View file

@ -5,11 +5,14 @@
package github
import "fmt"
import (
"context"
"fmt"
)
// Scope models a GitHub authorization scope.
//
// GitHub API docs:https://developer.github.com/v3/oauth/#scopes
// GitHub API docs: https://developer.github.com/v3/oauth/#scopes
type Scope string
// This is the set of scopes for GitHub API V3
@ -134,7 +137,7 @@ func (a AuthorizationUpdateRequest) String() string {
// List the authorizations for the authenticated user.
//
// GitHub API docs: https://developer.github.com/v3/oauth_authorizations/#list-your-authorizations
func (s *AuthorizationsService) List(opt *ListOptions) ([]*Authorization, *Response, error) {
func (s *AuthorizationsService) List(ctx context.Context, opt *ListOptions) ([]*Authorization, *Response, error) {
u := "authorizations"
u, err := addOptions(u, opt)
if err != nil {
@ -147,7 +150,7 @@ func (s *AuthorizationsService) List(opt *ListOptions) ([]*Authorization, *Respo
}
var auths []*Authorization
resp, err := s.client.Do(req, &auths)
resp, err := s.client.Do(ctx, req, &auths)
if err != nil {
return nil, resp, err
}
@ -157,7 +160,7 @@ func (s *AuthorizationsService) List(opt *ListOptions) ([]*Authorization, *Respo
// Get a single authorization.
//
// GitHub API docs: https://developer.github.com/v3/oauth_authorizations/#get-a-single-authorization
func (s *AuthorizationsService) Get(id int) (*Authorization, *Response, error) {
func (s *AuthorizationsService) Get(ctx context.Context, id int) (*Authorization, *Response, error) {
u := fmt.Sprintf("authorizations/%d", id)
req, err := s.client.NewRequest("GET", u, nil)
@ -166,7 +169,7 @@ func (s *AuthorizationsService) Get(id int) (*Authorization, *Response, error) {
}
a := new(Authorization)
resp, err := s.client.Do(req, a)
resp, err := s.client.Do(ctx, req, a)
if err != nil {
return nil, resp, err
}
@ -176,7 +179,7 @@ func (s *AuthorizationsService) Get(id int) (*Authorization, *Response, error) {
// Create a new authorization for the specified OAuth application.
//
// GitHub API docs: https://developer.github.com/v3/oauth_authorizations/#create-a-new-authorization
func (s *AuthorizationsService) Create(auth *AuthorizationRequest) (*Authorization, *Response, error) {
func (s *AuthorizationsService) Create(ctx context.Context, auth *AuthorizationRequest) (*Authorization, *Response, error) {
u := "authorizations"
req, err := s.client.NewRequest("POST", u, auth)
@ -185,7 +188,7 @@ func (s *AuthorizationsService) Create(auth *AuthorizationRequest) (*Authorizati
}
a := new(Authorization)
resp, err := s.client.Do(req, a)
resp, err := s.client.Do(ctx, req, a)
if err != nil {
return nil, resp, err
}
@ -204,9 +207,9 @@ func (s *AuthorizationsService) Create(auth *AuthorizationRequest) (*Authorizati
// clientID is the OAuth Client ID with which to create the token.
//
// GitHub API docs:
// - https://developer.github.com/v3/oauth_authorizations/#get-or-create-an-authorization-for-a-specific-app
// - https://developer.github.com/v3/oauth_authorizations/#get-or-create-an-authorization-for-a-specific-app-and-fingerprint
func (s *AuthorizationsService) GetOrCreateForApp(clientID string, auth *AuthorizationRequest) (*Authorization, *Response, error) {
// https://developer.github.com/v3/oauth_authorizations/#get-or-create-an-authorization-for-a-specific-app
// https://developer.github.com/v3/oauth_authorizations/#get-or-create-an-authorization-for-a-specific-app-and-fingerprint
func (s *AuthorizationsService) GetOrCreateForApp(ctx context.Context, clientID string, auth *AuthorizationRequest) (*Authorization, *Response, error) {
var u string
if auth.Fingerprint == nil || *auth.Fingerprint == "" {
u = fmt.Sprintf("authorizations/clients/%v", clientID)
@ -220,7 +223,7 @@ func (s *AuthorizationsService) GetOrCreateForApp(clientID string, auth *Authori
}
a := new(Authorization)
resp, err := s.client.Do(req, a)
resp, err := s.client.Do(ctx, req, a)
if err != nil {
return nil, resp, err
}
@ -231,7 +234,7 @@ func (s *AuthorizationsService) GetOrCreateForApp(clientID string, auth *Authori
// Edit a single authorization.
//
// GitHub API docs: https://developer.github.com/v3/oauth_authorizations/#update-an-existing-authorization
func (s *AuthorizationsService) Edit(id int, auth *AuthorizationUpdateRequest) (*Authorization, *Response, error) {
func (s *AuthorizationsService) Edit(ctx context.Context, id int, auth *AuthorizationUpdateRequest) (*Authorization, *Response, error) {
u := fmt.Sprintf("authorizations/%d", id)
req, err := s.client.NewRequest("PATCH", u, auth)
@ -240,7 +243,7 @@ func (s *AuthorizationsService) Edit(id int, auth *AuthorizationUpdateRequest) (
}
a := new(Authorization)
resp, err := s.client.Do(req, a)
resp, err := s.client.Do(ctx, req, a)
if err != nil {
return nil, resp, err
}
@ -251,7 +254,7 @@ func (s *AuthorizationsService) Edit(id int, auth *AuthorizationUpdateRequest) (
// Delete a single authorization.
//
// GitHub API docs: https://developer.github.com/v3/oauth_authorizations/#delete-an-authorization
func (s *AuthorizationsService) Delete(id int) (*Response, error) {
func (s *AuthorizationsService) Delete(ctx context.Context, id int) (*Response, error) {
u := fmt.Sprintf("authorizations/%d", id)
req, err := s.client.NewRequest("DELETE", u, nil)
@ -259,7 +262,7 @@ func (s *AuthorizationsService) Delete(id int) (*Response, error) {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}
// Check if an OAuth token is valid for a specific app.
@ -271,7 +274,7 @@ func (s *AuthorizationsService) Delete(id int) (*Response, error) {
// The returned Authorization.User field will be populated.
//
// GitHub API docs: https://developer.github.com/v3/oauth_authorizations/#check-an-authorization
func (s *AuthorizationsService) Check(clientID string, token string) (*Authorization, *Response, error) {
func (s *AuthorizationsService) Check(ctx context.Context, clientID string, token string) (*Authorization, *Response, error) {
u := fmt.Sprintf("applications/%v/tokens/%v", clientID, token)
req, err := s.client.NewRequest("GET", u, nil)
@ -280,7 +283,7 @@ func (s *AuthorizationsService) Check(clientID string, token string) (*Authoriza
}
a := new(Authorization)
resp, err := s.client.Do(req, a)
resp, err := s.client.Do(ctx, req, a)
if err != nil {
return nil, resp, err
}
@ -299,7 +302,7 @@ func (s *AuthorizationsService) Check(clientID string, token string) (*Authoriza
// The returned Authorization.User field will be populated.
//
// GitHub API docs: https://developer.github.com/v3/oauth_authorizations/#reset-an-authorization
func (s *AuthorizationsService) Reset(clientID string, token string) (*Authorization, *Response, error) {
func (s *AuthorizationsService) Reset(ctx context.Context, clientID string, token string) (*Authorization, *Response, error) {
u := fmt.Sprintf("applications/%v/tokens/%v", clientID, token)
req, err := s.client.NewRequest("POST", u, nil)
@ -308,7 +311,7 @@ func (s *AuthorizationsService) Reset(clientID string, token string) (*Authoriza
}
a := new(Authorization)
resp, err := s.client.Do(req, a)
resp, err := s.client.Do(ctx, req, a)
if err != nil {
return nil, resp, err
}
@ -323,7 +326,7 @@ func (s *AuthorizationsService) Reset(clientID string, token string) (*Authoriza
// clientSecret. Invalid tokens will return a 404 Not Found.
//
// GitHub API docs: https://developer.github.com/v3/oauth_authorizations/#revoke-an-authorization-for-an-application
func (s *AuthorizationsService) Revoke(clientID string, token string) (*Response, error) {
func (s *AuthorizationsService) Revoke(ctx context.Context, clientID string, token string) (*Response, error) {
u := fmt.Sprintf("applications/%v/tokens/%v", clientID, token)
req, err := s.client.NewRequest("DELETE", u, nil)
@ -331,7 +334,7 @@ func (s *AuthorizationsService) Revoke(clientID string, token string) (*Response
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}
// ListGrants lists the set of OAuth applications that have been granted
@ -340,14 +343,14 @@ func (s *AuthorizationsService) Revoke(clientID string, token string) (*Response
// tokens an application has generated for the user.
//
// GitHub API docs: https://developer.github.com/v3/oauth_authorizations/#list-your-grants
func (s *AuthorizationsService) ListGrants() ([]*Grant, *Response, error) {
func (s *AuthorizationsService) ListGrants(ctx context.Context) ([]*Grant, *Response, error) {
req, err := s.client.NewRequest("GET", "applications/grants", nil)
if err != nil {
return nil, nil, err
}
grants := []*Grant{}
resp, err := s.client.Do(req, &grants)
resp, err := s.client.Do(ctx, req, &grants)
if err != nil {
return nil, resp, err
}
@ -358,7 +361,7 @@ func (s *AuthorizationsService) ListGrants() ([]*Grant, *Response, error) {
// GetGrant gets a single OAuth application grant.
//
// GitHub API docs: https://developer.github.com/v3/oauth_authorizations/#get-a-single-grant
func (s *AuthorizationsService) GetGrant(id int) (*Grant, *Response, error) {
func (s *AuthorizationsService) GetGrant(ctx context.Context, id int) (*Grant, *Response, error) {
u := fmt.Sprintf("applications/grants/%d", id)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
@ -366,7 +369,7 @@ func (s *AuthorizationsService) GetGrant(id int) (*Grant, *Response, error) {
}
grant := new(Grant)
resp, err := s.client.Do(req, grant)
resp, err := s.client.Do(ctx, req, grant)
if err != nil {
return nil, resp, err
}
@ -379,14 +382,14 @@ func (s *AuthorizationsService) GetGrant(id int) (*Grant, *Response, error) {
// the user.
//
// GitHub API docs: https://developer.github.com/v3/oauth_authorizations/#delete-a-grant
func (s *AuthorizationsService) DeleteGrant(id int) (*Response, error) {
func (s *AuthorizationsService) DeleteGrant(ctx context.Context, id int) (*Response, error) {
u := fmt.Sprintf("applications/grants/%d", id)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}
// CreateImpersonation creates an impersonation OAuth token.
@ -396,7 +399,7 @@ func (s *AuthorizationsService) DeleteGrant(id int) (*Response, error) {
// new token automatically revokes an existing one.
//
// GitHub API docs: https://developer.github.com/enterprise/2.5/v3/users/administration/#create-an-impersonation-oauth-token
func (s *AuthorizationsService) CreateImpersonation(username string, authReq *AuthorizationRequest) (*Authorization, *Response, error) {
func (s *AuthorizationsService) CreateImpersonation(ctx context.Context, username string, authReq *AuthorizationRequest) (*Authorization, *Response, error) {
u := fmt.Sprintf("admin/users/%v/authorizations", username)
req, err := s.client.NewRequest("POST", u, authReq)
if err != nil {
@ -404,7 +407,7 @@ func (s *AuthorizationsService) CreateImpersonation(username string, authReq *Au
}
a := new(Authorization)
resp, err := s.client.Do(req, a)
resp, err := s.client.Do(ctx, req, a)
if err != nil {
return nil, resp, err
}
@ -416,12 +419,12 @@ func (s *AuthorizationsService) CreateImpersonation(username string, authReq *Au
// NOTE: there can be only one at a time.
//
// GitHub API docs: https://developer.github.com/enterprise/2.5/v3/users/administration/#delete-an-impersonation-oauth-token
func (s *AuthorizationsService) DeleteImpersonation(username string) (*Response, error) {
func (s *AuthorizationsService) DeleteImpersonation(ctx context.Context, username string) (*Response, error) {
u := fmt.Sprintf("admin/users/%v/authorizations", username)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}

View file

@ -16,7 +16,7 @@ access different parts of the GitHub API. For example:
client := github.NewClient(nil)
// list all organizations for user "willnorris"
orgs, _, err := client.Organizations.List("willnorris", nil)
orgs, _, err := client.Organizations.List(ctx, "willnorris", nil)
Some API methods have optional parameters that can be passed. For example:
@ -24,11 +24,11 @@ Some API methods have optional parameters that can be passed. For example:
// list public repositories for org "github"
opt := &github.RepositoryListByOrgOptions{Type: "public"}
repos, _, err := client.Repositories.ListByOrg("github", opt)
repos, _, err := client.Repositories.ListByOrg(ctx, "github", opt)
The services of a client divide the API into logical chunks and correspond to
the structure of the GitHub API documentation at
http://developer.github.com/v3/.
https://developer.github.com/v3/.
Authentication
@ -42,15 +42,16 @@ use it with the oauth2 library using:
import "golang.org/x/oauth2"
func main() {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: "... your access token ..."},
)
tc := oauth2.NewClient(oauth2.NoContext, ts)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
// list all repositories for the authenticated user
repos, _, err := client.Repositories.List("", nil)
repos, _, err := client.Repositories.List(ctx, "", nil)
}
Note that when using an authenticated Client, all calls made by the client will
@ -78,13 +79,13 @@ up-to-date rate limit data for the client.
To detect an API rate limit error, you can check if its type is *github.RateLimitError:
repos, _, err := client.Repositories.List("", nil)
repos, _, err := client.Repositories.List(ctx, "", nil)
if _, ok := err.(*github.RateLimitError); ok {
log.Println("hit rate limit")
}
Learn more about GitHub rate limiting at
http://developer.github.com/v3/#rate-limiting.
https://developer.github.com/v3/#rate-limiting.
Accepted Status
@ -96,7 +97,7 @@ this behavior.
To detect this condition of error, you can check if its type is
*github.AcceptedError:
stats, _, err := client.Repositories.ListContributorsStats(org, repo)
stats, _, err := client.Repositories.ListContributorsStats(ctx, org, repo)
if _, ok := err.(*github.AcceptedError); ok {
log.Println("scheduled on GitHub side")
}
@ -124,7 +125,7 @@ bool, and int values. For example:
Name: github.String("foo"),
Private: github.Bool(true),
}
client.Repositories.Create("", repo)
client.Repositories.Create(ctx, "", repo)
Users who have worked with protocol buffers should find this pattern familiar.
@ -145,7 +146,7 @@ github.Response struct.
// get all pages of results
var allRepos []*github.Repository
for {
repos, resp, err := client.Repositories.ListByOrg("github", opt)
repos, resp, err := client.Repositories.ListByOrg(ctx, "github", opt)
if err != nil {
return err
}

View file

@ -10,7 +10,7 @@ package github
// CommitCommentEvent is triggered when a commit comment is created.
// The Webhook event name is "commit_comment".
//
// GitHub docs: https://developer.github.com/v3/activity/events/types/#commitcommentevent
// GitHub API docs: https://developer.github.com/v3/activity/events/types/#commitcommentevent
type CommitCommentEvent struct {
Comment *RepositoryComment `json:"comment,omitempty"`
@ -28,7 +28,7 @@ type CommitCommentEvent struct {
// Additionally, webhooks will not receive this event for tags if more
// than three tags are pushed at once.
//
// GitHub docs: https://developer.github.com/v3/activity/events/types/#createevent
// GitHub API docs: https://developer.github.com/v3/activity/events/types/#createevent
type CreateEvent struct {
Ref *string `json:"ref,omitempty"`
// RefType is the object that was created. Possible values are: "repository", "branch", "tag".
@ -49,7 +49,7 @@ type CreateEvent struct {
// Note: webhooks will not receive this event for tags if more than three tags
// are deleted at once.
//
// GitHub docs: https://developer.github.com/v3/activity/events/types/#deleteevent
// GitHub API docs: https://developer.github.com/v3/activity/events/types/#deleteevent
type DeleteEvent struct {
Ref *string `json:"ref,omitempty"`
// RefType is the object that was deleted. Possible values are: "branch", "tag".
@ -67,7 +67,7 @@ type DeleteEvent struct {
//
// Events of this type are not visible in timelines, they are only used to trigger hooks.
//
// GitHub docs: https://developer.github.com/v3/activity/events/types/#deploymentevent
// GitHub API docs: https://developer.github.com/v3/activity/events/types/#deploymentevent
type DeploymentEvent struct {
Deployment *Deployment `json:"deployment,omitempty"`
Repo *Repository `json:"repository,omitempty"`
@ -82,7 +82,7 @@ type DeploymentEvent struct {
//
// Events of this type are not visible in timelines, they are only used to trigger hooks.
//
// GitHub docs: https://developer.github.com/v3/activity/events/types/#deploymentstatusevent
// GitHub API docs: https://developer.github.com/v3/activity/events/types/#deploymentstatusevent
type DeploymentStatusEvent struct {
Deployment *Deployment `json:"deployment,omitempty"`
DeploymentStatus *DeploymentStatus `json:"deployment_status,omitempty"`
@ -96,7 +96,7 @@ type DeploymentStatusEvent struct {
// ForkEvent is triggered when a user forks a repository.
// The Webhook event name is "fork".
//
// GitHub docs: https://developer.github.com/v3/activity/events/types/#forkevent
// GitHub API docs: https://developer.github.com/v3/activity/events/types/#forkevent
type ForkEvent struct {
// Forkee is the created repository.
Forkee *Repository `json:"forkee,omitempty"`
@ -120,7 +120,7 @@ type Page struct {
// GollumEvent is triggered when a Wiki page is created or updated.
// The Webhook event name is "gollum".
//
// GitHub docs: https://developer.github.com/v3/activity/events/types/#gollumevent
// GitHub API docs: https://developer.github.com/v3/activity/events/types/#gollumevent
type GollumEvent struct {
Pages []*Page `json:"pages,omitempty"`
@ -130,19 +130,6 @@ type GollumEvent struct {
Installation *Installation `json:"installation,omitempty"`
}
// IssueActivityEvent represents the payload delivered by Issue webhook.
//
// Deprecated: Use IssuesEvent instead.
type IssueActivityEvent struct {
Action *string `json:"action,omitempty"`
Issue *Issue `json:"issue,omitempty"`
// The following fields are only populated by Webhook events.
Repo *Repository `json:"repository,omitempty"`
Sender *User `json:"sender,omitempty"`
Installation *Installation `json:"installation,omitempty"`
}
// EditChange represents the changes when an issue, pull request, or comment has
// been edited.
type EditChange struct {
@ -157,7 +144,7 @@ type EditChange struct {
// IntegrationInstallationEvent is triggered when an integration is created or deleted.
// The Webhook event name is "integration_installation".
//
// GitHub docs: https://developer.github.com/early-access/integrations/webhooks/#integrationinstallationevent
// GitHub API docs: https://developer.github.com/early-access/integrations/webhooks/#integrationinstallationevent
type IntegrationInstallationEvent struct {
// The action that was performed. Possible values for an "integration_installation"
// event are: "created", "deleted".
@ -169,7 +156,7 @@ type IntegrationInstallationEvent struct {
// IntegrationInstallationRepositoriesEvent is triggered when an integration repository
// is added or removed. The Webhook event name is "integration_installation_repositories".
//
// GitHub docs: https://developer.github.com/early-access/integrations/webhooks/#integrationinstallationrepositoriesevent
// GitHub API docs: https://developer.github.com/early-access/integrations/webhooks/#integrationinstallationrepositoriesevent
type IntegrationInstallationRepositoriesEvent struct {
// The action that was performed. Possible values for an "integration_installation_repositories"
// event are: "added", "removed".
@ -184,7 +171,7 @@ type IntegrationInstallationRepositoriesEvent struct {
// or pull request.
// The Webhook event name is "issue_comment".
//
// GitHub docs: https://developer.github.com/v3/activity/events/types/#issuecommentevent
// GitHub API docs: https://developer.github.com/v3/activity/events/types/#issuecommentevent
type IssueCommentEvent struct {
// Action is the action that was performed on the comment.
// Possible values are: "created", "edited", "deleted".
@ -203,7 +190,7 @@ type IssueCommentEvent struct {
// unlabeled, opened, closed, or reopened.
// The Webhook event name is "issues".
//
// GitHub docs: https://developer.github.com/v3/activity/events/types/#issuesevent
// GitHub API docs: https://developer.github.com/v3/activity/events/types/#issuesevent
type IssuesEvent struct {
// Action is the action that was performed. Possible values are: "assigned",
// "unassigned", "labeled", "unlabeled", "opened", "closed", "reopened", "edited".
@ -222,7 +209,7 @@ type IssuesEvent struct {
// LabelEvent is triggered when a repository's label is created, edited, or deleted.
// The Webhook event name is "label"
//
// GitHub docs: https://developer.github.com/v3/activity/events/types/#labelevent
// GitHub API docs: https://developer.github.com/v3/activity/events/types/#labelevent
type LabelEvent struct {
// Action is the action that was performed. Possible values are:
// "created", "edited", "deleted"
@ -239,7 +226,7 @@ type LabelEvent struct {
// MemberEvent is triggered when a user is added as a collaborator to a repository.
// The Webhook event name is "member".
//
// GitHub docs: https://developer.github.com/v3/activity/events/types/#memberevent
// GitHub API docs: https://developer.github.com/v3/activity/events/types/#memberevent
type MemberEvent struct {
// Action is the action that was performed. Possible value is: "added".
Action *string `json:"action,omitempty"`
@ -257,7 +244,7 @@ type MemberEvent struct {
// Events of this type are not visible in timelines, they are only used to
// trigger organization webhooks.
//
// GitHub docs: https://developer.github.com/v3/activity/events/types/#membershipevent
// GitHub API docs: https://developer.github.com/v3/activity/events/types/#membershipevent
type MembershipEvent struct {
// Action is the action that was performed. Possible values are: "added", "removed".
Action *string `json:"action,omitempty"`
@ -275,7 +262,7 @@ type MembershipEvent struct {
// MilestoneEvent is triggered when a milestone is created, closed, opened, edited, or deleted.
// The Webhook event name is "milestone".
//
// Github docs: https://developer.github.com/v3/activity/events/types/#milestoneevent
// GitHub API docs: https://developer.github.com/v3/activity/events/types/#milestoneevent
type MilestoneEvent struct {
// Action is the action that was performed. Possible values are:
// "created", "closed", "opened", "edited", "deleted"
@ -294,7 +281,7 @@ type MilestoneEvent struct {
// Events of this type are not visible in timelines. These events are only used to trigger organization hooks.
// Webhook event name is "organization".
//
// Github docs: https://developer.github.com/v3/activity/events/types/#organizationevent
// GitHub API docs: https://developer.github.com/v3/activity/events/types/#organizationevent
type OrganizationEvent struct {
// Action is the action that was performed.
// Can be one of "member_added", "member_removed", or "member_invited".
@ -321,7 +308,7 @@ type OrganizationEvent struct {
//
// Events of this type are not visible in timelines, they are only used to trigger hooks.
//
// GitHub docs: https://developer.github.com/v3/activity/events/types/#pagebuildevent
// GitHub API docs: https://developer.github.com/v3/activity/events/types/#pagebuildevent
type PageBuildEvent struct {
Build *PagesBuild `json:"build,omitempty"`
@ -334,7 +321,7 @@ type PageBuildEvent struct {
// PingEvent is triggered when a Webhook is added to GitHub.
//
// GitHub docs: https://developer.github.com/webhooks/#ping-event
// GitHub API docs: https://developer.github.com/webhooks/#ping-event
type PingEvent struct {
// Random string of GitHub zen.
Zen *string `json:"zen,omitempty"`
@ -349,7 +336,7 @@ type PingEvent struct {
// According to GitHub: "Without a doubt: the best GitHub event."
// The Webhook event name is "public".
//
// GitHub docs: https://developer.github.com/v3/activity/events/types/#publicevent
// GitHub API docs: https://developer.github.com/v3/activity/events/types/#publicevent
type PublicEvent struct {
// The following fields are only populated by Webhook events.
Repo *Repository `json:"repository,omitempty"`
@ -361,7 +348,7 @@ type PublicEvent struct {
// labeled, unlabeled, opened, closed, reopened, or synchronized.
// The Webhook event name is "pull_request".
//
// GitHub docs: https://developer.github.com/v3/activity/events/types/#pullrequestevent
// GitHub API docs: https://developer.github.com/v3/activity/events/types/#pullrequestevent
type PullRequestEvent struct {
// Action is the action that was performed. Possible values are: "assigned",
// "unassigned", "labeled", "unlabeled", "opened", "closed", or "reopened",
@ -383,7 +370,7 @@ type PullRequestEvent struct {
// request.
// The Webhook event name is "pull_request_review".
//
// GitHub docs: https://developer.github.com/v3/activity/events/types/#pullrequestreviewevent
// GitHub API docs: https://developer.github.com/v3/activity/events/types/#pullrequestreviewevent
type PullRequestReviewEvent struct {
// Action is always "submitted".
Action *string `json:"action,omitempty"`
@ -404,7 +391,7 @@ type PullRequestReviewEvent struct {
// portion of the unified diff of a pull request.
// The Webhook event name is "pull_request_review_comment".
//
// GitHub docs: https://developer.github.com/v3/activity/events/types/#pullrequestreviewcommentevent
// GitHub API docs: https://developer.github.com/v3/activity/events/types/#pullrequestreviewcommentevent
type PullRequestReviewCommentEvent struct {
// Action is the action that was performed on the comment.
// Possible values are: "created", "edited", "deleted".
@ -421,7 +408,7 @@ type PullRequestReviewCommentEvent struct {
// PushEvent represents a git push to a GitHub repository.
//
// GitHub API docs: http://developer.github.com/v3/activity/events/types/#pushevent
// GitHub API docs: https://developer.github.com/v3/activity/events/types/#pushevent
type PushEvent struct {
PushID *int `json:"push_id,omitempty"`
Head *string `json:"head,omitempty"`
@ -517,7 +504,7 @@ type PushEventRepoOwner struct {
// ReleaseEvent is triggered when a release is published.
// The Webhook event name is "release".
//
// GitHub docs: https://developer.github.com/v3/activity/events/types/#releaseevent
// GitHub API docs: https://developer.github.com/v3/activity/events/types/#releaseevent
type ReleaseEvent struct {
// Action is the action that was performed. Possible value is: "published".
Action *string `json:"action,omitempty"`
@ -535,7 +522,7 @@ type ReleaseEvent struct {
// Events of this type are not visible in timelines, they are only used to
// trigger organization webhooks.
//
// GitHub docs: https://developer.github.com/v3/activity/events/types/#repositoryevent
// GitHub API docs: https://developer.github.com/v3/activity/events/types/#repositoryevent
type RepositoryEvent struct {
// Action is the action that was performed. Possible values are: "created", "deleted",
// "publicized", "privatized".
@ -554,7 +541,7 @@ type RepositoryEvent struct {
// Events of this type are not visible in timelines, they are only used to
// trigger hooks.
//
// GitHub docs: https://developer.github.com/v3/activity/events/types/#statusevent
// GitHub API docs: https://developer.github.com/v3/activity/events/types/#statusevent
type StatusEvent struct {
SHA *string `json:"sha,omitempty"`
// State is the new state. Possible values are: "pending", "success", "failure", "error".
@ -581,7 +568,7 @@ type StatusEvent struct {
// Events of this type are not visible in timelines. These events are only used
// to trigger hooks.
//
// GitHub docs: https://developer.github.com/v3/activity/events/types/#teamaddevent
// GitHub API docs: https://developer.github.com/v3/activity/events/types/#teamaddevent
type TeamAddEvent struct {
Team *Team `json:"team,omitempty"`
Repo *Repository `json:"repository,omitempty"`
@ -598,7 +585,7 @@ type TeamAddEvent struct {
// The events actor is the user who starred a repository, and the events
// repository is the repository that was starred.
//
// GitHub docs: https://developer.github.com/v3/activity/events/types/#watchevent
// GitHub API docs: https://developer.github.com/v3/activity/events/types/#watchevent
type WatchEvent struct {
// Action is the action that was performed. Possible value is: "started".
Action *string `json:"action,omitempty"`

View file

@ -6,6 +6,7 @@
package github
import (
"context"
"fmt"
"time"
)
@ -13,7 +14,7 @@ import (
// GistsService handles communication with the Gist related
// methods of the GitHub API.
//
// GitHub API docs: http://developer.github.com/v3/gists/
// GitHub API docs: https://developer.github.com/v3/gists/
type GistsService service
// Gist represents a GitHub's gist.
@ -92,8 +93,8 @@ type GistListOptions struct {
// is authenticated, it will returns all gists for the authenticated
// user.
//
// GitHub API docs: http://developer.github.com/v3/gists/#list-gists
func (s *GistsService) List(user string, opt *GistListOptions) ([]*Gist, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/gists/#list-gists
func (s *GistsService) List(ctx context.Context, user string, opt *GistListOptions) ([]*Gist, *Response, error) {
var u string
if user != "" {
u = fmt.Sprintf("users/%v/gists", user)
@ -111,7 +112,7 @@ func (s *GistsService) List(user string, opt *GistListOptions) ([]*Gist, *Respon
}
var gists []*Gist
resp, err := s.client.Do(req, &gists)
resp, err := s.client.Do(ctx, req, &gists)
if err != nil {
return nil, resp, err
}
@ -121,8 +122,8 @@ func (s *GistsService) List(user string, opt *GistListOptions) ([]*Gist, *Respon
// ListAll lists all public gists.
//
// GitHub API docs: http://developer.github.com/v3/gists/#list-gists
func (s *GistsService) ListAll(opt *GistListOptions) ([]*Gist, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/gists/#list-gists
func (s *GistsService) ListAll(ctx context.Context, opt *GistListOptions) ([]*Gist, *Response, error) {
u, err := addOptions("gists/public", opt)
if err != nil {
return nil, nil, err
@ -134,7 +135,7 @@ func (s *GistsService) ListAll(opt *GistListOptions) ([]*Gist, *Response, error)
}
var gists []*Gist
resp, err := s.client.Do(req, &gists)
resp, err := s.client.Do(ctx, req, &gists)
if err != nil {
return nil, resp, err
}
@ -144,8 +145,8 @@ func (s *GistsService) ListAll(opt *GistListOptions) ([]*Gist, *Response, error)
// ListStarred lists starred gists of authenticated user.
//
// GitHub API docs: http://developer.github.com/v3/gists/#list-gists
func (s *GistsService) ListStarred(opt *GistListOptions) ([]*Gist, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/gists/#list-gists
func (s *GistsService) ListStarred(ctx context.Context, opt *GistListOptions) ([]*Gist, *Response, error) {
u, err := addOptions("gists/starred", opt)
if err != nil {
return nil, nil, err
@ -157,7 +158,7 @@ func (s *GistsService) ListStarred(opt *GistListOptions) ([]*Gist, *Response, er
}
var gists []*Gist
resp, err := s.client.Do(req, &gists)
resp, err := s.client.Do(ctx, req, &gists)
if err != nil {
return nil, resp, err
}
@ -167,15 +168,15 @@ func (s *GistsService) ListStarred(opt *GistListOptions) ([]*Gist, *Response, er
// Get a single gist.
//
// GitHub API docs: http://developer.github.com/v3/gists/#get-a-single-gist
func (s *GistsService) Get(id string) (*Gist, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/gists/#get-a-single-gist
func (s *GistsService) Get(ctx context.Context, id string) (*Gist, *Response, error) {
u := fmt.Sprintf("gists/%v", id)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
gist := new(Gist)
resp, err := s.client.Do(req, gist)
resp, err := s.client.Do(ctx, req, gist)
if err != nil {
return nil, resp, err
}
@ -186,14 +187,14 @@ func (s *GistsService) Get(id string) (*Gist, *Response, error) {
// GetRevision gets a specific revision of a gist.
//
// GitHub API docs: https://developer.github.com/v3/gists/#get-a-specific-revision-of-a-gist
func (s *GistsService) GetRevision(id, sha string) (*Gist, *Response, error) {
func (s *GistsService) GetRevision(ctx context.Context, id, sha string) (*Gist, *Response, error) {
u := fmt.Sprintf("gists/%v/%v", id, sha)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
gist := new(Gist)
resp, err := s.client.Do(req, gist)
resp, err := s.client.Do(ctx, req, gist)
if err != nil {
return nil, resp, err
}
@ -203,15 +204,15 @@ func (s *GistsService) GetRevision(id, sha string) (*Gist, *Response, error) {
// Create a gist for authenticated user.
//
// GitHub API docs: http://developer.github.com/v3/gists/#create-a-gist
func (s *GistsService) Create(gist *Gist) (*Gist, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/gists/#create-a-gist
func (s *GistsService) Create(ctx context.Context, gist *Gist) (*Gist, *Response, error) {
u := "gists"
req, err := s.client.NewRequest("POST", u, gist)
if err != nil {
return nil, nil, err
}
g := new(Gist)
resp, err := s.client.Do(req, g)
resp, err := s.client.Do(ctx, req, g)
if err != nil {
return nil, resp, err
}
@ -221,15 +222,15 @@ func (s *GistsService) Create(gist *Gist) (*Gist, *Response, error) {
// Edit a gist.
//
// GitHub API docs: http://developer.github.com/v3/gists/#edit-a-gist
func (s *GistsService) Edit(id string, gist *Gist) (*Gist, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/gists/#edit-a-gist
func (s *GistsService) Edit(ctx context.Context, id string, gist *Gist) (*Gist, *Response, error) {
u := fmt.Sprintf("gists/%v", id)
req, err := s.client.NewRequest("PATCH", u, gist)
if err != nil {
return nil, nil, err
}
g := new(Gist)
resp, err := s.client.Do(req, g)
resp, err := s.client.Do(ctx, req, g)
if err != nil {
return nil, resp, err
}
@ -239,8 +240,8 @@ func (s *GistsService) Edit(id string, gist *Gist) (*Gist, *Response, error) {
// ListCommits lists commits of a gist.
//
// Github API docs: https://developer.github.com/v3/gists/#list-gist-commits
func (s *GistsService) ListCommits(id string) ([]*GistCommit, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/gists/#list-gist-commits
func (s *GistsService) ListCommits(ctx context.Context, id string) ([]*GistCommit, *Response, error) {
u := fmt.Sprintf("gists/%v/commits", id)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
@ -248,7 +249,7 @@ func (s *GistsService) ListCommits(id string) ([]*GistCommit, *Response, error)
}
var gistCommits []*GistCommit
resp, err := s.client.Do(req, &gistCommits)
resp, err := s.client.Do(ctx, req, &gistCommits)
if err != nil {
return nil, resp, err
}
@ -258,58 +259,58 @@ func (s *GistsService) ListCommits(id string) ([]*GistCommit, *Response, error)
// Delete a gist.
//
// GitHub API docs: http://developer.github.com/v3/gists/#delete-a-gist
func (s *GistsService) Delete(id string) (*Response, error) {
// GitHub API docs: https://developer.github.com/v3/gists/#delete-a-gist
func (s *GistsService) Delete(ctx context.Context, id string) (*Response, error) {
u := fmt.Sprintf("gists/%v", id)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}
// Star a gist on behalf of authenticated user.
//
// GitHub API docs: http://developer.github.com/v3/gists/#star-a-gist
func (s *GistsService) Star(id string) (*Response, error) {
// GitHub API docs: https://developer.github.com/v3/gists/#star-a-gist
func (s *GistsService) Star(ctx context.Context, id string) (*Response, error) {
u := fmt.Sprintf("gists/%v/star", id)
req, err := s.client.NewRequest("PUT", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}
// Unstar a gist on a behalf of authenticated user.
//
// Github API docs: http://developer.github.com/v3/gists/#unstar-a-gist
func (s *GistsService) Unstar(id string) (*Response, error) {
// GitHub API docs: https://developer.github.com/v3/gists/#unstar-a-gist
func (s *GistsService) Unstar(ctx context.Context, id string) (*Response, error) {
u := fmt.Sprintf("gists/%v/star", id)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}
// IsStarred checks if a gist is starred by authenticated user.
//
// GitHub API docs: http://developer.github.com/v3/gists/#check-if-a-gist-is-starred
func (s *GistsService) IsStarred(id string) (bool, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/gists/#check-if-a-gist-is-starred
func (s *GistsService) IsStarred(ctx context.Context, id string) (bool, *Response, error) {
u := fmt.Sprintf("gists/%v/star", id)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return false, nil, err
}
resp, err := s.client.Do(req, nil)
resp, err := s.client.Do(ctx, req, nil)
starred, err := parseBoolResponse(err)
return starred, resp, err
}
// Fork a gist.
//
// GitHub API docs: http://developer.github.com/v3/gists/#fork-a-gist
func (s *GistsService) Fork(id string) (*Gist, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/gists/#fork-a-gist
func (s *GistsService) Fork(ctx context.Context, id string) (*Gist, *Response, error) {
u := fmt.Sprintf("gists/%v/forks", id)
req, err := s.client.NewRequest("POST", u, nil)
if err != nil {
@ -317,7 +318,7 @@ func (s *GistsService) Fork(id string) (*Gist, *Response, error) {
}
g := new(Gist)
resp, err := s.client.Do(req, g)
resp, err := s.client.Do(ctx, req, g)
if err != nil {
return nil, resp, err
}
@ -327,8 +328,8 @@ func (s *GistsService) Fork(id string) (*Gist, *Response, error) {
// ListForks lists forks of a gist.
//
// Github API docs: https://developer.github.com/v3/gists/#list-gist-forks
func (s *GistsService) ListForks(id string) ([]*GistFork, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/gists/#list-gist-forks
func (s *GistsService) ListForks(ctx context.Context, id string) ([]*GistFork, *Response, error) {
u := fmt.Sprintf("gists/%v/forks", id)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
@ -336,7 +337,7 @@ func (s *GistsService) ListForks(id string) ([]*GistFork, *Response, error) {
}
var gistForks []*GistFork
resp, err := s.client.Do(req, &gistForks)
resp, err := s.client.Do(ctx, req, &gistForks)
if err != nil {
return nil, resp, err
}

View file

@ -6,6 +6,7 @@
package github
import (
"context"
"fmt"
"time"
)
@ -25,8 +26,8 @@ func (g GistComment) String() string {
// ListComments lists all comments for a gist.
//
// GitHub API docs: http://developer.github.com/v3/gists/comments/#list-comments-on-a-gist
func (s *GistsService) ListComments(gistID string, opt *ListOptions) ([]*GistComment, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/gists/comments/#list-comments-on-a-gist
func (s *GistsService) ListComments(ctx context.Context, gistID string, opt *ListOptions) ([]*GistComment, *Response, error) {
u := fmt.Sprintf("gists/%v/comments", gistID)
u, err := addOptions(u, opt)
if err != nil {
@ -39,7 +40,7 @@ func (s *GistsService) ListComments(gistID string, opt *ListOptions) ([]*GistCom
}
var comments []*GistComment
resp, err := s.client.Do(req, &comments)
resp, err := s.client.Do(ctx, req, &comments)
if err != nil {
return nil, resp, err
}
@ -49,8 +50,8 @@ func (s *GistsService) ListComments(gistID string, opt *ListOptions) ([]*GistCom
// GetComment retrieves a single comment from a gist.
//
// GitHub API docs: http://developer.github.com/v3/gists/comments/#get-a-single-comment
func (s *GistsService) GetComment(gistID string, commentID int) (*GistComment, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/gists/comments/#get-a-single-comment
func (s *GistsService) GetComment(ctx context.Context, gistID string, commentID int) (*GistComment, *Response, error) {
u := fmt.Sprintf("gists/%v/comments/%v", gistID, commentID)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
@ -58,7 +59,7 @@ func (s *GistsService) GetComment(gistID string, commentID int) (*GistComment, *
}
c := new(GistComment)
resp, err := s.client.Do(req, c)
resp, err := s.client.Do(ctx, req, c)
if err != nil {
return nil, resp, err
}
@ -68,8 +69,8 @@ func (s *GistsService) GetComment(gistID string, commentID int) (*GistComment, *
// CreateComment creates a comment for a gist.
//
// GitHub API docs: http://developer.github.com/v3/gists/comments/#create-a-comment
func (s *GistsService) CreateComment(gistID string, comment *GistComment) (*GistComment, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/gists/comments/#create-a-comment
func (s *GistsService) CreateComment(ctx context.Context, gistID string, comment *GistComment) (*GistComment, *Response, error) {
u := fmt.Sprintf("gists/%v/comments", gistID)
req, err := s.client.NewRequest("POST", u, comment)
if err != nil {
@ -77,7 +78,7 @@ func (s *GistsService) CreateComment(gistID string, comment *GistComment) (*Gist
}
c := new(GistComment)
resp, err := s.client.Do(req, c)
resp, err := s.client.Do(ctx, req, c)
if err != nil {
return nil, resp, err
}
@ -87,8 +88,8 @@ func (s *GistsService) CreateComment(gistID string, comment *GistComment) (*Gist
// EditComment edits an existing gist comment.
//
// GitHub API docs: http://developer.github.com/v3/gists/comments/#edit-a-comment
func (s *GistsService) EditComment(gistID string, commentID int, comment *GistComment) (*GistComment, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/gists/comments/#edit-a-comment
func (s *GistsService) EditComment(ctx context.Context, gistID string, commentID int, comment *GistComment) (*GistComment, *Response, error) {
u := fmt.Sprintf("gists/%v/comments/%v", gistID, commentID)
req, err := s.client.NewRequest("PATCH", u, comment)
if err != nil {
@ -96,7 +97,7 @@ func (s *GistsService) EditComment(gistID string, commentID int, comment *GistCo
}
c := new(GistComment)
resp, err := s.client.Do(req, c)
resp, err := s.client.Do(ctx, req, c)
if err != nil {
return nil, resp, err
}
@ -106,13 +107,13 @@ func (s *GistsService) EditComment(gistID string, commentID int, comment *GistCo
// DeleteComment deletes a gist comment.
//
// GitHub API docs: http://developer.github.com/v3/gists/comments/#delete-a-comment
func (s *GistsService) DeleteComment(gistID string, commentID int) (*Response, error) {
// GitHub API docs: https://developer.github.com/v3/gists/comments/#delete-a-comment
func (s *GistsService) DeleteComment(ctx context.Context, gistID string, commentID int) (*Response, error) {
u := fmt.Sprintf("gists/%v/comments/%v", gistID, commentID)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}

View file

@ -8,5 +8,5 @@ package github
// GitService handles communication with the git data related
// methods of the GitHub API.
//
// GitHub API docs: http://developer.github.com/v3/git/
// GitHub API docs: https://developer.github.com/v3/git/
type GitService service

View file

@ -5,7 +5,10 @@
package github
import "fmt"
import (
"context"
"fmt"
)
// Blob represents a blob object.
type Blob struct {
@ -18,8 +21,8 @@ type Blob struct {
// GetBlob fetchs a blob from a repo given a SHA.
//
// GitHub API docs: http://developer.github.com/v3/git/blobs/#get-a-blob
func (s *GitService) GetBlob(owner string, repo string, sha string) (*Blob, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/git/blobs/#get-a-blob
func (s *GitService) GetBlob(ctx context.Context, owner string, repo string, sha string) (*Blob, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/git/blobs/%v", owner, repo, sha)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
@ -27,14 +30,14 @@ func (s *GitService) GetBlob(owner string, repo string, sha string) (*Blob, *Res
}
blob := new(Blob)
resp, err := s.client.Do(req, blob)
resp, err := s.client.Do(ctx, req, blob)
return blob, resp, err
}
// CreateBlob creates a blob object.
//
// GitHub API docs: https://developer.github.com/v3/git/blobs/#create-a-blob
func (s *GitService) CreateBlob(owner string, repo string, blob *Blob) (*Blob, *Response, error) {
func (s *GitService) CreateBlob(ctx context.Context, owner string, repo string, blob *Blob) (*Blob, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/git/blobs", owner, repo)
req, err := s.client.NewRequest("POST", u, blob)
if err != nil {
@ -42,6 +45,6 @@ func (s *GitService) CreateBlob(owner string, repo string, blob *Blob) (*Blob, *
}
t := new(Blob)
resp, err := s.client.Do(req, t)
resp, err := s.client.Do(ctx, req, t)
return t, resp, err
}

View file

@ -6,6 +6,7 @@
package github
import (
"context"
"fmt"
"time"
)
@ -57,8 +58,8 @@ func (c CommitAuthor) String() string {
// GetCommit fetchs the Commit object for a given SHA.
//
// GitHub API docs: http://developer.github.com/v3/git/commits/#get-a-commit
func (s *GitService) GetCommit(owner string, repo string, sha string) (*Commit, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/git/commits/#get-a-commit
func (s *GitService) GetCommit(ctx context.Context, owner string, repo string, sha string) (*Commit, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/git/commits/%v", owner, repo, sha)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
@ -69,7 +70,7 @@ func (s *GitService) GetCommit(owner string, repo string, sha string) (*Commit,
req.Header.Set("Accept", mediaTypeGitSigningPreview)
c := new(Commit)
resp, err := s.client.Do(req, c)
resp, err := s.client.Do(ctx, req, c)
if err != nil {
return nil, resp, err
}
@ -92,8 +93,8 @@ type createCommit struct {
// data if omitted. If the commit.Author is omitted, it will be filled in with
// the authenticated users information and the current date.
//
// GitHub API docs: http://developer.github.com/v3/git/commits/#create-a-commit
func (s *GitService) CreateCommit(owner string, repo string, commit *Commit) (*Commit, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/git/commits/#create-a-commit
func (s *GitService) CreateCommit(ctx context.Context, owner string, repo string, commit *Commit) (*Commit, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/git/commits", owner, repo)
body := &createCommit{}
@ -118,7 +119,7 @@ func (s *GitService) CreateCommit(owner string, repo string, commit *Commit) (*C
}
c := new(Commit)
resp, err := s.client.Do(req, c)
resp, err := s.client.Do(ctx, req, c)
if err != nil {
return nil, resp, err
}

View file

@ -6,6 +6,7 @@
package github
import (
"context"
"fmt"
"strings"
)
@ -46,8 +47,8 @@ type updateRefRequest struct {
// GetRef fetches the Reference object for a given Git ref.
//
// GitHub API docs: http://developer.github.com/v3/git/refs/#get-a-reference
func (s *GitService) GetRef(owner string, repo string, ref string) (*Reference, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/git/refs/#get-a-reference
func (s *GitService) GetRef(ctx context.Context, owner string, repo string, ref string) (*Reference, *Response, error) {
ref = strings.TrimPrefix(ref, "refs/")
u := fmt.Sprintf("repos/%v/%v/git/refs/%v", owner, repo, ref)
req, err := s.client.NewRequest("GET", u, nil)
@ -56,7 +57,7 @@ func (s *GitService) GetRef(owner string, repo string, ref string) (*Reference,
}
r := new(Reference)
resp, err := s.client.Do(req, r)
resp, err := s.client.Do(ctx, req, r)
if err != nil {
return nil, resp, err
}
@ -74,8 +75,8 @@ type ReferenceListOptions struct {
// ListRefs lists all refs in a repository.
//
// GitHub API docs: http://developer.github.com/v3/git/refs/#get-all-references
func (s *GitService) ListRefs(owner, repo string, opt *ReferenceListOptions) ([]*Reference, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/git/refs/#get-all-references
func (s *GitService) ListRefs(ctx context.Context, owner, repo string, opt *ReferenceListOptions) ([]*Reference, *Response, error) {
var u string
if opt != nil && opt.Type != "" {
u = fmt.Sprintf("repos/%v/%v/git/refs/%v", owner, repo, opt.Type)
@ -93,7 +94,7 @@ func (s *GitService) ListRefs(owner, repo string, opt *ReferenceListOptions) ([]
}
var rs []*Reference
resp, err := s.client.Do(req, &rs)
resp, err := s.client.Do(ctx, req, &rs)
if err != nil {
return nil, resp, err
}
@ -103,8 +104,8 @@ func (s *GitService) ListRefs(owner, repo string, opt *ReferenceListOptions) ([]
// CreateRef creates a new ref in a repository.
//
// GitHub API docs: http://developer.github.com/v3/git/refs/#create-a-reference
func (s *GitService) CreateRef(owner string, repo string, ref *Reference) (*Reference, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/git/refs/#create-a-reference
func (s *GitService) CreateRef(ctx context.Context, owner string, repo string, ref *Reference) (*Reference, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/git/refs", owner, repo)
req, err := s.client.NewRequest("POST", u, &createRefRequest{
// back-compat with previous behavior that didn't require 'refs/' prefix
@ -116,7 +117,7 @@ func (s *GitService) CreateRef(owner string, repo string, ref *Reference) (*Refe
}
r := new(Reference)
resp, err := s.client.Do(req, r)
resp, err := s.client.Do(ctx, req, r)
if err != nil {
return nil, resp, err
}
@ -126,8 +127,8 @@ func (s *GitService) CreateRef(owner string, repo string, ref *Reference) (*Refe
// UpdateRef updates an existing ref in a repository.
//
// GitHub API docs: http://developer.github.com/v3/git/refs/#update-a-reference
func (s *GitService) UpdateRef(owner string, repo string, ref *Reference, force bool) (*Reference, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/git/refs/#update-a-reference
func (s *GitService) UpdateRef(ctx context.Context, owner string, repo string, ref *Reference, force bool) (*Reference, *Response, error) {
refPath := strings.TrimPrefix(*ref.Ref, "refs/")
u := fmt.Sprintf("repos/%v/%v/git/refs/%v", owner, repo, refPath)
req, err := s.client.NewRequest("PATCH", u, &updateRefRequest{
@ -139,7 +140,7 @@ func (s *GitService) UpdateRef(owner string, repo string, ref *Reference, force
}
r := new(Reference)
resp, err := s.client.Do(req, r)
resp, err := s.client.Do(ctx, req, r)
if err != nil {
return nil, resp, err
}
@ -149,8 +150,8 @@ func (s *GitService) UpdateRef(owner string, repo string, ref *Reference, force
// DeleteRef deletes a ref from a repository.
//
// GitHub API docs: http://developer.github.com/v3/git/refs/#delete-a-reference
func (s *GitService) DeleteRef(owner string, repo string, ref string) (*Response, error) {
// GitHub API docs: https://developer.github.com/v3/git/refs/#delete-a-reference
func (s *GitService) DeleteRef(ctx context.Context, owner string, repo string, ref string) (*Response, error) {
ref = strings.TrimPrefix(ref, "refs/")
u := fmt.Sprintf("repos/%v/%v/git/refs/%v", owner, repo, ref)
req, err := s.client.NewRequest("DELETE", u, nil)
@ -158,5 +159,5 @@ func (s *GitService) DeleteRef(owner string, repo string, ref string) (*Response
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}

View file

@ -6,6 +6,7 @@
package github
import (
"context"
"fmt"
)
@ -33,8 +34,8 @@ type createTagRequest struct {
// GetTag fetchs a tag from a repo given a SHA.
//
// GitHub API docs: http://developer.github.com/v3/git/tags/#get-a-tag
func (s *GitService) GetTag(owner string, repo string, sha string) (*Tag, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/git/tags/#get-a-tag
func (s *GitService) GetTag(ctx context.Context, owner string, repo string, sha string) (*Tag, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/git/tags/%v", owner, repo, sha)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
@ -45,14 +46,14 @@ func (s *GitService) GetTag(owner string, repo string, sha string) (*Tag, *Respo
req.Header.Set("Accept", mediaTypeGitSigningPreview)
tag := new(Tag)
resp, err := s.client.Do(req, tag)
resp, err := s.client.Do(ctx, req, tag)
return tag, resp, err
}
// CreateTag creates a tag object.
//
// GitHub API docs: http://developer.github.com/v3/git/tags/#create-a-tag-object
func (s *GitService) CreateTag(owner string, repo string, tag *Tag) (*Tag, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/git/tags/#create-a-tag-object
func (s *GitService) CreateTag(ctx context.Context, owner string, repo string, tag *Tag) (*Tag, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/git/tags", owner, repo)
// convert Tag into a createTagRequest
@ -72,6 +73,6 @@ func (s *GitService) CreateTag(owner string, repo string, tag *Tag) (*Tag, *Resp
}
t := new(Tag)
resp, err := s.client.Do(req, t)
resp, err := s.client.Do(ctx, req, t)
return t, resp, err
}

View file

@ -5,7 +5,10 @@
package github
import "fmt"
import (
"context"
"fmt"
)
// Tree represents a GitHub tree.
type Tree struct {
@ -35,8 +38,8 @@ func (t TreeEntry) String() string {
// GetTree fetches the Tree object for a given sha hash from a repository.
//
// GitHub API docs: http://developer.github.com/v3/git/trees/#get-a-tree
func (s *GitService) GetTree(owner string, repo string, sha string, recursive bool) (*Tree, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/git/trees/#get-a-tree
func (s *GitService) GetTree(ctx context.Context, owner string, repo string, sha string, recursive bool) (*Tree, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/git/trees/%v", owner, repo, sha)
if recursive {
u += "?recursive=1"
@ -48,7 +51,7 @@ func (s *GitService) GetTree(owner string, repo string, sha string, recursive bo
}
t := new(Tree)
resp, err := s.client.Do(req, t)
resp, err := s.client.Do(ctx, req, t)
if err != nil {
return nil, resp, err
}
@ -66,8 +69,8 @@ type createTree struct {
// path modifying that tree are specified, it will overwrite the contents of
// that tree with the new path contents and write a new tree out.
//
// GitHub API docs: http://developer.github.com/v3/git/trees/#create-a-tree
func (s *GitService) CreateTree(owner string, repo string, baseTree string, entries []TreeEntry) (*Tree, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/git/trees/#create-a-tree
func (s *GitService) CreateTree(ctx context.Context, owner string, repo string, baseTree string, entries []TreeEntry) (*Tree, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/git/trees", owner, repo)
body := &createTree{
@ -80,7 +83,7 @@ func (s *GitService) CreateTree(owner string, repo string, baseTree string, entr
}
t := new(Tree)
resp, err := s.client.Do(req, t)
resp, err := s.client.Do(ctx, req, t)
if err != nil {
return nil, resp, err
}

View file

@ -7,6 +7,7 @@ package github
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
@ -24,12 +25,7 @@ import (
)
const (
// StatusUnprocessableEntity is the status code returned when sending a request with invalid fields.
StatusUnprocessableEntity = 422
)
const (
libraryVersion = "2"
libraryVersion = "3"
defaultBaseURL = "https://api.github.com/"
uploadBaseURL = "https://uploads.github.com/"
userAgent = "go-github/" + libraryVersion
@ -119,7 +115,6 @@ type Client struct {
rateMu sync.Mutex
rateLimits [categories]Rate // Rate limits for the client as determined by the most recent API calls.
mostRecent rateLimitCategory
common service // Reuse a single struct instead of allocating one for each service on the heap.
@ -383,26 +378,18 @@ func parseRate(r *http.Response) Rate {
return rate
}
// Rate specifies the current rate limit for the client as determined by the
// most recent API call. If the client is used in a multi-user application,
// this rate may not always be up-to-date.
//
// Deprecated: Use the Response.Rate returned from most recent API call instead.
// Call RateLimits() to check the current rate.
func (c *Client) Rate() Rate {
c.rateMu.Lock()
rate := c.rateLimits[c.mostRecent]
c.rateMu.Unlock()
return rate
}
// Do sends an API request and returns the API response. The API response is
// JSON decoded and stored in the value pointed to by v, or returned as an
// error if an API error has occurred. If v implements the io.Writer
// interface, the raw response body will be written to v, without attempting to
// first decode it. If rate limit is exceeded and reset time is in the future,
// Do returns *RateLimitError immediately without making a network API call.
func (c *Client) Do(req *http.Request, v interface{}) (*Response, error) {
//
// The provided ctx must be non-nil. If it is canceled or times out,
// ctx.Err() will be returned.
func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*Response, error) {
req = req.WithContext(ctx)
rateLimitCategory := category(req.URL.Path)
// If we've hit rate limit, don't make further requests before Reset time.
@ -412,12 +399,22 @@ func (c *Client) Do(req *http.Request, v interface{}) (*Response, error) {
resp, err := c.client.Do(req)
if err != nil {
// If we got an error, and the context has been canceled,
// the context's error is probably more useful.
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
// If the error type is *url.Error, sanitize its URL before returning.
if e, ok := err.(*url.Error); ok {
if url, err := url.Parse(e.URL); err == nil {
e.URL = sanitizeURL(url).String()
return nil, e
}
}
return nil, err
}
@ -431,7 +428,6 @@ func (c *Client) Do(req *http.Request, v interface{}) (*Response, error) {
c.rateMu.Lock()
c.rateLimits[rateLimitCategory] = response.Rate
c.mostRecent = rateLimitCategory
c.rateMu.Unlock()
err = CheckResponse(resp)
@ -485,7 +481,7 @@ func (c *Client) checkRateLimitBeforeDo(req *http.Request, rateLimitCategory rat
/*
An ErrorResponse reports one or more errors caused by an API request.
GitHub API docs: http://developer.github.com/v3/#client-errors
GitHub API docs: https://developer.github.com/v3/#client-errors
*/
type ErrorResponse struct {
Response *http.Response // HTTP response that caused this error
@ -591,7 +587,7 @@ These are the possible validation error codes:
some resources return this (e.g. github.User.CreateKey()), additional
information is set in the Message field of the Error
GitHub API docs: http://developer.github.com/v3/#client-errors
GitHub API docs: https://developer.github.com/v3/#client-errors
*/
type Error struct {
Resource string `json:"resource"` // resource on which the error occurred
@ -730,22 +726,8 @@ func category(path string) rateLimitCategory {
}
}
// RateLimit returns the core rate limit for the current client.
//
// Deprecated: RateLimit is deprecated, use RateLimits instead.
func (c *Client) RateLimit() (*Rate, *Response, error) {
limits, resp, err := c.RateLimits()
if err != nil {
return nil, resp, err
}
if limits == nil {
return nil, resp, errors.New("RateLimits returned nil limits and error; unable to extract Core rate limit")
}
return limits.Core, resp, nil
}
// RateLimits returns the rate limits for the current client.
func (c *Client) RateLimits() (*RateLimits, *Response, error) {
func (c *Client) RateLimits(ctx context.Context) (*RateLimits, *Response, error) {
req, err := c.NewRequest("GET", "rate_limit", nil)
if err != nil {
return nil, nil, err
@ -754,7 +736,7 @@ func (c *Client) RateLimits() (*RateLimits, *Response, error) {
response := new(struct {
Resources *RateLimits `json:"resources"`
})
resp, err := c.Do(req, response)
resp, err := c.Do(ctx, req, response)
if err != nil {
return nil, nil, err
}
@ -786,7 +768,7 @@ that need to use a higher rate limit associated with your OAuth application.
This will append the querystring params client_id=xxx&client_secret=yyy to all
requests.
See http://developer.github.com/v3/#unauthenticated-rate-limited-requests for
See https://developer.github.com/v3/#unauthenticated-rate-limited-requests for
more information.
*/
type UnauthenticatedRateLimitedTransport struct {

View file

@ -5,12 +5,15 @@
package github
import "fmt"
import (
"context"
"fmt"
)
// GitignoresService provides access to the gitignore related functions in the
// GitHub API.
//
// GitHub API docs: http://developer.github.com/v3/gitignore/
// GitHub API docs: https://developer.github.com/v3/gitignore/
type GitignoresService service
// Gitignore represents a .gitignore file as returned by the GitHub API.
@ -25,15 +28,15 @@ func (g Gitignore) String() string {
// List all available Gitignore templates.
//
// http://developer.github.com/v3/gitignore/#listing-available-templates
func (s GitignoresService) List() ([]string, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/gitignore/#listing-available-templates
func (s GitignoresService) List(ctx context.Context) ([]string, *Response, error) {
req, err := s.client.NewRequest("GET", "gitignore/templates", nil)
if err != nil {
return nil, nil, err
}
var availableTemplates []string
resp, err := s.client.Do(req, &availableTemplates)
resp, err := s.client.Do(ctx, req, &availableTemplates)
if err != nil {
return nil, resp, err
}
@ -43,8 +46,8 @@ func (s GitignoresService) List() ([]string, *Response, error) {
// Get a Gitignore by name.
//
// http://developer.github.com/v3/gitignore/#get-a-single-template
func (s GitignoresService) Get(name string) (*Gitignore, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/gitignore/#get-a-single-template
func (s GitignoresService) Get(ctx context.Context, name string) (*Gitignore, *Response, error) {
u := fmt.Sprintf("gitignore/templates/%v", name)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
@ -52,7 +55,7 @@ func (s GitignoresService) Get(name string) (*Gitignore, *Response, error) {
}
gitignore := new(Gitignore)
resp, err := s.client.Do(req, gitignore)
resp, err := s.client.Do(ctx, req, gitignore)
if err != nil {
return nil, resp, err
}

View file

@ -5,6 +5,8 @@
package github
import "context"
// IntegrationsService provides access to the installation related functions
// in the GitHub API.
//
@ -14,7 +16,7 @@ type IntegrationsService service
// ListInstallations lists the installations that the current integration has.
//
// GitHub API docs: https://developer.github.com/v3/integrations/#find-installations
func (s *IntegrationsService) ListInstallations(opt *ListOptions) ([]*Installation, *Response, error) {
func (s *IntegrationsService) ListInstallations(ctx context.Context, opt *ListOptions) ([]*Installation, *Response, error) {
u, err := addOptions("integration/installations", opt)
if err != nil {
return nil, nil, err
@ -29,7 +31,7 @@ func (s *IntegrationsService) ListInstallations(opt *ListOptions) ([]*Installati
req.Header.Set("Accept", mediaTypeIntegrationPreview)
var i []*Installation
resp, err := s.client.Do(req, &i)
resp, err := s.client.Do(ctx, req, &i)
if err != nil {
return nil, resp, err
}

View file

@ -5,6 +5,8 @@
package github
import "context"
// Installation represents a GitHub integration installation.
type Installation struct {
ID *int `json:"id,omitempty"`
@ -20,7 +22,7 @@ func (i Installation) String() string {
// ListRepos lists the repositories that the current installation has access to.
//
// GitHub API docs: https://developer.github.com/v3/integrations/installations/#list-repositories
func (s *IntegrationsService) ListRepos(opt *ListOptions) ([]*Repository, *Response, error) {
func (s *IntegrationsService) ListRepos(ctx context.Context, opt *ListOptions) ([]*Repository, *Response, error) {
u, err := addOptions("installation/repositories", opt)
if err != nil {
return nil, nil, err
@ -37,7 +39,7 @@ func (s *IntegrationsService) ListRepos(opt *ListOptions) ([]*Repository, *Respo
var r struct {
Repositories []*Repository `json:"repositories"`
}
resp, err := s.client.Do(req, &r)
resp, err := s.client.Do(ctx, req, &r)
if err != nil {
return nil, resp, err
}

View file

@ -6,6 +6,7 @@
package github
import (
"context"
"fmt"
"time"
)
@ -13,7 +14,7 @@ import (
// IssuesService handles communication with the issue related
// methods of the GitHub API.
//
// GitHub API docs: http://developer.github.com/v3/issues/
// GitHub API docs: https://developer.github.com/v3/issues/
type IssuesService service
// Issue represents a GitHub issue on a repository.
@ -107,27 +108,27 @@ type PullRequestLinks struct {
// organization repositories; if false, list only owned and member
// repositories.
//
// GitHub API docs: http://developer.github.com/v3/issues/#list-issues
func (s *IssuesService) List(all bool, opt *IssueListOptions) ([]*Issue, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/issues/#list-issues
func (s *IssuesService) List(ctx context.Context, all bool, opt *IssueListOptions) ([]*Issue, *Response, error) {
var u string
if all {
u = "issues"
} else {
u = "user/issues"
}
return s.listIssues(u, opt)
return s.listIssues(ctx, u, opt)
}
// ListByOrg fetches the issues in the specified organization for the
// authenticated user.
//
// GitHub API docs: http://developer.github.com/v3/issues/#list-issues
func (s *IssuesService) ListByOrg(org string, opt *IssueListOptions) ([]*Issue, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/issues/#list-issues
func (s *IssuesService) ListByOrg(ctx context.Context, org string, opt *IssueListOptions) ([]*Issue, *Response, error) {
u := fmt.Sprintf("orgs/%v/issues", org)
return s.listIssues(u, opt)
return s.listIssues(ctx, u, opt)
}
func (s *IssuesService) listIssues(u string, opt *IssueListOptions) ([]*Issue, *Response, error) {
func (s *IssuesService) listIssues(ctx context.Context, u string, opt *IssueListOptions) ([]*Issue, *Response, error) {
u, err := addOptions(u, opt)
if err != nil {
return nil, nil, err
@ -142,7 +143,7 @@ func (s *IssuesService) listIssues(u string, opt *IssueListOptions) ([]*Issue, *
req.Header.Set("Accept", mediaTypeReactionsPreview)
var issues []*Issue
resp, err := s.client.Do(req, &issues)
resp, err := s.client.Do(ctx, req, &issues)
if err != nil {
return nil, resp, err
}
@ -192,8 +193,8 @@ type IssueListByRepoOptions struct {
// ListByRepo lists the issues for the specified repository.
//
// GitHub API docs: http://developer.github.com/v3/issues/#list-issues-for-a-repository
func (s *IssuesService) ListByRepo(owner string, repo string, opt *IssueListByRepoOptions) ([]*Issue, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/issues/#list-issues-for-a-repository
func (s *IssuesService) ListByRepo(ctx context.Context, owner string, repo string, opt *IssueListByRepoOptions) ([]*Issue, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/issues", owner, repo)
u, err := addOptions(u, opt)
if err != nil {
@ -209,7 +210,7 @@ func (s *IssuesService) ListByRepo(owner string, repo string, opt *IssueListByRe
req.Header.Set("Accept", mediaTypeReactionsPreview)
var issues []*Issue
resp, err := s.client.Do(req, &issues)
resp, err := s.client.Do(ctx, req, &issues)
if err != nil {
return nil, resp, err
}
@ -219,8 +220,8 @@ func (s *IssuesService) ListByRepo(owner string, repo string, opt *IssueListByRe
// Get a single issue.
//
// GitHub API docs: http://developer.github.com/v3/issues/#get-a-single-issue
func (s *IssuesService) Get(owner string, repo string, number int) (*Issue, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/issues/#get-a-single-issue
func (s *IssuesService) Get(ctx context.Context, owner string, repo string, number int) (*Issue, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/issues/%d", owner, repo, number)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
@ -231,7 +232,7 @@ func (s *IssuesService) Get(owner string, repo string, number int) (*Issue, *Res
req.Header.Set("Accept", mediaTypeReactionsPreview)
issue := new(Issue)
resp, err := s.client.Do(req, issue)
resp, err := s.client.Do(ctx, req, issue)
if err != nil {
return nil, resp, err
}
@ -241,8 +242,8 @@ func (s *IssuesService) Get(owner string, repo string, number int) (*Issue, *Res
// Create a new issue on the specified repository.
//
// GitHub API docs: http://developer.github.com/v3/issues/#create-an-issue
func (s *IssuesService) Create(owner string, repo string, issue *IssueRequest) (*Issue, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/issues/#create-an-issue
func (s *IssuesService) Create(ctx context.Context, owner string, repo string, issue *IssueRequest) (*Issue, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/issues", owner, repo)
req, err := s.client.NewRequest("POST", u, issue)
if err != nil {
@ -250,7 +251,7 @@ func (s *IssuesService) Create(owner string, repo string, issue *IssueRequest) (
}
i := new(Issue)
resp, err := s.client.Do(req, i)
resp, err := s.client.Do(ctx, req, i)
if err != nil {
return nil, resp, err
}
@ -260,8 +261,8 @@ func (s *IssuesService) Create(owner string, repo string, issue *IssueRequest) (
// Edit an issue.
//
// GitHub API docs: http://developer.github.com/v3/issues/#edit-an-issue
func (s *IssuesService) Edit(owner string, repo string, number int, issue *IssueRequest) (*Issue, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/issues/#edit-an-issue
func (s *IssuesService) Edit(ctx context.Context, owner string, repo string, number int, issue *IssueRequest) (*Issue, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/issues/%d", owner, repo, number)
req, err := s.client.NewRequest("PATCH", u, issue)
if err != nil {
@ -269,7 +270,7 @@ func (s *IssuesService) Edit(owner string, repo string, number int, issue *Issue
}
i := new(Issue)
resp, err := s.client.Do(req, i)
resp, err := s.client.Do(ctx, req, i)
if err != nil {
return nil, resp, err
}
@ -280,25 +281,25 @@ func (s *IssuesService) Edit(owner string, repo string, number int, issue *Issue
// Lock an issue's conversation.
//
// GitHub API docs: https://developer.github.com/v3/issues/#lock-an-issue
func (s *IssuesService) Lock(owner string, repo string, number int) (*Response, error) {
func (s *IssuesService) Lock(ctx context.Context, owner string, repo string, number int) (*Response, error) {
u := fmt.Sprintf("repos/%v/%v/issues/%d/lock", owner, repo, number)
req, err := s.client.NewRequest("PUT", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}
// Unlock an issue's conversation.
//
// GitHub API docs: https://developer.github.com/v3/issues/#unlock-an-issue
func (s *IssuesService) Unlock(owner string, repo string, number int) (*Response, error) {
func (s *IssuesService) Unlock(ctx context.Context, owner string, repo string, number int) (*Response, error) {
u := fmt.Sprintf("repos/%v/%v/issues/%d/lock", owner, repo, number)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}

View file

@ -5,13 +5,16 @@
package github
import "fmt"
import (
"context"
"fmt"
)
// ListAssignees fetches all available assignees (owners and collaborators) to
// which issues may be assigned.
//
// GitHub API docs: http://developer.github.com/v3/issues/assignees/#list-assignees
func (s *IssuesService) ListAssignees(owner, repo string, opt *ListOptions) ([]*User, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/issues/assignees/#list-assignees
func (s *IssuesService) ListAssignees(ctx context.Context, owner, repo string, opt *ListOptions) ([]*User, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/assignees", owner, repo)
u, err := addOptions(u, opt)
if err != nil {
@ -23,7 +26,7 @@ func (s *IssuesService) ListAssignees(owner, repo string, opt *ListOptions) ([]*
return nil, nil, err
}
var assignees []*User
resp, err := s.client.Do(req, &assignees)
resp, err := s.client.Do(ctx, req, &assignees)
if err != nil {
return nil, resp, err
}
@ -33,14 +36,14 @@ func (s *IssuesService) ListAssignees(owner, repo string, opt *ListOptions) ([]*
// IsAssignee checks if a user is an assignee for the specified repository.
//
// GitHub API docs: http://developer.github.com/v3/issues/assignees/#check-assignee
func (s *IssuesService) IsAssignee(owner, repo, user string) (bool, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/issues/assignees/#check-assignee
func (s *IssuesService) IsAssignee(ctx context.Context, owner, repo, user string) (bool, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/assignees/%v", owner, repo, user)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return false, nil, err
}
resp, err := s.client.Do(req, nil)
resp, err := s.client.Do(ctx, req, nil)
assignee, err := parseBoolResponse(err)
return assignee, resp, err
}
@ -48,7 +51,7 @@ func (s *IssuesService) IsAssignee(owner, repo, user string) (bool, *Response, e
// AddAssignees adds the provided GitHub users as assignees to the issue.
//
// GitHub API docs: https://developer.github.com/v3/issues/assignees/#add-assignees-to-an-issue
func (s *IssuesService) AddAssignees(owner, repo string, number int, assignees []string) (*Issue, *Response, error) {
func (s *IssuesService) AddAssignees(ctx context.Context, owner, repo string, number int, assignees []string) (*Issue, *Response, error) {
users := &struct {
Assignees []string `json:"assignees,omitempty"`
}{Assignees: assignees}
@ -59,14 +62,14 @@ func (s *IssuesService) AddAssignees(owner, repo string, number int, assignees [
}
issue := &Issue{}
resp, err := s.client.Do(req, issue)
resp, err := s.client.Do(ctx, req, issue)
return issue, resp, err
}
// RemoveAssignees removes the provided GitHub users as assignees from the issue.
//
// GitHub API docs: https://developer.github.com/v3/issues/assignees/#remove-assignees-from-an-issue
func (s *IssuesService) RemoveAssignees(owner, repo string, number int, assignees []string) (*Issue, *Response, error) {
func (s *IssuesService) RemoveAssignees(ctx context.Context, owner, repo string, number int, assignees []string) (*Issue, *Response, error) {
users := &struct {
Assignees []string `json:"assignees,omitempty"`
}{Assignees: assignees}
@ -77,6 +80,6 @@ func (s *IssuesService) RemoveAssignees(owner, repo string, number int, assignee
}
issue := &Issue{}
resp, err := s.client.Do(req, issue)
resp, err := s.client.Do(ctx, req, issue)
return issue, resp, err
}

View file

@ -6,6 +6,7 @@
package github
import (
"context"
"fmt"
"time"
)
@ -45,8 +46,8 @@ type IssueListCommentsOptions struct {
// ListComments lists all comments on the specified issue. Specifying an issue
// number of 0 will return all comments on all issues for the repository.
//
// GitHub API docs: http://developer.github.com/v3/issues/comments/#list-comments-on-an-issue
func (s *IssuesService) ListComments(owner string, repo string, number int, opt *IssueListCommentsOptions) ([]*IssueComment, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/issues/comments/#list-comments-on-an-issue
func (s *IssuesService) ListComments(ctx context.Context, owner string, repo string, number int, opt *IssueListCommentsOptions) ([]*IssueComment, *Response, error) {
var u string
if number == 0 {
u = fmt.Sprintf("repos/%v/%v/issues/comments", owner, repo)
@ -67,7 +68,7 @@ func (s *IssuesService) ListComments(owner string, repo string, number int, opt
req.Header.Set("Accept", mediaTypeReactionsPreview)
var comments []*IssueComment
resp, err := s.client.Do(req, &comments)
resp, err := s.client.Do(ctx, req, &comments)
if err != nil {
return nil, resp, err
}
@ -77,8 +78,8 @@ func (s *IssuesService) ListComments(owner string, repo string, number int, opt
// GetComment fetches the specified issue comment.
//
// GitHub API docs: http://developer.github.com/v3/issues/comments/#get-a-single-comment
func (s *IssuesService) GetComment(owner string, repo string, id int) (*IssueComment, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/issues/comments/#get-a-single-comment
func (s *IssuesService) GetComment(ctx context.Context, owner string, repo string, id int) (*IssueComment, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/issues/comments/%d", owner, repo, id)
req, err := s.client.NewRequest("GET", u, nil)
@ -90,7 +91,7 @@ func (s *IssuesService) GetComment(owner string, repo string, id int) (*IssueCom
req.Header.Set("Accept", mediaTypeReactionsPreview)
comment := new(IssueComment)
resp, err := s.client.Do(req, comment)
resp, err := s.client.Do(ctx, req, comment)
if err != nil {
return nil, resp, err
}
@ -100,15 +101,15 @@ func (s *IssuesService) GetComment(owner string, repo string, id int) (*IssueCom
// CreateComment creates a new comment on the specified issue.
//
// GitHub API docs: http://developer.github.com/v3/issues/comments/#create-a-comment
func (s *IssuesService) CreateComment(owner string, repo string, number int, comment *IssueComment) (*IssueComment, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/issues/comments/#create-a-comment
func (s *IssuesService) CreateComment(ctx context.Context, owner string, repo string, number int, comment *IssueComment) (*IssueComment, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/issues/%d/comments", owner, repo, number)
req, err := s.client.NewRequest("POST", u, comment)
if err != nil {
return nil, nil, err
}
c := new(IssueComment)
resp, err := s.client.Do(req, c)
resp, err := s.client.Do(ctx, req, c)
if err != nil {
return nil, resp, err
}
@ -118,15 +119,15 @@ func (s *IssuesService) CreateComment(owner string, repo string, number int, com
// EditComment updates an issue comment.
//
// GitHub API docs: http://developer.github.com/v3/issues/comments/#edit-a-comment
func (s *IssuesService) EditComment(owner string, repo string, id int, comment *IssueComment) (*IssueComment, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/issues/comments/#edit-a-comment
func (s *IssuesService) EditComment(ctx context.Context, owner string, repo string, id int, comment *IssueComment) (*IssueComment, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/issues/comments/%d", owner, repo, id)
req, err := s.client.NewRequest("PATCH", u, comment)
if err != nil {
return nil, nil, err
}
c := new(IssueComment)
resp, err := s.client.Do(req, c)
resp, err := s.client.Do(ctx, req, c)
if err != nil {
return nil, resp, err
}
@ -136,12 +137,12 @@ func (s *IssuesService) EditComment(owner string, repo string, id int, comment *
// DeleteComment deletes an issue comment.
//
// GitHub API docs: http://developer.github.com/v3/issues/comments/#delete-a-comment
func (s *IssuesService) DeleteComment(owner string, repo string, id int) (*Response, error) {
// GitHub API docs: https://developer.github.com/v3/issues/comments/#delete-a-comment
func (s *IssuesService) DeleteComment(ctx context.Context, owner string, repo string, id int) (*Response, error) {
u := fmt.Sprintf("repos/%v/%v/issues/comments/%d", owner, repo, id)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}

View file

@ -6,6 +6,7 @@
package github
import (
"context"
"fmt"
"time"
)
@ -73,7 +74,7 @@ type IssueEvent struct {
// ListIssueEvents lists events for the specified issue.
//
// GitHub API docs: https://developer.github.com/v3/issues/events/#list-events-for-an-issue
func (s *IssuesService) ListIssueEvents(owner, repo string, number int, opt *ListOptions) ([]*IssueEvent, *Response, error) {
func (s *IssuesService) ListIssueEvents(ctx context.Context, owner, repo string, number int, opt *ListOptions) ([]*IssueEvent, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/issues/%v/events", owner, repo, number)
u, err := addOptions(u, opt)
if err != nil {
@ -86,7 +87,7 @@ func (s *IssuesService) ListIssueEvents(owner, repo string, number int, opt *Lis
}
var events []*IssueEvent
resp, err := s.client.Do(req, &events)
resp, err := s.client.Do(ctx, req, &events)
if err != nil {
return nil, resp, err
}
@ -97,7 +98,7 @@ func (s *IssuesService) ListIssueEvents(owner, repo string, number int, opt *Lis
// ListRepositoryEvents lists events for the specified repository.
//
// GitHub API docs: https://developer.github.com/v3/issues/events/#list-events-for-a-repository
func (s *IssuesService) ListRepositoryEvents(owner, repo string, opt *ListOptions) ([]*IssueEvent, *Response, error) {
func (s *IssuesService) ListRepositoryEvents(ctx context.Context, owner, repo string, opt *ListOptions) ([]*IssueEvent, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/issues/events", owner, repo)
u, err := addOptions(u, opt)
if err != nil {
@ -110,7 +111,7 @@ func (s *IssuesService) ListRepositoryEvents(owner, repo string, opt *ListOption
}
var events []*IssueEvent
resp, err := s.client.Do(req, &events)
resp, err := s.client.Do(ctx, req, &events)
if err != nil {
return nil, resp, err
}
@ -121,7 +122,7 @@ func (s *IssuesService) ListRepositoryEvents(owner, repo string, opt *ListOption
// GetEvent returns the specified issue event.
//
// GitHub API docs: https://developer.github.com/v3/issues/events/#get-a-single-event
func (s *IssuesService) GetEvent(owner, repo string, id int) (*IssueEvent, *Response, error) {
func (s *IssuesService) GetEvent(ctx context.Context, owner, repo string, id int) (*IssueEvent, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/issues/events/%v", owner, repo, id)
req, err := s.client.NewRequest("GET", u, nil)
@ -130,7 +131,7 @@ func (s *IssuesService) GetEvent(owner, repo string, id int) (*IssueEvent, *Resp
}
event := new(IssueEvent)
resp, err := s.client.Do(req, event)
resp, err := s.client.Do(ctx, req, event)
if err != nil {
return nil, resp, err
}

View file

@ -5,7 +5,10 @@
package github
import "fmt"
import (
"context"
"fmt"
)
// Label represents a GitHub label on an Issue
type Label struct {
@ -20,8 +23,8 @@ func (l Label) String() string {
// ListLabels lists all labels for a repository.
//
// GitHub API docs: http://developer.github.com/v3/issues/labels/#list-all-labels-for-this-repository
func (s *IssuesService) ListLabels(owner string, repo string, opt *ListOptions) ([]*Label, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/issues/labels/#list-all-labels-for-this-repository
func (s *IssuesService) ListLabels(ctx context.Context, owner string, repo string, opt *ListOptions) ([]*Label, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/labels", owner, repo)
u, err := addOptions(u, opt)
if err != nil {
@ -34,7 +37,7 @@ func (s *IssuesService) ListLabels(owner string, repo string, opt *ListOptions)
}
var labels []*Label
resp, err := s.client.Do(req, &labels)
resp, err := s.client.Do(ctx, req, &labels)
if err != nil {
return nil, resp, err
}
@ -44,8 +47,8 @@ func (s *IssuesService) ListLabels(owner string, repo string, opt *ListOptions)
// GetLabel gets a single label.
//
// GitHub API docs: http://developer.github.com/v3/issues/labels/#get-a-single-label
func (s *IssuesService) GetLabel(owner string, repo string, name string) (*Label, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/issues/labels/#get-a-single-label
func (s *IssuesService) GetLabel(ctx context.Context, owner string, repo string, name string) (*Label, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/labels/%v", owner, repo, name)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
@ -53,7 +56,7 @@ func (s *IssuesService) GetLabel(owner string, repo string, name string) (*Label
}
label := new(Label)
resp, err := s.client.Do(req, label)
resp, err := s.client.Do(ctx, req, label)
if err != nil {
return nil, resp, err
}
@ -63,8 +66,8 @@ func (s *IssuesService) GetLabel(owner string, repo string, name string) (*Label
// CreateLabel creates a new label on the specified repository.
//
// GitHub API docs: http://developer.github.com/v3/issues/labels/#create-a-label
func (s *IssuesService) CreateLabel(owner string, repo string, label *Label) (*Label, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/issues/labels/#create-a-label
func (s *IssuesService) CreateLabel(ctx context.Context, owner string, repo string, label *Label) (*Label, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/labels", owner, repo)
req, err := s.client.NewRequest("POST", u, label)
if err != nil {
@ -72,7 +75,7 @@ func (s *IssuesService) CreateLabel(owner string, repo string, label *Label) (*L
}
l := new(Label)
resp, err := s.client.Do(req, l)
resp, err := s.client.Do(ctx, req, l)
if err != nil {
return nil, resp, err
}
@ -82,8 +85,8 @@ func (s *IssuesService) CreateLabel(owner string, repo string, label *Label) (*L
// EditLabel edits a label.
//
// GitHub API docs: http://developer.github.com/v3/issues/labels/#update-a-label
func (s *IssuesService) EditLabel(owner string, repo string, name string, label *Label) (*Label, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/issues/labels/#update-a-label
func (s *IssuesService) EditLabel(ctx context.Context, owner string, repo string, name string, label *Label) (*Label, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/labels/%v", owner, repo, name)
req, err := s.client.NewRequest("PATCH", u, label)
if err != nil {
@ -91,7 +94,7 @@ func (s *IssuesService) EditLabel(owner string, repo string, name string, label
}
l := new(Label)
resp, err := s.client.Do(req, l)
resp, err := s.client.Do(ctx, req, l)
if err != nil {
return nil, resp, err
}
@ -101,20 +104,20 @@ func (s *IssuesService) EditLabel(owner string, repo string, name string, label
// DeleteLabel deletes a label.
//
// GitHub API docs: http://developer.github.com/v3/issues/labels/#delete-a-label
func (s *IssuesService) DeleteLabel(owner string, repo string, name string) (*Response, error) {
// GitHub API docs: https://developer.github.com/v3/issues/labels/#delete-a-label
func (s *IssuesService) DeleteLabel(ctx context.Context, owner string, repo string, name string) (*Response, error) {
u := fmt.Sprintf("repos/%v/%v/labels/%v", owner, repo, name)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}
// ListLabelsByIssue lists all labels for an issue.
//
// GitHub API docs: https://developer.github.com/v3/issues/labels/#list-labels-on-an-issue
func (s *IssuesService) ListLabelsByIssue(owner string, repo string, number int, opt *ListOptions) ([]*Label, *Response, error) {
func (s *IssuesService) ListLabelsByIssue(ctx context.Context, owner string, repo string, number int, opt *ListOptions) ([]*Label, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/issues/%d/labels", owner, repo, number)
u, err := addOptions(u, opt)
if err != nil {
@ -127,7 +130,7 @@ func (s *IssuesService) ListLabelsByIssue(owner string, repo string, number int,
}
var labels []*Label
resp, err := s.client.Do(req, &labels)
resp, err := s.client.Do(ctx, req, &labels)
if err != nil {
return nil, resp, err
}
@ -138,7 +141,7 @@ func (s *IssuesService) ListLabelsByIssue(owner string, repo string, number int,
// AddLabelsToIssue adds labels to an issue.
//
// GitHub API docs: https://developer.github.com/v3/issues/labels/#add-labels-to-an-issue
func (s *IssuesService) AddLabelsToIssue(owner string, repo string, number int, labels []string) ([]*Label, *Response, error) {
func (s *IssuesService) AddLabelsToIssue(ctx context.Context, owner string, repo string, number int, labels []string) ([]*Label, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/issues/%d/labels", owner, repo, number)
req, err := s.client.NewRequest("POST", u, labels)
if err != nil {
@ -146,7 +149,7 @@ func (s *IssuesService) AddLabelsToIssue(owner string, repo string, number int,
}
var l []*Label
resp, err := s.client.Do(req, &l)
resp, err := s.client.Do(ctx, req, &l)
if err != nil {
return nil, resp, err
}
@ -156,20 +159,20 @@ func (s *IssuesService) AddLabelsToIssue(owner string, repo string, number int,
// RemoveLabelForIssue removes a label for an issue.
//
// GitHub API docs: http://developer.github.com/v3/issues/labels/#remove-a-label-from-an-issue
func (s *IssuesService) RemoveLabelForIssue(owner string, repo string, number int, label string) (*Response, error) {
// GitHub API docs: https://developer.github.com/v3/issues/labels/#remove-a-label-from-an-issue
func (s *IssuesService) RemoveLabelForIssue(ctx context.Context, owner string, repo string, number int, label string) (*Response, error) {
u := fmt.Sprintf("repos/%v/%v/issues/%d/labels/%v", owner, repo, number, label)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}
// ReplaceLabelsForIssue replaces all labels for an issue.
//
// GitHub API docs: http://developer.github.com/v3/issues/labels/#replace-all-labels-for-an-issue
func (s *IssuesService) ReplaceLabelsForIssue(owner string, repo string, number int, labels []string) ([]*Label, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/issues/labels/#replace-all-labels-for-an-issue
func (s *IssuesService) ReplaceLabelsForIssue(ctx context.Context, owner string, repo string, number int, labels []string) ([]*Label, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/issues/%d/labels", owner, repo, number)
req, err := s.client.NewRequest("PUT", u, labels)
if err != nil {
@ -177,7 +180,7 @@ func (s *IssuesService) ReplaceLabelsForIssue(owner string, repo string, number
}
var l []*Label
resp, err := s.client.Do(req, &l)
resp, err := s.client.Do(ctx, req, &l)
if err != nil {
return nil, resp, err
}
@ -187,20 +190,20 @@ func (s *IssuesService) ReplaceLabelsForIssue(owner string, repo string, number
// RemoveLabelsForIssue removes all labels for an issue.
//
// GitHub API docs: http://developer.github.com/v3/issues/labels/#remove-all-labels-from-an-issue
func (s *IssuesService) RemoveLabelsForIssue(owner string, repo string, number int) (*Response, error) {
// GitHub API docs: https://developer.github.com/v3/issues/labels/#remove-all-labels-from-an-issue
func (s *IssuesService) RemoveLabelsForIssue(ctx context.Context, owner string, repo string, number int) (*Response, error) {
u := fmt.Sprintf("repos/%v/%v/issues/%d/labels", owner, repo, number)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}
// ListLabelsForMilestone lists labels for every issue in a milestone.
//
// GitHub API docs: http://developer.github.com/v3/issues/labels/#get-labels-for-every-issue-in-a-milestone
func (s *IssuesService) ListLabelsForMilestone(owner string, repo string, number int, opt *ListOptions) ([]*Label, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/issues/labels/#get-labels-for-every-issue-in-a-milestone
func (s *IssuesService) ListLabelsForMilestone(ctx context.Context, owner string, repo string, number int, opt *ListOptions) ([]*Label, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/milestones/%d/labels", owner, repo, number)
u, err := addOptions(u, opt)
if err != nil {
@ -213,7 +216,7 @@ func (s *IssuesService) ListLabelsForMilestone(owner string, repo string, number
}
var labels []*Label
resp, err := s.client.Do(req, &labels)
resp, err := s.client.Do(ctx, req, &labels)
if err != nil {
return nil, resp, err
}

View file

@ -6,11 +6,12 @@
package github
import (
"context"
"fmt"
"time"
)
// Milestone represents a Github repository milestone.
// Milestone represents a GitHub repository milestone.
type Milestone struct {
URL *string `json:"url,omitempty"`
HTMLURL *string `json:"html_url,omitempty"`
@ -54,7 +55,7 @@ type MilestoneListOptions struct {
// ListMilestones lists all milestones for a repository.
//
// GitHub API docs: https://developer.github.com/v3/issues/milestones/#list-milestones-for-a-repository
func (s *IssuesService) ListMilestones(owner string, repo string, opt *MilestoneListOptions) ([]*Milestone, *Response, error) {
func (s *IssuesService) ListMilestones(ctx context.Context, owner string, repo string, opt *MilestoneListOptions) ([]*Milestone, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/milestones", owner, repo)
u, err := addOptions(u, opt)
if err != nil {
@ -67,7 +68,7 @@ func (s *IssuesService) ListMilestones(owner string, repo string, opt *Milestone
}
var milestones []*Milestone
resp, err := s.client.Do(req, &milestones)
resp, err := s.client.Do(ctx, req, &milestones)
if err != nil {
return nil, resp, err
}
@ -78,7 +79,7 @@ func (s *IssuesService) ListMilestones(owner string, repo string, opt *Milestone
// GetMilestone gets a single milestone.
//
// GitHub API docs: https://developer.github.com/v3/issues/milestones/#get-a-single-milestone
func (s *IssuesService) GetMilestone(owner string, repo string, number int) (*Milestone, *Response, error) {
func (s *IssuesService) GetMilestone(ctx context.Context, owner string, repo string, number int) (*Milestone, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/milestones/%d", owner, repo, number)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
@ -86,7 +87,7 @@ func (s *IssuesService) GetMilestone(owner string, repo string, number int) (*Mi
}
milestone := new(Milestone)
resp, err := s.client.Do(req, milestone)
resp, err := s.client.Do(ctx, req, milestone)
if err != nil {
return nil, resp, err
}
@ -97,7 +98,7 @@ func (s *IssuesService) GetMilestone(owner string, repo string, number int) (*Mi
// CreateMilestone creates a new milestone on the specified repository.
//
// GitHub API docs: https://developer.github.com/v3/issues/milestones/#create-a-milestone
func (s *IssuesService) CreateMilestone(owner string, repo string, milestone *Milestone) (*Milestone, *Response, error) {
func (s *IssuesService) CreateMilestone(ctx context.Context, owner string, repo string, milestone *Milestone) (*Milestone, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/milestones", owner, repo)
req, err := s.client.NewRequest("POST", u, milestone)
if err != nil {
@ -105,7 +106,7 @@ func (s *IssuesService) CreateMilestone(owner string, repo string, milestone *Mi
}
m := new(Milestone)
resp, err := s.client.Do(req, m)
resp, err := s.client.Do(ctx, req, m)
if err != nil {
return nil, resp, err
}
@ -116,7 +117,7 @@ func (s *IssuesService) CreateMilestone(owner string, repo string, milestone *Mi
// EditMilestone edits a milestone.
//
// GitHub API docs: https://developer.github.com/v3/issues/milestones/#update-a-milestone
func (s *IssuesService) EditMilestone(owner string, repo string, number int, milestone *Milestone) (*Milestone, *Response, error) {
func (s *IssuesService) EditMilestone(ctx context.Context, owner string, repo string, number int, milestone *Milestone) (*Milestone, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/milestones/%d", owner, repo, number)
req, err := s.client.NewRequest("PATCH", u, milestone)
if err != nil {
@ -124,7 +125,7 @@ func (s *IssuesService) EditMilestone(owner string, repo string, number int, mil
}
m := new(Milestone)
resp, err := s.client.Do(req, m)
resp, err := s.client.Do(ctx, req, m)
if err != nil {
return nil, resp, err
}
@ -135,12 +136,12 @@ func (s *IssuesService) EditMilestone(owner string, repo string, number int, mil
// DeleteMilestone deletes a milestone.
//
// GitHub API docs: https://developer.github.com/v3/issues/milestones/#delete-a-milestone
func (s *IssuesService) DeleteMilestone(owner string, repo string, number int) (*Response, error) {
func (s *IssuesService) DeleteMilestone(ctx context.Context, owner string, repo string, number int) (*Response, error) {
u := fmt.Sprintf("repos/%v/%v/milestones/%d", owner, repo, number)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}

View file

@ -6,6 +6,7 @@
package github
import (
"context"
"fmt"
"time"
)
@ -127,7 +128,7 @@ type Source struct {
// ListIssueTimeline lists events for the specified issue.
//
// GitHub API docs: https://developer.github.com/v3/issues/timeline/#list-events-for-an-issue
func (s *IssuesService) ListIssueTimeline(owner, repo string, number int, opt *ListOptions) ([]*Timeline, *Response, error) {
func (s *IssuesService) ListIssueTimeline(ctx context.Context, owner, repo string, number int, opt *ListOptions) ([]*Timeline, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/issues/%v/timeline", owner, repo, number)
u, err := addOptions(u, opt)
if err != nil {
@ -143,6 +144,6 @@ func (s *IssuesService) ListIssueTimeline(owner, repo string, number int, opt *L
req.Header.Set("Accept", mediaTypeTimelinePreview)
var events []*Timeline
resp, err := s.client.Do(req, &events)
resp, err := s.client.Do(ctx, req, &events)
return events, resp, err
}

View file

@ -5,7 +5,10 @@
package github
import "fmt"
import (
"context"
"fmt"
)
// LicensesService handles communication with the license related
// methods of the GitHub API.
@ -58,7 +61,7 @@ func (l License) String() string {
// List popular open source licenses.
//
// GitHub API docs: https://developer.github.com/v3/licenses/#list-all-licenses
func (s *LicensesService) List() ([]*License, *Response, error) {
func (s *LicensesService) List(ctx context.Context) ([]*License, *Response, error) {
req, err := s.client.NewRequest("GET", "licenses", nil)
if err != nil {
return nil, nil, err
@ -68,7 +71,7 @@ func (s *LicensesService) List() ([]*License, *Response, error) {
req.Header.Set("Accept", mediaTypeLicensesPreview)
var licenses []*License
resp, err := s.client.Do(req, &licenses)
resp, err := s.client.Do(ctx, req, &licenses)
if err != nil {
return nil, resp, err
}
@ -79,7 +82,7 @@ func (s *LicensesService) List() ([]*License, *Response, error) {
// Get extended metadata for one license.
//
// GitHub API docs: https://developer.github.com/v3/licenses/#get-an-individual-license
func (s *LicensesService) Get(licenseName string) (*License, *Response, error) {
func (s *LicensesService) Get(ctx context.Context, licenseName string) (*License, *Response, error) {
u := fmt.Sprintf("licenses/%s", licenseName)
req, err := s.client.NewRequest("GET", u, nil)
@ -91,7 +94,7 @@ func (s *LicensesService) Get(licenseName string) (*License, *Response, error) {
req.Header.Set("Accept", mediaTypeLicensesPreview)
license := new(License)
resp, err := s.client.Do(req, license)
resp, err := s.client.Do(ctx, req, license)
if err != nil {
return nil, resp, err
}

View file

@ -4,7 +4,7 @@
// license that can be found in the LICENSE file.
// This file provides functions for validating payloads from GitHub Webhooks.
// GitHub docs: https://developer.github.com/webhooks/securing/#validating-payloads-from-github
// GitHub API docs: https://developer.github.com/webhooks/securing/#validating-payloads-from-github
package github
@ -31,7 +31,7 @@ const (
sha512Prefix = "sha512"
// signatureHeader is the GitHub header key used to pass the HMAC hexdigest.
signatureHeader = "X-Hub-Signature"
// eventTypeHeader is the Github header key used to pass the event type.
// eventTypeHeader is the GitHub header key used to pass the event type.
eventTypeHeader = "X-Github-Event"
)
@ -143,7 +143,7 @@ func ValidatePayload(r *http.Request, secretKey []byte) (payload []byte, err err
// payload is the JSON payload sent by GitHub Webhooks.
// secretKey is the GitHub Webhook secret message.
//
// GitHub docs: https://developer.github.com/webhooks/securing/#validating-payloads-from-github
// GitHub API docs: https://developer.github.com/webhooks/securing/#validating-payloads-from-github
func validateSignature(signature string, payload, secretKey []byte) error {
messageMAC, hashFunc, err := messageMAC(signature)
if err != nil {

View file

@ -6,6 +6,7 @@
package github
import (
"context"
"errors"
"fmt"
"net/http"
@ -74,7 +75,7 @@ type startMigration struct {
// repos is a slice of repository names to migrate.
//
// GitHub API docs: https://developer.github.com/v3/migration/migrations/#start-a-migration
func (s *MigrationService) StartMigration(org string, repos []string, opt *MigrationOptions) (*Migration, *Response, error) {
func (s *MigrationService) StartMigration(ctx context.Context, org string, repos []string, opt *MigrationOptions) (*Migration, *Response, error) {
u := fmt.Sprintf("orgs/%v/migrations", org)
body := &startMigration{Repositories: repos}
@ -92,7 +93,7 @@ func (s *MigrationService) StartMigration(org string, repos []string, opt *Migra
req.Header.Set("Accept", mediaTypeMigrationsPreview)
m := &Migration{}
resp, err := s.client.Do(req, m)
resp, err := s.client.Do(ctx, req, m)
if err != nil {
return nil, resp, err
}
@ -103,7 +104,7 @@ func (s *MigrationService) StartMigration(org string, repos []string, opt *Migra
// ListMigrations lists the most recent migrations.
//
// GitHub API docs: https://developer.github.com/v3/migration/migrations/#get-a-list-of-migrations
func (s *MigrationService) ListMigrations(org string) ([]*Migration, *Response, error) {
func (s *MigrationService) ListMigrations(ctx context.Context, org string) ([]*Migration, *Response, error) {
u := fmt.Sprintf("orgs/%v/migrations", org)
req, err := s.client.NewRequest("GET", u, nil)
@ -115,7 +116,7 @@ func (s *MigrationService) ListMigrations(org string) ([]*Migration, *Response,
req.Header.Set("Accept", mediaTypeMigrationsPreview)
var m []*Migration
resp, err := s.client.Do(req, &m)
resp, err := s.client.Do(ctx, req, &m)
if err != nil {
return nil, resp, err
}
@ -127,7 +128,7 @@ func (s *MigrationService) ListMigrations(org string) ([]*Migration, *Response,
// id is the migration ID.
//
// GitHub API docs: https://developer.github.com/v3/migration/migrations/#get-the-status-of-a-migration
func (s *MigrationService) MigrationStatus(org string, id int) (*Migration, *Response, error) {
func (s *MigrationService) MigrationStatus(ctx context.Context, org string, id int) (*Migration, *Response, error) {
u := fmt.Sprintf("orgs/%v/migrations/%v", org, id)
req, err := s.client.NewRequest("GET", u, nil)
@ -139,7 +140,7 @@ func (s *MigrationService) MigrationStatus(org string, id int) (*Migration, *Res
req.Header.Set("Accept", mediaTypeMigrationsPreview)
m := &Migration{}
resp, err := s.client.Do(req, m)
resp, err := s.client.Do(ctx, req, m)
if err != nil {
return nil, resp, err
}
@ -151,7 +152,7 @@ func (s *MigrationService) MigrationStatus(org string, id int) (*Migration, *Res
// id is the migration ID.
//
// GitHub API docs: https://developer.github.com/v3/migration/migrations/#download-a-migration-archive
func (s *MigrationService) MigrationArchiveURL(org string, id int) (url string, err error) {
func (s *MigrationService) MigrationArchiveURL(ctx context.Context, org string, id int) (url string, err error) {
u := fmt.Sprintf("orgs/%v/migrations/%v/archive", org, id)
req, err := s.client.NewRequest("GET", u, nil)
@ -174,7 +175,7 @@ func (s *MigrationService) MigrationArchiveURL(org string, id int) (url string,
}
defer func() { s.client.client.CheckRedirect = saveRedirect }()
_, err = s.client.Do(req, nil) // expect error from disable redirect
_, err = s.client.Do(ctx, req, nil) // expect error from disable redirect
if err == nil {
return "", errors.New("expected redirect, none provided")
}
@ -188,7 +189,7 @@ func (s *MigrationService) MigrationArchiveURL(org string, id int) (url string,
// id is the migration ID.
//
// GitHub API docs: https://developer.github.com/v3/migration/migrations/#delete-a-migration-archive
func (s *MigrationService) DeleteMigration(org string, id int) (*Response, error) {
func (s *MigrationService) DeleteMigration(ctx context.Context, org string, id int) (*Response, error) {
u := fmt.Sprintf("orgs/%v/migrations/%v/archive", org, id)
req, err := s.client.NewRequest("DELETE", u, nil)
@ -199,7 +200,7 @@ func (s *MigrationService) DeleteMigration(org string, id int) (*Response, error
// TODO: remove custom Accept header when this API fully launches.
req.Header.Set("Accept", mediaTypeMigrationsPreview)
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}
// UnlockRepo unlocks a repository that was locked for migration.
@ -208,7 +209,7 @@ func (s *MigrationService) DeleteMigration(org string, id int) (*Response, error
// is complete and you no longer need the source data.
//
// GitHub API docs: https://developer.github.com/v3/migration/migrations/#unlock-a-repository
func (s *MigrationService) UnlockRepo(org string, id int, repo string) (*Response, error) {
func (s *MigrationService) UnlockRepo(ctx context.Context, org string, id int, repo string) (*Response, error) {
u := fmt.Sprintf("orgs/%v/migrations/%v/repos/%v/lock", org, id, repo)
req, err := s.client.NewRequest("DELETE", u, nil)
@ -219,5 +220,5 @@ func (s *MigrationService) UnlockRepo(org string, id int, repo string) (*Respons
// TODO: remove custom Accept header when this API fully launches.
req.Header.Set("Accept", mediaTypeMigrationsPreview)
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}

View file

@ -5,7 +5,10 @@
package github
import "fmt"
import (
"context"
"fmt"
)
// Import represents a repository import request.
type Import struct {
@ -144,7 +147,7 @@ func (f LargeFile) String() string {
// StartImport initiates a repository import.
//
// GitHub API docs: https://developer.github.com/v3/migration/source_imports/#start-an-import
func (s *MigrationService) StartImport(owner, repo string, in *Import) (*Import, *Response, error) {
func (s *MigrationService) StartImport(ctx context.Context, owner, repo string, in *Import) (*Import, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/import", owner, repo)
req, err := s.client.NewRequest("PUT", u, in)
if err != nil {
@ -155,7 +158,7 @@ func (s *MigrationService) StartImport(owner, repo string, in *Import) (*Import,
req.Header.Set("Accept", mediaTypeImportPreview)
out := new(Import)
resp, err := s.client.Do(req, out)
resp, err := s.client.Do(ctx, req, out)
if err != nil {
return nil, resp, err
}
@ -166,7 +169,7 @@ func (s *MigrationService) StartImport(owner, repo string, in *Import) (*Import,
// ImportProgress queries for the status and progress of an ongoing repository import.
//
// GitHub API docs: https://developer.github.com/v3/migration/source_imports/#get-import-progress
func (s *MigrationService) ImportProgress(owner, repo string) (*Import, *Response, error) {
func (s *MigrationService) ImportProgress(ctx context.Context, owner, repo string) (*Import, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/import", owner, repo)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
@ -177,7 +180,7 @@ func (s *MigrationService) ImportProgress(owner, repo string) (*Import, *Respons
req.Header.Set("Accept", mediaTypeImportPreview)
out := new(Import)
resp, err := s.client.Do(req, out)
resp, err := s.client.Do(ctx, req, out)
if err != nil {
return nil, resp, err
}
@ -188,7 +191,7 @@ func (s *MigrationService) ImportProgress(owner, repo string) (*Import, *Respons
// UpdateImport initiates a repository import.
//
// GitHub API docs: https://developer.github.com/v3/migration/source_imports/#update-existing-import
func (s *MigrationService) UpdateImport(owner, repo string, in *Import) (*Import, *Response, error) {
func (s *MigrationService) UpdateImport(ctx context.Context, owner, repo string, in *Import) (*Import, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/import", owner, repo)
req, err := s.client.NewRequest("PATCH", u, in)
if err != nil {
@ -199,7 +202,7 @@ func (s *MigrationService) UpdateImport(owner, repo string, in *Import) (*Import
req.Header.Set("Accept", mediaTypeImportPreview)
out := new(Import)
resp, err := s.client.Do(req, out)
resp, err := s.client.Do(ctx, req, out)
if err != nil {
return nil, resp, err
}
@ -220,7 +223,7 @@ func (s *MigrationService) UpdateImport(owner, repo string, in *Import) (*Import
// information.
//
// GitHub API docs: https://developer.github.com/v3/migration/source_imports/#get-commit-authors
func (s *MigrationService) CommitAuthors(owner, repo string) ([]*SourceImportAuthor, *Response, error) {
func (s *MigrationService) CommitAuthors(ctx context.Context, owner, repo string) ([]*SourceImportAuthor, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/import/authors", owner, repo)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
@ -231,7 +234,7 @@ func (s *MigrationService) CommitAuthors(owner, repo string) ([]*SourceImportAut
req.Header.Set("Accept", mediaTypeImportPreview)
var authors []*SourceImportAuthor
resp, err := s.client.Do(req, &authors)
resp, err := s.client.Do(ctx, req, &authors)
if err != nil {
return nil, resp, err
}
@ -244,7 +247,7 @@ func (s *MigrationService) CommitAuthors(owner, repo string) ([]*SourceImportAut
// commits to the repository.
//
// GitHub API docs: https://developer.github.com/v3/migration/source_imports/#map-a-commit-author
func (s *MigrationService) MapCommitAuthor(owner, repo string, id int, author *SourceImportAuthor) (*SourceImportAuthor, *Response, error) {
func (s *MigrationService) MapCommitAuthor(ctx context.Context, owner, repo string, id int, author *SourceImportAuthor) (*SourceImportAuthor, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/import/authors/%v", owner, repo, id)
req, err := s.client.NewRequest("PATCH", u, author)
if err != nil {
@ -255,7 +258,7 @@ func (s *MigrationService) MapCommitAuthor(owner, repo string, id int, author *S
req.Header.Set("Accept", mediaTypeImportPreview)
out := new(SourceImportAuthor)
resp, err := s.client.Do(req, out)
resp, err := s.client.Do(ctx, req, out)
if err != nil {
return nil, resp, err
}
@ -268,7 +271,7 @@ func (s *MigrationService) MapCommitAuthor(owner, repo string, id int, author *S
// used.
//
// GitHub API docs: https://developer.github.com/v3/migration/source_imports/#set-git-lfs-preference
func (s *MigrationService) SetLFSPreference(owner, repo string, in *Import) (*Import, *Response, error) {
func (s *MigrationService) SetLFSPreference(ctx context.Context, owner, repo string, in *Import) (*Import, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/import/lfs", owner, repo)
req, err := s.client.NewRequest("PATCH", u, in)
if err != nil {
@ -279,7 +282,7 @@ func (s *MigrationService) SetLFSPreference(owner, repo string, in *Import) (*Im
req.Header.Set("Accept", mediaTypeImportPreview)
out := new(Import)
resp, err := s.client.Do(req, out)
resp, err := s.client.Do(ctx, req, out)
if err != nil {
return nil, resp, err
}
@ -290,7 +293,7 @@ func (s *MigrationService) SetLFSPreference(owner, repo string, in *Import) (*Im
// LargeFiles lists files larger than 100MB found during the import.
//
// GitHub API docs: https://developer.github.com/v3/migration/source_imports/#get-large-files
func (s *MigrationService) LargeFiles(owner, repo string) ([]*LargeFile, *Response, error) {
func (s *MigrationService) LargeFiles(ctx context.Context, owner, repo string) ([]*LargeFile, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/import/large_files", owner, repo)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
@ -301,7 +304,7 @@ func (s *MigrationService) LargeFiles(owner, repo string) ([]*LargeFile, *Respon
req.Header.Set("Accept", mediaTypeImportPreview)
var files []*LargeFile
resp, err := s.client.Do(req, &files)
resp, err := s.client.Do(ctx, req, &files)
if err != nil {
return nil, resp, err
}
@ -312,7 +315,7 @@ func (s *MigrationService) LargeFiles(owner, repo string) ([]*LargeFile, *Respon
// CancelImport stops an import for a repository.
//
// GitHub API docs: https://developer.github.com/v3/migration/source_imports/#cancel-an-import
func (s *MigrationService) CancelImport(owner, repo string) (*Response, error) {
func (s *MigrationService) CancelImport(ctx context.Context, owner, repo string) (*Response, error) {
u := fmt.Sprintf("repos/%v/%v/import", owner, repo)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
@ -322,5 +325,5 @@ func (s *MigrationService) CancelImport(owner, repo string) (*Response, error) {
// TODO: remove custom Accept header when this API fully launches
req.Header.Set("Accept", mediaTypeImportPreview)
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}

View file

@ -7,6 +7,7 @@ package github
import (
"bytes"
"context"
"fmt"
"net/url"
)
@ -39,7 +40,7 @@ type markdownRequest struct {
// Markdown renders an arbitrary Markdown document.
//
// GitHub API docs: https://developer.github.com/v3/markdown/
func (c *Client) Markdown(text string, opt *MarkdownOptions) (string, *Response, error) {
func (c *Client) Markdown(ctx context.Context, text string, opt *MarkdownOptions) (string, *Response, error) {
request := &markdownRequest{Text: String(text)}
if opt != nil {
if opt.Mode != "" {
@ -56,7 +57,7 @@ func (c *Client) Markdown(text string, opt *MarkdownOptions) (string, *Response,
}
buf := new(bytes.Buffer)
resp, err := c.Do(req, buf)
resp, err := c.Do(ctx, req, buf)
if err != nil {
return "", resp, err
}
@ -67,14 +68,14 @@ func (c *Client) Markdown(text string, opt *MarkdownOptions) (string, *Response,
// ListEmojis returns the emojis available to use on GitHub.
//
// GitHub API docs: https://developer.github.com/v3/emojis/
func (c *Client) ListEmojis() (map[string]string, *Response, error) {
func (c *Client) ListEmojis(ctx context.Context) (map[string]string, *Response, error) {
req, err := c.NewRequest("GET", "emojis", nil)
if err != nil {
return nil, nil, err
}
var emoji map[string]string
resp, err := c.Do(req, &emoji)
resp, err := c.Do(ctx, req, &emoji)
if err != nil {
return nil, resp, err
}
@ -109,14 +110,14 @@ type APIMeta struct {
// endpoint provides information about that installation.
//
// GitHub API docs: https://developer.github.com/v3/meta/
func (c *Client) APIMeta() (*APIMeta, *Response, error) {
func (c *Client) APIMeta(ctx context.Context) (*APIMeta, *Response, error) {
req, err := c.NewRequest("GET", "meta", nil)
if err != nil {
return nil, nil, err
}
meta := new(APIMeta)
resp, err := c.Do(req, meta)
resp, err := c.Do(ctx, req, meta)
if err != nil {
return nil, resp, err
}
@ -126,7 +127,7 @@ func (c *Client) APIMeta() (*APIMeta, *Response, error) {
// Octocat returns an ASCII art octocat with the specified message in a speech
// bubble. If message is empty, a random zen phrase is used.
func (c *Client) Octocat(message string) (string, *Response, error) {
func (c *Client) Octocat(ctx context.Context, message string) (string, *Response, error) {
u := "octocat"
if message != "" {
u = fmt.Sprintf("%s?s=%s", u, url.QueryEscape(message))
@ -138,7 +139,7 @@ func (c *Client) Octocat(message string) (string, *Response, error) {
}
buf := new(bytes.Buffer)
resp, err := c.Do(req, buf)
resp, err := c.Do(ctx, req, buf)
if err != nil {
return "", resp, err
}
@ -149,14 +150,14 @@ func (c *Client) Octocat(message string) (string, *Response, error) {
// Zen returns a random line from The Zen of GitHub.
//
// see also: http://warpspire.com/posts/taste/
func (c *Client) Zen() (string, *Response, error) {
func (c *Client) Zen(ctx context.Context) (string, *Response, error) {
req, err := c.NewRequest("GET", "zen", nil)
if err != nil {
return "", nil, err
}
buf := new(bytes.Buffer)
resp, err := c.Do(req, buf)
resp, err := c.Do(ctx, req, buf)
if err != nil {
return "", resp, err
}
@ -180,7 +181,7 @@ func (s *ServiceHook) String() string {
// ListServiceHooks lists all of the available service hooks.
//
// GitHub API docs: https://developer.github.com/webhooks/#services
func (c *Client) ListServiceHooks() ([]*ServiceHook, *Response, error) {
func (c *Client) ListServiceHooks(ctx context.Context) ([]*ServiceHook, *Response, error) {
u := "hooks"
req, err := c.NewRequest("GET", u, nil)
if err != nil {
@ -188,7 +189,7 @@ func (c *Client) ListServiceHooks() ([]*ServiceHook, *Response, error) {
}
var hooks []*ServiceHook
resp, err := c.Do(req, &hooks)
resp, err := c.Do(ctx, req, &hooks)
if err != nil {
return nil, resp, err
}

View file

@ -6,6 +6,7 @@
package github
import (
"context"
"fmt"
"time"
)
@ -13,7 +14,7 @@ import (
// OrganizationsService provides access to the organization related functions
// in the GitHub API.
//
// GitHub API docs: http://developer.github.com/v3/orgs/
// GitHub API docs: https://developer.github.com/v3/orgs/
type OrganizationsService service
// Organization represents a GitHub organization account.
@ -85,7 +86,7 @@ type OrganizationsListOptions struct {
// as the opts.Since parameter for the next call.
//
// GitHub API docs: https://developer.github.com/v3/orgs/#list-all-organizations
func (s *OrganizationsService) ListAll(opt *OrganizationsListOptions) ([]*Organization, *Response, error) {
func (s *OrganizationsService) ListAll(ctx context.Context, opt *OrganizationsListOptions) ([]*Organization, *Response, error) {
u, err := addOptions("organizations", opt)
if err != nil {
return nil, nil, err
@ -97,7 +98,7 @@ func (s *OrganizationsService) ListAll(opt *OrganizationsListOptions) ([]*Organi
}
orgs := []*Organization{}
resp, err := s.client.Do(req, &orgs)
resp, err := s.client.Do(ctx, req, &orgs)
if err != nil {
return nil, resp, err
}
@ -107,8 +108,8 @@ func (s *OrganizationsService) ListAll(opt *OrganizationsListOptions) ([]*Organi
// List the organizations for a user. Passing the empty string will list
// organizations for the authenticated user.
//
// GitHub API docs: http://developer.github.com/v3/orgs/#list-user-organizations
func (s *OrganizationsService) List(user string, opt *ListOptions) ([]*Organization, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/orgs/#list-user-organizations
func (s *OrganizationsService) List(ctx context.Context, user string, opt *ListOptions) ([]*Organization, *Response, error) {
var u string
if user != "" {
u = fmt.Sprintf("users/%v/orgs", user)
@ -126,7 +127,7 @@ func (s *OrganizationsService) List(user string, opt *ListOptions) ([]*Organizat
}
var orgs []*Organization
resp, err := s.client.Do(req, &orgs)
resp, err := s.client.Do(ctx, req, &orgs)
if err != nil {
return nil, resp, err
}
@ -136,8 +137,8 @@ func (s *OrganizationsService) List(user string, opt *ListOptions) ([]*Organizat
// Get fetches an organization by name.
//
// GitHub API docs: http://developer.github.com/v3/orgs/#get-an-organization
func (s *OrganizationsService) Get(org string) (*Organization, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/orgs/#get-an-organization
func (s *OrganizationsService) Get(ctx context.Context, org string) (*Organization, *Response, error) {
u := fmt.Sprintf("orgs/%v", org)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
@ -145,7 +146,7 @@ func (s *OrganizationsService) Get(org string) (*Organization, *Response, error)
}
organization := new(Organization)
resp, err := s.client.Do(req, organization)
resp, err := s.client.Do(ctx, req, organization)
if err != nil {
return nil, resp, err
}
@ -155,8 +156,8 @@ func (s *OrganizationsService) Get(org string) (*Organization, *Response, error)
// Edit an organization.
//
// GitHub API docs: http://developer.github.com/v3/orgs/#edit-an-organization
func (s *OrganizationsService) Edit(name string, org *Organization) (*Organization, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/orgs/#edit-an-organization
func (s *OrganizationsService) Edit(ctx context.Context, name string, org *Organization) (*Organization, *Response, error) {
u := fmt.Sprintf("orgs/%v", name)
req, err := s.client.NewRequest("PATCH", u, org)
if err != nil {
@ -164,7 +165,7 @@ func (s *OrganizationsService) Edit(name string, org *Organization) (*Organizati
}
o := new(Organization)
resp, err := s.client.Do(req, o)
resp, err := s.client.Do(ctx, req, o)
if err != nil {
return nil, resp, err
}

View file

@ -5,12 +5,15 @@
package github
import "fmt"
import (
"context"
"fmt"
)
// ListHooks lists all Hooks for the specified organization.
//
// GitHub API docs: https://developer.github.com/v3/orgs/hooks/#list-hooks
func (s *OrganizationsService) ListHooks(org string, opt *ListOptions) ([]*Hook, *Response, error) {
func (s *OrganizationsService) ListHooks(ctx context.Context, org string, opt *ListOptions) ([]*Hook, *Response, error) {
u := fmt.Sprintf("orgs/%v/hooks", org)
u, err := addOptions(u, opt)
if err != nil {
@ -23,7 +26,7 @@ func (s *OrganizationsService) ListHooks(org string, opt *ListOptions) ([]*Hook,
}
var hooks []*Hook
resp, err := s.client.Do(req, &hooks)
resp, err := s.client.Do(ctx, req, &hooks)
if err != nil {
return nil, resp, err
}
@ -34,14 +37,14 @@ func (s *OrganizationsService) ListHooks(org string, opt *ListOptions) ([]*Hook,
// GetHook returns a single specified Hook.
//
// GitHub API docs: https://developer.github.com/v3/orgs/hooks/#get-single-hook
func (s *OrganizationsService) GetHook(org string, id int) (*Hook, *Response, error) {
func (s *OrganizationsService) GetHook(ctx context.Context, org string, id int) (*Hook, *Response, error) {
u := fmt.Sprintf("orgs/%v/hooks/%d", org, id)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
hook := new(Hook)
resp, err := s.client.Do(req, hook)
resp, err := s.client.Do(ctx, req, hook)
return hook, resp, err
}
@ -49,7 +52,7 @@ func (s *OrganizationsService) GetHook(org string, id int) (*Hook, *Response, er
// Name and Config are required fields.
//
// GitHub API docs: https://developer.github.com/v3/orgs/hooks/#create-a-hook
func (s *OrganizationsService) CreateHook(org string, hook *Hook) (*Hook, *Response, error) {
func (s *OrganizationsService) CreateHook(ctx context.Context, org string, hook *Hook) (*Hook, *Response, error) {
u := fmt.Sprintf("orgs/%v/hooks", org)
req, err := s.client.NewRequest("POST", u, hook)
if err != nil {
@ -57,7 +60,7 @@ func (s *OrganizationsService) CreateHook(org string, hook *Hook) (*Hook, *Respo
}
h := new(Hook)
resp, err := s.client.Do(req, h)
resp, err := s.client.Do(ctx, req, h)
if err != nil {
return nil, resp, err
}
@ -68,37 +71,37 @@ func (s *OrganizationsService) CreateHook(org string, hook *Hook) (*Hook, *Respo
// EditHook updates a specified Hook.
//
// GitHub API docs: https://developer.github.com/v3/orgs/hooks/#edit-a-hook
func (s *OrganizationsService) EditHook(org string, id int, hook *Hook) (*Hook, *Response, error) {
func (s *OrganizationsService) EditHook(ctx context.Context, org string, id int, hook *Hook) (*Hook, *Response, error) {
u := fmt.Sprintf("orgs/%v/hooks/%d", org, id)
req, err := s.client.NewRequest("PATCH", u, hook)
if err != nil {
return nil, nil, err
}
h := new(Hook)
resp, err := s.client.Do(req, h)
resp, err := s.client.Do(ctx, req, h)
return h, resp, err
}
// PingHook triggers a 'ping' event to be sent to the Hook.
//
// GitHub API docs: https://developer.github.com/v3/orgs/hooks/#ping-a-hook
func (s *OrganizationsService) PingHook(org string, id int) (*Response, error) {
func (s *OrganizationsService) PingHook(ctx context.Context, org string, id int) (*Response, error) {
u := fmt.Sprintf("orgs/%v/hooks/%d/pings", org, id)
req, err := s.client.NewRequest("POST", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}
// DeleteHook deletes a specified Hook.
//
// GitHub API docs: https://developer.github.com/v3/orgs/hooks/#delete-a-hook
func (s *OrganizationsService) DeleteHook(org string, id int) (*Response, error) {
func (s *OrganizationsService) DeleteHook(ctx context.Context, org string, id int) (*Response, error) {
u := fmt.Sprintf("orgs/%v/hooks/%d", org, id)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}

View file

@ -5,7 +5,10 @@
package github
import "fmt"
import (
"context"
"fmt"
)
// Membership represents the status of a user's membership in an organization or team.
type Membership struct {
@ -68,8 +71,8 @@ type ListMembersOptions struct {
// user is an owner of the organization, this will return both concealed and
// public members, otherwise it will only return public members.
//
// GitHub API docs: http://developer.github.com/v3/orgs/members/#members-list
func (s *OrganizationsService) ListMembers(org string, opt *ListMembersOptions) ([]*User, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/orgs/members/#members-list
func (s *OrganizationsService) ListMembers(ctx context.Context, org string, opt *ListMembersOptions) ([]*User, *Response, error) {
var u string
if opt != nil && opt.PublicOnly {
u = fmt.Sprintf("orgs/%v/public_members", org)
@ -87,7 +90,7 @@ func (s *OrganizationsService) ListMembers(org string, opt *ListMembersOptions)
}
var members []*User
resp, err := s.client.Do(req, &members)
resp, err := s.client.Do(ctx, req, &members)
if err != nil {
return nil, resp, err
}
@ -97,72 +100,72 @@ func (s *OrganizationsService) ListMembers(org string, opt *ListMembersOptions)
// IsMember checks if a user is a member of an organization.
//
// GitHub API docs: http://developer.github.com/v3/orgs/members/#check-membership
func (s *OrganizationsService) IsMember(org, user string) (bool, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/orgs/members/#check-membership
func (s *OrganizationsService) IsMember(ctx context.Context, org, user string) (bool, *Response, error) {
u := fmt.Sprintf("orgs/%v/members/%v", org, user)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return false, nil, err
}
resp, err := s.client.Do(req, nil)
resp, err := s.client.Do(ctx, req, nil)
member, err := parseBoolResponse(err)
return member, resp, err
}
// IsPublicMember checks if a user is a public member of an organization.
//
// GitHub API docs: http://developer.github.com/v3/orgs/members/#check-public-membership
func (s *OrganizationsService) IsPublicMember(org, user string) (bool, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/orgs/members/#check-public-membership
func (s *OrganizationsService) IsPublicMember(ctx context.Context, org, user string) (bool, *Response, error) {
u := fmt.Sprintf("orgs/%v/public_members/%v", org, user)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return false, nil, err
}
resp, err := s.client.Do(req, nil)
resp, err := s.client.Do(ctx, req, nil)
member, err := parseBoolResponse(err)
return member, resp, err
}
// RemoveMember removes a user from all teams of an organization.
//
// GitHub API docs: http://developer.github.com/v3/orgs/members/#remove-a-member
func (s *OrganizationsService) RemoveMember(org, user string) (*Response, error) {
// GitHub API docs: https://developer.github.com/v3/orgs/members/#remove-a-member
func (s *OrganizationsService) RemoveMember(ctx context.Context, org, user string) (*Response, error) {
u := fmt.Sprintf("orgs/%v/members/%v", org, user)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}
// PublicizeMembership publicizes a user's membership in an organization. (A
// user cannot publicize the membership for another user.)
//
// GitHub API docs: http://developer.github.com/v3/orgs/members/#publicize-a-users-membership
func (s *OrganizationsService) PublicizeMembership(org, user string) (*Response, error) {
// GitHub API docs: https://developer.github.com/v3/orgs/members/#publicize-a-users-membership
func (s *OrganizationsService) PublicizeMembership(ctx context.Context, org, user string) (*Response, error) {
u := fmt.Sprintf("orgs/%v/public_members/%v", org, user)
req, err := s.client.NewRequest("PUT", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}
// ConcealMembership conceals a user's membership in an organization.
//
// GitHub API docs: http://developer.github.com/v3/orgs/members/#conceal-a-users-membership
func (s *OrganizationsService) ConcealMembership(org, user string) (*Response, error) {
// GitHub API docs: https://developer.github.com/v3/orgs/members/#conceal-a-users-membership
func (s *OrganizationsService) ConcealMembership(ctx context.Context, org, user string) (*Response, error) {
u := fmt.Sprintf("orgs/%v/public_members/%v", org, user)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}
// ListOrgMembershipsOptions specifies optional parameters to the
@ -178,7 +181,7 @@ type ListOrgMembershipsOptions struct {
// ListOrgMemberships lists the organization memberships for the authenticated user.
//
// GitHub API docs: https://developer.github.com/v3/orgs/members/#list-your-organization-memberships
func (s *OrganizationsService) ListOrgMemberships(opt *ListOrgMembershipsOptions) ([]*Membership, *Response, error) {
func (s *OrganizationsService) ListOrgMemberships(ctx context.Context, opt *ListOrgMembershipsOptions) ([]*Membership, *Response, error) {
u := "user/memberships/orgs"
u, err := addOptions(u, opt)
if err != nil {
@ -191,7 +194,7 @@ func (s *OrganizationsService) ListOrgMemberships(opt *ListOrgMembershipsOptions
}
var memberships []*Membership
resp, err := s.client.Do(req, &memberships)
resp, err := s.client.Do(ctx, req, &memberships)
if err != nil {
return nil, resp, err
}
@ -203,9 +206,10 @@ func (s *OrganizationsService) ListOrgMemberships(opt *ListOrgMembershipsOptions
// Passing an empty string for user will get the membership for the
// authenticated user.
//
// GitHub API docs: https://developer.github.com/v3/orgs/members/#get-organization-membership
// GitHub API docs: https://developer.github.com/v3/orgs/members/#get-your-organization-membership
func (s *OrganizationsService) GetOrgMembership(user, org string) (*Membership, *Response, error) {
// GitHub API docs:
// https://developer.github.com/v3/orgs/members/#get-organization-membership
// https://developer.github.com/v3/orgs/members/#get-your-organization-membership
func (s *OrganizationsService) GetOrgMembership(ctx context.Context, user, org string) (*Membership, *Response, error) {
var u string
if user != "" {
u = fmt.Sprintf("orgs/%v/memberships/%v", org, user)
@ -219,7 +223,7 @@ func (s *OrganizationsService) GetOrgMembership(user, org string) (*Membership,
}
membership := new(Membership)
resp, err := s.client.Do(req, membership)
resp, err := s.client.Do(ctx, req, membership)
if err != nil {
return nil, resp, err
}
@ -233,7 +237,7 @@ func (s *OrganizationsService) GetOrgMembership(user, org string) (*Membership,
//
// GitHub API docs: https://developer.github.com/v3/orgs/members/#add-or-update-organization-membership
// GitHub API docs: https://developer.github.com/v3/orgs/members/#edit-your-organization-membership
func (s *OrganizationsService) EditOrgMembership(user, org string, membership *Membership) (*Membership, *Response, error) {
func (s *OrganizationsService) EditOrgMembership(ctx context.Context, user, org string, membership *Membership) (*Membership, *Response, error) {
var u, method string
if user != "" {
u = fmt.Sprintf("orgs/%v/memberships/%v", org, user)
@ -249,7 +253,7 @@ func (s *OrganizationsService) EditOrgMembership(user, org string, membership *M
}
m := new(Membership)
resp, err := s.client.Do(req, m)
resp, err := s.client.Do(ctx, req, m)
if err != nil {
return nil, resp, err
}
@ -261,20 +265,20 @@ func (s *OrganizationsService) EditOrgMembership(user, org string, membership *M
// user has been invited to the organization, this will cancel their invitation.
//
// GitHub API docs: https://developer.github.com/v3/orgs/members/#remove-organization-membership
func (s *OrganizationsService) RemoveOrgMembership(user, org string) (*Response, error) {
func (s *OrganizationsService) RemoveOrgMembership(ctx context.Context, user, org string) (*Response, error) {
u := fmt.Sprintf("orgs/%v/memberships/%v", org, user)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}
// ListPendingOrgInvitations returns a list of pending invitations.
//
// GitHub API docs: https://developer.github.com/v3/orgs/members/#list-pending-organization-invitations
func (s *OrganizationsService) ListPendingOrgInvitations(org int, opt *ListOptions) ([]*Invitation, *Response, error) {
func (s *OrganizationsService) ListPendingOrgInvitations(ctx context.Context, org int, opt *ListOptions) ([]*Invitation, *Response, error) {
u := fmt.Sprintf("orgs/%v/invitations", org)
u, err := addOptions(u, opt)
if err != nil {
@ -290,7 +294,7 @@ func (s *OrganizationsService) ListPendingOrgInvitations(org int, opt *ListOptio
req.Header.Set("Accept", mediaTypeOrgMembershipPreview)
var pendingInvitations []*Invitation
resp, err := s.client.Do(req, &pendingInvitations)
resp, err := s.client.Do(ctx, req, &pendingInvitations)
if err != nil {
return nil, resp, err
}

View file

@ -0,0 +1,53 @@
// Copyright 2017 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package github
import (
"context"
"fmt"
)
// ListOutsideCollaboratorsOptions specifies optional parameters to the
// OrganizationsService.ListOutsideCollaborators method.
type ListOutsideCollaboratorsOptions struct {
// Filter outside collaborators returned in the list. Possible values are:
// 2fa_disabled, all. Default is "all".
Filter string `url:"filter,omitempty"`
ListOptions
}
// ListOutsideCollaborators lists outside collaborators of organization's repositories.
// This will only work if the authenticated
// user is an owner of the organization.
//
// Warning: The API may change without advance notice during the preview period.
// Preview features are not supported for production use.
//
// GitHub API docs: https://developer.github.com/v3/orgs/outside_collaborators/#list-outside-collaborators
func (s *OrganizationsService) ListOutsideCollaborators(ctx context.Context, org string, opt *ListOutsideCollaboratorsOptions) ([]*User, *Response, error) {
u := fmt.Sprintf("orgs/%v/outside_collaborators", org)
u, err := addOptions(u, opt)
if err != nil {
return nil, nil, err
}
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
// TODO: remove custom Accept header when this API fully launches.
req.Header.Set("Accept", mediaTypeOrgMembershipPreview)
var members []*User
resp, err := s.client.Do(ctx, req, &members)
if err != nil {
return nil, resp, err
}
return members, resp, nil
}

View file

@ -6,6 +6,7 @@
package github
import (
"context"
"fmt"
"time"
)
@ -61,8 +62,8 @@ func (i Invitation) String() string {
// ListTeams lists all of the teams for an organization.
//
// GitHub API docs: http://developer.github.com/v3/orgs/teams/#list-teams
func (s *OrganizationsService) ListTeams(org string, opt *ListOptions) ([]*Team, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/orgs/teams/#list-teams
func (s *OrganizationsService) ListTeams(ctx context.Context, org string, opt *ListOptions) ([]*Team, *Response, error) {
u := fmt.Sprintf("orgs/%v/teams", org)
u, err := addOptions(u, opt)
if err != nil {
@ -75,7 +76,7 @@ func (s *OrganizationsService) ListTeams(org string, opt *ListOptions) ([]*Team,
}
var teams []*Team
resp, err := s.client.Do(req, &teams)
resp, err := s.client.Do(ctx, req, &teams)
if err != nil {
return nil, resp, err
}
@ -85,8 +86,8 @@ func (s *OrganizationsService) ListTeams(org string, opt *ListOptions) ([]*Team,
// GetTeam fetches a team by ID.
//
// GitHub API docs: http://developer.github.com/v3/orgs/teams/#get-team
func (s *OrganizationsService) GetTeam(team int) (*Team, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/orgs/teams/#get-team
func (s *OrganizationsService) GetTeam(ctx context.Context, team int) (*Team, *Response, error) {
u := fmt.Sprintf("teams/%v", team)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
@ -94,7 +95,7 @@ func (s *OrganizationsService) GetTeam(team int) (*Team, *Response, error) {
}
t := new(Team)
resp, err := s.client.Do(req, t)
resp, err := s.client.Do(ctx, req, t)
if err != nil {
return nil, resp, err
}
@ -104,8 +105,8 @@ func (s *OrganizationsService) GetTeam(team int) (*Team, *Response, error) {
// CreateTeam creates a new team within an organization.
//
// GitHub API docs: http://developer.github.com/v3/orgs/teams/#create-team
func (s *OrganizationsService) CreateTeam(org string, team *Team) (*Team, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/orgs/teams/#create-team
func (s *OrganizationsService) CreateTeam(ctx context.Context, org string, team *Team) (*Team, *Response, error) {
u := fmt.Sprintf("orgs/%v/teams", org)
req, err := s.client.NewRequest("POST", u, team)
if err != nil {
@ -113,7 +114,7 @@ func (s *OrganizationsService) CreateTeam(org string, team *Team) (*Team, *Respo
}
t := new(Team)
resp, err := s.client.Do(req, t)
resp, err := s.client.Do(ctx, req, t)
if err != nil {
return nil, resp, err
}
@ -123,8 +124,8 @@ func (s *OrganizationsService) CreateTeam(org string, team *Team) (*Team, *Respo
// EditTeam edits a team.
//
// GitHub API docs: http://developer.github.com/v3/orgs/teams/#edit-team
func (s *OrganizationsService) EditTeam(id int, team *Team) (*Team, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/orgs/teams/#edit-team
func (s *OrganizationsService) EditTeam(ctx context.Context, id int, team *Team) (*Team, *Response, error) {
u := fmt.Sprintf("teams/%v", id)
req, err := s.client.NewRequest("PATCH", u, team)
if err != nil {
@ -132,7 +133,7 @@ func (s *OrganizationsService) EditTeam(id int, team *Team) (*Team, *Response, e
}
t := new(Team)
resp, err := s.client.Do(req, t)
resp, err := s.client.Do(ctx, req, t)
if err != nil {
return nil, resp, err
}
@ -142,15 +143,15 @@ func (s *OrganizationsService) EditTeam(id int, team *Team) (*Team, *Response, e
// DeleteTeam deletes a team.
//
// GitHub API docs: http://developer.github.com/v3/orgs/teams/#delete-team
func (s *OrganizationsService) DeleteTeam(team int) (*Response, error) {
// GitHub API docs: https://developer.github.com/v3/orgs/teams/#delete-team
func (s *OrganizationsService) DeleteTeam(ctx context.Context, team int) (*Response, error) {
u := fmt.Sprintf("teams/%v", team)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}
// OrganizationListTeamMembersOptions specifies the optional parameters to the
@ -166,8 +167,8 @@ type OrganizationListTeamMembersOptions struct {
// ListTeamMembers lists all of the users who are members of the specified
// team.
//
// GitHub API docs: http://developer.github.com/v3/orgs/teams/#list-team-members
func (s *OrganizationsService) ListTeamMembers(team int, opt *OrganizationListTeamMembersOptions) ([]*User, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/orgs/teams/#list-team-members
func (s *OrganizationsService) ListTeamMembers(ctx context.Context, team int, opt *OrganizationListTeamMembersOptions) ([]*User, *Response, error) {
u := fmt.Sprintf("teams/%v/members", team)
u, err := addOptions(u, opt)
if err != nil {
@ -180,7 +181,7 @@ func (s *OrganizationsService) ListTeamMembers(team int, opt *OrganizationListTe
}
var members []*User
resp, err := s.client.Do(req, &members)
resp, err := s.client.Do(ctx, req, &members)
if err != nil {
return nil, resp, err
}
@ -190,23 +191,23 @@ func (s *OrganizationsService) ListTeamMembers(team int, opt *OrganizationListTe
// IsTeamMember checks if a user is a member of the specified team.
//
// GitHub API docs: http://developer.github.com/v3/orgs/teams/#get-team-member
func (s *OrganizationsService) IsTeamMember(team int, user string) (bool, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/orgs/teams/#get-team-member
func (s *OrganizationsService) IsTeamMember(ctx context.Context, team int, user string) (bool, *Response, error) {
u := fmt.Sprintf("teams/%v/members/%v", team, user)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return false, nil, err
}
resp, err := s.client.Do(req, nil)
resp, err := s.client.Do(ctx, req, nil)
member, err := parseBoolResponse(err)
return member, resp, err
}
// ListTeamRepos lists the repositories that the specified team has access to.
//
// GitHub API docs: http://developer.github.com/v3/orgs/teams/#list-team-repos
func (s *OrganizationsService) ListTeamRepos(team int, opt *ListOptions) ([]*Repository, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/orgs/teams/#list-team-repos
func (s *OrganizationsService) ListTeamRepos(ctx context.Context, team int, opt *ListOptions) ([]*Repository, *Response, error) {
u := fmt.Sprintf("teams/%v/repos", team)
u, err := addOptions(u, opt)
if err != nil {
@ -219,7 +220,7 @@ func (s *OrganizationsService) ListTeamRepos(team int, opt *ListOptions) ([]*Rep
}
var repos []*Repository
resp, err := s.client.Do(req, &repos)
resp, err := s.client.Do(ctx, req, &repos)
if err != nil {
return nil, resp, err
}
@ -232,7 +233,7 @@ func (s *OrganizationsService) ListTeamRepos(team int, opt *ListOptions) ([]*Rep
// permissions team has for that repo.
//
// GitHub API docs: https://developer.github.com/v3/orgs/teams/#check-if-a-team-manages-a-repository
func (s *OrganizationsService) IsTeamRepo(team int, owner string, repo string) (*Repository, *Response, error) {
func (s *OrganizationsService) IsTeamRepo(ctx context.Context, team int, owner string, repo string) (*Repository, *Response, error) {
u := fmt.Sprintf("teams/%v/repos/%v/%v", team, owner, repo)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
@ -242,7 +243,7 @@ func (s *OrganizationsService) IsTeamRepo(team int, owner string, repo string) (
req.Header.Set("Accept", mediaTypeOrgPermissionRepo)
repository := new(Repository)
resp, err := s.client.Do(req, repository)
resp, err := s.client.Do(ctx, req, repository)
if err != nil {
return nil, resp, err
}
@ -267,35 +268,35 @@ type OrganizationAddTeamRepoOptions struct {
// specified repository must be owned by the organization to which the team
// belongs, or a direct fork of a repository owned by the organization.
//
// GitHub API docs: http://developer.github.com/v3/orgs/teams/#add-team-repo
func (s *OrganizationsService) AddTeamRepo(team int, owner string, repo string, opt *OrganizationAddTeamRepoOptions) (*Response, error) {
// GitHub API docs: https://developer.github.com/v3/orgs/teams/#add-team-repo
func (s *OrganizationsService) AddTeamRepo(ctx context.Context, team int, owner string, repo string, opt *OrganizationAddTeamRepoOptions) (*Response, error) {
u := fmt.Sprintf("teams/%v/repos/%v/%v", team, owner, repo)
req, err := s.client.NewRequest("PUT", u, opt)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}
// RemoveTeamRepo removes a repository from being managed by the specified
// team. Note that this does not delete the repository, it just removes it
// from the team.
//
// GitHub API docs: http://developer.github.com/v3/orgs/teams/#remove-team-repo
func (s *OrganizationsService) RemoveTeamRepo(team int, owner string, repo string) (*Response, error) {
// GitHub API docs: https://developer.github.com/v3/orgs/teams/#remove-team-repo
func (s *OrganizationsService) RemoveTeamRepo(ctx context.Context, team int, owner string, repo string) (*Response, error) {
u := fmt.Sprintf("teams/%v/repos/%v/%v", team, owner, repo)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}
// ListUserTeams lists a user's teams
// GitHub API docs: https://developer.github.com/v3/orgs/teams/#list-user-teams
func (s *OrganizationsService) ListUserTeams(opt *ListOptions) ([]*Team, *Response, error) {
func (s *OrganizationsService) ListUserTeams(ctx context.Context, opt *ListOptions) ([]*Team, *Response, error) {
u := "user/teams"
u, err := addOptions(u, opt)
if err != nil {
@ -308,7 +309,7 @@ func (s *OrganizationsService) ListUserTeams(opt *ListOptions) ([]*Team, *Respon
}
var teams []*Team
resp, err := s.client.Do(req, &teams)
resp, err := s.client.Do(ctx, req, &teams)
if err != nil {
return nil, resp, err
}
@ -319,7 +320,7 @@ func (s *OrganizationsService) ListUserTeams(opt *ListOptions) ([]*Team, *Respon
// GetTeamMembership returns the membership status for a user in a team.
//
// GitHub API docs: https://developer.github.com/v3/orgs/teams/#get-team-membership
func (s *OrganizationsService) GetTeamMembership(team int, user string) (*Membership, *Response, error) {
func (s *OrganizationsService) GetTeamMembership(ctx context.Context, team int, user string) (*Membership, *Response, error) {
u := fmt.Sprintf("teams/%v/memberships/%v", team, user)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
@ -327,7 +328,7 @@ func (s *OrganizationsService) GetTeamMembership(team int, user string) (*Member
}
t := new(Membership)
resp, err := s.client.Do(req, t)
resp, err := s.client.Do(ctx, req, t)
if err != nil {
return nil, resp, err
}
@ -367,7 +368,7 @@ type OrganizationAddTeamMembershipOptions struct {
// added as a member of the team.
//
// GitHub API docs: https://developer.github.com/v3/orgs/teams/#add-team-membership
func (s *OrganizationsService) AddTeamMembership(team int, user string, opt *OrganizationAddTeamMembershipOptions) (*Membership, *Response, error) {
func (s *OrganizationsService) AddTeamMembership(ctx context.Context, team int, user string, opt *OrganizationAddTeamMembershipOptions) (*Membership, *Response, error) {
u := fmt.Sprintf("teams/%v/memberships/%v", team, user)
req, err := s.client.NewRequest("PUT", u, opt)
if err != nil {
@ -375,7 +376,7 @@ func (s *OrganizationsService) AddTeamMembership(team int, user string, opt *Org
}
t := new(Membership)
resp, err := s.client.Do(req, t)
resp, err := s.client.Do(ctx, req, t)
if err != nil {
return nil, resp, err
}
@ -386,14 +387,14 @@ func (s *OrganizationsService) AddTeamMembership(team int, user string, opt *Org
// RemoveTeamMembership removes a user from a team.
//
// GitHub API docs: https://developer.github.com/v3/orgs/teams/#remove-team-membership
func (s *OrganizationsService) RemoveTeamMembership(team int, user string) (*Response, error) {
func (s *OrganizationsService) RemoveTeamMembership(ctx context.Context, team int, user string) (*Response, error) {
u := fmt.Sprintf("teams/%v/memberships/%v", team, user)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}
// ListPendingTeamInvitations get pending invitaion list in team.
@ -401,7 +402,7 @@ func (s *OrganizationsService) RemoveTeamMembership(team int, user string) (*Res
// Preview features are not supported for production use.
//
// GitHub API docs: https://developer.github.com/v3/orgs/teams/#list-pending-team-invitations
func (s *OrganizationsService) ListPendingTeamInvitations(team int, opt *ListOptions) ([]*Invitation, *Response, error) {
func (s *OrganizationsService) ListPendingTeamInvitations(ctx context.Context, team int, opt *ListOptions) ([]*Invitation, *Response, error) {
u := fmt.Sprintf("teams/%v/invitations", team)
u, err := addOptions(u, opt)
if err != nil {
@ -417,7 +418,7 @@ func (s *OrganizationsService) ListPendingTeamInvitations(team int, opt *ListOpt
req.Header.Set("Accept", mediaTypeOrgMembershipPreview)
var pendingInvitations []*Invitation
resp, err := s.client.Do(req, &pendingInvitations)
resp, err := s.client.Do(ctx, req, &pendingInvitations)
if err != nil {
return nil, resp, err
}

View file

@ -5,7 +5,10 @@
package github
import "fmt"
import (
"context"
"fmt"
)
// ProjectsService provides access to the projects functions in the
// GitHub API.
@ -35,7 +38,7 @@ func (p Project) String() string {
// GetProject gets a GitHub Project for a repo.
//
// GitHub API docs: https://developer.github.com/v3/projects/#get-a-project
func (s *ProjectsService) GetProject(id int) (*Project, *Response, error) {
func (s *ProjectsService) GetProject(ctx context.Context, id int) (*Project, *Response, error) {
u := fmt.Sprintf("projects/%v", id)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
@ -46,7 +49,7 @@ func (s *ProjectsService) GetProject(id int) (*Project, *Response, error) {
req.Header.Set("Accept", mediaTypeProjectsPreview)
project := &Project{}
resp, err := s.client.Do(req, project)
resp, err := s.client.Do(ctx, req, project)
if err != nil {
return nil, resp, err
}
@ -67,7 +70,7 @@ type ProjectOptions struct {
// UpdateProject updates a repository project.
//
// GitHub API docs: https://developer.github.com/v3/projects/#update-a-project
func (s *ProjectsService) UpdateProject(id int, opt *ProjectOptions) (*Project, *Response, error) {
func (s *ProjectsService) UpdateProject(ctx context.Context, id int, opt *ProjectOptions) (*Project, *Response, error) {
u := fmt.Sprintf("projects/%v", id)
req, err := s.client.NewRequest("PATCH", u, opt)
if err != nil {
@ -78,7 +81,7 @@ func (s *ProjectsService) UpdateProject(id int, opt *ProjectOptions) (*Project,
req.Header.Set("Accept", mediaTypeProjectsPreview)
project := &Project{}
resp, err := s.client.Do(req, project)
resp, err := s.client.Do(ctx, req, project)
if err != nil {
return nil, resp, err
}
@ -89,7 +92,7 @@ func (s *ProjectsService) UpdateProject(id int, opt *ProjectOptions) (*Project,
// DeleteProject deletes a GitHub Project from a repository.
//
// GitHub API docs: https://developer.github.com/v3/projects/#delete-a-project
func (s *ProjectsService) DeleteProject(id int) (*Response, error) {
func (s *ProjectsService) DeleteProject(ctx context.Context, id int) (*Response, error) {
u := fmt.Sprintf("projects/%v", id)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
@ -99,7 +102,7 @@ func (s *ProjectsService) DeleteProject(id int) (*Response, error) {
// TODO: remove custom Accept header when this API fully launches.
req.Header.Set("Accept", mediaTypeProjectsPreview)
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}
// ProjectColumn represents a column of a GitHub Project.
@ -116,7 +119,7 @@ type ProjectColumn struct {
// ListProjectColumns lists the columns of a GitHub Project for a repo.
//
// GitHub API docs: https://developer.github.com/v3/projects/columns/#list-project-columns
func (s *ProjectsService) ListProjectColumns(projectID int, opt *ListOptions) ([]*ProjectColumn, *Response, error) {
func (s *ProjectsService) ListProjectColumns(ctx context.Context, projectID int, opt *ListOptions) ([]*ProjectColumn, *Response, error) {
u := fmt.Sprintf("projects/%v/columns", projectID)
u, err := addOptions(u, opt)
if err != nil {
@ -132,7 +135,7 @@ func (s *ProjectsService) ListProjectColumns(projectID int, opt *ListOptions) ([
req.Header.Set("Accept", mediaTypeProjectsPreview)
columns := []*ProjectColumn{}
resp, err := s.client.Do(req, &columns)
resp, err := s.client.Do(ctx, req, &columns)
if err != nil {
return nil, resp, err
}
@ -143,7 +146,7 @@ func (s *ProjectsService) ListProjectColumns(projectID int, opt *ListOptions) ([
// GetProjectColumn gets a column of a GitHub Project for a repo.
//
// GitHub API docs: https://developer.github.com/v3/projects/columns/#get-a-project-column
func (s *ProjectsService) GetProjectColumn(id int) (*ProjectColumn, *Response, error) {
func (s *ProjectsService) GetProjectColumn(ctx context.Context, id int) (*ProjectColumn, *Response, error) {
u := fmt.Sprintf("projects/columns/%v", id)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
@ -154,7 +157,7 @@ func (s *ProjectsService) GetProjectColumn(id int) (*ProjectColumn, *Response, e
req.Header.Set("Accept", mediaTypeProjectsPreview)
column := &ProjectColumn{}
resp, err := s.client.Do(req, column)
resp, err := s.client.Do(ctx, req, column)
if err != nil {
return nil, resp, err
}
@ -173,7 +176,7 @@ type ProjectColumnOptions struct {
// CreateProjectColumn creates a column for the specified (by number) project.
//
// GitHub API docs: https://developer.github.com/v3/projects/columns/#create-a-project-column
func (s *ProjectsService) CreateProjectColumn(projectID int, opt *ProjectColumnOptions) (*ProjectColumn, *Response, error) {
func (s *ProjectsService) CreateProjectColumn(ctx context.Context, projectID int, opt *ProjectColumnOptions) (*ProjectColumn, *Response, error) {
u := fmt.Sprintf("projects/%v/columns", projectID)
req, err := s.client.NewRequest("POST", u, opt)
if err != nil {
@ -184,7 +187,7 @@ func (s *ProjectsService) CreateProjectColumn(projectID int, opt *ProjectColumnO
req.Header.Set("Accept", mediaTypeProjectsPreview)
column := &ProjectColumn{}
resp, err := s.client.Do(req, column)
resp, err := s.client.Do(ctx, req, column)
if err != nil {
return nil, resp, err
}
@ -195,7 +198,7 @@ func (s *ProjectsService) CreateProjectColumn(projectID int, opt *ProjectColumnO
// UpdateProjectColumn updates a column of a GitHub Project.
//
// GitHub API docs: https://developer.github.com/v3/projects/columns/#update-a-project-column
func (s *ProjectsService) UpdateProjectColumn(columnID int, opt *ProjectColumnOptions) (*ProjectColumn, *Response, error) {
func (s *ProjectsService) UpdateProjectColumn(ctx context.Context, columnID int, opt *ProjectColumnOptions) (*ProjectColumn, *Response, error) {
u := fmt.Sprintf("projects/columns/%v", columnID)
req, err := s.client.NewRequest("PATCH", u, opt)
if err != nil {
@ -206,7 +209,7 @@ func (s *ProjectsService) UpdateProjectColumn(columnID int, opt *ProjectColumnOp
req.Header.Set("Accept", mediaTypeProjectsPreview)
column := &ProjectColumn{}
resp, err := s.client.Do(req, column)
resp, err := s.client.Do(ctx, req, column)
if err != nil {
return nil, resp, err
}
@ -217,7 +220,7 @@ func (s *ProjectsService) UpdateProjectColumn(columnID int, opt *ProjectColumnOp
// DeleteProjectColumn deletes a column from a GitHub Project.
//
// GitHub API docs: https://developer.github.com/v3/projects/columns/#delete-a-project-column
func (s *ProjectsService) DeleteProjectColumn(columnID int) (*Response, error) {
func (s *ProjectsService) DeleteProjectColumn(ctx context.Context, columnID int) (*Response, error) {
u := fmt.Sprintf("projects/columns/%v", columnID)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
@ -227,7 +230,7 @@ func (s *ProjectsService) DeleteProjectColumn(columnID int) (*Response, error) {
// TODO: remove custom Accept header when this API fully launches.
req.Header.Set("Accept", mediaTypeProjectsPreview)
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}
// ProjectColumnMoveOptions specifies the parameters to the
@ -241,7 +244,7 @@ type ProjectColumnMoveOptions struct {
// MoveProjectColumn moves a column within a GitHub Project.
//
// GitHub API docs: https://developer.github.com/v3/projects/columns/#move-a-project-column
func (s *ProjectsService) MoveProjectColumn(columnID int, opt *ProjectColumnMoveOptions) (*Response, error) {
func (s *ProjectsService) MoveProjectColumn(ctx context.Context, columnID int, opt *ProjectColumnMoveOptions) (*Response, error) {
u := fmt.Sprintf("projects/columns/%v/moves", columnID)
req, err := s.client.NewRequest("POST", u, opt)
if err != nil {
@ -251,7 +254,7 @@ func (s *ProjectsService) MoveProjectColumn(columnID int, opt *ProjectColumnMove
// TODO: remove custom Accept header when this API fully launches.
req.Header.Set("Accept", mediaTypeProjectsPreview)
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}
// ProjectCard represents a card in a column of a GitHub Project.
@ -269,7 +272,7 @@ type ProjectCard struct {
// ListProjectCards lists the cards in a column of a GitHub Project.
//
// GitHub API docs: https://developer.github.com/v3/projects/cards/#list-project-cards
func (s *ProjectsService) ListProjectCards(columnID int, opt *ListOptions) ([]*ProjectCard, *Response, error) {
func (s *ProjectsService) ListProjectCards(ctx context.Context, columnID int, opt *ListOptions) ([]*ProjectCard, *Response, error) {
u := fmt.Sprintf("projects/columns/%v/cards", columnID)
u, err := addOptions(u, opt)
if err != nil {
@ -285,7 +288,7 @@ func (s *ProjectsService) ListProjectCards(columnID int, opt *ListOptions) ([]*P
req.Header.Set("Accept", mediaTypeProjectsPreview)
cards := []*ProjectCard{}
resp, err := s.client.Do(req, &cards)
resp, err := s.client.Do(ctx, req, &cards)
if err != nil {
return nil, resp, err
}
@ -296,7 +299,7 @@ func (s *ProjectsService) ListProjectCards(columnID int, opt *ListOptions) ([]*P
// GetProjectCard gets a card in a column of a GitHub Project.
//
// GitHub API docs: https://developer.github.com/v3/projects/cards/#get-a-project-card
func (s *ProjectsService) GetProjectCard(columnID int) (*ProjectCard, *Response, error) {
func (s *ProjectsService) GetProjectCard(ctx context.Context, columnID int) (*ProjectCard, *Response, error) {
u := fmt.Sprintf("projects/columns/cards/%v", columnID)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
@ -307,7 +310,7 @@ func (s *ProjectsService) GetProjectCard(columnID int) (*ProjectCard, *Response,
req.Header.Set("Accept", mediaTypeProjectsPreview)
card := &ProjectCard{}
resp, err := s.client.Do(req, card)
resp, err := s.client.Do(ctx, req, card)
if err != nil {
return nil, resp, err
}
@ -331,7 +334,7 @@ type ProjectCardOptions struct {
// CreateProjectCard creates a card in the specified column of a GitHub Project.
//
// GitHub API docs: https://developer.github.com/v3/projects/cards/#create-a-project-card
func (s *ProjectsService) CreateProjectCard(columnID int, opt *ProjectCardOptions) (*ProjectCard, *Response, error) {
func (s *ProjectsService) CreateProjectCard(ctx context.Context, columnID int, opt *ProjectCardOptions) (*ProjectCard, *Response, error) {
u := fmt.Sprintf("projects/columns/%v/cards", columnID)
req, err := s.client.NewRequest("POST", u, opt)
if err != nil {
@ -342,7 +345,7 @@ func (s *ProjectsService) CreateProjectCard(columnID int, opt *ProjectCardOption
req.Header.Set("Accept", mediaTypeProjectsPreview)
card := &ProjectCard{}
resp, err := s.client.Do(req, card)
resp, err := s.client.Do(ctx, req, card)
if err != nil {
return nil, resp, err
}
@ -353,7 +356,7 @@ func (s *ProjectsService) CreateProjectCard(columnID int, opt *ProjectCardOption
// UpdateProjectCard updates a card of a GitHub Project.
//
// GitHub API docs: https://developer.github.com/v3/projects/cards/#update-a-project-card
func (s *ProjectsService) UpdateProjectCard(cardID int, opt *ProjectCardOptions) (*ProjectCard, *Response, error) {
func (s *ProjectsService) UpdateProjectCard(ctx context.Context, cardID int, opt *ProjectCardOptions) (*ProjectCard, *Response, error) {
u := fmt.Sprintf("projects/columns/cards/%v", cardID)
req, err := s.client.NewRequest("PATCH", u, opt)
if err != nil {
@ -364,7 +367,7 @@ func (s *ProjectsService) UpdateProjectCard(cardID int, opt *ProjectCardOptions)
req.Header.Set("Accept", mediaTypeProjectsPreview)
card := &ProjectCard{}
resp, err := s.client.Do(req, card)
resp, err := s.client.Do(ctx, req, card)
if err != nil {
return nil, resp, err
}
@ -375,7 +378,7 @@ func (s *ProjectsService) UpdateProjectCard(cardID int, opt *ProjectCardOptions)
// DeleteProjectCard deletes a card from a GitHub Project.
//
// GitHub API docs: https://developer.github.com/v3/projects/cards/#delete-a-project-card
func (s *ProjectsService) DeleteProjectCard(cardID int) (*Response, error) {
func (s *ProjectsService) DeleteProjectCard(ctx context.Context, cardID int) (*Response, error) {
u := fmt.Sprintf("projects/columns/cards/%v", cardID)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
@ -385,7 +388,7 @@ func (s *ProjectsService) DeleteProjectCard(cardID int) (*Response, error) {
// TODO: remove custom Accept header when this API fully launches.
req.Header.Set("Accept", mediaTypeProjectsPreview)
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}
// ProjectCardMoveOptions specifies the parameters to the
@ -403,7 +406,7 @@ type ProjectCardMoveOptions struct {
// MoveProjectCard moves a card within a GitHub Project.
//
// GitHub API docs: https://developer.github.com/v3/projects/cards/#move-a-project-card
func (s *ProjectsService) MoveProjectCard(cardID int, opt *ProjectCardMoveOptions) (*Response, error) {
func (s *ProjectsService) MoveProjectCard(ctx context.Context, cardID int, opt *ProjectCardMoveOptions) (*Response, error) {
u := fmt.Sprintf("projects/columns/cards/%v/moves", cardID)
req, err := s.client.NewRequest("POST", u, opt)
if err != nil {
@ -413,5 +416,5 @@ func (s *ProjectsService) MoveProjectCard(cardID int, opt *ProjectCardMoveOption
// TODO: remove custom Accept header when this API fully launches.
req.Header.Set("Accept", mediaTypeProjectsPreview)
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}

View file

@ -7,6 +7,7 @@ package github
import (
"bytes"
"context"
"fmt"
"time"
)
@ -14,7 +15,7 @@ import (
// PullRequestsService handles communication with the pull request related
// methods of the GitHub API.
//
// GitHub API docs: http://developer.github.com/v3/pulls/
// GitHub API docs: https://developer.github.com/v3/pulls/
type PullRequestsService service
// PullRequest represents a GitHub pull request on a repository.
@ -94,8 +95,8 @@ type PullRequestListOptions struct {
// List the pull requests for the specified repository.
//
// GitHub API docs: http://developer.github.com/v3/pulls/#list-pull-requests
func (s *PullRequestsService) List(owner string, repo string, opt *PullRequestListOptions) ([]*PullRequest, *Response, error) {
// GitHub API docs: https://developer.github.com/v3/pulls/#list-pull-requests
func (s *PullRequestsService) List(ctx context.Context, owner string, repo string, opt *PullRequestListOptions) ([]*PullRequest, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/pulls", owner, repo)
u, err := addOptions(u, opt)
if err != nil {
@ -108,7 +109,7 @@ func (s *PullRequestsService) List(owner string, repo string, opt *PullRequestLi
}
var pulls []*PullRequest
resp, err := s.client.Do(req, &pulls)
resp, err := s.client.Do(ctx, req, &pulls)
if err != nil {
return nil, resp, err
}
@ -119,7 +120,7 @@ func (s *PullRequestsService) List(owner string, repo string, opt *PullRequestLi
// Get a single pull request.
//
// GitHub API docs: https://developer.github.com/v3/pulls/#get-a-single-pull-request
func (s *PullRequestsService) Get(owner string, repo string, number int) (*PullRequest, *Response, error) {
func (s *PullRequestsService) Get(ctx context.Context, owner string, repo string, number int) (*PullRequest, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/pulls/%d", owner, repo, number)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
@ -127,7 +128,7 @@ func (s *PullRequestsService) Get(owner string, repo string, number int) (*PullR
}
pull := new(PullRequest)
resp, err := s.client.Do(req, pull)
resp, err := s.client.Do(ctx, req, pull)
if err != nil {
return nil, resp, err
}
@ -136,7 +137,7 @@ func (s *PullRequestsService) Get(owner string, repo string, number int) (*PullR
}
// GetRaw gets raw (diff or patch) format of a pull request.
func (s *PullRequestsService) GetRaw(owner string, repo string, number int, opt RawOptions) (string, *Response, error) {
func (s *PullRequestsService) GetRaw(ctx context.Context, owner string, repo string, number int, opt RawOptions) (string, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/pulls/%d", owner, repo, number)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
@ -153,7 +154,7 @@ func (s *PullRequestsService) GetRaw(owner string, repo string, number int, opt
}
ret := new(bytes.Buffer)
resp, err := s.client.Do(req, ret)
resp, err := s.client.Do(ctx, req, ret)
if err != nil {
return "", resp, err
}
@ -173,7 +174,7 @@ type NewPullRequest struct {
// Create a new pull request on the specified repository.
//
// GitHub API docs: https://developer.github.com/v3/pulls/#create-a-pull-request
func (s *PullRequestsService) Create(owner string, repo string, pull *NewPullRequest) (*PullRequest, *Response, error) {
func (s *PullRequestsService) Create(ctx context.Context, owner string, repo string, pull *NewPullRequest) (*PullRequest, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/pulls", owner, repo)
req, err := s.client.NewRequest("POST", u, pull)
if err != nil {
@ -181,7 +182,7 @@ func (s *PullRequestsService) Create(owner string, repo string, pull *NewPullReq
}
p := new(PullRequest)
resp, err := s.client.Do(req, p)
resp, err := s.client.Do(ctx, req, p)
if err != nil {
return nil, resp, err
}
@ -202,7 +203,7 @@ type pullRequestUpdate struct {
// Base.Ref updates the base branch of the pull request.
//
// GitHub API docs: https://developer.github.com/v3/pulls/#update-a-pull-request
func (s *PullRequestsService) Edit(owner string, repo string, number int, pull *PullRequest) (*PullRequest, *Response, error) {
func (s *PullRequestsService) Edit(ctx context.Context, owner string, repo string, number int, pull *PullRequest) (*PullRequest, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/pulls/%d", owner, repo, number)
update := new(pullRequestUpdate)
@ -221,7 +222,7 @@ func (s *PullRequestsService) Edit(owner string, repo string, number int, pull *
}
p := new(PullRequest)
resp, err := s.client.Do(req, p)
resp, err := s.client.Do(ctx, req, p)
if err != nil {
return nil, resp, err
}
@ -232,7 +233,7 @@ func (s *PullRequestsService) Edit(owner string, repo string, number int, pull *
// ListCommits lists the commits in a pull request.
//
// GitHub API docs: https://developer.github.com/v3/pulls/#list-commits-on-a-pull-request
func (s *PullRequestsService) ListCommits(owner string, repo string, number int, opt *ListOptions) ([]*RepositoryCommit, *Response, error) {
func (s *PullRequestsService) ListCommits(ctx context.Context, owner string, repo string, number int, opt *ListOptions) ([]*RepositoryCommit, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/pulls/%d/commits", owner, repo, number)
u, err := addOptions(u, opt)
if err != nil {
@ -245,7 +246,7 @@ func (s *PullRequestsService) ListCommits(owner string, repo string, number int,
}
var commits []*RepositoryCommit
resp, err := s.client.Do(req, &commits)
resp, err := s.client.Do(ctx, req, &commits)
if err != nil {
return nil, resp, err
}
@ -256,7 +257,7 @@ func (s *PullRequestsService) ListCommits(owner string, repo string, number int,
// ListFiles lists the files in a pull request.
//
// GitHub API docs: https://developer.github.com/v3/pulls/#list-pull-requests-files
func (s *PullRequestsService) ListFiles(owner string, repo string, number int, opt *ListOptions) ([]*CommitFile, *Response, error) {
func (s *PullRequestsService) ListFiles(ctx context.Context, owner string, repo string, number int, opt *ListOptions) ([]*CommitFile, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/pulls/%d/files", owner, repo, number)
u, err := addOptions(u, opt)
if err != nil {
@ -269,7 +270,7 @@ func (s *PullRequestsService) ListFiles(owner string, repo string, number int, o
}
var commitFiles []*CommitFile
resp, err := s.client.Do(req, &commitFiles)
resp, err := s.client.Do(ctx, req, &commitFiles)
if err != nil {
return nil, resp, err
}
@ -280,14 +281,14 @@ func (s *PullRequestsService) ListFiles(owner string, repo string, number int, o
// IsMerged checks if a pull request has been merged.
//
// GitHub API docs: https://developer.github.com/v3/pulls/#get-if-a-pull-request-has-been-merged
func (s *PullRequestsService) IsMerged(owner string, repo string, number int) (bool, *Response, error) {
func (s *PullRequestsService) IsMerged(ctx context.Context, owner string, repo string, number int) (bool, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/pulls/%d/merge", owner, repo, number)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return false, nil, err
}
resp, err := s.client.Do(req, nil)
resp, err := s.client.Do(ctx, req, nil)
merged, err := parseBoolResponse(err)
return merged, resp, err
}
@ -319,7 +320,7 @@ type pullRequestMergeRequest struct {
// commitMessage is the title for the automatic commit message.
//
// GitHub API docs: https://developer.github.com/v3/pulls/#merge-a-pull-request-merge-buttontrade
func (s *PullRequestsService) Merge(owner string, repo string, number int, commitMessage string, options *PullRequestOptions) (*PullRequestMergeResult, *Response, error) {
func (s *PullRequestsService) Merge(ctx context.Context, owner string, repo string, number int, commitMessage string, options *PullRequestOptions) (*PullRequestMergeResult, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/pulls/%d/merge", owner, repo, number)
pullRequestBody := &pullRequestMergeRequest{CommitMessage: commitMessage}
@ -337,7 +338,7 @@ func (s *PullRequestsService) Merge(owner string, repo string, number int, commi
req.Header.Set("Accept", mediaTypeSquashPreview)
mergeResult := new(PullRequestMergeResult)
resp, err := s.client.Do(req, mergeResult)
resp, err := s.client.Do(ctx, req, mergeResult)
if err != nil {
return nil, resp, err
}

View file

@ -6,6 +6,7 @@
package github
import (
"context"
"fmt"
"time"
)
@ -54,7 +55,7 @@ type PullRequestListCommentsOptions struct {
// the repository.
//
// GitHub API docs: https://developer.github.com/v3/pulls/comments/#list-comments-on-a-pull-request
func (s *PullRequestsService) ListComments(owner string, repo string, number int, opt *PullRequestListCommentsOptions) ([]*PullRequestComment, *Response, error) {
func (s *PullRequestsService) ListComments(ctx context.Context, owner string, repo string, number int, opt *PullRequestListCommentsOptions) ([]*PullRequestComment, *Response, error) {
var u string
if number == 0 {
u = fmt.Sprintf("repos/%v/%v/pulls/comments", owner, repo)
@ -75,7 +76,7 @@ func (s *PullRequestsService) ListComments(owner string, repo string, number int
req.Header.Set("Accept", mediaTypeReactionsPreview)
var comments []*PullRequestComment
resp, err := s.client.Do(req, &comments)
resp, err := s.client.Do(ctx, req, &comments)
if err != nil {
return nil, resp, err
}
@ -86,7 +87,7 @@ func (s *PullRequestsService) ListComments(owner string, repo string, number int
// GetComment fetches the specified pull request comment.
//
// GitHub API docs: https://developer.github.com/v3/pulls/comments/#get-a-single-comment
func (s *PullRequestsService) GetComment(owner string, repo string, number int) (*PullRequestComment, *Response, error) {
func (s *PullRequestsService) GetComment(ctx context.Context, owner string, repo string, number int) (*PullRequestComment, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/pulls/comments/%d", owner, repo, number)
req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
@ -97,7 +98,7 @@ func (s *PullRequestsService) GetComment(owner string, repo string, number int)
req.Header.Set("Accept", mediaTypeReactionsPreview)
comment := new(PullRequestComment)
resp, err := s.client.Do(req, comment)
resp, err := s.client.Do(ctx, req, comment)
if err != nil {
return nil, resp, err
}
@ -108,7 +109,7 @@ func (s *PullRequestsService) GetComment(owner string, repo string, number int)
// CreateComment creates a new comment on the specified pull request.
//
// GitHub API docs: https://developer.github.com/v3/pulls/comments/#create-a-comment
func (s *PullRequestsService) CreateComment(owner string, repo string, number int, comment *PullRequestComment) (*PullRequestComment, *Response, error) {
func (s *PullRequestsService) CreateComment(ctx context.Context, owner string, repo string, number int, comment *PullRequestComment) (*PullRequestComment, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/pulls/%d/comments", owner, repo, number)
req, err := s.client.NewRequest("POST", u, comment)
if err != nil {
@ -116,7 +117,7 @@ func (s *PullRequestsService) CreateComment(owner string, repo string, number in
}
c := new(PullRequestComment)
resp, err := s.client.Do(req, c)
resp, err := s.client.Do(ctx, req, c)
if err != nil {
return nil, resp, err
}
@ -127,7 +128,7 @@ func (s *PullRequestsService) CreateComment(owner string, repo string, number in
// EditComment updates a pull request comment.
//
// GitHub API docs: https://developer.github.com/v3/pulls/comments/#edit-a-comment
func (s *PullRequestsService) EditComment(owner string, repo string, number int, comment *PullRequestComment) (*PullRequestComment, *Response, error) {
func (s *PullRequestsService) EditComment(ctx context.Context, owner string, repo string, number int, comment *PullRequestComment) (*PullRequestComment, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/pulls/comments/%d", owner, repo, number)
req, err := s.client.NewRequest("PATCH", u, comment)
if err != nil {
@ -135,7 +136,7 @@ func (s *PullRequestsService) EditComment(owner string, repo string, number int,
}
c := new(PullRequestComment)
resp, err := s.client.Do(req, c)
resp, err := s.client.Do(ctx, req, c)
if err != nil {
return nil, resp, err
}
@ -146,11 +147,11 @@ func (s *PullRequestsService) EditComment(owner string, repo string, number int,
// DeleteComment deletes a pull request comment.
//
// GitHub API docs: https://developer.github.com/v3/pulls/comments/#delete-a-comment
func (s *PullRequestsService) DeleteComment(owner string, repo string, number int) (*Response, error) {
func (s *PullRequestsService) DeleteComment(ctx context.Context, owner string, repo string, number int) (*Response, error) {
u := fmt.Sprintf("repos/%v/%v/pulls/comments/%d", owner, repo, number)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}

View file

@ -6,6 +6,7 @@
package github
import (
"context"
"fmt"
"time"
)
@ -64,7 +65,7 @@ func (r PullRequestReviewDismissalRequest) String() string {
// Read more about it here - https://github.com/google/go-github/issues/540
//
// GitHub API docs: https://developer.github.com/v3/pulls/reviews/#list-reviews-on-a-pull-request
func (s *PullRequestsService) ListReviews(owner, repo string, number int) ([]*PullRequestReview, *Response, error) {
func (s *PullRequestsService) ListReviews(ctx context.Context, owner, repo string, number int) ([]*PullRequestReview, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/pulls/%d/reviews", owner, repo, number)
req, err := s.client.NewRequest("GET", u, nil)
@ -76,7 +77,7 @@ func (s *PullRequestsService) ListReviews(owner, repo string, number int) ([]*Pu
req.Header.Set("Accept", mediaTypePullRequestReviewsPreview)
var reviews []*PullRequestReview
resp, err := s.client.Do(req, &reviews)
resp, err := s.client.Do(ctx, req, &reviews)
if err != nil {
return nil, resp, err
}
@ -91,7 +92,7 @@ func (s *PullRequestsService) ListReviews(owner, repo string, number int) ([]*Pu
// Read more about it here - https://github.com/google/go-github/issues/540
//
// GitHub API docs: https://developer.github.com/v3/pulls/reviews/#get-a-single-review
func (s *PullRequestsService) GetReview(owner, repo string, number, reviewID int) (*PullRequestReview, *Response, error) {
func (s *PullRequestsService) GetReview(ctx context.Context, owner, repo string, number, reviewID int) (*PullRequestReview, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/pulls/%d/reviews/%d", owner, repo, number, reviewID)
req, err := s.client.NewRequest("GET", u, nil)
@ -103,7 +104,7 @@ func (s *PullRequestsService) GetReview(owner, repo string, number, reviewID int
req.Header.Set("Accept", mediaTypePullRequestReviewsPreview)
review := new(PullRequestReview)
resp, err := s.client.Do(req, review)
resp, err := s.client.Do(ctx, req, review)
if err != nil {
return nil, resp, err
}
@ -118,7 +119,7 @@ func (s *PullRequestsService) GetReview(owner, repo string, number, reviewID int
// Read more about it here - https://github.com/google/go-github/issues/540
//
// GitHub API docs: https://developer.github.com/v3/pulls/reviews/#delete-a-pending-review
func (s *PullRequestsService) DeletePendingReview(owner, repo string, number, reviewID int) (*PullRequestReview, *Response, error) {
func (s *PullRequestsService) DeletePendingReview(ctx context.Context, owner, repo string, number, reviewID int) (*PullRequestReview, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/pulls/%d/reviews/%d", owner, repo, number, reviewID)
req, err := s.client.NewRequest("DELETE", u, nil)
@ -130,7 +131,7 @@ func (s *PullRequestsService) DeletePendingReview(owner, repo string, number, re
req.Header.Set("Accept", mediaTypePullRequestReviewsPreview)
review := new(PullRequestReview)
resp, err := s.client.Do(req, review)
resp, err := s.client.Do(ctx, req, review)
if err != nil {
return nil, resp, err
}
@ -145,7 +146,7 @@ func (s *PullRequestsService) DeletePendingReview(owner, repo string, number, re
// Read more about it here - https://github.com/google/go-github/issues/540
//
// GitHub API docs: https://developer.github.com/v3/pulls/reviews/#get-a-single-reviews-comments
func (s *PullRequestsService) ListReviewComments(owner, repo string, number, reviewID int) ([]*PullRequestComment, *Response, error) {
func (s *PullRequestsService) ListReviewComments(ctx context.Context, owner, repo string, number, reviewID int) ([]*PullRequestComment, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/pulls/%d/reviews/%d/comments", owner, repo, number, reviewID)
req, err := s.client.NewRequest("GET", u, nil)
@ -157,7 +158,7 @@ func (s *PullRequestsService) ListReviewComments(owner, repo string, number, rev
req.Header.Set("Accept", mediaTypePullRequestReviewsPreview)
var comments []*PullRequestComment
resp, err := s.client.Do(req, &comments)
resp, err := s.client.Do(ctx, req, &comments)
if err != nil {
return nil, resp, err
}
@ -172,7 +173,7 @@ func (s *PullRequestsService) ListReviewComments(owner, repo string, number, rev
// Read more about it here - https://github.com/google/go-github/issues/540
//
// GitHub API docs: https://developer.github.com/v3/pulls/reviews/#create-a-pull-request-review
func (s *PullRequestsService) CreateReview(owner, repo string, number int, review *PullRequestReviewRequest) (*PullRequestReview, *Response, error) {
func (s *PullRequestsService) CreateReview(ctx context.Context, owner, repo string, number int, review *PullRequestReviewRequest) (*PullRequestReview, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/pulls/%d/reviews", owner, repo, number)
req, err := s.client.NewRequest("POST", u, review)
@ -184,7 +185,7 @@ func (s *PullRequestsService) CreateReview(owner, repo string, number int, revie
req.Header.Set("Accept", mediaTypePullRequestReviewsPreview)
r := new(PullRequestReview)
resp, err := s.client.Do(req, r)
resp, err := s.client.Do(ctx, req, r)
if err != nil {
return nil, resp, err
}
@ -199,7 +200,7 @@ func (s *PullRequestsService) CreateReview(owner, repo string, number int, revie
// Read more about it here - https://github.com/google/go-github/issues/540
//
// GitHub API docs: https://developer.github.com/v3/pulls/reviews/#submit-a-pull-request-review
func (s *PullRequestsService) SubmitReview(owner, repo string, number, reviewID int, review *PullRequestReviewRequest) (*PullRequestReview, *Response, error) {
func (s *PullRequestsService) SubmitReview(ctx context.Context, owner, repo string, number, reviewID int, review *PullRequestReviewRequest) (*PullRequestReview, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/pulls/%d/reviews/%d/events", owner, repo, number, reviewID)
req, err := s.client.NewRequest("POST", u, review)
@ -211,7 +212,7 @@ func (s *PullRequestsService) SubmitReview(owner, repo string, number, reviewID
req.Header.Set("Accept", mediaTypePullRequestReviewsPreview)
r := new(PullRequestReview)
resp, err := s.client.Do(req, r)
resp, err := s.client.Do(ctx, req, r)
if err != nil {
return nil, resp, err
}
@ -226,7 +227,7 @@ func (s *PullRequestsService) SubmitReview(owner, repo string, number, reviewID
// Read more about it here - https://github.com/google/go-github/issues/540
//
// GitHub API docs: https://developer.github.com/v3/pulls/reviews/#dismiss-a-pull-request-review
func (s *PullRequestsService) DismissReview(owner, repo string, number, reviewID int, review *PullRequestReviewDismissalRequest) (*PullRequestReview, *Response, error) {
func (s *PullRequestsService) DismissReview(ctx context.Context, owner, repo string, number, reviewID int, review *PullRequestReviewDismissalRequest) (*PullRequestReview, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/pulls/%d/reviews/%d/dismissals", owner, repo, number, reviewID)
req, err := s.client.NewRequest("PUT", u, review)
@ -238,7 +239,7 @@ func (s *PullRequestsService) DismissReview(owner, repo string, number, reviewID
req.Header.Set("Accept", mediaTypePullRequestReviewsPreview)
r := new(PullRequestReview)
resp, err := s.client.Do(req, r)
resp, err := s.client.Do(ctx, req, r)
if err != nil {
return nil, resp, err
}

View file

@ -5,7 +5,10 @@
package github
import "fmt"
import (
"context"
"fmt"
)
// ReactionsService provides access to the reactions-related functions in the
// GitHub API.
@ -43,7 +46,7 @@ func (r Reaction) String() string {
// ListCommentReactions lists the reactions for a commit comment.
//
// GitHub API docs: https://developer.github.com/v3/reactions/#list-reactions-for-a-commit-comment
func (s *ReactionsService) ListCommentReactions(owner, repo string, id int, opt *ListOptions) ([]*Reaction, *Response, error) {
func (s *ReactionsService) ListCommentReactions(ctx context.Context, owner, repo string, id int, opt *ListOptions) ([]*Reaction, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/comments/%v/reactions", owner, repo, id)
u, err := addOptions(u, opt)
if err != nil {
@ -59,7 +62,7 @@ func (s *ReactionsService) ListCommentReactions(owner, repo string, id int, opt
req.Header.Set("Accept", mediaTypeReactionsPreview)
var m []*Reaction
resp, err := s.client.Do(req, &m)
resp, err := s.client.Do(ctx, req, &m)
if err != nil {
return nil, resp, err
}
@ -72,7 +75,7 @@ func (s *ReactionsService) ListCommentReactions(owner, repo string, id int, opt
// previously created reaction will be returned with Status: 200 OK.
//
// GitHub API docs: https://developer.github.com/v3/reactions/#create-reaction-for-a-commit-comment
func (s ReactionsService) CreateCommentReaction(owner, repo string, id int, content string) (*Reaction, *Response, error) {
func (s ReactionsService) CreateCommentReaction(ctx context.Context, owner, repo string, id int, content string) (*Reaction, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/comments/%v/reactions", owner, repo, id)
body := &Reaction{Content: String(content)}
@ -85,7 +88,7 @@ func (s ReactionsService) CreateCommentReaction(owner, repo string, id int, cont
req.Header.Set("Accept", mediaTypeReactionsPreview)
m := &Reaction{}
resp, err := s.client.Do(req, m)
resp, err := s.client.Do(ctx, req, m)
if err != nil {
return nil, resp, err
}
@ -96,7 +99,7 @@ func (s ReactionsService) CreateCommentReaction(owner, repo string, id int, cont
// ListIssueReactions lists the reactions for an issue.
//
// GitHub API docs: https://developer.github.com/v3/reactions/#list-reactions-for-an-issue
func (s *ReactionsService) ListIssueReactions(owner, repo string, number int, opt *ListOptions) ([]*Reaction, *Response, error) {
func (s *ReactionsService) ListIssueReactions(ctx context.Context, owner, repo string, number int, opt *ListOptions) ([]*Reaction, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/issues/%v/reactions", owner, repo, number)
u, err := addOptions(u, opt)
if err != nil {
@ -112,7 +115,7 @@ func (s *ReactionsService) ListIssueReactions(owner, repo string, number int, op
req.Header.Set("Accept", mediaTypeReactionsPreview)
var m []*Reaction
resp, err := s.client.Do(req, &m)
resp, err := s.client.Do(ctx, req, &m)
if err != nil {
return nil, resp, err
}
@ -125,7 +128,7 @@ func (s *ReactionsService) ListIssueReactions(owner, repo string, number int, op
// previously created reaction will be returned with Status: 200 OK.
//
// GitHub API docs: https://developer.github.com/v3/reactions/#create-reaction-for-an-issue
func (s ReactionsService) CreateIssueReaction(owner, repo string, number int, content string) (*Reaction, *Response, error) {
func (s ReactionsService) CreateIssueReaction(ctx context.Context, owner, repo string, number int, content string) (*Reaction, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/issues/%v/reactions", owner, repo, number)
body := &Reaction{Content: String(content)}
@ -138,7 +141,7 @@ func (s ReactionsService) CreateIssueReaction(owner, repo string, number int, co
req.Header.Set("Accept", mediaTypeReactionsPreview)
m := &Reaction{}
resp, err := s.client.Do(req, m)
resp, err := s.client.Do(ctx, req, m)
if err != nil {
return nil, resp, err
}
@ -149,7 +152,7 @@ func (s ReactionsService) CreateIssueReaction(owner, repo string, number int, co
// ListIssueCommentReactions lists the reactions for an issue comment.
//
// GitHub API docs: https://developer.github.com/v3/reactions/#list-reactions-for-an-issue-comment
func (s *ReactionsService) ListIssueCommentReactions(owner, repo string, id int, opt *ListOptions) ([]*Reaction, *Response, error) {
func (s *ReactionsService) ListIssueCommentReactions(ctx context.Context, owner, repo string, id int, opt *ListOptions) ([]*Reaction, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/issues/comments/%v/reactions", owner, repo, id)
u, err := addOptions(u, opt)
if err != nil {
@ -165,7 +168,7 @@ func (s *ReactionsService) ListIssueCommentReactions(owner, repo string, id int,
req.Header.Set("Accept", mediaTypeReactionsPreview)
var m []*Reaction
resp, err := s.client.Do(req, &m)
resp, err := s.client.Do(ctx, req, &m)
if err != nil {
return nil, resp, err
}
@ -178,7 +181,7 @@ func (s *ReactionsService) ListIssueCommentReactions(owner, repo string, id int,
// previously created reaction will be returned with Status: 200 OK.
//
// GitHub API docs: https://developer.github.com/v3/reactions/#create-reaction-for-an-issue-comment
func (s ReactionsService) CreateIssueCommentReaction(owner, repo string, id int, content string) (*Reaction, *Response, error) {
func (s ReactionsService) CreateIssueCommentReaction(ctx context.Context, owner, repo string, id int, content string) (*Reaction, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/issues/comments/%v/reactions", owner, repo, id)
body := &Reaction{Content: String(content)}
@ -191,7 +194,7 @@ func (s ReactionsService) CreateIssueCommentReaction(owner, repo string, id int,
req.Header.Set("Accept", mediaTypeReactionsPreview)
m := &Reaction{}
resp, err := s.client.Do(req, m)
resp, err := s.client.Do(ctx, req, m)
if err != nil {
return nil, resp, err
}
@ -202,7 +205,7 @@ func (s ReactionsService) CreateIssueCommentReaction(owner, repo string, id int,
// ListPullRequestCommentReactions lists the reactions for a pull request review comment.
//
// GitHub API docs: https://developer.github.com/v3/reactions/#list-reactions-for-an-issue-comment
func (s *ReactionsService) ListPullRequestCommentReactions(owner, repo string, id int, opt *ListOptions) ([]*Reaction, *Response, error) {
func (s *ReactionsService) ListPullRequestCommentReactions(ctx context.Context, owner, repo string, id int, opt *ListOptions) ([]*Reaction, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/pulls/comments/%v/reactions", owner, repo, id)
u, err := addOptions(u, opt)
if err != nil {
@ -218,7 +221,7 @@ func (s *ReactionsService) ListPullRequestCommentReactions(owner, repo string, i
req.Header.Set("Accept", mediaTypeReactionsPreview)
var m []*Reaction
resp, err := s.client.Do(req, &m)
resp, err := s.client.Do(ctx, req, &m)
if err != nil {
return nil, resp, err
}
@ -231,7 +234,7 @@ func (s *ReactionsService) ListPullRequestCommentReactions(owner, repo string, i
// previously created reaction will be returned with Status: 200 OK.
//
// GitHub API docs: https://developer.github.com/v3/reactions/#create-reaction-for-an-issue-comment
func (s ReactionsService) CreatePullRequestCommentReaction(owner, repo string, id int, content string) (*Reaction, *Response, error) {
func (s ReactionsService) CreatePullRequestCommentReaction(ctx context.Context, owner, repo string, id int, content string) (*Reaction, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/pulls/comments/%v/reactions", owner, repo, id)
body := &Reaction{Content: String(content)}
@ -244,7 +247,7 @@ func (s ReactionsService) CreatePullRequestCommentReaction(owner, repo string, i
req.Header.Set("Accept", mediaTypeReactionsPreview)
m := &Reaction{}
resp, err := s.client.Do(req, m)
resp, err := s.client.Do(ctx, req, m)
if err != nil {
return nil, resp, err
}
@ -255,7 +258,7 @@ func (s ReactionsService) CreatePullRequestCommentReaction(owner, repo string, i
// DeleteReaction deletes a reaction.
//
// GitHub API docs: https://developer.github.com/v3/reaction/reactions/#delete-a-reaction-archive
func (s *ReactionsService) DeleteReaction(id int) (*Response, error) {
func (s *ReactionsService) DeleteReaction(ctx context.Context, id int) (*Response, error) {
u := fmt.Sprintf("reactions/%v", id)
req, err := s.client.NewRequest("DELETE", u, nil)
@ -266,5 +269,5 @@ func (s *ReactionsService) DeleteReaction(id int) (*Response, error) {
// TODO: remove custom Accept header when this API fully launches.
req.Header.Set("Accept", mediaTypeReactionsPreview)
return s.client.Do(req, nil)
return s.client.Do(ctx, req, nil)
}

Some files were not shown because too many files have changed in this diff Show more