new wiki.go content.go, fix api.go

This commit is contained in:
wonderful 2021-08-05 16:53:03 +08:00
commit 757a3816f7
5 changed files with 57 additions and 32 deletions

View File

@ -156,8 +156,6 @@ type Repository struct {
OriginalURL string `xorm:"VARCHAR(2048)"`
DefaultBranch string
NumWikis int
NumWatches int
NumStars int
NumForks int
@ -183,10 +181,6 @@ type Repository struct {
Units []*RepoUnit `xorm:"-"`
PrimaryLanguage *LanguageStat `xorm:"-"`
IsWiki bool `xorm:"INDEX NOT NULL DEFAULT false"`
WikiID int64 `xorm:"INDEX"`
IsFork bool `xorm:"INDEX NOT NULL DEFAULT false"`
ForkID int64 `xorm:"INDEX"`
BaseRepo *Repository `xorm:"-"`
@ -2125,6 +2119,7 @@ func CopyLFS(ctx DBContext, newRepo, oldRepo *Repository) error {
return nil
}
<<<<<<< HEAD
/////////////////////////////GetWiki
func (repo *Repository) GetWikis(listOptions ListOptions) ([]*Repository, error) {
if listOptions.Page == 0 {
@ -2137,6 +2132,8 @@ func (repo *Repository) GetWikis(listOptions ListOptions) ([]*Repository, error)
return wikis, sess.Find(&wikis, &Repository{WikiID: repo.ID})
}
=======
>>>>>>> 3b5dff2e2323aa944690ad52574c3e3b389689ac
// GetForks returns all the forks of the repository
func (repo *Repository) GetForks(listOptions ListOptions) ([]*Repository, error) {

View File

@ -55,6 +55,13 @@ func IsValidHookContentType(name string) bool {
return ok
}
func IsValidHookHttpMethod(name string) bool {
if name == "POST" || name == "GET" {
return true
}
return false
}
// HookEvents is a set of web hook events
type HookEvents struct {
Create bool `json:"create"`
@ -83,7 +90,6 @@ type HookEvent struct {
SendEverything bool `json:"send_everything"`
ChooseEvents bool `json:"choose_events"`
BranchFilter string `json:"branch_filter"`
HTTPMethod string `json:"http_method"`
HookEvents `json:"events"`
}
@ -116,7 +122,7 @@ type Webhook struct {
HookTaskType HookTaskType
Meta string `xorm:"TEXT"` // store hook-specific attributes
LastStatus HookStatus // Last delivery status
BranchFilter string `xorm:"TEXT"`
BranchFilter string `xorm:"TEXT"`
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`

View File

@ -255,6 +255,7 @@ func ToHook(repoLink string, w *models.Webhook) *api.Hook {
config := map[string]string{
"url": w.URL,
"content_type": w.ContentType.Name(),
"http_method": w.HTTPMethod,
}
if w.HookTaskType == models.SLACK {
s := webhook.GetSlackHook(w)
@ -265,16 +266,15 @@ func ToHook(repoLink string, w *models.Webhook) *api.Hook {
}
return &api.Hook{
ID: w.ID,
Type: w.HookTaskType.Name(),
URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID),
BranchFilter:w.HookEvent.BranchFilter,
Active: w.IsActive,
Config: config,
Events: w.EventsArray(),
Updated: w.UpdatedUnix.AsTime(),
Created: w.CreatedUnix.AsTime(),
HTTPMethod: w.HookEvent.HTTPMethod,
ID: w.ID,
Type: w.HookTaskType.Name(),
URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID),
BranchFilter: w.HookEvent.BranchFilter,
Active: w.IsActive,
Config: config,
Events: w.EventsArray(),
Updated: w.UpdatedUnix.AsTime(),
Created: w.CreatedUnix.AsTime(),
}
}

View File

@ -19,14 +19,13 @@ var (
// Hook a hook is a web hook when one repository changed
type Hook struct {
ID int64 `json:"id"`
Type string `json:"type"`
URL string `json:"-"`
Config map[string]string `json:"config"`
Events []string `json:"events"`
Active bool `json:"active"`
BranchFilter string `json:"branch_filter"`
HTTPMethod string `json:"http_method"`
ID int64 `json:"id"`
Type string `json:"type"`
URL string `json:"-"`
Config map[string]string `json:"config"`
Events []string `json:"events"`
Active bool `json:"active"`
BranchFilter string `json:"branch_filter"`
// swagger:strfmt date-time
Updated time.Time `json:"updated_at"`
// swagger:strfmt date-time

View File

@ -146,6 +146,11 @@ func CheckCreateHookOption(ctx *context.APIContext, form *api.CreateHookOption)
ctx.Error(http.StatusUnprocessableEntity, "", "Invalid content type")
return false
}
if !models.IsValidHookHttpMethod(form.Config["http_method"]) {
ctx.Error(http.StatusUnprocessableEntity, "", "Invalid http method")
return false
}
return true
}
@ -292,6 +297,9 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *models.Webho
if url, ok := form.Config["url"]; ok {
w.URL = url
}
if secret, ok := form.Config["secret"]; ok {
w.Secret = secret
}
if ct, ok := form.Config["content_type"]; ok {
if !models.IsValidHookContentType(ct) {
ctx.Error(http.StatusUnprocessableEntity, "", "Invalid content type")
@ -300,6 +308,14 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *models.Webho
w.ContentType = models.ToHookContentType(ct)
}
if hm, ok := form.Config["http_method"]; ok {
if !models.IsValidHookHttpMethod(hm) {
ctx.Error(http.StatusUnprocessableEntity, "", "Invalid http method")
return false
}
w.HTTPMethod = hm
}
if w.HookTaskType == models.SLACK {
if channel, ok := form.Config["channel"]; ok {
meta, err := json.Marshal(&webhook.SlackMeta{
@ -325,18 +341,25 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *models.Webho
w.SendEverything = false
w.ChooseEvents = true
w.Create = com.IsSliceContainsStr(form.Events, string(models.HookEventCreate))
w.Push = com.IsSliceContainsStr(form.Events, string(models.HookEventPush))
w.PullRequest = com.IsSliceContainsStr(form.Events, string(models.HookEventPullRequest))
w.Create = com.IsSliceContainsStr(form.Events, string(models.HookEventCreate))
w.Delete = com.IsSliceContainsStr(form.Events, string(models.HookEventDelete))
w.Fork = com.IsSliceContainsStr(form.Events, string(models.HookEventFork))
w.Issues = com.IsSliceContainsStr(form.Events, string(models.HookEventIssues))
w.IssueComment = com.IsSliceContainsStr(form.Events, string(models.HookEventIssueComment))
w.Issues = issuesHook(form.Events, "issues_only")
w.IssueAssign = issuesHook(form.Events, string(models.HookEventIssueAssign))
w.IssueLabel = issuesHook(form.Events, string(models.HookEventIssueLabel))
w.IssueMilestone = issuesHook(form.Events, string(models.HookEventIssueMilestone))
w.IssueComment = issuesHook(form.Events, string(models.HookEventIssueComment))
w.Push = com.IsSliceContainsStr(form.Events, string(models.HookEventPush))
w.PullRequest = com.IsSliceContainsStr(form.Events, string(models.HookEventPullRequest))
w.PullRequest = pullHook(form.Events, "pull_request_only")
w.PullRequestAssign = pullHook(form.Events, string(models.HookEventPullRequestAssign))
w.PullRequestLabel = pullHook(form.Events, string(models.HookEventPullRequestLabel))
w.PullRequestMilestone = pullHook(form.Events, string(models.HookEventPullRequestMilestone))
w.PullRequestComment = pullHook(form.Events, string(models.HookEventPullRequestComment))
w.PullRequestReview = pullHook(form.Events, "pull_request_review")
w.PullRequestSync = pullHook(form.Events, string(models.HookEventPullRequestSync))
w.Repository = com.IsSliceContainsStr(form.Events, string(models.HookEventRepository))
w.Release = com.IsSliceContainsStr(form.Events, string(models.HookEventRelease))
w.BranchFilter = form.BranchFilter
w.HookEvent.BranchFilter = form.BranchFilter
if err := w.UpdateEvent(); err != nil {
ctx.Error(http.StatusInternalServerError, "UpdateEvent", err)