diff --git a/models/webhook.go b/models/webhook.go index 4545fd0e1..34a2da13a 100644 --- a/models/webhook.go +++ b/models/webhook.go @@ -130,6 +130,7 @@ const ( MSTEAMS HookType = "msteams" FEISHU HookType = "feishu" MATRIX HookType = "matrix" + JIANMU HookType = "jianmu" ) // HookStatus is the status of a web hook diff --git a/modules/git/repo_compare.go b/modules/git/repo_compare.go index 3255e6839..32fdbc97e 100644 --- a/modules/git/repo_compare.go +++ b/modules/git/repo_compare.go @@ -217,6 +217,11 @@ func (repo *Repository) GetDiff(base, head string, w io.Writer) error { RunInDirPipeline(repo.Path, w, nil) } +func (repo *Repository) GetDiffStringByFilePath(base, head, filepath string) (string, error) { + return NewCommand("diff", "-p", base, head, "--", filepath). + RunInDir(repo.Path) +} + // GetPatch generates and returns format-patch data between given revisions. func (repo *Repository) GetPatch(base, head string, w io.Writer) error { stderr := new(bytes.Buffer) diff --git a/modules/repofiles/file.go b/modules/repofiles/file.go index 7581552a4..0313d0430 100644 --- a/modules/repofiles/file.go +++ b/modules/repofiles/file.go @@ -24,6 +24,9 @@ func GetBatchFileResponseFromCommit(repo *models.Repository, commit *git.Commit, } for _, treeName := range treeNames { fileContent, _ := GetContents(repo, treeName, branch, false) + if fileContent == nil { + continue + } batchFileResponse.Contents = append(batchFileResponse.Contents, fileContent) } diff --git a/modules/structs/hook.go b/modules/structs/hook.go index b985575ae..0d3818593 100644 --- a/modules/structs/hook.go +++ b/modules/structs/hook.go @@ -58,7 +58,7 @@ type CreateHookOptionConfig map[string]string // CreateHookOption options when create a hook type CreateHookOption struct { // required: true - // enum: dingtalk,discord,gitea,gogs,msteams,slack,telegram,feishu + // enum: dingtalk,discord,gitea,gogs,msteams,slack,telegram,feishu,jianmu Type string `json:"type" binding:"Required"` // required: true Config CreateHookOptionConfig `json:"config" binding:"Required"` diff --git a/routers/api/v1/repo/file.go b/routers/api/v1/repo/file.go index 301ab5a0a..bb7fe4666 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -761,8 +761,23 @@ func GetReadmeContents(ctx *context.APIContext) { // treePath := ctx.Params("*") ref := ctx.QueryTrim("ref") + var readmePath string + filesListInterface, _ := repofiles.GetContentsOrList(ctx, ctx.Repo.Repository, "", ref) + filesList, ok := filesListInterface.([]*api.ContentsResponse) + if ok { + for _, file := range filesList { + if strings.ToLower(file.Name) == "readme.md" { + readmePath = file.Name + } + if readmePath != "" { + break + } + } + } else { + readmePath = "README.md" + } - if fileList, err := repofiles.GetContentsOrList(ctx, ctx.Repo.Repository, "README.md", ref); err != nil { + if fileList, err := repofiles.GetContentsOrList(ctx, ctx.Repo.Repository, readmePath, ref); err != nil { if git.IsErrNotExist(err) { ctx.NotFound("GetContentsOrList", err) return @@ -816,7 +831,22 @@ func GetReadmeContentsByPath(ctx *context.APIContext) { treePath := ctx.Params("*") ref := ctx.QueryTrim("ref") - newTreePath := treePath + "/" + "README.md" + var readmePath string + filesListInterface, _ := repofiles.GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref) + filesList, ok := filesListInterface.([]*api.ContentsResponse) + if ok { + for _, file := range filesList { + if strings.ToLower(file.Name) == "readme.md" { + readmePath = file.Name + } + if readmePath != "" { + break + } + } + } else { + readmePath = "README.md" + } + newTreePath := treePath + "/" + readmePath if fileList, err := repofiles.GetContentsOrList(ctx, ctx.Repo.Repository, newTreePath, ref); err != nil { if git.IsErrNotExist(err) { ctx.NotFound("GetContentsOrList", err) diff --git a/routers/api/v1/repo/pull_version.go b/routers/api/v1/repo/pull_version.go index bfe0083ff..8f0e9e21b 100644 --- a/routers/api/v1/repo/pull_version.go +++ b/routers/api/v1/repo/pull_version.go @@ -3,6 +3,7 @@ package repo import ( "fmt" "net/http" + "strings" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" @@ -157,11 +158,39 @@ func GetPullRequestVersionDiff(ctx *context.APIContext) { var targetDiffFile *gitdiff.DiffFile for _, file := range diffs.Files { if file.Name == filepath { + if diffString, err := ctx.Repo.GitRepo.GetDiffStringByFilePath(prv.BaseCommitID, prv.HeadCommitID, file.Name); err == nil { + stringArray := strings.Split(diffString, "\n") + for i, item := range stringArray { + switch { + case strings.HasPrefix(item, "diff --git"): + stringArray[i] = "" + case strings.HasPrefix(item, "index"): + stringArray[i] = "" + case strings.HasPrefix(item, "---"): + stringArray[i] = "" + case strings.HasPrefix(item, "+++"): + stringArray[i] = "" + case strings.HasPrefix(item, "new file mode"): + stringArray[i] = "" + case strings.HasPrefix(item, "deleted file mode"): + stringArray[i] = "" + case strings.HasSuffix(item, "No newline at end of file"): + stringArray[i] = "" + default: + continue + } + } + file.Diff = strings.Join(stringArray, "\n") + } targetDiffFile = file break } } - ctx.JSON(http.StatusOK, targetDiffFile) + if targetDiffFile == nil { + ctx.NotFound() + } else { + ctx.JSON(http.StatusOK, targetDiffFile) + } } } diff --git a/routers/api/v1/repo/wiki.go b/routers/api/v1/repo/wiki.go index 1d8e3b3b1..ef4bab5c1 100644 --- a/routers/api/v1/repo/wiki.go +++ b/routers/api/v1/repo/wiki.go @@ -326,7 +326,12 @@ func ListWikiPages(ctx *context.APIContext) { continue } if entry.IsRegular() { - c, err := wikiRepo.GetCommitByPath(fmt.Sprintf("%s/%s", filePath, entry.Name())) + var commit *git.Commit + if filePath == "" { + commit, err = wikiRepo.GetCommitByPath(entry.Name()) + } else { + commit, err = wikiRepo.GetCommitByPath(fmt.Sprintf("%s/%s", filePath, entry.Name())) + } if err != nil { ctx.Error(http.StatusInternalServerError, "GetCommit", err) return @@ -339,15 +344,20 @@ func ListWikiPages(ctx *context.APIContext) { ctx.Error(http.StatusInternalServerError, "WikiFilenameToName", err) return } - lists = append(lists, convert.RegularToWikiPageMetaData(wikiName, c, ctx.Repo.Repository)) + lists = append(lists, convert.RegularToWikiPageMetaData(wikiName, commit, ctx.Repo.Repository)) } if entry.IsDir() { - c, err := wikiRepo.GetCommitByPath(fmt.Sprintf("%s/%s", filePath, entry.Name())) + var commit *git.Commit + if filePath == "" { + commit, err = wikiRepo.GetCommitByPath(entry.Name()) + } else { + commit, err = wikiRepo.GetCommitByPath(fmt.Sprintf("%s/%s", filePath, entry.Name())) + } if err != nil { ctx.Error(http.StatusInternalServerError, "GetCommit", err) return } - lists = append(lists, convert.DirToWikiPageMetaData(entry.Name(), c, ctx.Repo.Repository)) + lists = append(lists, convert.DirToWikiPageMetaData(entry.Name(), commit, ctx.Repo.Repository)) } } diff --git a/services/gitdiff/gitdiff.go b/services/gitdiff/gitdiff.go index 687259efa..0abfee5bb 100644 --- a/services/gitdiff/gitdiff.go +++ b/services/gitdiff/gitdiff.go @@ -593,6 +593,7 @@ type DiffFile struct { IsIncomplete bool IsIncompleteLineTooLong bool IsProtected bool + Diff string } // GetType returns type of diff file. diff --git a/services/webhook/webhook.go b/services/webhook/webhook.go index d094a7754..ff9e8711b 100644 --- a/services/webhook/webhook.go +++ b/services/webhook/webhook.go @@ -65,6 +65,12 @@ func IsValidHookTaskType(name string) bool { if name == models.GITEA || name == models.GOGS { return true } + + // 建木devops + if name == models.JIANMU { + return true + } + _, ok := webhooks[models.HookType(name)] return ok } @@ -135,7 +141,7 @@ func prepareWebhook(w *models.Webhook, repo *models.Repository, event models.Hoo // Avoid sending "0 new commits" to non-integration relevant webhooks (e.g. slack, discord, etc.). // Integration webhooks (e.g. drone) still receive the required data. if pushEvent, ok := p.(*api.PushPayload); ok && - w.Type != models.GITEA && w.Type != models.GOGS && + w.Type != models.GITEA && w.Type != models.GOGS && w.Type != models.JIANMU && len(pushEvent.Commits) == 0 { return nil } diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 50a035cae..2966ecd4b 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -14615,7 +14615,8 @@ "msteams", "slack", "telegram", - "feishu" + "feishu", + "jianmu" ], "x-go-name": "Type" } @@ -15387,6 +15388,9 @@ "format": "int64", "x-go-name": "Deletion" }, + "Diff": { + "type": "string" + }, "Index": { "type": "integer", "format": "int64"