github.com/joselitofilho/goreleaser@v0.155.1-0.20210123221854-e4891856c593/internal/client/client.go (about)

     1  // Package client contains the client implementations for several providers.
     2  package client
     3  
     4  import (
     5  	"errors"
     6  	"fmt"
     7  	"os"
     8  
     9  	"github.com/apex/log"
    10  	"github.com/goreleaser/goreleaser/internal/artifact"
    11  	"github.com/goreleaser/goreleaser/pkg/config"
    12  	"github.com/goreleaser/goreleaser/pkg/context"
    13  )
    14  
    15  // Info of the repository.
    16  type Info struct {
    17  	Description string
    18  	Homepage    string
    19  	URL         string
    20  }
    21  
    22  type Repo struct {
    23  	Owner string
    24  	Name  string
    25  }
    26  
    27  func (r Repo) String() string {
    28  	if r.Owner == "" && r.Name == "" {
    29  		return ""
    30  	}
    31  	return r.Owner + "/" + r.Name
    32  }
    33  
    34  // Client interface.
    35  type Client interface {
    36  	CloseMilestone(ctx *context.Context, repo Repo, title string) (err error)
    37  	CreateRelease(ctx *context.Context, body string) (releaseID string, err error)
    38  	ReleaseURLTemplate(ctx *context.Context) (string, error)
    39  	CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo Repo, content []byte, path, message string) (err error)
    40  	Upload(ctx *context.Context, releaseID string, artifact *artifact.Artifact, file *os.File) (err error)
    41  }
    42  
    43  // New creates a new client depending on the token type.
    44  func New(ctx *context.Context) (Client, error) {
    45  	log.WithField("type", ctx.TokenType).Debug("token type")
    46  	if ctx.TokenType == context.TokenTypeGitHub {
    47  		return NewGitHub(ctx, ctx.Token)
    48  	}
    49  	if ctx.TokenType == context.TokenTypeGitLab {
    50  		return NewGitLab(ctx, ctx.Token)
    51  	}
    52  	if ctx.TokenType == context.TokenTypeGitea {
    53  		return NewGitea(ctx, ctx.Token)
    54  	}
    55  	return nil, nil
    56  }
    57  
    58  func NewWithToken(ctx *context.Context, token string) (Client, error) {
    59  	if ctx.TokenType == context.TokenTypeGitHub {
    60  		return NewGitHub(ctx, token)
    61  	}
    62  	if ctx.TokenType == context.TokenTypeGitLab {
    63  		return NewGitLab(ctx, token)
    64  	}
    65  	if ctx.TokenType == context.TokenTypeGitea {
    66  		return NewGitea(ctx, token)
    67  	}
    68  	return nil, nil
    69  }
    70  
    71  // ErrNoMilestoneFound is an error when no milestone is found.
    72  type ErrNoMilestoneFound struct {
    73  	Title string
    74  }
    75  
    76  func (e ErrNoMilestoneFound) Error() string {
    77  	return fmt.Sprintf("no milestone found: %s", e.Title)
    78  }
    79  
    80  // RetriableError is an error that will cause the action to be retried.
    81  type RetriableError struct {
    82  	Err error
    83  }
    84  
    85  func (e RetriableError) Error() string {
    86  	return e.Err.Error()
    87  }
    88  
    89  // NotImplementedError happens when trying to use something a client does not
    90  // implement.
    91  type NotImplementedError struct {
    92  	TokenType context.TokenType
    93  }
    94  
    95  func (e NotImplementedError) Error() string {
    96  	return fmt.Sprintf("not implemented for %s", e.TokenType)
    97  }
    98  
    99  // IsNotImplementedErr returns true if given error is a NotImplementedError.
   100  func IsNotImplementedErr(err error) bool {
   101  	return errors.As(err, &NotImplementedError{})
   102  }