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.
|
|
|
|
|
2016-07-24 21:07:44 +00:00
|
|
|
package bitbucketserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
2016-08-11 20:35:47 +00:00
|
|
|
"time"
|
2016-07-24 21:07:44 +00:00
|
|
|
|
2021-06-22 10:34:35 +00:00
|
|
|
"github.com/mrjones/oauth"
|
2021-10-12 07:25:13 +00:00
|
|
|
|
2022-11-04 23:35:06 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/forge/bitbucketserver/internal"
|
2021-09-27 17:51:55 +00:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2016-09-01 16:10:35 +00:00
|
|
|
)
|
2016-08-14 02:06:15 +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-08-14 02:06:15 +00:00
|
|
|
// Bitbucket commit status.
|
2021-11-22 11:55:13 +00:00
|
|
|
func convertStatus(status model.StatusValue) string {
|
2016-08-14 02:06:15 +00:00
|
|
|
switch status {
|
|
|
|
case model.StatusPending, model.StatusRunning:
|
|
|
|
return statusPending
|
|
|
|
case model.StatusSuccess:
|
|
|
|
return statusSuccess
|
|
|
|
default:
|
|
|
|
return statusFailure
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-24 21:07:44 +00:00
|
|
|
// convertRepo is a helper function used to convert a Bitbucket server repository
|
2021-10-02 08:59:34 +00:00
|
|
|
// structure to the common Woodpecker repository structure.
|
2016-07-24 21:07:44 +00:00
|
|
|
func convertRepo(from *internal.Repo) *model.Repo {
|
|
|
|
repo := model.Repo{
|
2022-11-15 14:01:23 +00:00
|
|
|
ForgeRemoteID: model.ForgeRemoteID(fmt.Sprint(from.ID)),
|
|
|
|
Name: from.Slug,
|
|
|
|
Owner: from.Project.Key,
|
|
|
|
Branch: "master",
|
|
|
|
SCMKind: model.RepoGit,
|
|
|
|
IsSCMPrivate: true, // Since we have to use Netrc it has to always be private :/
|
|
|
|
FullName: fmt.Sprintf("%s/%s", from.Project.Key, from.Slug),
|
2016-07-24 21:07:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range from.Links.Clone {
|
|
|
|
if item.Name == "http" {
|
|
|
|
uri, err := url.Parse(item.Href)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
uri.User = nil
|
|
|
|
repo.Clone = uri.String()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, item := range from.Links.Self {
|
|
|
|
if item.Href != "" {
|
|
|
|
repo.Link = item.Href
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &repo
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.PostHook, baseURL string) *model.Pipeline {
|
2016-09-19 14:19:12 +00:00
|
|
|
branch := strings.TrimPrefix(
|
|
|
|
strings.TrimPrefix(
|
|
|
|
hook.RefChanges[0].RefID,
|
|
|
|
"refs/heads/",
|
|
|
|
),
|
|
|
|
"refs/tags/",
|
|
|
|
)
|
2016-07-24 22:13:50 +00:00
|
|
|
|
2022-01-05 20:50:23 +00:00
|
|
|
// Ensuring the author label is not longer then 40 for the label of the commit author (default size in the db)
|
2016-08-12 21:20:26 +00:00
|
|
|
authorLabel := hook.Changesets.Values[0].ToCommit.Author.Name
|
2016-08-13 06:07:45 +00:00
|
|
|
if len(authorLabel) > 40 {
|
|
|
|
authorLabel = authorLabel[0:37] + "..."
|
2016-08-12 20:14:58 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline := &model.Pipeline{
|
2016-08-13 06:07:45 +00:00
|
|
|
Commit: hook.RefChanges[0].ToHash, // TODO check for index value
|
2016-09-19 14:19:12 +00:00
|
|
|
Branch: branch,
|
2022-01-05 20:50:23 +00:00
|
|
|
Message: hook.Changesets.Values[0].ToCommit.Message, // TODO check for index Values
|
2016-08-11 20:35:47 +00:00
|
|
|
Avatar: avatarLink(hook.Changesets.Values[0].ToCommit.Author.EmailAddress),
|
2016-08-12 20:14:58 +00:00
|
|
|
Author: authorLabel,
|
2016-08-11 20:35:47 +00:00
|
|
|
Email: hook.Changesets.Values[0].ToCommit.Author.EmailAddress,
|
|
|
|
Timestamp: time.Now().UTC().Unix(),
|
|
|
|
Ref: hook.RefChanges[0].RefID, // TODO check for index Values
|
|
|
|
Link: fmt.Sprintf("%s/projects/%s/repos/%s/commits/%s", baseURL, hook.Repository.Project.Key, hook.Repository.Slug, hook.RefChanges[0].ToHash),
|
2016-07-24 22:13:50 +00:00
|
|
|
}
|
2016-09-19 14:19:12 +00:00
|
|
|
if strings.HasPrefix(hook.RefChanges[0].RefID, "refs/tags/") {
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline.Event = model.EventTag
|
2016-09-19 14:19:12 +00:00
|
|
|
} else {
|
2022-10-18 01:24:12 +00:00
|
|
|
pipeline.Event = model.EventPush
|
2016-07-24 21:07:44 +00:00
|
|
|
}
|
2016-07-24 22:13:50 +00:00
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
return pipeline
|
2016-07-24 21:07:44 +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-07-24 21:07:44 +00:00
|
|
|
func convertUser(from *internal.User, token *oauth.AccessToken) *model.User {
|
|
|
|
return &model.User{
|
|
|
|
Login: from.Slug,
|
|
|
|
Token: token.Token,
|
|
|
|
Email: from.EmailAddress,
|
|
|
|
Avatar: avatarLink(from.EmailAddress),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func avatarLink(email string) string {
|
|
|
|
hasher := md5.New()
|
|
|
|
hasher.Write([]byte(strings.ToLower(email)))
|
|
|
|
emailHash := fmt.Sprintf("%v", hex.EncodeToString(hasher.Sum(nil)))
|
|
|
|
avatarURL := fmt.Sprintf("https://www.gravatar.com/avatar/%s.jpg", emailHash)
|
|
|
|
return avatarURL
|
|
|
|
}
|