From 5a812e32549fdc23842e20cd4d4581f2a0aa7506 Mon Sep 17 00:00:00 2001 From: Anbraten Date: Sun, 30 Jul 2023 18:28:52 +0200 Subject: [PATCH] Remove default branch fallbacks (#2065) --- server/forge/bitbucket/bitbucket.go | 8 ++++---- server/forge/bitbucket/convert.go | 2 +- server/forge/bitbucket/internal/types.go | 22 +++++++++++++--------- server/forge/github/convert.go | 8 -------- server/forge/github/parse.go | 3 --- server/forge/gitlab/convert.go | 10 +--------- 6 files changed, 19 insertions(+), 34 deletions(-) diff --git a/server/forge/bitbucket/bitbucket.go b/server/forge/bitbucket/bitbucket.go index d0d4c1982..96c73aec8 100644 --- a/server/forge/bitbucket/bitbucket.go +++ b/server/forge/bitbucket/bitbucket.go @@ -77,7 +77,7 @@ func (c *config) URL() string { // Login authenticates an account with Bitbucket using the oauth2 protocol. The // Bitbucket account details are returned when the user is successfully authenticated. func (c *config) Login(ctx context.Context, w http.ResponseWriter, req *http.Request) (*model.User, error) { - config := c.newConfig(server.Config.Server.Host) + config := c.newOAuth2Config() // get the OAuth errors if err := req.FormValue("error"); err != "" { @@ -122,7 +122,7 @@ func (c *config) Auth(ctx context.Context, token, secret string) (string, error) // Refresh refreshes the Bitbucket oauth2 access token. If the token is // refreshed the user is updated and a true value is returned. func (c *config) Refresh(ctx context.Context, user *model.User) (bool, error) { - config := c.newConfig("") + config := c.newOAuth2Config() source := config.TokenSource( ctx, &oauth2.Token{RefreshToken: user.Secret}) @@ -369,7 +369,7 @@ func (c *config) newClientToken(ctx context.Context, token, secret string) *inte } // helper function to return the bitbucket oauth2 config -func (c *config) newConfig(redirect string) *oauth2.Config { +func (c *config) newOAuth2Config() *oauth2.Config { return &oauth2.Config{ ClientID: c.Client, ClientSecret: c.Secret, @@ -377,7 +377,7 @@ func (c *config) newConfig(redirect string) *oauth2.Config { AuthURL: fmt.Sprintf("%s/site/oauth2/authorize", c.url), TokenURL: fmt.Sprintf("%s/site/oauth2/access_token", c.url), }, - RedirectURL: fmt.Sprintf("%s/authorize", redirect), + RedirectURL: fmt.Sprintf("%s/authorize", server.Config.Server.OAuthHost), } } diff --git a/server/forge/bitbucket/convert.go b/server/forge/bitbucket/convert.go index 58d396c4a..edd6d40a9 100644 --- a/server/forge/bitbucket/convert.go +++ b/server/forge/bitbucket/convert.go @@ -59,7 +59,7 @@ func convertRepo(from *internal.Repo, perm *internal.RepoPerm) *model.Repo { IsSCMPrivate: from.IsPrivate, Avatar: from.Owner.Links.Avatar.Href, SCMKind: model.SCMKind(from.Scm), - Branch: "master", + Branch: from.Mainbranch.Name, Perm: convertPerm(perm), } if repo.SCMKind == model.RepoHg { diff --git a/server/forge/bitbucket/internal/types.go b/server/forge/bitbucket/internal/types.go index 88a39874f..ff2f77f01 100644 --- a/server/forge/bitbucket/internal/types.go +++ b/server/forge/bitbucket/internal/types.go @@ -99,15 +99,19 @@ type LinkClone struct { } type Repo struct { - UUID string `json:"uuid"` - Owner Account `json:"owner"` - Name string `json:"name"` - FullName string `json:"full_name"` - Language string `json:"language"` - IsPrivate bool `json:"is_private"` - Scm string `json:"scm"` - Desc string `json:"desc"` - Links Links `json:"links"` + UUID string `json:"uuid"` + Owner Account `json:"owner"` + Name string `json:"name"` + FullName string `json:"full_name"` + Language string `json:"language"` + IsPrivate bool `json:"is_private"` + Scm string `json:"scm"` + Desc string `json:"desc"` + Links Links `json:"links"` + Mainbranch struct { + Type string `json:"type"` + Name string `json:"name"` + } `json:"mainbranch"` } type RepoResp struct { diff --git a/server/forge/github/convert.go b/server/forge/github/convert.go index b02d2c601..8eda746b5 100644 --- a/server/forge/github/convert.go +++ b/server/forge/github/convert.go @@ -23,8 +23,6 @@ import ( "github.com/woodpecker-ci/woodpecker/server/model" ) -const defaultBranch = "master" - const ( statusPending = "pending" statusSuccess = "success" @@ -97,9 +95,6 @@ func convertRepo(from *github.Repository) *model.Repo { Perm: convertPerm(from.GetPermissions()), SCMKind: model.RepoGit, } - if len(repo.Branch) == 0 { - repo.Branch = defaultBranch - } return repo } @@ -156,9 +151,6 @@ func convertRepoHook(eventRepo *github.PushEventRepository) *model.Repo { Branch: eventRepo.GetDefaultBranch(), SCMKind: model.RepoGit, } - if repo.Branch == "" { - repo.Branch = defaultBranch - } if repo.FullName == "" { repo.FullName = repo.Owner + "/" + repo.Name } diff --git a/server/forge/github/parse.go b/server/forge/github/parse.go index 799bbb163..7d599bf36 100644 --- a/server/forge/github/parse.go +++ b/server/forge/github/parse.go @@ -132,9 +132,6 @@ func parseDeployHook(hook *github.DeploymentEvent) (*model.Repo, *model.Pipeline // if the ref is a sha or short sha we need to manually construct the ref. if strings.HasPrefix(pipeline.Commit, pipeline.Ref) || pipeline.Commit == pipeline.Ref { pipeline.Branch = hook.GetRepo().GetDefaultBranch() - if pipeline.Branch == "" { - pipeline.Branch = defaultBranch - } pipeline.Ref = fmt.Sprintf("refs/heads/%s", pipeline.Branch) } // if the ref is a branch we should make sure it has refs/heads prefix diff --git a/server/forge/gitlab/convert.go b/server/forge/gitlab/convert.go index c68b84ba5..097199ea7 100644 --- a/server/forge/gitlab/convert.go +++ b/server/forge/gitlab/convert.go @@ -53,10 +53,6 @@ func (g *GitLab) convertGitLabRepo(_repo *gitlab.Project) (*model.Repo, error) { }, } - if len(repo.Branch) == 0 { // TODO: do we need that? - repo.Branch = "master" - } - if len(repo.Avatar) != 0 && !strings.HasPrefix(repo.Avatar, "http") { repo.Avatar = fmt.Sprintf("%s/%s", g.url, repo.Avatar) } @@ -101,11 +97,7 @@ func convertMergeRequestHook(hook *gitlab.MergeEvent, req *http.Request) (int, * repo.Clone = target.HTTPURL } - if target.DefaultBranch != "" { - repo.Branch = target.DefaultBranch - } else { - repo.Branch = "master" - } + repo.Branch = target.DefaultBranch if target.AvatarURL != "" { repo.Avatar = target.AvatarURL