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-05-03 00:47:58 +00:00
|
|
|
package github
|
|
|
|
|
|
|
|
import (
|
2022-09-05 15:08:51 +00:00
|
|
|
"fmt"
|
|
|
|
|
2024-01-13 07:13:22 +00:00
|
|
|
"github.com/google/go-github/v58/github"
|
2021-10-12 07:25:13 +00:00
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
2016-05-03 00:47:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
statusPending = "pending"
|
|
|
|
statusSuccess = "success"
|
|
|
|
statusFailure = "failure"
|
|
|
|
statusError = "error"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-10-18 01:24:12 +00:00
|
|
|
descPending = "this pipeline is pending"
|
|
|
|
descSuccess = "the pipeline was successful"
|
|
|
|
descFailure = "the pipeline failed"
|
|
|
|
descBlocked = "the pipeline requires approval"
|
|
|
|
descDeclined = "the pipeline was rejected"
|
2017-03-18 08:49:27 +00:00
|
|
|
descError = "oops, something went wrong"
|
2016-05-03 00:47:58 +00:00
|
|
|
)
|
|
|
|
|
2016-05-03 20:01:16 +00:00
|
|
|
const (
|
|
|
|
headRefs = "refs/pull/%d/head" // pull request unmerged
|
|
|
|
mergeRefs = "refs/pull/%d/merge" // pull request merged with base
|
2022-01-17 22:46:59 +00:00
|
|
|
refSpec = "%s:%s"
|
2016-05-03 20:01:16 +00:00
|
|
|
)
|
|
|
|
|
2021-10-02 08:59:34 +00:00
|
|
|
// convertStatus is a helper function used to convert a Woodpecker status to a
|
2016-05-03 00:47:58 +00:00
|
|
|
// GitHub commit status.
|
2021-11-22 11:55:13 +00:00
|
|
|
func convertStatus(status model.StatusValue) string {
|
2016-05-03 00:47:58 +00:00
|
|
|
switch status {
|
2019-06-19 06:36:13 +00:00
|
|
|
case model.StatusPending, model.StatusRunning, model.StatusBlocked, model.StatusSkipped:
|
2016-05-03 00:47:58 +00:00
|
|
|
return statusPending
|
2017-03-18 08:49:27 +00:00
|
|
|
case model.StatusFailure, model.StatusDeclined:
|
|
|
|
return statusFailure
|
2016-05-03 00:47:58 +00:00
|
|
|
case model.StatusSuccess:
|
|
|
|
return statusSuccess
|
|
|
|
default:
|
|
|
|
return statusError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-02 08:59:34 +00:00
|
|
|
// convertDesc is a helper function used to convert a Woodpecker status to a
|
2016-05-03 00:47:58 +00:00
|
|
|
// GitHub status description.
|
2021-11-22 11:55:13 +00:00
|
|
|
func convertDesc(status model.StatusValue) string {
|
2016-05-03 00:47:58 +00:00
|
|
|
switch status {
|
|
|
|
case model.StatusPending, model.StatusRunning:
|
|
|
|
return descPending
|
|
|
|
case model.StatusSuccess:
|
|
|
|
return descSuccess
|
|
|
|
case model.StatusFailure:
|
|
|
|
return descFailure
|
2017-03-18 08:49:27 +00:00
|
|
|
case model.StatusBlocked:
|
|
|
|
return descBlocked
|
|
|
|
case model.StatusDeclined:
|
|
|
|
return descDeclined
|
2016-05-03 00:47:58 +00:00
|
|
|
default:
|
|
|
|
return descError
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertRepo is a helper function used to convert a GitHub repository
|
2021-10-02 08:59:34 +00:00
|
|
|
// structure to the common Woodpecker repository structure.
|
2022-01-31 14:38:00 +00:00
|
|
|
func convertRepo(from *github.Repository) *model.Repo {
|
2016-05-03 00:47:58 +00:00
|
|
|
repo := &model.Repo{
|
2022-11-15 14:01:23 +00:00
|
|
|
ForgeRemoteID: model.ForgeRemoteID(fmt.Sprint(from.GetID())),
|
|
|
|
Name: from.GetName(),
|
|
|
|
FullName: from.GetFullName(),
|
2023-11-14 16:12:12 +00:00
|
|
|
ForgeURL: from.GetHTMLURL(),
|
2022-11-15 14:01:23 +00:00
|
|
|
IsSCMPrivate: from.GetPrivate(),
|
|
|
|
Clone: from.GetCloneURL(),
|
2023-08-12 15:39:13 +00:00
|
|
|
CloneSSH: from.GetSSHURL(),
|
2022-11-15 14:01:23 +00:00
|
|
|
Branch: from.GetDefaultBranch(),
|
|
|
|
Owner: from.GetOwner().GetLogin(),
|
|
|
|
Avatar: from.GetOwner().GetAvatarURL(),
|
|
|
|
Perm: convertPerm(from.GetPermissions()),
|
|
|
|
SCMKind: model.RepoGit,
|
2023-12-24 09:04:18 +00:00
|
|
|
PREnabled: true,
|
2016-05-03 00:47:58 +00:00
|
|
|
}
|
|
|
|
return repo
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertPerm is a helper function used to convert a GitHub repository
|
2021-10-02 08:59:34 +00:00
|
|
|
// permissions to the common Woodpecker permissions structure.
|
2022-01-17 22:46:59 +00:00
|
|
|
func convertPerm(perm map[string]bool) *model.Perm {
|
2016-05-03 00:47:58 +00:00
|
|
|
return &model.Perm{
|
2022-01-17 22:46:59 +00:00
|
|
|
Admin: perm["admin"],
|
|
|
|
Push: perm["push"],
|
|
|
|
Pull: perm["pull"],
|
2016-05-03 00:47:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertRepoList is a helper function used to convert a GitHub repository
|
2021-10-02 08:59:34 +00:00
|
|
|
// list to the common Woodpecker repository structure.
|
2022-01-31 14:38:00 +00:00
|
|
|
func convertRepoList(from []*github.Repository) []*model.Repo {
|
2017-07-14 19:58:38 +00:00
|
|
|
var repos []*model.Repo
|
2016-05-03 00:47:58 +00:00
|
|
|
for _, repo := range from {
|
2022-01-31 14:38:00 +00:00
|
|
|
repos = append(repos, convertRepo(repo))
|
2016-05-03 00:47:58 +00:00
|
|
|
}
|
|
|
|
return repos
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertTeamList is a helper function used to convert a GitHub team list to
|
2021-10-02 08:59:34 +00:00
|
|
|
// the common Woodpecker repository structure.
|
2021-09-29 05:59:46 +00:00
|
|
|
func convertTeamList(from []*github.Organization) []*model.Team {
|
2016-05-03 00:47:58 +00:00
|
|
|
var teams []*model.Team
|
|
|
|
for _, team := range from {
|
|
|
|
teams = append(teams, convertTeam(team))
|
|
|
|
}
|
|
|
|
return teams
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertTeam is a helper function used to convert a GitHub team structure
|
2021-10-02 08:59:34 +00:00
|
|
|
// to the common Woodpecker repository structure.
|
2021-09-29 05:59:46 +00:00
|
|
|
func convertTeam(from *github.Organization) *model.Team {
|
2016-05-03 00:47:58 +00:00
|
|
|
return &model.Team{
|
2022-01-17 22:46:59 +00:00
|
|
|
Login: from.GetLogin(),
|
|
|
|
Avatar: from.GetAvatarURL(),
|
2016-05-03 00:47:58 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-03 20:01:16 +00:00
|
|
|
|
|
|
|
// convertRepoHook is a helper function used to extract the Repository details
|
2021-10-02 08:59:34 +00:00
|
|
|
// from a webhook and convert to the common Woodpecker repository structure.
|
2022-01-17 22:46:59 +00:00
|
|
|
func convertRepoHook(eventRepo *github.PushEventRepository) *model.Repo {
|
2016-05-03 20:01:16 +00:00
|
|
|
repo := &model.Repo{
|
2022-11-15 14:01:23 +00:00
|
|
|
ForgeRemoteID: model.ForgeRemoteID(fmt.Sprint(eventRepo.GetID())),
|
|
|
|
Owner: eventRepo.GetOwner().GetLogin(),
|
|
|
|
Name: eventRepo.GetName(),
|
|
|
|
FullName: eventRepo.GetFullName(),
|
2023-11-14 16:12:12 +00:00
|
|
|
ForgeURL: eventRepo.GetHTMLURL(),
|
2022-11-15 14:01:23 +00:00
|
|
|
IsSCMPrivate: eventRepo.GetPrivate(),
|
|
|
|
Clone: eventRepo.GetCloneURL(),
|
2023-08-12 15:39:13 +00:00
|
|
|
CloneSSH: eventRepo.GetSSHURL(),
|
2022-11-15 14:01:23 +00:00
|
|
|
Branch: eventRepo.GetDefaultBranch(),
|
|
|
|
SCMKind: model.RepoGit,
|
2023-12-24 09:04:18 +00:00
|
|
|
PREnabled: true,
|
2016-05-03 20:01:16 +00:00
|
|
|
}
|
|
|
|
if repo.FullName == "" {
|
|
|
|
repo.FullName = repo.Owner + "/" + repo.Name
|
|
|
|
}
|
|
|
|
return repo
|
|
|
|
}
|
2023-03-17 02:43:04 +00:00
|
|
|
|
|
|
|
// covertLabels is a helper function used to convert a GitHub label list to
|
|
|
|
// the common Woodpecker label structure.
|
|
|
|
func convertLabels(from []*github.Label) []string {
|
|
|
|
labels := make([]string, len(from))
|
|
|
|
for i, label := range from {
|
|
|
|
labels[i] = *label.Name
|
|
|
|
}
|
|
|
|
return labels
|
|
|
|
}
|