Initial commit

This commit is contained in:
2019-08-02 16:21:29 +02:00
commit fb5bdd2581
9 changed files with 835 additions and 0 deletions

38
gitea/git.go Normal file
View File

@ -0,0 +1,38 @@
package gitea
import (
"fmt"
"strings"
"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/pkg/errors"
)
func CurrentRevision() string {
if mg.Verbose() {
fmt.Println("Retrieving current Git revision")
}
revision, err := sh.Output("git", "log", "--pretty=format:'%h'", "-n", "1")
if err != nil {
if mg.Verbose() {
fmt.Println(errors.Wrap(err, "error while retrieving current git revision"))
}
return ""
}
return strings.Trim(strings.TrimSpace(revision), "'")
}
func LatestTag() string {
if mg.Verbose() {
fmt.Println("Retrieving Git current branch latest tag")
}
tag, err := sh.Output("git", "describe", "--abbrev=0")
if err != nil {
if mg.Verbose() {
fmt.Println(errors.Wrap(err, "error while retrieving latest git tag"))
}
return fmt.Sprintf("0.0.0-rev.%s", CurrentRevision())
}
return strings.TrimSpace(tag)
}

170
gitea/option.go Normal file
View File

@ -0,0 +1,170 @@
package gitea
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
"syscall"
"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 WithAttachmentPatterns(patterns ...string) OptionFunc {
attachments := make([]string, 0)
for _, p := range patterns {
files, err := filepath.Glob(p)
if err != nil {
panic(errors.Wrapf(err, "error while searching files matching '%s' pattern", p))
}
attachments = append(attachments, files...)
}
return WithAttachments(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)
fmt.Print("\n")
if err != nil {
panic(errors.Wrap(err, "error while asking password"))
}
opts.Password = strings.TrimSpace(string(password))
}
}
func WithInteractiveUsername() OptionFunc {
return func(opts *Options) {
username, err := ask("Enter Username:", "")
if err != nil {
panic(errors.Wrap(err, "error while asking username"))
}
opts.Username = 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 WithInteractiveTarget() OptionFunc {
return func(opts *Options) {
target, err := ask("Enter Target:", CurrentRevision())
if err != nil {
panic(errors.Wrap(err, "error while asking target"))
}
opts.Target = target
}
}
func WithInteractiveTag() OptionFunc {
return func(opts *Options) {
tagName, err := ask("Enter Tag:", LatestTag())
if err != nil {
panic(errors.Wrap(err, "error while asking tag"))
}
opts.TagName = tagName
}
}
func WithInteractiveTitle() OptionFunc {
return func(opts *Options) {
defaultTitle := fmt.Sprintf("Release %s", LatestTag())
title, err := ask("Enter Title:", defaultTitle)
if err != nil {
panic(errors.Wrap(err, "error while asking title"))
}
opts.Title = title
}
}
func ask(question, defaultValue string) (string, error) {
reader := bufio.NewReader(os.Stdin)
if defaultValue != "" {
fmt.Printf("%s (default: %s) ", question, defaultValue)
} else {
fmt.Printf("%s ", question)
}
res, err := reader.ReadString('\n')
if err != nil {
return "", err
}
result := strings.TrimSpace(res)
if result == "" {
return defaultValue, nil
}
return result, nil
}
func defaultOptions() *Options {
return &Options{
CreateReleaseOption: structs.CreateReleaseOption{
IsDraft: false,
IsPrerelease: true,
Note: "",
Target: "master",
Title: "test",
TagName: "test",
},
Attachments: make([]string, 0),
}
}

85
gitea/release.go Normal file
View File

@ -0,0 +1,85 @@
package gitea
import (
"fmt"
"os"
"path/filepath"
"code.gitea.io/gitea/modules/structs"
"code.gitea.io/sdk/gitea"
"github.com/magefile/mage/mg"
"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)
}
if mg.Verbose() {
fmt.Printf("Creating release '%s' for repo '%s/%s'\n", options.Title, user, repo)
}
release, err := client.CreateRelease(
user,
repo,
options.CreateReleaseOption,
)
if err != nil {
return errors.Wrap(err, "error while creating gitea release")
}
for _, filePath := range options.Attachments {
file, err := os.Open(filePath)
if err != nil {
return errors.Wrapf(err, "error while opening file '%s'", filePath)
}
fileName := filepath.Base(filePath)
if mg.Verbose() {
fmt.Printf("Uploading attachment '%s'\n", fileName)
}
_, err = client.CreateReleaseAttachment(user, repo, release.ID, file, fileName)
if err != nil {
return errors.Wrapf(err, "error while creating attachment with file '%s'", filePath)
}
if err := file.Close(); err != nil {
return errors.Wrapf(err, "error while closing file '%s'", filePath)
}
}
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
}