From 747f423125647ed4c7ba4d40d2c79a5780bc7a08 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Sat, 14 Jan 2023 08:16:30 +0100 Subject: [PATCH] [BRANDING] add X-Forgejo-* headers (cherry picked from commit 0a3388f93f53f53cce79ebcc194d67979cbe61cc) (cherry picked from commit 7eba0a440a3138443d0b7461560f9f6f1c46b256) (cherry picked from commit eb9646c7ef0b1a9e73bf22806acbc2caeaee7ff3) (cherry picked from commit f1972578f5cb72ad9e1ef1ded9aa324b1d669864) Conflicts: (cherry picked from commit 3d72f6b3ac6019db1a44bf6517e4fec4775c367b) (cherry picked from commit 6a94f830c0ddaa4e1c59f689d3bb6b59186d55b3) (cherry picked from commit 522a09dbc186e72250fe233bdf4b25e07eee51db) (cherry picked from commit 471ad9f728fe897c5f295088c97950246a00a3bc) (cherry picked from commit a27221e653141fb2bf5989b8f31cdca8b5b27ddc) --- modules/httpcache/httpcache.go | 1 + modules/httpcache/httpcache_test.go | 3 +++ routers/api/v1/api.go | 2 +- routers/api/v1/repo/file.go | 7 ++++++- services/mailer/mail.go | 10 ++++++++++ services/webhook/deliver.go | 4 ++++ 6 files changed, 25 insertions(+), 2 deletions(-) diff --git a/modules/httpcache/httpcache.go b/modules/httpcache/httpcache.go index 46e0152ef4..ee83706d37 100644 --- a/modules/httpcache/httpcache.go +++ b/modules/httpcache/httpcache.go @@ -32,6 +32,7 @@ func SetCacheControlInHeader(h http.Header, maxAge time.Duration, additionalDire // to remind users they are using non-prod setting. h.Set("X-Gitea-Debug", "RUN_MODE="+setting.RunMode) + h.Set("X-Forgejo-Debug", "RUN_MODE="+setting.RunMode) } h.Set("Cache-Control", strings.Join(append(directives, additionalDirectives...), ", ")) diff --git a/modules/httpcache/httpcache_test.go b/modules/httpcache/httpcache_test.go index 624d2f4d4b..dac1a63793 100644 --- a/modules/httpcache/httpcache_test.go +++ b/modules/httpcache/httpcache_test.go @@ -29,6 +29,9 @@ func countFormalHeaders(h http.Header) (c int) { if strings.HasPrefix(k, "X-Gitea-") { continue } + if strings.HasPrefix(k, "X-Forgejo-") { + continue + } c++ } return c diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 386c3508d8..b30c231f2e 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -675,7 +675,7 @@ func Routes(ctx gocontext.Context) *web.Route { // setting.CORSConfig.AllowSubdomain // FIXME: the cors middleware needs allowSubdomain option AllowedMethods: setting.CORSConfig.Methods, AllowCredentials: setting.CORSConfig.AllowCredentials, - AllowedHeaders: append([]string{"Authorization", "X-Gitea-OTP"}, setting.CORSConfig.Headers...), + AllowedHeaders: append([]string{"Authorization", "X-Gitea-OTP", "X-Forgejo-OTP"}, setting.CORSConfig.Headers...), MaxAge: int(setting.CORSConfig.MaxAge.Seconds()), })) } diff --git a/routers/api/v1/repo/file.go b/routers/api/v1/repo/file.go index d5e8924f5d..c76a25f71f 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -32,7 +32,10 @@ import ( files_service "code.gitea.io/gitea/services/repository/files" ) -const giteaObjectTypeHeader = "X-Gitea-Object-Type" +const ( + giteaObjectTypeHeader = "X-Gitea-Object-Type" + forgejoObjectTypeHeader = "X-Forgejo-Object-Type" +) // GetRawFile get a file by path on a repository func GetRawFile(ctx *context.APIContext) { @@ -79,6 +82,7 @@ func GetRawFile(ctx *context.APIContext) { } ctx.RespHeader().Set(giteaObjectTypeHeader, string(files_service.GetObjectTypeFromTreeEntry(entry))) + ctx.RespHeader().Set(forgejoObjectTypeHeader, string(files_service.GetObjectTypeFromTreeEntry(entry))) if err := common.ServeBlob(ctx.Context, blob, lastModified); err != nil { ctx.Error(http.StatusInternalServerError, "ServeBlob", err) @@ -128,6 +132,7 @@ func GetRawFileOrLFS(ctx *context.APIContext) { } ctx.RespHeader().Set(giteaObjectTypeHeader, string(files_service.GetObjectTypeFromTreeEntry(entry))) + ctx.RespHeader().Set(forgejoObjectTypeHeader, string(files_service.GetObjectTypeFromTreeEntry(entry))) // LFS Pointer files are at most 1024 bytes - so any blob greater than 1024 bytes cannot be an LFS file if blob.Size() > 1024 { diff --git a/services/mailer/mail.go b/services/mailer/mail.go index 351b79b5df..030491a467 100644 --- a/services/mailer/mail.go +++ b/services/mailer/mail.go @@ -415,6 +415,16 @@ func generateAdditionalHeaders(ctx *mailCommentContext, reason string, recipient "X-Gitea-Issue-ID": strconv.FormatInt(ctx.Issue.Index, 10), "X-Gitea-Issue-Link": ctx.Issue.HTMLURL(), + "X-Forgejo-Reason": reason, + "X-Forgejo-Sender": ctx.Doer.DisplayName(), + "X-Forgejo-Recipient": recipient.DisplayName(), + "X-Forgejo-Recipient-Address": recipient.Email, + "X-Forgejo-Repository": repo.Name, + "X-Forgejo-Repository-Path": repo.FullName(), + "X-Forgejo-Repository-Link": repo.HTMLURL(), + "X-Forgejo-Issue-ID": strconv.FormatInt(ctx.Issue.Index, 10), + "X-Forgejo-Issue-Link": ctx.Issue.HTMLURL(), + "X-GitHub-Reason": reason, "X-GitHub-Sender": ctx.Doer.DisplayName(), "X-GitHub-Recipient": recipient.DisplayName(), diff --git a/services/webhook/deliver.go b/services/webhook/deliver.go index effbe45e56..e8fd664ec8 100644 --- a/services/webhook/deliver.go +++ b/services/webhook/deliver.go @@ -122,6 +122,10 @@ func Deliver(ctx context.Context, t *webhook_model.HookTask) error { event := t.EventType.Event() eventType := string(t.EventType) + req.Header.Add("X-Forgejo-Delivery", t.UUID) + req.Header.Add("X-Forgejo-Event", event) + req.Header.Add("X-Forgejo-Event-Type", eventType) + req.Header.Add("X-Forgejo-Signature", signatureSHA256) req.Header.Add("X-Gitea-Delivery", t.UUID) req.Header.Add("X-Gitea-Event", event) req.Header.Add("X-Gitea-Event-Type", eventType)