feat: add git username and password options for http

This commit is contained in:
RaviAnand Mohabir 2024-07-03 14:16:40 +02:00
parent 0d7cc87b37
commit e11f467042
3 changed files with 41 additions and 6 deletions

View File

@ -30,7 +30,7 @@ steps:
from_secret: docker_username from_secret: docker_username
password: password:
from_secret: docker_password from_secret: docker_password
- image: gitea.dikurium.ch/innopeak/drone-gitea-release:0.6 - image: gitea.dikurium.ch/innopeak/drone-gitea-release:0.7
name: create release name: create release
settings: settings:
gitea_password: gitea_password:

View File

@ -26,6 +26,8 @@ type Args struct {
GiteaUrl string `envconfig:"PLUGIN_GITEA_URL"` GiteaUrl string `envconfig:"PLUGIN_GITEA_URL"`
GiteaUsername string `envconfig:"PLUGIN_GITEA_USERNAME"` GiteaUsername string `envconfig:"PLUGIN_GITEA_USERNAME"`
GiteaPassword string `envconfig:"PLUGIN_GITEA_PASSWORD"` GiteaPassword string `envconfig:"PLUGIN_GITEA_PASSWORD"`
GitUsername string `envconfig:"PLUGIN_GIT_USERNAME"`
GitPassword string `envconfig:"PLUGIN_GIT_PASSWORD"`
Owner string `envconfig:"PLUGIN_OWNER"` Owner string `envconfig:"PLUGIN_OWNER"`
Repo string `envconfig:"PLUGIN_REPO"` Repo string `envconfig:"PLUGIN_REPO"`
TitleFormat string `envconfig:"PLUGIN_TITLE_FORMAT"` TitleFormat string `envconfig:"PLUGIN_TITLE_FORMAT"`
@ -76,8 +78,11 @@ func Exec(ctx context.Context, args Args) error {
if args.UseLatestGitTag { if args.UseLatestGitTag {
var options = make([]GetLatestGitTagOption, 0) var options = make([]GetLatestGitTagOption, 0)
if args.GitUsername != "" && args.GitPassword != "" {
options = append(options, SetBasicAuth(args.GitUsername, args.GitPassword))
}
if args.FetchGitTags { if args.FetchGitTags {
options = append(options, FetchTags) options = append(options, FetchTags())
} }
tag, err = getLatestGitTag(options...) tag, err = getLatestGitTag(options...)

View File

@ -9,15 +9,44 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"net/http"
"os" "os"
"strings" "strings"
"github.com/go-git/go-git/v5" "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"
) )
type GetLatestGitTagOption func(repo *git.Repository) error type GetLatestGitTagOption func(repo *git.Repository) error
var FetchTags = 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 {
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 {
err := repo.Fetch(&git.FetchOptions{Tags: git.AllTags}) err := repo.Fetch(&git.FetchOptions{Tags: git.AllTags})
if err == git.NoErrAlreadyUpToDate { if err == git.NoErrAlreadyUpToDate {
@ -26,6 +55,7 @@ var FetchTags = func(repo *git.Repository) error {
return err return err
} }
}
func getLatestGitTag(options ...GetLatestGitTagOption) (tag string, err error) { func getLatestGitTag(options ...GetLatestGitTagOption) (tag string, err error) {
r, err := git.PlainOpen(".") r, err := git.PlainOpen(".")