Skip to content

Commit

Permalink
Remove default branch fallbacks (#2065)
Browse files Browse the repository at this point in the history
  • Loading branch information
anbraten authored Jul 30, 2023
1 parent 4731eeb commit 5a812e3
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 34 deletions.
8 changes: 4 additions & 4 deletions server/forge/bitbucket/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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})

Expand Down Expand Up @@ -369,15 +369,15 @@ 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,
Endpoint: oauth2.Endpoint{
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),
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/forge/bitbucket/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 13 additions & 9 deletions server/forge/bitbucket/internal/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 0 additions & 8 deletions server/forge/github/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import (
"github.com/woodpecker-ci/woodpecker/server/model"
)

const defaultBranch = "master"

const (
statusPending = "pending"
statusSuccess = "success"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 0 additions & 3 deletions server/forge/github/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 1 addition & 9 deletions server/forge/gitlab/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5a812e3

Please sign in to comment.