2022-10-18 01:24:12 +00:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
2018-02-19 22:24:10 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-10-05 00:40:27 +00:00
|
|
|
package bitbucket
|
|
|
|
|
|
|
|
import (
|
2016-04-30 08:00:39 +00:00
|
|
|
"fmt"
|
2015-10-05 01:34:06 +00:00
|
|
|
"net/url"
|
2017-03-18 08:49:27 +00:00
|
|
|
"regexp"
|
2015-10-05 00:40:27 +00:00
|
|
|
"strings"
|
|
|
|
|
2021-10-12 07:25:13 +00:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
|
2022-11-04 23:35:06 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/forge/bitbucket/internal"
|
2021-09-27 17:51:55 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2015-10-05 00:40:27 +00:00
|
|
|
)
|
|
|
|
|
2016-04-30 08:00:39 +00:00
|
|
|
const (
|
|
|
|
statusPending = "INPROGRESS"
|
|
|
|
statusSuccess = "SUCCESSFUL"
|
|
|
|
statusFailure = "FAILED"
|
|
|
|
)
|
|
|
|
|
2021-10-02 08:59:34 +00:00
|
|
|
// convertStatus is a helper function used to convert a Woodpecker status to a
|
2016-04-30 08:00:39 +00:00
|
|
|
// Bitbucket commit status.
|
2021-11-22 11:55:13 +00:00
|
|
|
func convertStatus(status model.StatusValue) string {
|
2016-04-30 08:00:39 +00:00
|
|
|
switch status {
|
2017-03-18 08:49:27 +00:00
|
|
|
case model.StatusPending, model.StatusRunning, model.StatusBlocked:
|
2016-04-30 08:00:39 +00:00
|
|
|
return statusPending
|
|
|
|
case model.StatusSuccess:
|
|
|
|
return statusSuccess
|
|
|
|
default:
|
|
|
|
return statusFailure
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 19:39:56 +00:00
|
|
|
// convertRepo is a helper function used to convert a Bitbucket repository
|
2021-10-02 08:59:34 +00:00
|
|
|
// structure to the common Woodpecker repository structure.
|
2023-03-21 22:01:59 +00:00
|
|
|
func convertRepo(from *internal.Repo, perm *internal.RepoPerm) *model.Repo {
|
2015-10-05 00:40:27 +00:00
|
|
|
repo := model.Repo{
|
2022-11-15 14:01:23 +00:00
|
|
|
ForgeRemoteID: model.ForgeRemoteID(from.UUID),
|
|
|
|
Clone: cloneLink(from),
|
|
|
|
Owner: strings.Split(from.FullName, "/")[0],
|
|
|
|
Name: strings.Split(from.FullName, "/")[1],
|
|
|
|
FullName: from.FullName,
|
|
|
|
Link: from.Links.HTML.Href,
|
|
|
|
IsSCMPrivate: from.IsPrivate,
|
|
|
|
Avatar: from.Owner.Links.Avatar.Href,
|
|
|
|
SCMKind: model.SCMKind(from.Scm),
|
2023-07-30 16:28:52 +00:00
|
|
|
Branch: from.Mainbranch.Name,
|
2023-03-21 22:01:59 +00:00
|
|
|
Perm: convertPerm(perm),
|
2015-10-05 00:40:27 +00:00
|
|
|
}
|
2021-11-22 11:55:13 +00:00
|
|
|
if repo.SCMKind == model.RepoHg {
|
2015-10-27 21:53:55 +00:00
|
|
|
repo.Branch = "default"
|
|
|
|
}
|
2015-10-05 00:40:27 +00:00
|
|
|
return &repo
|
|
|
|
}
|
|
|
|
|
2023-03-21 22:01:59 +00:00
|
|
|
func convertPerm(from *internal.RepoPerm) *model.Perm {
|
|
|
|
perms := new(model.Perm)
|
|
|
|
switch from.Permission {
|
|
|
|
case "admin":
|
|
|
|
perms.Admin = true
|
|
|
|
fallthrough
|
|
|
|
case "write":
|
|
|
|
perms.Push = true
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
perms.Pull = true
|
|
|
|
}
|
|
|
|
return perms
|
|
|
|
}
|
|
|
|
|
2016-04-29 19:39:56 +00:00
|
|
|
// cloneLink is a helper function that tries to extract the clone url from the
|
|
|
|
// repository object.
|
|
|
|
func cloneLink(repo *internal.Repo) string {
|
2015-10-06 06:17:59 +00:00
|
|
|
var clone string
|
|
|
|
|
2016-04-29 19:39:56 +00:00
|
|
|
// above we manually constructed the repository clone url. below we will
|
|
|
|
// iterate through the list of clone links and attempt to instead use the
|
|
|
|
// clone url provided by bitbucket.
|
2015-10-06 06:17:59 +00:00
|
|
|
for _, link := range repo.Links.Clone {
|
|
|
|
if link.Name == "https" {
|
|
|
|
clone = link.Href
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 19:39:56 +00:00
|
|
|
// if no repository name is provided, we use the Html link. this excludes the
|
|
|
|
// .git suffix, but will still clone the repo.
|
2015-10-06 06:17:59 +00:00
|
|
|
if len(clone) == 0 {
|
2021-12-01 13:22:06 +00:00
|
|
|
clone = repo.Links.HTML.Href
|
2015-10-06 06:17:59 +00:00
|
|
|
}
|
|
|
|
|
2016-04-29 19:39:56 +00:00
|
|
|
// if bitbucket tries to automatically populate the user in the url we must
|
|
|
|
// strip it out.
|
2015-10-06 06:17:59 +00:00
|
|
|
cloneurl, err := url.Parse(clone)
|
|
|
|
if err == nil {
|
|
|
|
cloneurl.User = nil
|
|
|
|
clone = cloneurl.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return clone
|
|
|
|
}
|
|
|
|
|
2016-04-29 19:39:56 +00:00
|
|
|
// convertUser is a helper function used to convert a Bitbucket user account
|
2021-10-02 08:59:34 +00:00
|
|
|
// structure to the Woodpecker User structure.
|
2016-04-29 19:39:56 +00:00
|
|
|
func convertUser(from *internal.Account, token *oauth2.Token) *model.User {
|
|
|
|
return &model.User{
|
2023-05-11 03:19:35 +00:00
|
|
|
Login: from.Login,
|
|
|
|
Token: token.AccessToken,
|
|
|
|
Secret: token.RefreshToken,
|
|
|
|
Expiry: token.Expiry.UTC().Unix(),
|
|
|
|
Avatar: from.Links.Avatar.Href,
|
|
|
|
ForgeRemoteID: model.ForgeRemoteID(fmt.Sprint(from.ID)),
|
2016-04-29 19:39:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertTeamList is a helper function used to convert a Bitbucket team list
|
2021-10-02 08:59:34 +00:00
|
|
|
// structure to the Woodpecker Team structure.
|
2022-09-18 07:26:13 +00:00
|
|
|
func convertWorkspaceList(from []*internal.Workspace) []*model.Team {
|
2016-04-29 19:39:56 +00:00
|
|
|
var teams []*model.Team
|
2022-09-18 07:26:13 +00:00
|
|
|
for _, workspace := range from {
|
|
|
|
teams = append(teams, convertWorkspace(workspace))
|
2016-04-29 19:39:56 +00:00
|
|
|
}
|
|
|
|
return teams
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertTeam is a helper function used to convert a Bitbucket team account
|
2021-10-02 08:59:34 +00:00
|
|
|
// structure to the Woodpecker Team structure.
|
2022-09-18 07:26:13 +00:00
|
|
|
func convertWorkspace(from *internal.Workspace) *model.Team {
|
2016-04-29 19:39:56 +00:00
|
|
|
return &model.Team{
|
2022-09-18 07:26:13 +00:00
|
|
|
Login: from.Slug,
|
2016-04-29 19:39:56 +00:00
|
|
|
Avatar: from.Links.Avatar.Href,
|
|
|
|
}
|
|
|
|
}
|
2016-04-30 08:00:39 +00:00
|
|
|
|
|
|
|
// convertPullHook is a helper function used to convert a Bitbucket pull request
|
2022-10-18 01:24:12 +00:00
|
|
|
// hook to the Woodpecker pipeline struct holding commit information.
|
|
|
|
func convertPullHook(from *internal.PullRequestHook) *model.Pipeline {
|
|
|
|
return &model.Pipeline{
|
2016-11-19 22:44:22 +00:00
|
|
|
Event: model.EventPull,
|
|
|
|
Commit: from.PullRequest.Dest.Commit.Hash,
|
|
|
|
Ref: fmt.Sprintf("refs/heads/%s", from.PullRequest.Dest.Branch.Name),
|
|
|
|
Refspec: fmt.Sprintf("%s:%s",
|
|
|
|
from.PullRequest.Source.Branch.Name,
|
|
|
|
from.PullRequest.Dest.Branch.Name,
|
|
|
|
),
|
2022-11-04 23:35:06 +00:00
|
|
|
CloneURL: fmt.Sprintf("https://bitbucket.org/%s", from.PullRequest.Source.Repo.FullName),
|
2021-12-01 13:22:06 +00:00
|
|
|
Link: from.PullRequest.Links.HTML.Href,
|
2016-04-30 08:00:39 +00:00
|
|
|
Branch: from.PullRequest.Dest.Branch.Name,
|
|
|
|
Message: from.PullRequest.Desc,
|
|
|
|
Avatar: from.Actor.Links.Avatar.Href,
|
|
|
|
Author: from.Actor.Login,
|
2017-03-18 08:49:27 +00:00
|
|
|
Sender: from.Actor.Login,
|
2016-04-30 08:00:39 +00:00
|
|
|
Timestamp: from.PullRequest.Updated.UTC().Unix(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertPushHook is a helper function used to convert a Bitbucket push
|
2022-10-18 01:24:12 +00:00
|
|
|
// hook to the Woodpecker pipeline struct holding commit information.
|
|
|
|
func convertPushHook(hook *internal.PushHook, change *internal.Change) *model.Pipeline {
|
|
|
|
pipeline := &model.Pipeline{
|
2016-04-30 08:00:39 +00:00
|
|
|
Commit: change.New.Target.Hash,
|
2021-12-01 13:22:06 +00:00
|
|
|
Link: change.New.Target.Links.HTML.Href,
|
2016-04-30 08:00:39 +00:00
|
|
|
Branch: change.New.Name,
|
|
|
|
Message: change.New.Target.Message,
|
|
|
|
Avatar: hook.Actor.Links.Avatar.Href,
|
|
|
|
Author: hook.Actor.Login,
|
2017-03-18 08:49:27 +00:00
|
|
|
Sender: hook.Actor.Login,
|
2016-04-30 08:00:39 +00:00
|
|
|
Timestamp: change.New.Target.Date.UTC().Unix(),
|
|
|
|
}
|
|
|
|
switch change.New.Type {
|
|
|
|
case "tag", "annotated_tag", "bookmark":
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline.Event = model.EventTag
|
|
|
|
pipeline.Ref = fmt.Sprintf("refs/tags/%s", change.New.Name)
|
2016-04-30 08:00:39 +00:00
|
|
|
default:
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline.Event = model.EventPush
|
|
|
|
pipeline.Ref = fmt.Sprintf("refs/heads/%s", change.New.Name)
|
2016-04-30 08:00:39 +00:00
|
|
|
}
|
2016-11-25 21:37:58 +00:00
|
|
|
if len(change.New.Target.Author.Raw) != 0 {
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline.Email = extractEmail(change.New.Target.Author.Raw)
|
2016-11-25 21:37:58 +00:00
|
|
|
}
|
2022-10-18 01:24:12 +00:00
|
|
|
return pipeline
|
2016-04-30 08:00:39 +00:00
|
|
|
}
|
2016-11-25 21:37:58 +00:00
|
|
|
|
2017-01-20 09:52:17 +00:00
|
|
|
// regex for git author fields ("name <name@mail.tld>")
|
|
|
|
var reGitMail = regexp.MustCompile("<(.*)>")
|
|
|
|
|
2016-11-25 21:37:58 +00:00
|
|
|
// extracts the email from a git commit author string
|
|
|
|
func extractEmail(gitauthor string) (author string) {
|
2017-03-18 08:49:27 +00:00
|
|
|
matches := reGitMail.FindAllStringSubmatch(gitauthor, -1)
|
|
|
|
if len(matches) == 1 {
|
|
|
|
author = matches[0][1]
|
|
|
|
}
|
|
|
|
return
|
2016-11-25 21:37:58 +00:00
|
|
|
}
|