summaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
authorEarl Warren <earl-warren@noreply.codeberg.org>2024-05-21 10:05:01 +0200
committerEarl Warren <earl-warren@noreply.codeberg.org>2024-05-21 10:05:01 +0200
commit74e07656d20dea1b5969f190596c751c716f8246 (patch)
tree663feb91f97ee872ffa7f6e92afb2e298d389049 /routers
parentMerge pull request '[FEAT] Wiki Search' (#3847) from snematoda/wiki-search-gr... (diff)
parentAlways load or generate oauth2 jwt secret (#30942) (diff)
downloadforgejo-74e07656d20dea1b5969f190596c751c716f8246.tar.xz
forgejo-74e07656d20dea1b5969f190596c751c716f8246.zip
Merge pull request '[gitea] week 2024-21 cherry pick (gitea/main -> forgejo)' (#3838) from algernon/wcp/2024-21 into forgejo
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/3838 Reviewed-by: 0ko <0ko@noreply.codeberg.org> Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Diffstat (limited to 'routers')
-rw-r--r--routers/api/v1/org/avatar.go2
-rw-r--r--routers/api/v1/repo/issue_label.go29
-rw-r--r--routers/api/v1/repo/release.go11
-rw-r--r--routers/api/v1/repo/release_tags.go6
-rw-r--r--routers/api/v1/repo/repo.go12
-rw-r--r--routers/api/v1/repo/tag.go8
-rw-r--r--routers/api/v1/user/avatar.go2
-rw-r--r--routers/install/install.go11
8 files changed, 63 insertions, 18 deletions
diff --git a/routers/api/v1/org/avatar.go b/routers/api/v1/org/avatar.go
index e34c68dfc9..f11eb6c1cd 100644
--- a/routers/api/v1/org/avatar.go
+++ b/routers/api/v1/org/avatar.go
@@ -46,6 +46,7 @@ func UpdateAvatar(ctx *context.APIContext) {
err = user_service.UploadAvatar(ctx, ctx.Org.Organization.AsUser(), content)
if err != nil {
ctx.Error(http.StatusInternalServerError, "UploadAvatar", err)
+ return
}
ctx.Status(http.StatusNoContent)
@@ -72,6 +73,7 @@ func DeleteAvatar(ctx *context.APIContext) {
err := user_service.DeleteAvatar(ctx, ctx.Org.Organization.AsUser())
if err != nil {
ctx.Error(http.StatusInternalServerError, "DeleteAvatar", err)
+ return
}
ctx.Status(http.StatusNoContent)
diff --git a/routers/api/v1/repo/issue_label.go b/routers/api/v1/repo/issue_label.go
index fd9625c0fb..ae05544365 100644
--- a/routers/api/v1/repo/issue_label.go
+++ b/routers/api/v1/repo/issue_label.go
@@ -5,7 +5,9 @@
package repo
import (
+ "fmt"
"net/http"
+ "reflect"
issues_model "code.gitea.io/gitea/models/issues"
api "code.gitea.io/gitea/modules/structs"
@@ -337,7 +339,32 @@ func prepareForReplaceOrAdd(ctx *context.APIContext, form api.IssueLabelsOption)
return nil, nil, err
}
- labels, err := issues_model.GetLabelsByIDs(ctx, form.Labels, "id", "repo_id", "org_id", "name", "exclusive")
+ var (
+ labelIDs []int64
+ labelNames []string
+ )
+ for _, label := range form.Labels {
+ rv := reflect.ValueOf(label)
+ switch rv.Kind() {
+ case reflect.Float64:
+ labelIDs = append(labelIDs, int64(rv.Float()))
+ case reflect.String:
+ labelNames = append(labelNames, rv.String())
+ }
+ }
+ if len(labelIDs) > 0 && len(labelNames) > 0 {
+ ctx.Error(http.StatusBadRequest, "InvalidLabels", "labels should be an array of strings or integers")
+ return nil, nil, fmt.Errorf("invalid labels")
+ }
+ if len(labelNames) > 0 {
+ labelIDs, err = issues_model.GetLabelIDsInRepoByNames(ctx, ctx.Repo.Repository.ID, labelNames)
+ if err != nil {
+ ctx.Error(http.StatusInternalServerError, "GetLabelIDsInRepoByNames", err)
+ return nil, nil, err
+ }
+ }
+
+ labels, err := issues_model.GetLabelsByIDs(ctx, labelIDs, "id", "repo_id", "org_id", "name", "exclusive")
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetLabelsByIDs", err)
return nil, nil, err
diff --git a/routers/api/v1/repo/release.go b/routers/api/v1/repo/release.go
index 057282b210..1544a64273 100644
--- a/routers/api/v1/repo/release.go
+++ b/routers/api/v1/repo/release.go
@@ -215,6 +215,9 @@ func CreateRelease(ctx *context.APIContext) {
// "$ref": "#/responses/notFound"
// "409":
// "$ref": "#/responses/error"
+ // "422":
+ // "$ref": "#/responses/validationError"
+
form := web.GetForm(ctx).(*api.CreateReleaseOption)
if ctx.Repo.Repository.IsEmpty {
ctx.Error(http.StatusUnprocessableEntity, "RepoIsEmpty", fmt.Errorf("repo is empty"))
@@ -247,6 +250,8 @@ func CreateRelease(ctx *context.APIContext) {
if err := release_service.CreateRelease(ctx.Repo.GitRepo, rel, nil, ""); err != nil {
if repo_model.IsErrReleaseAlreadyExist(err) {
ctx.Error(http.StatusConflict, "ReleaseAlreadyExist", err)
+ } else if models.IsErrProtectedTagName(err) {
+ ctx.Error(http.StatusUnprocessableEntity, "ProtectedTagName", err)
} else {
ctx.Error(http.StatusInternalServerError, "CreateRelease", err)
}
@@ -391,8 +396,8 @@ func DeleteRelease(ctx *context.APIContext) {
// "$ref": "#/responses/empty"
// "404":
// "$ref": "#/responses/notFound"
- // "405":
- // "$ref": "#/responses/empty"
+ // "422":
+ // "$ref": "#/responses/validationError"
id := ctx.ParamsInt64(":id")
rel, err := repo_model.GetReleaseForRepoByID(ctx, ctx.Repo.Repository.ID, id)
@@ -406,7 +411,7 @@ func DeleteRelease(ctx *context.APIContext) {
}
if err := release_service.DeleteReleaseByID(ctx, ctx.Repo.Repository, rel, ctx.Doer, false); err != nil {
if models.IsErrProtectedTagName(err) {
- ctx.Error(http.StatusMethodNotAllowed, "delTag", "user not allowed to delete protected tag")
+ ctx.Error(http.StatusUnprocessableEntity, "delTag", "user not allowed to delete protected tag")
return
}
ctx.Error(http.StatusInternalServerError, "DeleteReleaseByID", err)
diff --git a/routers/api/v1/repo/release_tags.go b/routers/api/v1/repo/release_tags.go
index fec91164a2..f845fad53b 100644
--- a/routers/api/v1/repo/release_tags.go
+++ b/routers/api/v1/repo/release_tags.go
@@ -92,8 +92,8 @@ func DeleteReleaseByTag(ctx *context.APIContext) {
// "$ref": "#/responses/empty"
// "404":
// "$ref": "#/responses/notFound"
- // "405":
- // "$ref": "#/responses/empty"
+ // "422":
+ // "$ref": "#/responses/validationError"
tag := ctx.Params(":tag")
@@ -114,7 +114,7 @@ func DeleteReleaseByTag(ctx *context.APIContext) {
if err = releaseservice.DeleteReleaseByID(ctx, ctx.Repo.Repository, release, ctx.Doer, false); err != nil {
if models.IsErrProtectedTagName(err) {
- ctx.Error(http.StatusMethodNotAllowed, "delTag", "user not allowed to delete protected tag")
+ ctx.Error(http.StatusUnprocessableEntity, "delTag", "user not allowed to delete protected tag")
return
}
ctx.Error(http.StatusInternalServerError, "DeleteReleaseByID", err)
diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go
index 4be8c8ee72..08bc86eed8 100644
--- a/routers/api/v1/repo/repo.go
+++ b/routers/api/v1/repo/repo.go
@@ -1072,16 +1072,10 @@ func updateRepoArchivedState(ctx *context.APIContext, opts api.EditRepoOption) e
func updateMirror(ctx *context.APIContext, opts api.EditRepoOption) error {
repo := ctx.Repo.Repository
- // only update mirror if interval or enable prune are provided
- if opts.MirrorInterval == nil && opts.EnablePrune == nil {
- return nil
- }
-
- // these values only make sense if the repo is a mirror
+ // Skip this update if the repo is not a mirror, do not return error.
+ // Because reporting errors only makes the logic more complex&fragile, it doesn't really help end users.
if !repo.IsMirror {
- err := fmt.Errorf("repo is not a mirror, can not change mirror interval")
- ctx.Error(http.StatusUnprocessableEntity, err.Error(), err)
- return err
+ return nil
}
// get the mirror from the repo
diff --git a/routers/api/v1/repo/tag.go b/routers/api/v1/repo/tag.go
index b498f0e792..69dd844298 100644
--- a/routers/api/v1/repo/tag.go
+++ b/routers/api/v1/repo/tag.go
@@ -204,6 +204,8 @@ func CreateTag(ctx *context.APIContext) {
// "$ref": "#/responses/empty"
// "409":
// "$ref": "#/responses/conflict"
+ // "422":
+ // "$ref": "#/responses/validationError"
// "423":
// "$ref": "#/responses/repoArchivedError"
form := web.GetForm(ctx).(*api.CreateTagOption)
@@ -225,7 +227,7 @@ func CreateTag(ctx *context.APIContext) {
return
}
if models.IsErrProtectedTagName(err) {
- ctx.Error(http.StatusMethodNotAllowed, "CreateNewTag", "user not allowed to create protected tag")
+ ctx.Error(http.StatusUnprocessableEntity, "CreateNewTag", "user not allowed to create protected tag")
return
}
@@ -280,6 +282,8 @@ func DeleteTag(ctx *context.APIContext) {
// "$ref": "#/responses/empty"
// "409":
// "$ref": "#/responses/conflict"
+ // "422":
+ // "$ref": "#/responses/validationError"
// "423":
// "$ref": "#/responses/repoArchivedError"
tagName := ctx.Params("*")
@@ -301,7 +305,7 @@ func DeleteTag(ctx *context.APIContext) {
if err = releaseservice.DeleteReleaseByID(ctx, ctx.Repo.Repository, tag, ctx.Doer, true); err != nil {
if models.IsErrProtectedTagName(err) {
- ctx.Error(http.StatusMethodNotAllowed, "delTag", "user not allowed to delete protected tag")
+ ctx.Error(http.StatusUnprocessableEntity, "delTag", "user not allowed to delete protected tag")
return
}
ctx.Error(http.StatusInternalServerError, "DeleteReleaseByID", err)
diff --git a/routers/api/v1/user/avatar.go b/routers/api/v1/user/avatar.go
index f912296228..30ccb63587 100644
--- a/routers/api/v1/user/avatar.go
+++ b/routers/api/v1/user/avatar.go
@@ -39,6 +39,7 @@ func UpdateAvatar(ctx *context.APIContext) {
err = user_service.UploadAvatar(ctx, ctx.Doer, content)
if err != nil {
ctx.Error(http.StatusInternalServerError, "UploadAvatar", err)
+ return
}
ctx.Status(http.StatusNoContent)
@@ -57,6 +58,7 @@ func DeleteAvatar(ctx *context.APIContext) {
err := user_service.DeleteAvatar(ctx, ctx.Doer)
if err != nil {
ctx.Error(http.StatusInternalServerError, "DeleteAvatar", err)
+ return
}
ctx.Status(http.StatusNoContent)
diff --git a/routers/install/install.go b/routers/install/install.go
index 282ebe9ead..b84d77cfc2 100644
--- a/routers/install/install.go
+++ b/routers/install/install.go
@@ -486,6 +486,17 @@ func SubmitInstall(ctx *context.Context) {
cfg.Section("security").Key("INTERNAL_TOKEN").SetValue(internalToken)
}
+ // FIXME: at the moment, no matter oauth2 is enabled or not, it must generate a "oauth2 JWT_SECRET"
+ // see the "loadOAuth2From" in "setting/oauth2.go"
+ if !cfg.Section("oauth2").HasKey("JWT_SECRET") && !cfg.Section("oauth2").HasKey("JWT_SECRET_URI") {
+ _, jwtSecretBase64, err := generate.NewJwtSecret()
+ if err != nil {
+ ctx.RenderWithErr(ctx.Tr("install.secret_key_failed", err), tplInstall, &form)
+ return
+ }
+ cfg.Section("oauth2").Key("JWT_SECRET").SetValue(jwtSecretBase64)
+ }
+
// if there is already a SECRET_KEY, we should not overwrite it, otherwise the encrypted data will not be able to be decrypted
if setting.SecretKey == "" {
var secretKey string