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 (
|
2016-05-03 20:01:16 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2021-09-29 05:59:46 +00:00
|
|
|
"github.com/google/go-github/v39/github"
|
2021-10-12 07:25:13 +00:00
|
|
|
|
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2016-05-03 00:47:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const defaultBranch = "master"
|
|
|
|
|
|
|
|
const (
|
|
|
|
statusPending = "pending"
|
|
|
|
statusSuccess = "success"
|
|
|
|
statusFailure = "failure"
|
|
|
|
statusError = "error"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-03-18 08:49:27 +00:00
|
|
|
descPending = "this build is pending"
|
|
|
|
descSuccess = "the build was successful"
|
|
|
|
descFailure = "the build failed"
|
|
|
|
descBlocked = "the build requires approval"
|
|
|
|
descDeclined = "the build was rejected"
|
|
|
|
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
|
2016-11-19 21:36:07 +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.
|
|
|
|
func convertStatus(status string) string {
|
|
|
|
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.
|
|
|
|
func convertDesc(status string) string {
|
|
|
|
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.
|
2016-05-03 00:47:58 +00:00
|
|
|
func convertRepo(from *github.Repository, private bool) *model.Repo {
|
|
|
|
repo := &model.Repo{
|
|
|
|
Owner: *from.Owner.Login,
|
|
|
|
Name: *from.Name,
|
|
|
|
FullName: *from.FullName,
|
|
|
|
Link: *from.HTMLURL,
|
|
|
|
IsPrivate: *from.Private,
|
|
|
|
Clone: *from.CloneURL,
|
|
|
|
Avatar: *from.Owner.AvatarURL,
|
|
|
|
Kind: model.RepoGit,
|
|
|
|
Branch: defaultBranch,
|
2017-07-14 19:58:38 +00:00
|
|
|
Perm: convertPerm(from),
|
2016-05-03 00:47:58 +00:00
|
|
|
}
|
|
|
|
if from.DefaultBranch != nil {
|
|
|
|
repo.Branch = *from.DefaultBranch
|
|
|
|
}
|
|
|
|
if private {
|
|
|
|
repo.IsPrivate = true
|
|
|
|
}
|
|
|
|
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.
|
2016-05-03 00:47:58 +00:00
|
|
|
func convertPerm(from *github.Repository) *model.Perm {
|
|
|
|
return &model.Perm{
|
2021-09-29 05:59:46 +00:00
|
|
|
Admin: from.Permissions["admin"],
|
|
|
|
Push: from.Permissions["push"],
|
|
|
|
Pull: from.Permissions["pull"],
|
2016-05-03 00:47:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-11 18:56:48 +00:00
|
|
|
// convertTeamPerm is a helper function used to convert a GitHub organization
|
2021-10-02 08:59:34 +00:00
|
|
|
// permissions to the common Woodpecker permissions structure.
|
2016-11-11 18:56:48 +00:00
|
|
|
func convertTeamPerm(from *github.Membership) *model.Perm {
|
|
|
|
admin := false
|
|
|
|
if *from.Role == "admin" {
|
|
|
|
admin = true
|
|
|
|
}
|
|
|
|
return &model.Perm{
|
|
|
|
Admin: admin,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
2021-09-29 05:59:46 +00:00
|
|
|
func convertRepoList(from []*github.Repository, private bool) []*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 {
|
2021-09-29 05:59:46 +00:00
|
|
|
repos = append(repos, convertRepo(repo, private))
|
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{
|
|
|
|
Login: *from.Login,
|
|
|
|
Avatar: *from.AvatarURL,
|
|
|
|
}
|
|
|
|
}
|
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.
|
2016-05-03 20:01:16 +00:00
|
|
|
func convertRepoHook(from *webhook) *model.Repo {
|
|
|
|
repo := &model.Repo{
|
|
|
|
Owner: from.Repo.Owner.Login,
|
|
|
|
Name: from.Repo.Name,
|
|
|
|
FullName: from.Repo.FullName,
|
|
|
|
Link: from.Repo.HTMLURL,
|
|
|
|
IsPrivate: from.Repo.Private,
|
|
|
|
Clone: from.Repo.CloneURL,
|
|
|
|
Branch: from.Repo.DefaultBranch,
|
|
|
|
Kind: model.RepoGit,
|
|
|
|
}
|
|
|
|
if repo.Branch == "" {
|
|
|
|
repo.Branch = defaultBranch
|
|
|
|
}
|
|
|
|
if repo.Owner == "" { // legacy webhooks
|
|
|
|
repo.Owner = from.Repo.Owner.Name
|
|
|
|
}
|
|
|
|
if repo.FullName == "" {
|
|
|
|
repo.FullName = repo.Owner + "/" + repo.Name
|
|
|
|
}
|
|
|
|
return repo
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertPushHook is a helper function used to extract the Build details
|
2021-10-02 08:59:34 +00:00
|
|
|
// from a push webhook and convert to the common Woodpecker Build structure.
|
2016-05-03 20:01:16 +00:00
|
|
|
func convertPushHook(from *webhook) *model.Build {
|
2021-06-28 21:50:35 +00:00
|
|
|
files := getChangedFilesFromWebhook(from)
|
2016-05-03 20:01:16 +00:00
|
|
|
build := &model.Build{
|
2021-06-28 21:50:35 +00:00
|
|
|
Event: model.EventPush,
|
|
|
|
Commit: from.Head.ID,
|
|
|
|
Ref: from.Ref,
|
|
|
|
Link: from.Head.URL,
|
|
|
|
Branch: strings.Replace(from.Ref, "refs/heads/", "", -1),
|
|
|
|
Message: from.Head.Message,
|
|
|
|
Email: from.Head.Author.Email,
|
|
|
|
Avatar: from.Sender.Avatar,
|
|
|
|
Author: from.Sender.Login,
|
|
|
|
Remote: from.Repo.CloneURL,
|
|
|
|
Sender: from.Sender.Login,
|
|
|
|
ChangedFiles: files,
|
2016-05-03 20:01:16 +00:00
|
|
|
}
|
|
|
|
if len(build.Author) == 0 {
|
|
|
|
build.Author = from.Head.Author.Username
|
|
|
|
}
|
|
|
|
if len(build.Email) == 0 {
|
|
|
|
// default to gravatar?
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(build.Ref, "refs/tags/") {
|
|
|
|
// just kidding, this is actually a tag event. Why did this come as a push
|
|
|
|
// event we'll never know!
|
|
|
|
build.Event = model.EventTag
|
2017-09-14 23:53:52 +00:00
|
|
|
// For tags, if the base_ref (tag's base branch) is set, we're using it
|
|
|
|
// as build's branch so that we can filter events base on it
|
|
|
|
if strings.HasPrefix(from.BaseRef, "refs/heads/") {
|
|
|
|
build.Branch = strings.Replace(from.BaseRef, "refs/heads/", "", -1)
|
|
|
|
}
|
2016-05-03 20:01:16 +00:00
|
|
|
}
|
|
|
|
return build
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertPushHook is a helper function used to extract the Build details
|
2021-10-02 08:59:34 +00:00
|
|
|
// from a deploy webhook and convert to the common Woodpecker Build structure.
|
2016-05-03 20:01:16 +00:00
|
|
|
func convertDeployHook(from *webhook) *model.Build {
|
|
|
|
build := &model.Build{
|
|
|
|
Event: model.EventDeploy,
|
|
|
|
Commit: from.Deployment.Sha,
|
|
|
|
Link: from.Deployment.URL,
|
|
|
|
Message: from.Deployment.Desc,
|
|
|
|
Avatar: from.Sender.Avatar,
|
|
|
|
Author: from.Sender.Login,
|
|
|
|
Ref: from.Deployment.Ref,
|
|
|
|
Branch: from.Deployment.Ref,
|
|
|
|
Deploy: from.Deployment.Env,
|
2017-03-18 08:49:27 +00:00
|
|
|
Sender: from.Sender.Login,
|
2016-05-03 20:01:16 +00:00
|
|
|
}
|
|
|
|
// if the ref is a sha or short sha we need to manuallyconstruct the ref.
|
|
|
|
if strings.HasPrefix(build.Commit, build.Ref) || build.Commit == build.Ref {
|
|
|
|
build.Branch = from.Repo.DefaultBranch
|
|
|
|
if build.Branch == "" {
|
|
|
|
build.Branch = defaultBranch
|
|
|
|
}
|
|
|
|
build.Ref = fmt.Sprintf("refs/heads/%s", build.Branch)
|
|
|
|
}
|
|
|
|
// if the ref is a branch we should make sure it has refs/heads prefix
|
|
|
|
if !strings.HasPrefix(build.Ref, "refs/") { // branch or tag
|
|
|
|
build.Ref = fmt.Sprintf("refs/heads/%s", build.Branch)
|
|
|
|
}
|
|
|
|
return build
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertPullHook is a helper function used to extract the Build details
|
2021-10-02 08:59:34 +00:00
|
|
|
// from a pull request webhook and convert to the common Woodpecker Build structure.
|
2016-05-03 20:01:16 +00:00
|
|
|
func convertPullHook(from *webhook, merge bool) *model.Build {
|
|
|
|
build := &model.Build{
|
|
|
|
Event: model.EventPull,
|
|
|
|
Commit: from.PullRequest.Head.SHA,
|
|
|
|
Link: from.PullRequest.HTMLURL,
|
|
|
|
Ref: fmt.Sprintf(headRefs, from.PullRequest.Number),
|
2016-11-20 22:21:27 +00:00
|
|
|
Branch: from.PullRequest.Base.Ref,
|
2016-05-03 20:01:16 +00:00
|
|
|
Message: from.PullRequest.Title,
|
|
|
|
Author: from.PullRequest.User.Login,
|
|
|
|
Avatar: from.PullRequest.User.Avatar,
|
|
|
|
Title: from.PullRequest.Title,
|
2017-03-18 08:49:27 +00:00
|
|
|
Sender: from.Sender.Login,
|
2016-11-19 21:54:05 +00:00
|
|
|
Remote: from.PullRequest.Head.Repo.CloneURL,
|
2016-11-19 21:36:07 +00:00
|
|
|
Refspec: fmt.Sprintf(refspec,
|
|
|
|
from.PullRequest.Head.Ref,
|
|
|
|
from.PullRequest.Base.Ref,
|
|
|
|
),
|
2016-05-03 20:01:16 +00:00
|
|
|
}
|
|
|
|
if merge {
|
|
|
|
build.Ref = fmt.Sprintf(mergeRefs, from.PullRequest.Number)
|
|
|
|
}
|
|
|
|
return build
|
|
|
|
}
|
2021-06-28 21:50:35 +00:00
|
|
|
|
|
|
|
func getChangedFilesFromWebhook(from *webhook) []string {
|
|
|
|
var files []string
|
|
|
|
files = append(files, from.Head.Added...)
|
|
|
|
files = append(files, from.Head.Removed...)
|
|
|
|
files = append(files, from.Head.Modified...)
|
|
|
|
if len(files) == 0 {
|
|
|
|
files = make([]string, 0)
|
|
|
|
}
|
|
|
|
return files
|
|
|
|
}
|