2024-06-22 14:19:00 +00:00
|
|
|
// 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"
|
2024-07-03 12:16:40 +00:00
|
|
|
"net/http"
|
2024-06-22 14:19:00 +00:00
|
|
|
"os"
|
2024-06-27 14:08:27 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/go-git/go-git/v5"
|
2024-07-03 12:16:40 +00:00
|
|
|
"github.com/go-git/go-git/v5/plumbing/transport/client"
|
|
|
|
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
|
2024-07-11 15:25:35 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2024-06-22 14:19:00 +00:00
|
|
|
)
|
|
|
|
|
2024-06-27 14:08:27 +00:00
|
|
|
type GetLatestGitTagOption func(repo *git.Repository) error
|
|
|
|
|
2024-07-03 12:16:40 +00:00
|
|
|
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")
|
|
|
|
|
2024-07-03 12:16:40 +00:00
|
|
|
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))
|
2024-06-26 14:09:18 +00:00
|
|
|
|
2024-06-27 14:08:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-07-03 12:16:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func FetchTags() func(repo *git.Repository) error {
|
|
|
|
return func(repo *git.Repository) error {
|
2024-07-11 15:25:35 +00:00
|
|
|
log.Info("Fetching latest git tags")
|
2024-07-11 15:09:36 +00:00
|
|
|
|
2024-07-03 12:16:40 +00:00
|
|
|
err := repo.Fetch(&git.FetchOptions{Tags: git.AllTags})
|
2024-06-26 14:09:18 +00:00
|
|
|
|
2024-07-03 12:16:40 +00:00
|
|
|
if err == git.NoErrAlreadyUpToDate {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2024-06-26 14:09:18 +00:00
|
|
|
}
|
|
|
|
|
2024-06-27 14:08:27 +00:00
|
|
|
func getLatestGitTag(options ...GetLatestGitTagOption) (tag string, err error) {
|
|
|
|
r, err := git.PlainOpen(".")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error opening git repo at %s: %w", ".", err)
|
|
|
|
}
|
|
|
|
|
2024-06-26 14:09:18 +00:00
|
|
|
for _, opt := range options {
|
2024-06-27 14:08:27 +00:00
|
|
|
err := opt(r)
|
2024-06-26 14:09:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-27 14:08:27 +00:00
|
|
|
tagRefs, err := r.Tags()
|
2024-06-22 14:19:00 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2024-06-27 14:08:27 +00:00
|
|
|
return "", fmt.Errorf("error getting git tag refs %w", err)
|
|
|
|
}
|
|
|
|
|
2024-07-11 15:25:35 +00:00
|
|
|
for {
|
|
|
|
r, err := tagRefs.Next()
|
2024-06-27 14:08:27 +00:00
|
|
|
|
2024-07-11 15:25:35 +00:00
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
2024-06-22 14:19:00 +00:00
|
|
|
|
2024-07-11 15:25:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error iterating tags %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Split(string(r.Name()), "/")
|
|
|
|
tag = strings.Join(parts[2:], "/")
|
2024-06-27 14:20:14 +00:00
|
|
|
}
|
2024-06-27 14:08:27 +00:00
|
|
|
|
2024-07-11 15:25:35 +00:00
|
|
|
if tag == "" {
|
|
|
|
return tag, fmt.Errorf("couldn't find any git tags")
|
|
|
|
}
|
2024-07-11 14:55:20 +00:00
|
|
|
|
2024-06-27 14:08:27 +00:00
|
|
|
return
|
2024-06-22 14:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|