From 1f09ce21bc18d95917542e936045a885192c446c Mon Sep 17 00:00:00 2001 From: Anbraten Date: Sun, 30 Jul 2023 16:14:09 +0200 Subject: [PATCH 1/2] Improve default branch code --- 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/gitlab/convert.go | 10 +--------- 5 files changed, 19 insertions(+), 31 deletions(-) diff --git a/server/forge/bitbucket/bitbucket.go b/server/forge/bitbucket/bitbucket.go index d0d4c19820..96c73aec89 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 58d396c4aa..edd6d40a9d 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 88a39874f2..ff2f77f013 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 b02d2c6018..8eda746b51 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/gitlab/convert.go b/server/forge/gitlab/convert.go index c68b84ba5f..097199ea78 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 From bc456b8e30c45170b235c3f484cade72d9874006 Mon Sep 17 00:00:00 2001 From: Anbraten Date: Sun, 30 Jul 2023 16:41:45 +0200 Subject: [PATCH 2/2] ix --- server/forge/github/parse.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/server/forge/github/parse.go b/server/forge/github/parse.go index 799bbb163b..7d599bf36e 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