diff --git a/.drone.yml b/.drone.yml index 732de28..f0235da 100644 --- a/.drone.yml +++ b/.drone.yml @@ -30,7 +30,7 @@ steps: from_secret: docker_username 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 settings: gitea_password: diff --git a/plugin/plugin.go b/plugin/plugin.go index 97571a4..5d82cfc 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -26,6 +26,8 @@ type Args struct { GiteaUrl string `envconfig:"PLUGIN_GITEA_URL"` GiteaUsername string `envconfig:"PLUGIN_GITEA_USERNAME"` GiteaPassword string `envconfig:"PLUGIN_GITEA_PASSWORD"` + GitUsername string `envconfig:"PLUGIN_GIT_USERNAME"` + GitPassword string `envconfig:"PLUGIN_GIT_PASSWORD"` Owner string `envconfig:"PLUGIN_OWNER"` Repo string `envconfig:"PLUGIN_REPO"` TitleFormat string `envconfig:"PLUGIN_TITLE_FORMAT"` @@ -76,8 +78,11 @@ func Exec(ctx context.Context, args Args) error { if args.UseLatestGitTag { var options = make([]GetLatestGitTagOption, 0) + if args.GitUsername != "" && args.GitPassword != "" { + options = append(options, SetBasicAuth(args.GitUsername, args.GitPassword)) + } if args.FetchGitTags { - options = append(options, FetchTags) + options = append(options, FetchTags()) } tag, err = getLatestGitTag(options...) diff --git a/plugin/util.go b/plugin/util.go index 58aee99..0f4255e 100644 --- a/plugin/util.go +++ b/plugin/util.go @@ -9,22 +9,52 @@ import ( "encoding/json" "fmt" "io" + "net/http" "os" "strings" "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 -var FetchTags = func(repo *git.Repository) error { - err := repo.Fetch(&git.FetchOptions{Tags: git.AllTags}) +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)) - if err == git.NoErrAlreadyUpToDate { return nil } +} - return err +func FetchTags() func(repo *git.Repository) error { + return func(repo *git.Repository) error { + err := repo.Fetch(&git.FetchOptions{Tags: git.AllTags}) + + if err == git.NoErrAlreadyUpToDate { + return nil + } + + return err + } } func getLatestGitTag(options ...GetLatestGitTagOption) (tag string, err error) {