From c51ed903ea329ae42b25d4f6d7719e5b2cff46b7 Mon Sep 17 00:00:00 2001
From: George Harvey <11440490+HarvsG@users.noreply.github.com>
Date: Thu, 26 Sep 2019 20:19:48 +0100
Subject: [PATCH 1/4] allow external rendering of other filetypes

fixes #4996 and #7614
allows rendering of non-tex files, or otherwise accounted for filetypes
---
 routers/repo/view.go | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/routers/repo/view.go b/routers/repo/view.go
index 00790a4ef35be..6700c5f47afba 100644
--- a/routers/repo/view.go
+++ b/routers/repo/view.go
@@ -342,7 +342,22 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
 		ctx.Data["IsAudioFile"] = true
 	case base.IsImageFile(buf):
 		ctx.Data["IsImageFile"] = true
+	default:
+		d, _ := ioutil.ReadAll(dataRc)
+		buf = append(buf, d...)
+
+		if fileSize >= setting.UI.MaxDisplayFileSize {
+			ctx.Data["IsFileTooLarge"] = true
+			break
+		}
+
+		if markupType := markup.Type(blob.Name()); markupType != "" {
+			ctx.Data["IsMarkup"] = true
+			ctx.Data["MarkupType"] = markupType
+			ctx.Data["FileContent"] = string(markup.Render(blob.Name(), buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas()))
+			} 
 	}
