github.com/triarius/goreleaser@v1.12.5/internal/client/client.go (about) 1 // Package client contains the client implementations for several providers. 2 package client 3 4 import ( 5 "fmt" 6 "os" 7 8 "github.com/caarlos0/log" 9 "github.com/triarius/goreleaser/internal/artifact" 10 "github.com/triarius/goreleaser/internal/tmpl" 11 "github.com/triarius/goreleaser/pkg/config" 12 "github.com/triarius/goreleaser/pkg/context" 13 ) 14 15 const ( 16 // maxReleaseBodyLength defines the max characters size of the body 17 maxReleaseBodyLength = 125000 18 // ellipsis to be used when release notes body is too long 19 ellipsis = "..." 20 ) 21 22 // ErrNotImplemented is returned when a client does not implement certain feature. 23 var ErrNotImplemented = fmt.Errorf("not implemented") 24 25 // Info of the repository. 26 type Info struct { 27 Description string 28 Homepage string 29 URL string 30 } 31 32 type Repo struct { 33 Owner string 34 Name string 35 Branch string 36 } 37 38 func (r Repo) String() string { 39 if r.Owner == "" && r.Name == "" { 40 return "" 41 } 42 return r.Owner + "/" + r.Name 43 } 44 45 // Client interface. 46 type Client interface { 47 CloseMilestone(ctx *context.Context, repo Repo, title string) (err error) 48 CreateRelease(ctx *context.Context, body string) (releaseID string, err error) 49 ReleaseURLTemplate(ctx *context.Context) (string, error) 50 CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo Repo, content []byte, path, message string) (err error) 51 Upload(ctx *context.Context, releaseID string, artifact *artifact.Artifact, file *os.File) (err error) 52 GetDefaultBranch(ctx *context.Context, repo Repo) (string, error) 53 Changelog(ctx *context.Context, repo Repo, prev, current string) (string, error) 54 } 55 56 // GitHubClient is the client with GitHub-only features. 57 type GitHubClient interface { 58 Client 59 GenerateReleaseNotes(ctx *context.Context, repo Repo, prev, current string) (string, error) 60 } 61 62 // New creates a new client depending on the token type. 63 func New(ctx *context.Context) (Client, error) { 64 return newWithToken(ctx, ctx.Token) 65 } 66 67 func newWithToken(ctx *context.Context, token string) (Client, error) { 68 log.WithField("type", ctx.TokenType).Debug("token type") 69 switch ctx.TokenType { 70 case context.TokenTypeGitHub: 71 return NewGitHub(ctx, token) 72 case context.TokenTypeGitLab: 73 return NewGitLab(ctx, token) 74 case context.TokenTypeGitea: 75 return NewGitea(ctx, token) 76 default: 77 return nil, fmt.Errorf("invalid client token type: %q", ctx.TokenType) 78 } 79 } 80 81 func NewIfToken(ctx *context.Context, cli Client, token string) (Client, error) { 82 if token == "" { 83 return cli, nil 84 } 85 token, err := tmpl.New(ctx).ApplySingleEnvOnly(token) 86 if err != nil { 87 return nil, err 88 } 89 log.Debug("using custom token") 90 return newWithToken(ctx, token) 91 } 92 93 func truncateReleaseBody(body string) string { 94 if len(body) > maxReleaseBodyLength { 95 body = body[1:(maxReleaseBodyLength-len(ellipsis))] + ellipsis 96 } 97 return body 98 } 99 100 // ErrNoMilestoneFound is an error when no milestone is found. 101 type ErrNoMilestoneFound struct { 102 Title string 103 } 104 105 func (e ErrNoMilestoneFound) Error() string { 106 return fmt.Sprintf("no milestone found: %s", e.Title) 107 } 108 109 // RetriableError is an error that will cause the action to be retried. 110 type RetriableError struct { 111 Err error 112 } 113 114 func (e RetriableError) Error() string { 115 return e.Err.Error() 116 }