Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(providers): Added bitbucket enterprise provider #568

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pb/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ message Job {
string sshPrivateKey = 21;
bool sshClone = 22;
string branch = 23;
string providerHttpUser = 24;
string providerHttpPass = 25;
}

message Command {
Expand Down
9 changes: 9 additions & 0 deletions pkg/gitscm/scm.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/drone/go-scm/scm/driver/github"
"github.com/drone/go-scm/scm/driver/gitlab"
"github.com/drone/go-scm/scm/driver/gogs"
"github.com/drone/go-scm/scm/driver/stash"
"github.com/drone/go-scm/scm/transport"
)

Expand Down Expand Up @@ -39,6 +40,8 @@ func New(ctx context.Context, provider, url, token string) (SCM, error) {
scm.client, err = github.New(scm.url)
case "bitbucket":
scm.client, err = bitbucket.New(scm.url)
case "stash":
scm.client, err = stash.New(scm.url)
case "gitea":
scm.client, err = gitea.New(scm.url)
case "gitlab":
Expand Down Expand Up @@ -68,6 +71,12 @@ func (s SCM) FindRepo(name string) (*scm.Repository, error) {
return repo, err
}

// FindPerms returns perms of repo.
func (s SCM) FindPerms(repo *scm.Repository) (*scm.Perm, error) {
perm, _, err := s.client.Repositories.FindPerms(s.ctx, fmt.Sprintf("%s/%s", repo.Namespace, repo.Name))
return perm, err
}

// ListCommits returns list of commits.
func (s SCM) ListCommits(repo, branch string) ([]*scm.Commit, error) {
if s.provider != "gitea" {
Expand Down
4 changes: 4 additions & 0 deletions server/api/provider/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func HandleCreate(providers core.ProviderStore) http.HandlerFunc {
Host string `json:"host" valid:"url,required"`
AccessToken string `json:"accessToken" valid:"stringlength(12|50),required"`
Secret string `json:"secret" valid:"stringlength(5|50),required"`
HttpUser string `json:"httpUser"`
HttpPass string `json:"httpPass"`
}

return func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -44,6 +46,8 @@ func HandleCreate(providers core.ProviderStore) http.HandlerFunc {
AccessToken: f.AccessToken,
Secret: f.Secret,
UserID: claims.ID,
HttpUser: f.HttpUser,
HttpPass: f.HttpPass,
}

if err := providers.Create(provider); err != nil {
Expand Down
16 changes: 10 additions & 6 deletions server/api/provider/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ func HandleUpdate(providers core.ProviderStore, users core.UserStore) http.Handl
Host string `json:"host" valid:"url,required"`
AccessToken string `json:"accessToken"`
Secret string `json:"secret" valid:"stringlength(5|50),required"`
HttpUser string `json:"httpUser"`
HttpPass string `json:"httpPass"`
}

return func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -52,12 +54,14 @@ func HandleUpdate(providers core.ProviderStore, users core.UserStore) http.Handl

if p.UserID == claims.ID || user.Role == "admin" {
provider := &core.Provider{
ID: f.ID,
Name: f.Name,
URL: f.URL,
Host: f.Host,
Secret: f.Secret,
UserID: claims.ID,
ID: f.ID,
Name: f.Name,
URL: f.URL,
Host: f.Host,
Secret: f.Secret,
UserID: claims.ID,
HttpUser: f.HttpUser,
HttpPass: f.HttpPass,
}

if f.AccessToken != "" {
Expand Down
2 changes: 2 additions & 0 deletions server/core/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type (
Name string `gorm:"not null" json:"name"`
URL string `gorm:"not null" json:"url"`
AccessToken string `gorm:"not null" json:"-"`
HttpUser string `json:"httpUser"`
HttpPass string `json:"httpPass"`
Secret string `gorm:"not null" json:"secret"`
Host string `gorm:"not null" json:"host"`
LastSync *time.Time `json:"lastSync"`
Expand Down
47 changes: 27 additions & 20 deletions server/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,27 +312,34 @@ func (s *scheduler) startJob(job *core.Job, worker *core.Worker) {
s.logger.Errorf("error parsing commands for job %d: %s", job.ID, err.Error())
}

url := job.Build.Repository.Clone
if url == "" {
url = job.Build.Repository.URL
}

j := &pb.Job{
Id: uint64(job.ID),
BuildId: uint64(job.BuildID),
Commands: commands.Commands,
Image: job.Image,
Env: envs,
Url: job.Build.Repository.URL,
SshURL: job.Build.Repository.CloneSSH,
ProviderName: job.Build.Repository.Provider.Name,
ProviderURL: job.Build.Repository.Provider.URL,
ProviderToken: job.Build.Repository.Provider.AccessToken,
Ref: job.Build.Ref,
CommitSHA: job.Build.Commit,
Branch: job.Build.Branch,
RepoName: job.Build.Repository.FullName,
Action: pb.Job_JobStart,
WorkerId: worker.ID,
Cache: strings.Split(job.Cache, ","),
Mount: strings.Split(job.Mount, ","),
SshPrivateKey: job.Build.Repository.SSHPrivateKey,
SshClone: job.Build.Repository.UseSSH,
Id: uint64(job.ID),
BuildId: uint64(job.BuildID),
Commands: commands.Commands,
Image: job.Image,
Env: envs,
Url: url,
SshURL: job.Build.Repository.CloneSSH,
ProviderName: job.Build.Repository.Provider.Name,
ProviderURL: job.Build.Repository.Provider.URL,
ProviderToken: job.Build.Repository.Provider.AccessToken,
Ref: job.Build.Ref,
CommitSHA: job.Build.Commit,
Branch: job.Build.Branch,
RepoName: job.Build.Repository.FullName,
Action: pb.Job_JobStart,
WorkerId: worker.ID,
Cache: strings.Split(job.Cache, ","),
Mount: strings.Split(job.Mount, ","),
SshPrivateKey: job.Build.Repository.SSHPrivateKey,
SshClone: job.Build.Repository.UseSSH,
ProviderHttpUser: job.Build.Repository.Provider.HttpUser,
ProviderHttpPass: job.Build.Repository.Provider.HttpPass,
}

s.mu.Lock()
Expand Down
3 changes: 0 additions & 3 deletions server/service/githook/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ func (p *parser) Parse(req *http.Request, secretFunc func(string) *core.Reposito
}

payload, err := p.client.Webhooks.Parse(req, fn)
if err == scm.ErrUnknownEvent {
return nil, nil, nil
}
if err != nil {
return nil, nil, err
}
Expand Down
7 changes: 4 additions & 3 deletions server/store/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ func (s buildStore) GenerateBuild(repo *core.Repository, base *core.GitHook) ([]
base.After = commit.Sha
}
if base.Message == "" {
base.Message = commit.Message
// Fix Database Error 1406: Data too long for column 'commit_message' at row 1
base.Message = strings.Split(commit.Message, "\n")[0]
}
}
content, err := scm.FindContent(repo.FullName, base.After, ".abstruse.yml")
Expand Down Expand Up @@ -257,7 +258,7 @@ func (s buildStore) TriggerBuild(opts core.TriggerBuildOpts) ([]*core.Job, error
sha = commit.Sha

build.Commit = commit.Sha
build.CommitMessage = commit.Message
build.CommitMessage = strings.Split(commit.Message, "\n")[0]
build.AuthorLogin = commit.Author.Login
build.AuthorName = commit.Author.Name
build.AuthorEmail = commit.Author.Email
Expand All @@ -274,7 +275,7 @@ func (s buildStore) TriggerBuild(opts core.TriggerBuildOpts) ([]*core.Job, error
}

build.Commit = commit.Sha
build.CommitMessage = commit.Message
build.CommitMessage = strings.Split(commit.Message, "\n")[0]
build.AuthorLogin = commit.Author.Login
build.AuthorName = commit.Author.Name
build.AuthorEmail = commit.Author.Email
Expand Down
26 changes: 25 additions & 1 deletion server/store/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ func (s providerStore) Create(provider *core.Provider) error {
}

func (s providerStore) Update(provider *core.Provider) error {
return s.db.Model(provider).Updates(&provider).Error
updateHttp := make(map[string]interface{})
updateHttp["HttpUser"] = provider.HttpUser
updateHttp["HttpPass"] = provider.HttpPass
return s.db.Model(provider).Updates(updateHttp).Updates(&provider).Error
}

func (s providerStore) Delete(provider *core.Provider) error {
Expand Down Expand Up @@ -101,7 +104,28 @@ func (s providerStore) findRepos(id uint, page, size int) ([]*scm.Repository, er
if err != nil {
return repos, err
}

// git-scm does not work correctly with Stash pagination.
// Opened issue https://github.com/drone/go-scm/issues/166
// See https://docs.atlassian.com/bitbucket-server/rest/5.16.0/bitbucket-rest.html
//> Identifiers of adjacent objects in a page may not be contiguous,
//> so the start of the next page is not necessarily the start of the last page plus the last page's size.
//> A client should always use nextPageStart to avoid unexpected results from a paged API.
if provider.Name == "stash" {
size = 1000
}
repos, err = gitscm.ListRepos(page, size)

// Some providers like Stash don't return perms in ListRepos response
for _, repo := range repos {
if repo.Perm == nil {
repo.Perm, err = gitscm.FindPerms(repo)
if err != nil {
repo.Perm = &scm.Perm{}
}
}
}

return repos, err
}

Expand Down
15 changes: 15 additions & 0 deletions server/store/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,21 @@ func filterHooks(hooks []*scm.Hook, provider core.Provider) []*scm.Hook {
for _, hook := range hooks {
url, _ := url.Parse(hook.Target)
if strings.HasPrefix(hook.Target, provider.Host) && strings.HasSuffix(url.Path, "/webhooks") {
if provider.Name == "stash" {
stashMap := map[string]string{
"pr:merged": "pull_request",
"pr:modified": "pull_request",
"pr:opened": "pull_request",
"repo:refs_changed": "push",
}
mappedEvents := make([]string, 0)
for _, event := range hook.Events {
if stashMap[event] != "" {
mappedEvents = append(mappedEvents, stashMap[event])
}
}
hook.Events = mappedEvents
}
webhooks = append(webhooks, hook)
}
}
Expand Down
Loading