Add Magefile and release target
This commit is contained in:
115
mage/gitea/option.go
Normal file
115
mage/gitea/option.go
Normal file
@ -0,0 +1,115 @@
|
||||
package gitea
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/structs"
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/crypto/ssh/terminal"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
structs.CreateReleaseOption
|
||||
Attachments []string
|
||||
Token string
|
||||
Username string
|
||||
Password string
|
||||
BaseURL string
|
||||
}
|
||||
|
||||
type OptionFunc func(*Options)
|
||||
|
||||
func WithAttachments(attachments ...string) OptionFunc {
|
||||
return func(opts *Options) {
|
||||
opts.Attachments = attachments
|
||||
}
|
||||
}
|
||||
|
||||
func WithBaseURL(baseURL string) OptionFunc {
|
||||
return func(opts *Options) {
|
||||
opts.BaseURL = baseURL
|
||||
}
|
||||
}
|
||||
|
||||
func WithInteractivePassword() OptionFunc {
|
||||
return func(opts *Options) {
|
||||
fmt.Print("Enter Password: ")
|
||||
password, err := terminal.ReadPassword(syscall.Stdin)
|
||||
if err != nil {
|
||||
panic(errors.Wrap(err, "error while asking password"))
|
||||
}
|
||||
opts.Password = strings.TrimSpace(string(password))
|
||||
}
|
||||
}
|
||||
|
||||
func WithInteractiveUsername() OptionFunc {
|
||||
return func(opts *Options) {
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
fmt.Print("Enter Username: ")
|
||||
username, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
panic(errors.Wrap(err, "error while asking username"))
|
||||
}
|
||||
opts.Username = strings.TrimSpace(username)
|
||||
}
|
||||
}
|
||||
|
||||
func WithEnvUsername() OptionFunc {
|
||||
return func(opts *Options) {
|
||||
opts.Username = os.Getenv("GITEA_USERNAME")
|
||||
}
|
||||
}
|
||||
|
||||
func WithEnvPassword() OptionFunc {
|
||||
return func(opts *Options) {
|
||||
opts.Password = os.Getenv("GITEA_PASSWORD")
|
||||
}
|
||||
}
|
||||
|
||||
func WithAutoUsername() OptionFunc {
|
||||
envUsername := WithEnvUsername()
|
||||
interactiveUsername := WithInteractiveUsername()
|
||||
return func(opts *Options) {
|
||||
envUsername(opts)
|
||||
if opts.Username != "" {
|
||||
return
|
||||
}
|
||||
interactiveUsername(opts)
|
||||
}
|
||||
}
|
||||
|
||||
func WithAutoPassword() OptionFunc {
|
||||
envPassword := WithEnvPassword()
|
||||
interactivePassword := WithInteractivePassword()
|
||||
return func(opts *Options) {
|
||||
envPassword(opts)
|
||||
if opts.Password != "" {
|
||||
return
|
||||
}
|
||||
interactivePassword(opts)
|
||||
}
|
||||
}
|
||||
|
||||
func DefaultTagName() string {
|
||||
now := time.Now()
|
||||
return fmt.Sprintf("r%s", now.Format("20060102150405"))
|
||||
}
|
||||
|
||||
func defaultOptions() *Options {
|
||||
return &Options{
|
||||
CreateReleaseOption: structs.CreateReleaseOption{
|
||||
IsDraft: false,
|
||||
IsPrerelease: true,
|
||||
Note: "test",
|
||||
Target: "master",
|
||||
Title: "test",
|
||||
TagName: "test",
|
||||
},
|
||||
Attachments: make([]string, 0),
|
||||
}
|
||||
}
|
61
mage/gitea/release.go
Normal file
61
mage/gitea/release.go
Normal file
@ -0,0 +1,61 @@
|
||||
package gitea
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/gitea/modules/structs"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
AccessTokenName = "mage-gitea"
|
||||
)
|
||||
|
||||
func Release(user, repo string, opts ...OptionFunc) error {
|
||||
options := defaultOptions()
|
||||
for _, o := range opts {
|
||||
o(options)
|
||||
}
|
||||
client := gitea.NewClient(options.BaseURL, options.Token)
|
||||
|
||||
if options.Token == "" {
|
||||
accessToken, err := retrieveAccessToken(client, options.Username, options.Password)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error while retrieving gitea access token")
|
||||
}
|
||||
client = gitea.NewClient(options.BaseURL, accessToken.Token)
|
||||
}
|
||||
|
||||
release, err := client.CreateRelease(
|
||||
user,
|
||||
repo,
|
||||
options.CreateReleaseOption,
|
||||
)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error while creating gitea release")
|
||||
}
|
||||
|
||||
fmt.Println(release)
|
||||
return nil
|
||||
}
|
||||
|
||||
func retrieveAccessToken(client *gitea.Client, username, password string) (*gitea.AccessToken, error) {
|
||||
tokens, err := client.ListAccessTokens(username, password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, at := range tokens {
|
||||
if at.Name == AccessTokenName {
|
||||
return at, nil
|
||||
}
|
||||
}
|
||||
accessToken, err := client.CreateAccessToken(username, password, structs.CreateAccessTokenOption{
|
||||
Name: AccessTokenName,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return accessToken, nil
|
||||
}
|
Reference in New Issue
Block a user