+	
 
 	if ctx.Repo.CanEnableEditor() {
 		ctx.Data["CanDeleteFile"] = true

From eb11ca68470e4ab60ed4ec31f4d170a4b36a528e Mon Sep 17 00:00:00 2001
From: Lunny Xiao <xiaolunwen@gmail.com>
Date: Fri, 27 Sep 2019 08:22:36 +0800
Subject: [PATCH 2/4] Extract actions on new pull request from models to pulls
 service and move code.gitea.io/gitea/modules/pull to
 code.gitea.io/gitea/services/pull (#8218)

* extract actions on new pull request from models to pulls service

* improve code

* move code.gitea.io/gitea/modules/pull to code.gitea.io/gitea/services/pull

* fix fmt

* Rename pulls.go to pull.go
---
 models/pull.go                              | 27 ------------
 routers/api/v1/repo/pull.go                 |  7 ++-
 routers/repo/pull.go                        |  7 ++-
 routers/repo/pull_review.go                 |  2 +-
 {modules => services}/pull/commit_status.go |  0
 {modules => services}/pull/lfs.go           |  0
 {modules => services}/pull/merge.go         |  0
 services/pull/pull.go                       | 49 +++++++++++++++++++++
 {modules => services}/pull/review.go        |  0
 9 files changed, 56 insertions(+), 36 deletions(-)
 rename {modules => services}/pull/commit_status.go (100%)
 rename {modules => services}/pull/lfs.go (100%)
 rename {modules => services}/pull/merge.go (100%)
 create mode 100644 services/pull/pull.go
 rename {modules => services}/pull/review.go (100%)

diff --git a/models/pull.go b/models/pull.go
index 2f7218f4157e3..60ccb144f510a 100644
--- a/models/pull.go
+++ b/models/pull.go
@@ -700,33 +700,6 @@ func NewPullRequest(repo *Repository, pull *Issue, labelIDs []int64, uuids []str
 		return fmt.Errorf("Commit: %v", err)
 	}
 
-	if err = NotifyWatchers(&Action{
-		ActUserID: pull.Poster.ID,
-		ActUser:   pull.Poster,
-		OpType:    ActionCreatePullRequest,
-		Content:   fmt.Sprintf("%d|%s", pull.Index, pull.Title),
-		RepoID:    repo.ID,
-		Repo:      repo,
-		IsPrivate: repo.IsPrivate,
-	}); err != nil {
-		log.Error("NotifyWatchers: %v", err)
-	}
-
-	pr.Issue = pull
-	pull.PullRequest = pr
-	mode, _ := AccessLevel(pull.Poster, repo)
-	if err = PrepareWebhooks(repo, HookEventPullRequest, &api.PullRequestPayload{
-		Action:      api.HookIssueOpened,
-		Index:       pull.Index,
-		PullRequest: pr.APIFormat(),
-		Repository:  repo.APIFormat(mode),
-		Sender:      pull.Poster.APIFormat(),
-	}); err != nil {
-		log.Error("PrepareWebhooks: %v", err)
-	} else {
-		go HookQueue.Add(repo.ID)
-	}
-
 	return nil
 }
 
diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go
index 0f9eab2f5029d..978c8a3f1f491 100644
--- a/routers/api/v1/repo/pull.go
+++ b/routers/api/v1/repo/pull.go
@@ -15,11 +15,10 @@ import (
 	"code.gitea.io/gitea/modules/git"
 	"code.gitea.io/gitea/modules/log"
 	"code.gitea.io/gitea/modules/notification"
-	"code.gitea.io/gitea/modules/pull"
-	pull_service "code.gitea.io/gitea/modules/pull"
 	api "code.gitea.io/gitea/modules/structs"
 	"code.gitea.io/gitea/modules/timeutil"
 	milestone_service "code.gitea.io/gitea/services/milestone"
+	pull_service "code.gitea.io/gitea/services/pull"
 )
 
 // ListPullRequests returns a list of all PRs
@@ -288,7 +287,7 @@ func CreatePullRequest(ctx *context.APIContext, form api.CreatePullRequestOption
 		return
 	}
 
-	if err := models.NewPullRequest(repo, prIssue, labelIDs, []string{}, pr, patch, assigneeIDs); err != nil {
+	if err := pull_service.NewPullRequest(repo, prIssue, labelIDs, []string{}, pr, patch, assigneeIDs); err != nil {
 		if models.IsErrUserDoesNotHaveAccessToRepo(err) {
 			ctx.Error(400, "UserDoesNotHaveAccessToRepo", err)
 			return
@@ -602,7 +601,7 @@ func MergePullRequest(ctx *context.APIContext, form auth.MergePullRequestForm) {
 		message += "\n\n" + form.MergeMessageField
 	}
 
-	if err := pull.Merge(pr, ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil {
+	if err := pull_service.Merge(pr, ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil {
 		if models.IsErrInvalidMergeStyle(err) {
 			ctx.Status(405)
 			return
diff --git a/routers/repo/pull.go b/routers/repo/pull.go
index 180d592e3dbf8..72d2ffcaa7d66 100644
--- a/routers/repo/pull.go
+++ b/routers/repo/pull.go
@@ -21,11 +21,10 @@ import (
 	"code.gitea.io/gitea/modules/git"
 	"code.gitea.io/gitea/modules/log"
 	"code.gitea.io/gitea/modules/notification"
-	"code.gitea.io/gitea/modules/pull"
-	pull_service "code.gitea.io/gitea/modules/pull"
 	"code.gitea.io/gitea/modules/setting"
 	"code.gitea.io/gitea/modules/util"
 	"code.gitea.io/gitea/services/gitdiff"
+	pull_service "code.gitea.io/gitea/services/pull"
 
 	"github.com/unknwon/com"
 )
@@ -676,7 +675,7 @@ func MergePullRequest(ctx *context.Context, form auth.MergePullRequestForm) {
 		return
 	}
 
-	if err = pull.Merge(pr, ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil {
+	if err = pull_service.Merge(pr, ctx.User, ctx.Repo.GitRepo, models.MergeStyle(form.Do), message); err != nil {
 		if models.IsErrInvalidMergeStyle(err) {
 			ctx.Flash.Error(ctx.Tr("repo.pulls.invalid_merge_option"))
 			ctx.Redirect(ctx.Repo.RepoLink + "/pulls/" + com.ToStr(pr.Index))
@@ -789,7 +788,7 @@ func CompareAndPullRequestPost(ctx *context.Context, form auth.CreateIssueForm)
 	// FIXME: check error in the case two people send pull request at almost same time, give nice error prompt
 	// instead of 500.
 
-	if err := models.NewPullRequest(repo, pullIssue, labelIDs, attachments, pullRequest, patch, assigneeIDs); err != nil {
+	if err := pull_service.NewPullRequest(repo, pullIssue, labelIDs, attachments, pullRequest, patch, assigneeIDs); err != nil {
 		if models.IsErrUserDoesNotHaveAccessToRepo(err) {
 			ctx.Error(400, "UserDoesNotHaveAccessToRepo", err.Error())
 			return
diff --git a/routers/repo/pull_review.go b/routers/repo/pull_review.go
index d4e3a3326a1b3..5eb0dfe9a73d8 100644
--- a/routers/repo/pull_review.go
+++ b/routers/repo/pull_review.go
@@ -12,8 +12,8 @@ import (
 	"code.gitea.io/gitea/modules/context"
 	"code.gitea.io/gitea/modules/log"
 	"code.gitea.io/gitea/modules/notification"
-	pull_service "code.gitea.io/gitea/modules/pull"
 	comment_service "code.gitea.io/gitea/services/comments"
+	pull_service "code.gitea.io/gitea/services/pull"
 )
 
 // CreateCodeComment will create a code comment including an pending review if required
diff --git a/modules/pull/commit_status.go b/services/pull/commit_status.go
similarity index 100%
rename from modules/pull/commit_status.go
rename to services/pull/commit_status.go
diff --git a/modules/pull/lfs.go b/services/pull/lfs.go
similarity index 100%
rename from modules/pull/lfs.go
rename to services/pull/lfs.go
diff --git a/modules/pull/merge.go b/services/pull/merge.go
similarity index 100%
rename from modules/pull/merge.go
rename to services/pull/merge.go
diff --git a/services/pull/pull.go b/services/pull/pull.go
new file mode 100644
index 0000000000000..0dbd8fcd1a148
--- /dev/null
+++ b/services/pull/pull.go
@@ -0,0 +1,49 @@
+// Copyright 2019 The Gitea Authors. All rights reserved.
+// Use of this source code is governed by a MIT-style
+// license that can be found in the LICENSE file.
+
+package pull
+
+import (
+	"fmt"
+
+	"code.gitea.io/gitea/models"
+	"code.gitea.io/gitea/modules/log"
+	api "code.gitea.io/gitea/modules/structs"
+)
+
+// NewPullRequest creates new pull request with labels for repository.
+func NewPullRequest(repo *models.Repository, pull *models.Issue, labelIDs []int64, uuids []string, pr *models.PullRequest, patch []byte, assigneeIDs []int64) error {
+	if err := models.NewPullRequest(repo, pull, labelIDs, uuids, pr, patch, assigneeIDs); err != nil {
+		return err
+	}
+
+	if err := models.NotifyWatchers(&models.Action{
+		ActUserID: pull.Poster.ID,
+		ActUser:   pull.Poster,
+		OpType:    models.ActionCreatePullRequest,
+		Content:   fmt.Sprintf("%d|%s", pull.Index, pull.Title),
+		RepoID:    repo.ID,
+		Repo:      repo,
+		IsPrivate: repo.IsPrivate,
+	}); err != nil {
+		log.Error("NotifyWatchers: %v", err)
+	}
+
+	pr.Issue = pull
+	pull.PullRequest = pr
+	mode, _ := models.AccessLevel(pull.Poster, repo)
+	if err := models.PrepareWebhooks(repo, models.HookEventPullRequest, &api.PullRequestPayload{
+		Action:      api.HookIssueOpened,
+		Index:       pull.Index,
+		PullRequest: pr.APIFormat(),
+		Repository:  repo.APIFormat(mode),
+		Sender:      pull.Poster.APIFormat(),
+	}); err != nil {
+		log.Error("PrepareWebhooks: %v", err)
+	} else {
+		go models.HookQueue.Add(repo.ID)
+	}
+
+	return nil
+}
diff --git a/modules/pull/review.go b/services/pull/review.go
similarity index 100%
rename from modules/pull/review.go
rename to services/pull/review.go

From c6fb7fe27c16c4e43d4d8dbe4d2ff4b3c4c52a29 Mon Sep 17 00:00:00 2001
From: David Svantesson <davidsvantesson@gmail.com>
Date: Fri, 27 Sep 2019 04:24:06 +0200
Subject: [PATCH 3/4] Fix API for edit and delete release attachment (#8285)

* Add logging for when user requested attachment doesn't belong to the specified release.

* Fix API to use correct variable for release asset (attachment)
---
 routers/api/v1/repo/release_attachment.go | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/routers/api/v1/repo/release_attachment.go b/routers/api/v1/repo/release_attachment.go
index 67802fd9e7cc3..c49e4d3e346ec 100644
--- a/routers/api/v1/repo/release_attachment.go
+++ b/routers/api/v1/repo/release_attachment.go
@@ -9,6 +9,7 @@ import (
 
 	"code.gitea.io/gitea/models"
 	"code.gitea.io/gitea/modules/context"
+	"code.gitea.io/gitea/modules/log"
 	"code.gitea.io/gitea/modules/setting"
 	api "code.gitea.io/gitea/modules/structs"
 	"code.gitea.io/gitea/modules/upload"
@@ -55,6 +56,7 @@ func GetReleaseAttachment(ctx *context.APIContext) {
 		return
 	}
 	if attach.ReleaseID != releaseID {
+		log.Info("User requested attachment is not in release, release_id %v, attachment_id: %v", releaseID, attachID)
 		ctx.NotFound()
 		return
 	}
@@ -242,13 +244,14 @@ func EditReleaseAttachment(ctx *context.APIContext, form api.EditAttachmentOptio
 
 	// Check if release exists an load release
 	releaseID := ctx.ParamsInt64(":id")
-	attachID := ctx.ParamsInt64(":attachment")
+	attachID := ctx.ParamsInt64(":asset")
 	attach, err := models.GetAttachmentByID(attachID)
 	if err != nil {
 		ctx.Error(500, "GetAttachmentByID", err)
 		return
 	}
 	if attach.ReleaseID != releaseID {
+		log.Info("User requested attachment is not in release, release_id %v, attachment_id: %v", releaseID, attachID)
 		ctx.NotFound()
 		return
 	}
@@ -299,13 +302,14 @@ func DeleteReleaseAttachment(ctx *context.APIContext) {
 
 	// Check if release exists an load release
 	releaseID := ctx.ParamsInt64(":id")
-	attachID := ctx.ParamsInt64(":attachment")
+	attachID := ctx.ParamsInt64(":asset")
 	attach, err := models.GetAttachmentByID(attachID)
 	if err != nil {
 		ctx.Error(500, "GetAttachmentByID", err)
 		return
 	}
 	if attach.ReleaseID != releaseID {
+		log.Info("User requested attachment is not in release, release_id %v, attachment_id: %v", releaseID, attachID)
 		ctx.NotFound()
 		return
 	}

From 420319622ed9fc5a2260d519ca28c33562c44123 Mon Sep 17 00:00:00 2001
From: Gitea <gitea@fake.local>
Date: Fri, 27 Sep 2019 08:15:44 +0000
Subject: [PATCH 4/4] go fmted with -s

---
 routers/repo/view.go | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/routers/repo/view.go b/routers/repo/view.go
index 00790a4ef35be..b507fd7987105 100644
--- a/routers/repo/view.go
+++ b/routers/repo/view.go
@@ -342,6 +342,20 @@ func renderFile(ctx *context.Context, entry *git.TreeEntry, treeLink, rawLink st
 		ctx.Data["IsAudioFile"] = true
 	case base.IsImageFile(buf):
 		ctx.Data["IsImageFile"] = true
+	default:
+		d, _ := ioutil.ReadAll(dataRc)
+		buf = append(buf, d...)
+
+		if fileSize >= setting.UI.MaxDisplayFileSize {
+			ctx.Data["IsFileTooLarge"] = true
+			break
+		}
+
+		if markupType := markup.Type(blob.Name()); markupType != "" {
+			ctx.Data["IsMarkup"] = true
+			ctx.Data["MarkupType"] = markupType
+			ctx.Data["FileContent"] = string(markup.Render(blob.Name(), buf, path.Dir(treeLink), ctx.Repo.Repository.ComposeMetas()))
+		}
 	}
 
 	if ctx.Repo.CanEnableEditor() {