albatros/pkg/gitea.go
2023-04-17 11:25:26 +02:00

86 lines
2.6 KiB
Go

package pkg
import (
"fmt"
"code.gitea.io/sdk/gitea"
nomad "github.com/hashicorp/nomad/api"
)
type GitUser struct {
Name string `json:"name"`
Email string `json:"email"`
Username string `json:username"`
}
type GiteaCommit struct {
Id string `json:"id"`
Message string `json:"message"`
Url string `json:"string"`
Author GitUser `json:"author"`
Committer GitUser `json:"committer"`
Timestamp string `json:"timestamp"`
}
type GiteaAccount struct {
Id int64 `json:"id"`
Login string `json:"login"`
FullName string `json:"full_name"`
Email string `json:"email"`
AvatarUrl string `json:"avatar_url"`
Username string `json:"username"`
}
type GiteaRepository struct {
Id int64 `json:"id"`
Owner GiteaAccount `json:"owner"`
Name string `json:"name"`
FullName string `json:"full_name"`
Description string `json:"description"`
Private bool `json:"private"`
Fork bool `json:"private"`
HtmlUrl string `json:"html_url"`
SshUrl string `json:"ssh_url"`
CloneUrl string `json:"clone_url"`
Website string `json:"website"`
StarsCount int64 `json:"stars_count"`
ForksCount int64 `json:"forks_count"`
WatchersCount int64 `json:"watchers_count"`
OpenIssuesCount int64 `json:"open_issues_count"`
DefaultBranch string `json:"default_branch"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
type GiteaNotification struct {
Secret string `json:"secret"`
Ref string `json:"ref"`
Before string `json:"before"`
After string `json:"after"`
CompareUrl string `json:"compare_url"`
Commits []GiteaCommit `json:"commits"`
Repository GiteaRepository `json:"repository"`
Pusher GiteaAccount `json:"pusher"`
Sender GiteaAccount `json:"sender"`
}
func nomadToGiteaStatus(summary *nomad.TaskGroupSummary) gitea.StatusState {
if summary.Failed > 0 {
return gitea.StatusError
}
if summary.Lost > 0 || summary.Unknown > 0 {
return gitea.StatusFailure
}
if summary.Queued > 0 || summary.Starting > 0 || summary.Running > 0 {
return gitea.StatusPending
}
if summary.Complete > 0 {
return gitea.StatusSuccess
}
// When the job is just started, all the counters are = 0.
return gitea.StatusPending
}
func notifSummary(notification *GiteaNotification) string {
return fmt.Sprintf("%s/%s:%s", notification.Repository.Owner.Username, notification.Repository.Name, notification.After)
}