cadoles-profile/mage/gitea/release.go

62 lines
1.3 KiB
Go

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
}