drone-gitea-release/plugin/util.go

139 lines
3.0 KiB
Go
Raw Permalink Normal View History

// Copyright 2020 the Drone Authors. All rights reserved.
// Use of this source code is governed by the Blue Oak Model License
// that can be found in the LICENSE file.
package plugin
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/transport/client"
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
log "github.com/sirupsen/logrus"
)
type GetLatestGitTagOption func(repo *git.Repository) error
type BasicAuthTransport struct {
Username string
Password string
RoundTriper http.RoundTripper
}
func (bat BasicAuthTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
creds := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", bat.Username, bat.Password)))
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", creds))
return bat.RoundTriper.RoundTrip(req)
}
func SetBasicAuth(username, password string) func(repo *git.Repository) error {
return func(repo *git.Repository) error {
2024-07-11 15:09:36 +00:00
log.Println("Configuring basic auth for https")
customClient := &http.Client{
Transport: BasicAuthTransport{username, password, http.DefaultTransport},
}
// Override http(s) default protocol to use our custom client
client.InstallProtocol("https", githttp.NewClient(customClient))
return nil
}
}
func FetchTags() func(repo *git.Repository) error {
return func(repo *git.Repository) error {
2024-07-11 15:33:47 +00:00
log.Info("Fetching git tags")
2024-07-11 15:09:36 +00:00
err := repo.Fetch(&git.FetchOptions{Tags: git.AllTags})
if err == git.NoErrAlreadyUpToDate {
return nil
}
return err
}
}
func getLatestGitTag(options ...GetLatestGitTagOption) (string, error) {
var (
tag string
tagDate *time.Time
err error
)
repo, err := git.PlainOpen(".")
if err != nil {
return "", fmt.Errorf("error opening git repo at %s: %w", ".", err)
}
for _, opt := range options {
err := opt(repo)
if err != nil {
return "", err
}
}
tags, err := repo.TagObjects()
if err != nil {
return "", fmt.Errorf("error getting git tags %w", err)
}
for {
t, err := tags.Next()
if err == io.EOF {
break
}
if err != nil {
return "", fmt.Errorf("error iterating tags %w", err)
}
if tagDate != nil && t.Tagger.When.Sub(*tagDate) > 0 {
continue
}
tagDate = &t.Tagger.When
tag = t.Name
}
if tag == "" {
return tag, fmt.Errorf("couldn't find any git tags")
}
return tag, err
}
func writeCard(path, schema string, card interface{}) {
data, _ := json.Marshal(map[string]interface{}{
"schema": schema,
"data": card,
})
switch {
case path == "/dev/stdout":
writeCardTo(os.Stdout, data)
case path == "/dev/stderr":
writeCardTo(os.Stderr, data)
case path != "":
os.WriteFile(path, data, 0644)
}
}
func writeCardTo(out io.Writer, data []byte) {
encoded := base64.StdEncoding.EncodeToString(data)
io.WriteString(out, "\u001B]1338;")
io.WriteString(out, encoded)
io.WriteString(out, "\u001B]0m")
io.WriteString(out, "\n")
}