Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"gitlab.go",
"gitlab_comment.go",
"gitlab_mr.go",
"gitlab_pipeline.go",
"gitlab_push.go",
"mail.go",
"model.go",
"wechat.go",
],
importpath = "go-common/app/tool/saga/model",
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
go_test(
name = "go_default_test",
srcs = ["model_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,77 @@
package model
// User def
type User struct {
Name string `json:"name"`
UserName string `json:"username"`
AvatarURL string `json:"avatar_url"`
}
// Project def
type Project struct {
ID int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
WebURL string `json:"web_url"`
AvatarURL string `json:"avatar_url"`
GitSSHURL string `json:"git_ssh_url"`
GitHTTPURL string `json:"git_http_url"`
Namespace string `json:"namespace"`
VisibilityLevel int64 `json:"visibility_level"`
PathWithNamespace string `json:"path_with_namespace"`
DefaultBranch string `json:"default_branch"`
Homepage string `json:"homepage"`
URL string `json:"url"`
SSHURL string `json:"ssh_url"`
HTTPURL string `json:"http_url"`
}
// Repository def
type Repository struct {
Name string `json:"name"`
URL string `json:"url"`
Description string `json:"description"`
Homepage string `json:"homepage"`
GitHTTPURL string `json:"git_http_url"`
GitSSHURL string `json:"git_ssh_url"`
VisibilityLevel int64 `json:"visibility_level"`
}
// Commit def
type Commit struct {
ID string `json:"id"`
Message string `json:"message"`
Timestamp string `json:"timestamp"`
URL string `json:"url"`
Author *Author `json:"author"`
Added []string `json:"added"`
Modified []string `json:"modified"`
Removed []string `json:"removed"`
}
// Author def
type Author struct {
Name string `json:"name"`
Email string `json:"email"`
}
// WebHook def
type WebHook struct {
URL string `json:"url,omitempty"`
PushEvents bool `json:"push_events,omitempty"`
IssuesEvents bool `json:"issues_events,omitempty"`
ConfidentialIssuesEvents bool `json:"confidential_issues_events,omitempty"`
MergeRequestsEvents bool `json:"merge_requests_events,omitempty"`
TagPushEvents bool `json:"tag_push_events,omitempty"`
NoteEvents bool `json:"note_events,omitempty"`
JobEvents bool `json:"job_events,omitempty"`
PipelineEvents bool `json:"pipeline_events,omitempty"`
WikiPageEvents bool `json:"wiki_page_events,omitempty"`
}
// RepoInfo ...
type RepoInfo struct {
Group string `json:"group"`
Name string `json:"name"`
Branch string `json:"branch"`
}

View File

@@ -0,0 +1,53 @@
package model
const (
//HookCommentTypeMR ...
HookCommentTypeMR = "MergeRequest"
)
const (
// CommentTypeStandard iota
CommentTypeStandard = iota
// CommentTypeMisaka ...
CommentTypeMisaka
// CommentTypeMmerge ...
CommentTypeMmerge
// CommentTypeMerge ...
CommentTypeMerge
// CommentTypeRider ...
CommentTypeRider
// CommentTypeDeploy ...
CommentTypeDeploy
// CommentTypeAddOne ...
CommentTypeAddOne
)
// HookComment struct
type HookComment struct {
ObjectKind string `json:"object_kind"`
User *User `json:"user"`
ProjectID int64 `json:"project_id"`
Project *Project `json:"project"`
Repository *Repository `json:"repository"`
ObjectAttributes *Comment `json:"object_attributes"`
MergeRequest *MergeRequest `json:"merge_request"`
Commit *Commit `json:"commit"`
}
// Comment struct
type Comment struct {
ID int64 `json:"id"`
Note string `json:"note"`
NoteableType string `json:"noteable_type"`
AuthorID int64 `json:"author_id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
ProjectID int64 `json:"project_id"`
Attachment string `json:"attachment"`
LineCode string `json:"line_code"`
CommitID string `json:"commit_id"`
NoteableID int64 `json:"noteable_id"`
System bool `json:"system"`
STDiff string `json:"st_diff"`
URL string `json:"url"`
}

View File

@@ -0,0 +1,113 @@
package model
const (
// MRActionOpen ...
MRActionOpen = "open"
// MRActionReopen ...
MRActionReopen = "reopen"
// MRActionMerge ...
MRActionMerge = "merge"
)
const (
// MRStateOpened ...
MRStateOpened = "opened"
// MRStateClosed ...
MRStateClosed = "closed"
// MRStateMerged ...
MRStateMerged = "merged"
)
const (
// MRMergeOK ...
MRMergeOK = "can_be_merged"
// MRMergeFailed ...
MRMergeFailed = "cannot_be_merged"
// MRMergeUnchecked ...
MRMergeUnchecked = "unchecked"
)
// HookMR def
type HookMR struct {
ObjectKind string `json:"object_kind"`
Project *Project `json:"project"`
User *User `json:"user"`
ObjectAttributes *MergeRequest `json:"object_attributes"`
Assignee *User `json:"assignee"`
}
// MergeRequest struct
type MergeRequest struct {
ID int64 `json:"id"`
TargetBranch string `json:"target_branch"`
SourceBranch string `json:"source_branch"`
SourceProjectID int64 `json:"source_project_id"`
AuthorID int64 `json:"author_id"`
AssigneeID int64 `json:"assignee_id"`
Title string `json:"title"`
CreateAt string `json:"created_at"`
UpdateAt string `json:"updated_at"`
STCommits int64 `json:"st_commits"`
STDiffs int64 `json:"st_diffs"`
MilestoneID int64 `json:"milestone_id"`
State string `json:"state"`
MergeStatus string `json:"merge_status"`
TargetProjectID int64 `json:"target_project_id"`
IID int64 `json:"iid"`
Description string `json:"description"`
Source *Project `json:"source"`
Target *Project `json:"target"`
LastCommit *Commit `json:"last_commit"`
WorkInProgress bool `json:"work_in_progress"`
URL string `json:"url"`
Action string `json:"action"` // "open","update","close"
Sha string `json:"sha"`
}
// MRRecord def
type MRRecord struct {
ProjectID int `json:"pid"`
MRID int `json:"mrid"`
LastCommit string `json:"lc"`
Mail bool `json:"mail"` // 是否发送过邮件
NoteID int `json:"note"`
Report struct {
TimeSpend int64 `json:"rts"`
MergeFlag bool `json:"rmf"`
BuildFlag bool `json:"rbf"`
StaticCheckFlag bool `json:"rsf"`
VetFlag bool `json:"rvf"`
LintFlag bool `json:"rlf"`
RuleFlag bool `json:"rrf"`
} `json:"report"`
Rider struct {
BuildID int64 `json:"ribi"`
BuildFlag bool `json:"ribf"`
BuildCommit string `json:"ribc"`
DeployID int64 `json:"ridi"`
DeployFlag bool `json:"ridf"`
DeployCommit string `json:"ridc"`
} `json:"rider"`
Reviwers []Reviewer `json:"mus"`
ReviewNotify struct {
Reviewer []string `json:"rnr"`
Assign string `json:"rna"`
} `json:"rn"`
}
// Reviewer struct
type Reviewer struct {
Name string `json:"mun"`
CommitID string `json:"muci"`
}
const (
// MRTypeCommon iota
MRTypeCommon = iota
// MRTypeBiz ...
MRTypeBiz
// MRTypeRevert ...
MRTypeRevert
// MRTypeInvalid ...
MRTypeInvalid
)

View File

@@ -0,0 +1,56 @@
package model
const (
// HookPipelineType ...
HookPipelineType = "pipeline"
// PipelineFailed ...
PipelineFailed = "failed"
// PipelineSuccess ...
PipelineSuccess = "success"
// PipelineSkipped ...
PipelineSkipped = "skipped"
// PipelineCanceled ...
PipelineCanceled = "canceled"
// PipelineRunning ...
PipelineRunning = "running"
// PipelinePending ...
PipelinePending = "pending"
// MergeStatusOk ...
MergeStatusOk = "can_be_merged"
// MergeStateOpened ...
MergeStateOpened = "opened"
)
// QueryStatus ...
type QueryStatus int
// query pipeline type.
const (
QueryProcessing QueryStatus = iota
QuerySuccess
QuerySuccessRmNote
QueryID
)
// HookPipeline webhook for pipeline
type HookPipeline struct {
ObjectKind string `json:"object_kind"`
User *User `json:"user"`
Project *Project `json:"project"`
ObjectAttributes *Pipeline `json:"object_attributes"`
Commit *Commit `json:"commit"`
}
// Pipeline object_attributes for pipeline
type Pipeline struct {
ID int64 `json:"id"`
Ref string `json:"ref"`
Tag bool `json:"tag"`
Sha string `json:"sha"`
BeforeSha string `json:"before_sha"`
Status string `json:"status"`
Stages []string `json:"stages"`
CreatedAt string `json:"created_at"`
FinishedAt string `json:"finished_at"`
Duration uint64 `json:"duration"`
}

View File

@@ -0,0 +1,20 @@
package model
// HookPush def
type HookPush struct {
ObjectKind string `json:"object_kind"`
Before string `json:"before"`
After string `json:"after"`
Ref string `json:"ref"`
CheckoutSHA string `json:"checkout_sha"`
UserID int64 `json:"user_id"`
UserName string `json:"user_name"`
UserUserName string `json:"user_username"`
UserEmail string `json:"user_email"`
UserAvatar string `json:"user_avatar"`
ProjectID int64 `json:"project_id"`
Project *Project `json:"project"`
Repository *Repository `json:"repository"`
Commits []*Commit `json:"commits"`
TotalCommitsCount int64 `json:"total_commits_count"`
}

View File

@@ -0,0 +1,27 @@
package model
// Mail def.
type Mail struct {
ToAddress []*MailAddress
Subject string
Body string
}
// MailAddress def.
type MailAddress struct {
Address string
Name string
}
// MailData def.
type MailData struct {
UserName string
SourceBranch string
TargetBranch string
Title string
Description string
URL string
Info string
PipelineStatus string
PipeStatus string
}

View File

@@ -0,0 +1,165 @@
package model
import (
"reflect"
"regexp"
)
// reids锁
const (
SagaTask = "_SagaTask_%d"
SagaRepoLockKey = "_SagaRepoLockKey_%d"
SagaLockValue = "_SagaLockValue"
)
// gitlab指令
const (
SagaCommandPlusOne = "+ok"
SagaCommandMerge = "+mr"
SagaCommandPlusOne1 = "+1"
SagaCommandMerge1 = "+merge"
)
// 任务状态
const (
TaskStatusFailed = 1 // 任务失败
TaskStatusSuccess = 2 // 任务成功
TaskStatusRunning = 3 // 任务运行中
TaskStatusWaiting = 4 // 任务等待
)
// CONTRIBUTORS define
const (
SagaContributorsName = "CONTRIBUTORS.md"
)
// RepoConfig def
type RepoConfig struct {
URL string
Group string
Name string
GName string // gitlab仓库别名
Language string
AuthBranches []string // 鉴权分支
TargetBranches []string // 分支白名单
TargetBranchRegexes []*regexp.Regexp
LockTimeout int32
MinReviewer int
RelatePipeline bool
DelayMerge bool
LimitAuth bool
AllowLabel string
SuperAuthUsers []string
}
// RequireReviewFolder ...
type RequireReviewFolder struct {
Folder string
Owners []string
Reviewers []string
}
// AuthUsers ...
type AuthUsers struct {
Owners []string
Reviewers []string
}
// ContactInfo def
type ContactInfo struct {
ID string `json:"id,omitempty" gorm:"column:id"`
UserName string `json:"english_name" gorm:"column:user_name"`
UserID string `json:"userid" gorm:"column:user_id"`
NickName string `json:"name" gorm:"column:nick_name"`
VisibleSaga bool `json:"visible_saga" gorm:"column:visible_saga"`
}
// RequireVisibleUser def
type RequireVisibleUser struct {
UserName string
NickName string
}
// AlmostEqual return the compare result with fields
func (contact *ContactInfo) AlmostEqual(other *ContactInfo) bool {
if contact.UserID == other.UserID &&
contact.UserName == other.UserName &&
contact.NickName == other.NickName {
return true
}
return false
}
// TaskInfo ...
type TaskInfo struct {
NoteID int
Event *HookComment
Repo *Repo
}
// MergeInfo ...
type MergeInfo struct {
PipelineID int
NoteID int
AuthorID int
UserName string
MRIID int
ProjID int
URL string
AuthBranches []string
SourceBranch string
TargetBranch string
MinReviewer int
LockTimeout int32
Title string
Description string
}
// Repo structure
type Repo struct {
Config *RepoConfig
}
// Update if config is changed
func (r *Repo) Update(conf *RepoConfig) bool {
if r.confEqual(conf) {
return false
}
r.Config = conf
return true
}
func (r *Repo) confEqual(conf *RepoConfig) bool {
if r.Config.URL == conf.URL &&
r.Config.Group == conf.Group &&
r.Config.Name == conf.Name &&
r.Config.GName == conf.GName &&
r.Config.Language == conf.Language &&
reflect.DeepEqual(r.Config.AuthBranches, conf.AuthBranches) &&
reflect.DeepEqual(r.Config.TargetBranches, conf.TargetBranches) &&
r.Config.LockTimeout == conf.LockTimeout &&
r.Config.MinReviewer == conf.MinReviewer &&
r.Config.RelatePipeline == conf.RelatePipeline &&
r.Config.DelayMerge == conf.DelayMerge &&
r.Config.LimitAuth == conf.LimitAuth &&
r.Config.AllowLabel == conf.AllowLabel &&
reflect.DeepEqual(r.Config.SuperAuthUsers, conf.SuperAuthUsers) {
return true
}
return false
}
// AuthUpdate ...
func (r *Repo) AuthUpdate(conf *RepoConfig) bool {
if r.Config.Group == conf.Group &&
r.Config.Name == conf.Name &&
reflect.DeepEqual(r.Config.AuthBranches, conf.AuthBranches) {
return false
}
return true
}
// WebHookUpdate ...
func (r *Repo) WebHookUpdate(conf *RepoConfig) bool {
return r.Config.URL != conf.URL
}

View File

@@ -0,0 +1,227 @@
package model
import (
"encoding/json"
"testing"
)
func TestGitlab(t *testing.T) {
var (
jsonStr = `{
"object_kind": "merge_request",
"user": {
"name": "Administrator",
"username": "root",
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
},
"object_attributes": {
"id": 99,
"target_branch": "master",
"source_branch": "ms-viewport",
"source_project_id": 14,
"author_id": 51,
"assignee_id": 6,
"title": "MS-Viewport",
"created_at": "2013-12-03T17:23:34Z",
"updated_at": "2013-12-03T17:23:34Z",
"st_commits": null,
"st_diffs": null,
"milestone_id": null,
"state": "opened",
"merge_status": "unchecked",
"target_project_id": 14,
"iid": 1,
"description": "",
"source":{
"name":"Awesome Project",
"description":"Aut reprehenderit ut est.",
"web_url":"http://example.com/awesome_space/awesome_project",
"avatar_url":null,
"git_ssh_url":"git@example.com:awesome_space/awesome_project.git",
"git_http_url":"http://example.com/awesome_space/awesome_project.git",
"namespace":"Awesome Space",
"visibility_level":20,
"path_with_namespace":"awesome_space/awesome_project",
"default_branch":"master",
"homepage":"http://example.com/awesome_space/awesome_project",
"url":"http://example.com/awesome_space/awesome_project.git",
"ssh_url":"git@example.com:awesome_space/awesome_project.git",
"http_url":"http://example.com/awesome_space/awesome_project.git"
},
"target": {
"name":"Awesome Project",
"description":"Aut reprehenderit ut est.",
"web_url":"http://example.com/awesome_space/awesome_project",
"avatar_url":null,
"git_ssh_url":"git@example.com:awesome_space/awesome_project.git",
"git_http_url":"http://example.com/awesome_space/awesome_project.git",
"namespace":"Awesome Space",
"visibility_level":20,
"path_with_namespace":"awesome_space/awesome_project",
"default_branch":"master",
"homepage":"http://example.com/awesome_space/awesome_project",
"url":"http://example.com/awesome_space/awesome_project.git",
"ssh_url":"git@example.com:awesome_space/awesome_project.git",
"http_url":"http://example.com/awesome_space/awesome_project.git"
},
"last_commit": {
"id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
"message": "fixed readme",
"timestamp": "2012-01-03T23:36:29+02:00",
"url": "http://example.com/awesome_space/awesome_project/commits/da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
"author": {
"name": "GitLab dev user",
"email": "gitlabdev@dv6700.(none)"
}
},
"work_in_progress": false,
"url": "http://example.com/diaspora/merge_requests/1",
"action": "open",
"assignee": {
"name": "User1",
"username": "user1",
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
}
}
}
`
mrHook = &HookMR{}
err error
)
err = json.Unmarshal([]byte(jsonStr), mrHook)
if err != nil {
t.Fatal(err)
}
t.Log(mrHook)
t.Log(mrHook.User)
t.Log(mrHook.ObjectAttributes)
var (
commentJSONStr = `{
"object_kind": "note",
"user": {
"name": "Administrator",
"username": "root",
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
},
"project_id": 5,
"project":{
"name":"Gitlab Test",
"description":"Aut reprehenderit ut est.",
"web_url":"http://example.com/gitlab-org/gitlab-test",
"avatar_url":null,
"git_ssh_url":"git@example.com:gitlab-org/gitlab-test.git",
"git_http_url":"http://example.com/gitlab-org/gitlab-test.git",
"namespace":"Gitlab Org",
"visibility_level":10,
"path_with_namespace":"gitlab-org/gitlab-test",
"default_branch":"master",
"homepage":"http://example.com/gitlab-org/gitlab-test",
"url":"http://example.com/gitlab-org/gitlab-test.git",
"ssh_url":"git@example.com:gitlab-org/gitlab-test.git",
"http_url":"http://example.com/gitlab-org/gitlab-test.git"
},
"repository":{
"name": "Gitlab Test",
"url": "http://localhost/gitlab-org/gitlab-test.git",
"description": "Aut reprehenderit ut est.",
"homepage": "http://example.com/gitlab-org/gitlab-test"
},
"object_attributes": {
"id": 1244,
"note": "This MR needs work.",
"noteable_type": "MergeRequest",
"author_id": 1,
"created_at": "2015-05-17 18:21:36 UTC",
"updated_at": "2015-05-17 18:21:36 UTC",
"project_id": 5,
"attachment": null,
"line_code": null,
"commit_id": "",
"noteable_id": 7,
"system": false,
"st_diff": null,
"url": "http://example.com/gitlab-org/gitlab-test/merge_requests/1#note_1244"
},
"merge_request": {
"id": 7,
"target_branch": "markdown",
"source_branch": "master",
"source_project_id": 5,
"author_id": 8,
"assignee_id": 28,
"title": "Tempora et eos debitis quae laborum et.",
"created_at": "2015-03-01 20:12:53 UTC",
"updated_at": "2015-03-21 18:27:27 UTC",
"milestone_id": 11,
"state": "opened",
"merge_status": "cannot_be_merged",
"target_project_id": 5,
"iid": 1,
"description": "Et voluptas corrupti assumenda temporibus. Architecto cum animi eveniet amet asperiores. Vitae numquam voluptate est natus sit et ad id.",
"position": 0,
"source":{
"name":"Gitlab Test",
"description":"Aut reprehenderit ut est.",
"web_url":"http://example.com/gitlab-org/gitlab-test",
"avatar_url":null,
"git_ssh_url":"git@example.com:gitlab-org/gitlab-test.git",
"git_http_url":"http://example.com/gitlab-org/gitlab-test.git",
"namespace":"Gitlab Org",
"visibility_level":10,
"path_with_namespace":"gitlab-org/gitlab-test",
"default_branch":"master",
"homepage":"http://example.com/gitlab-org/gitlab-test",
"url":"http://example.com/gitlab-org/gitlab-test.git",
"ssh_url":"git@example.com:gitlab-org/gitlab-test.git",
"http_url":"http://example.com/gitlab-org/gitlab-test.git"
},
"target": {
"name":"Gitlab Test",
"description":"Aut reprehenderit ut est.",
"web_url":"http://example.com/gitlab-org/gitlab-test",
"avatar_url":null,
"git_ssh_url":"git@example.com:gitlab-org/gitlab-test.git",
"git_http_url":"http://example.com/gitlab-org/gitlab-test.git",
"namespace":"Gitlab Org",
"visibility_level":10,
"path_with_namespace":"gitlab-org/gitlab-test",
"default_branch":"master",
"homepage":"http://example.com/gitlab-org/gitlab-test",
"url":"http://example.com/gitlab-org/gitlab-test.git",
"ssh_url":"git@example.com:gitlab-org/gitlab-test.git",
"http_url":"http://example.com/gitlab-org/gitlab-test.git"
},
"last_commit": {
"id": "562e173be03b8ff2efb05345d12df18815438a4b",
"message": "Merge branch 'another-branch' into 'master'\n\nCheck in this test\n",
"timestamp": "2015-04-08T21:00:25-07:00",
"url": "http://example.com/gitlab-org/gitlab-test/commit/562e173be03b8ff2efb05345d12df18815438a4b",
"author": {
"name": "John Smith",
"email": "john@example.com"
}
},
"work_in_progress": false,
"assignee": {
"name": "User1",
"username": "user1",
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=40\u0026d=identicon"
}
}
}
`
hookComment = &HookComment{}
)
err = json.Unmarshal([]byte(commentJSONStr), hookComment)
if err != nil {
t.Fatal(err)
}
t.Log(hookComment)
t.Log(hookComment.User)
t.Log(hookComment.Project)
t.Log(hookComment.Repository)
t.Log(hookComment.ObjectAttributes)
t.Log(hookComment.MergeRequest)
t.Log(hookComment.Commit)
}

View File

@@ -0,0 +1,38 @@
package model
// AppConfig def
type AppConfig struct {
AppID int // 企业微信SAGA应用的appId
AppSecret string // 企业微信SAGA应用的secret
}
// Notification def
type Notification struct {
ToUser string `json:"touser"`
ToParty string `json:"toparty"`
ToTag string `json:"totag"`
MsgType string `json:"msgtype"`
AgentID int `json:"agentid"`
}
// Text def
type Text struct {
Content string `json:"content"`
}
// TxtNotification 文本消息
type TxtNotification struct {
Notification
Body Text `json:"text"`
Safe int `json:"safe"`
}
// AllowUserInfo 应用可见名单列表
type AllowUserInfo struct {
Users []*UserInfo `json:"user"`
}
// UserInfo only contain userid now
type UserInfo struct {
UserID string `json:"userid"`
}