From 17174d7eee1ee706e6ae23c55b3c38f199dcc3e5 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 16 Dec 2020 22:09:45 +0100 Subject: [PATCH 01/16] RM unused struct --- modules/structs/status.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/modules/structs/status.go b/modules/structs/status.go index e833bd69e5d6b..e956cfd6c755e 100644 --- a/modules/structs/status.go +++ b/modules/structs/status.go @@ -58,8 +58,3 @@ type CreateStatusOption struct { Description string `json:"description"` Context string `json:"context"` } - -// ListStatusesOption holds pagination information -type ListStatusesOption struct { - Page int -} From 514b792a3932157e713aca32bc854d3f7df0d5ae Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 16 Dec 2020 22:50:43 +0100 Subject: [PATCH 02/16] rename (*CommitStatus) loadRepo() -> loadAttributes() --- models/commit_status.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/commit_status.go b/models/commit_status.go index 15fcbff6f9196..44f7be490fcd7 100644 --- a/models/commit_status.go +++ b/models/commit_status.go @@ -38,7 +38,7 @@ type CommitStatus struct { UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"` } -func (status *CommitStatus) loadRepo(e Engine) (err error) { +func (status *CommitStatus) loadAttributes(e Engine) (err error) { if status.Repo == nil { status.Repo, err = getRepositoryByID(e, status.RepoID) if err != nil { @@ -56,7 +56,7 @@ func (status *CommitStatus) loadRepo(e Engine) (err error) { // APIURL returns the absolute APIURL to this commit-status. func (status *CommitStatus) APIURL() string { - _ = status.loadRepo(x) + _ = status.loadAttributes(x) return fmt.Sprintf("%sapi/v1/repos/%s/statuses/%s", setting.AppURL, status.Repo.FullName(), status.SHA) } From 40f03e7c5db647fbf7dfb679959c531c99d8b4c8 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 16 Dec 2020 22:52:53 +0100 Subject: [PATCH 03/16] move ToCommitStatus into its own file --- modules/convert/convert.go | 21 --------------------- modules/convert/status.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 21 deletions(-) create mode 100644 modules/convert/status.go diff --git a/modules/convert/convert.go b/modules/convert/convert.go index f9f4a641e2035..63dd072fc4d37 100644 --- a/modules/convert/convert.go +++ b/modules/convert/convert.go @@ -347,27 +347,6 @@ func ToOAuth2Application(app *models.OAuth2Application) *api.OAuth2Application { } } -// ToCommitStatus converts models.CommitStatus to api.Status -func ToCommitStatus(status *models.CommitStatus) *api.Status { - apiStatus := &api.Status{ - Created: status.CreatedUnix.AsTime(), - Updated: status.CreatedUnix.AsTime(), - State: api.StatusState(status.State), - TargetURL: status.TargetURL, - Description: status.Description, - ID: status.Index, - URL: status.APIURL(), - Context: status.Context, - } - - if status.CreatorID != 0 { - creator, _ := models.GetUserByID(status.CreatorID) - apiStatus.Creator = ToUser(creator, false, false) - } - - return apiStatus -} - // ToLFSLock convert a LFSLock to api.LFSLock func ToLFSLock(l *models.LFSLock) *api.LFSLock { return &api.LFSLock{ diff --git a/modules/convert/status.go b/modules/convert/status.go new file mode 100644 index 0000000000000..be8e22447419d --- /dev/null +++ b/modules/convert/status.go @@ -0,0 +1,31 @@ +// Copyright 2020 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 convert + +import ( + "code.gitea.io/gitea/models" + api "code.gitea.io/gitea/modules/structs" +) + +// ToCommitStatus converts models.CommitStatus to api.Status +func ToCommitStatus(status *models.CommitStatus) *api.Status { + apiStatus := &api.Status{ + Created: status.CreatedUnix.AsTime(), + Updated: status.CreatedUnix.AsTime(), + State: status.State, + TargetURL: status.TargetURL, + Description: status.Description, + ID: status.Index, + URL: status.APIURL(), + Context: status.Context, + } + + if status.CreatorID != 0 { + creator, _ := models.GetUserByID(status.CreatorID) + apiStatus.Creator = ToUser(creator, false, false) + } + + return apiStatus +} From 12c80a837a64b84f20a692852867bd74d30da904 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 16 Dec 2020 22:53:59 +0100 Subject: [PATCH 04/16] use CommitStatusState instead of StatusState --- modules/structs/status.go | 53 +++++++++++++-------------------------- 1 file changed, 18 insertions(+), 35 deletions(-) diff --git a/modules/structs/status.go b/modules/structs/status.go index e956cfd6c755e..6d6bdb4df3be7 100644 --- a/modules/structs/status.go +++ b/modules/structs/status.go @@ -8,32 +8,15 @@ import ( "time" ) -// StatusState holds the state of a Status -// It can be "pending", "success", "error", "failure", and "warning" -type StatusState string - -const ( - // StatusPending is for when the Status is Pending - StatusPending StatusState = "pending" - // StatusSuccess is for when the Status is Success - StatusSuccess StatusState = "success" - // StatusError is for when the Status is Error - StatusError StatusState = "error" - // StatusFailure is for when the Status is Failure - StatusFailure StatusState = "failure" - // StatusWarning is for when the Status is Warning - StatusWarning StatusState = "warning" -) - // Status holds a single Status of a single Commit type Status struct { - ID int64 `json:"id"` - State StatusState `json:"status"` - TargetURL string `json:"target_url"` - Description string `json:"description"` - URL string `json:"url"` - Context string `json:"context"` - Creator *User `json:"creator"` + ID int64 `json:"id"` + State CommitStatusState `json:"status"` + TargetURL string `json:"target_url"` + Description string `json:"description"` + URL string `json:"url"` + Context string `json:"context"` + Creator *User `json:"creator"` // swagger:strfmt date-time Created time.Time `json:"created_at"` // swagger:strfmt date-time @@ -42,19 +25,19 @@ type Status struct { // CombinedStatus holds the combined state of several statuses for a single commit type CombinedStatus struct { - State StatusState `json:"state"` - SHA string `json:"sha"` - TotalCount int `json:"total_count"` - Statuses []*Status `json:"statuses"` - Repository *Repository `json:"repository"` - CommitURL string `json:"commit_url"` - URL string `json:"url"` + State CommitStatusState `json:"state"` + SHA string `json:"sha"` + TotalCount int `json:"total_count"` + Statuses []*Status `json:"statuses"` + Repository *Repository `json:"repository"` + CommitURL string `json:"commit_url"` + URL string `json:"url"` } // CreateStatusOption holds the information needed to create a new Status for a Commit type CreateStatusOption struct { - State StatusState `json:"state"` - TargetURL string `json:"target_url"` - Description string `json:"description"` - Context string `json:"context"` + State CommitStatusState `json:"state"` + TargetURL string `json:"target_url"` + Description string `json:"description"` + Context string `json:"context"` } From 52d515774f65103000e2448ca590d81dfcf35032 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 16 Dec 2020 22:54:24 +0100 Subject: [PATCH 05/16] move CombinedStatus convertion into convert package --- modules/convert/status.go | 25 +++++++++++++++++++++++++ routers/api/v1/repo/status.go | 27 ++------------------------- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/modules/convert/status.go b/modules/convert/status.go index be8e22447419d..56fc8d74a8573 100644 --- a/modules/convert/status.go +++ b/modules/convert/status.go @@ -29,3 +29,28 @@ func ToCommitStatus(status *models.CommitStatus) *api.Status { return apiStatus } + +// ToCombinedStatus converts List of CommitStatus to a CombinedStatus +func ToCombinedStatus(statuses []*models.CommitStatus, repo *api.Repository) *api.CombinedStatus { + + if len(statuses) == 0 { + return nil + } + + retStatus := &api.CombinedStatus{ + SHA: statuses[0].SHA, + TotalCount: len(statuses), + Repository: repo, + URL: "", + } + + retStatus.Statuses = make([]*api.Status, 0, len(statuses)) + for _, status := range statuses { + retStatus.Statuses = append(retStatus.Statuses, ToCommitStatus(status)) + if status.State.NoBetterThan(retStatus.State) { + retStatus.State = status.State + } + } + + return retStatus +} diff --git a/routers/api/v1/repo/status.go b/routers/api/v1/repo/status.go index 3b6d9b04c1ad1..cf6f1e42dcc06 100644 --- a/routers/api/v1/repo/status.go +++ b/routers/api/v1/repo/status.go @@ -233,16 +233,6 @@ func getCommitStatuses(ctx *context.APIContext, sha string) { ctx.JSON(http.StatusOK, apiStatuses) } -type combinedCommitStatus struct { - State api.CommitStatusState `json:"state"` - SHA string `json:"sha"` - TotalCount int `json:"total_count"` - Statuses []*api.Status `json:"statuses"` - Repo *api.Repository `json:"repository"` - CommitURL string `json:"commit_url"` - URL string `json:"url"` -} - // GetCombinedCommitStatusByRef returns the combined status for any given commit hash func GetCombinedCommitStatusByRef(ctx *context.APIContext) { // swagger:operation GET /repos/{owner}/{repo}/commits/{ref}/statuses repository repoGetCombinedStatusByRef @@ -297,20 +287,7 @@ func GetCombinedCommitStatusByRef(ctx *context.APIContext) { return } - retStatus := &combinedCommitStatus{ - SHA: sha, - TotalCount: len(statuses), - Repo: convert.ToRepo(repo, ctx.Repo.AccessMode), - URL: "", - } - - retStatus.Statuses = make([]*api.Status, 0, len(statuses)) - for _, status := range statuses { - retStatus.Statuses = append(retStatus.Statuses, convert.ToCommitStatus(status)) - if status.State.NoBetterThan(retStatus.State) { - retStatus.State = status.State - } - } + combiStatus := convert.ToCombinedStatus(statuses, convert.ToRepo(repo, ctx.Repo.AccessMode)) - ctx.JSON(http.StatusOK, retStatus) + ctx.JSON(http.StatusOK, combiStatus) } From d0bbbb44cfe82076cf127592f40e315155ee94b6 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 16 Dec 2020 23:11:01 +0100 Subject: [PATCH 06/16] let models.GetLatestCommitStatus use repoID direct and accept ListOptions --- models/commit_status.go | 19 +++++++++++++------ modules/gitgraph/graph_models.go | 2 +- routers/api/v1/repo/status.go | 2 +- routers/repo/blame.go | 2 +- routers/repo/commit.go | 2 +- routers/repo/pull.go | 4 ++-- routers/repo/view.go | 2 +- services/pull/commit_status.go | 2 +- services/pull/pull.go | 2 +- 9 files changed, 22 insertions(+), 15 deletions(-) diff --git a/models/commit_status.go b/models/commit_status.go index 44f7be490fcd7..9dffece3785f2 100644 --- a/models/commit_status.go +++ b/models/commit_status.go @@ -139,13 +139,20 @@ func sortCommitStatusesSession(sess *xorm.Session, sortType string) { } // GetLatestCommitStatus returns all statuses with a unique context for a given commit. -func GetLatestCommitStatus(repo *Repository, sha string, page int) ([]*CommitStatus, error) { +func GetLatestCommitStatus(repoID int64, sha string, listOptions ListOptions) ([]*CommitStatus, error) { + return getLatestCommitStatus(x, repoID, sha, listOptions) +} + +func getLatestCommitStatus(e Engine, repoID int64, sha string, listOptions ListOptions) ([]*CommitStatus, error) { ids := make([]int64, 0, 10) - err := x.Limit(10, page*10). - Table(&CommitStatus{}). - Where("repo_id = ?", repo.ID).And("sha = ?", sha). + sess := e.Table(&CommitStatus{}). + Where("repo_id = ?", repoID).And("sha = ?", sha). Select("max( id ) as id"). - GroupBy("context_hash").OrderBy("max( id ) desc").Find(&ids) + GroupBy("context_hash").OrderBy("max( id ) desc") + + sess = listOptions.setSessionPagination(sess) + + err := sess.Find(&ids) if err != nil { return nil, err } @@ -261,7 +268,7 @@ func ParseCommitsWithStatus(oldCommits *list.List, repo *Repository) *list.List commit := SignCommitWithStatuses{ SignCommit: &c, } - statuses, err := GetLatestCommitStatus(repo, commit.ID.String(), 0) + statuses, err := GetLatestCommitStatus(repo.ID, commit.ID.String(), ListOptions{}) if err != nil { log.Error("GetLatestCommitStatus: %v", err) } else { diff --git a/modules/gitgraph/graph_models.go b/modules/gitgraph/graph_models.go index ba168ab19de32..2ae38188a6a17 100644 --- a/modules/gitgraph/graph_models.go +++ b/modules/gitgraph/graph_models.go @@ -113,7 +113,7 @@ func (graph *Graph) LoadAndProcessCommits(repository *models.Repository, gitRepo _ = models.CalculateTrustStatus(c.Verification, repository, &keyMap) - statuses, err := models.GetLatestCommitStatus(repository, c.Commit.ID.String(), 0) + statuses, err := models.GetLatestCommitStatus(repository.ID, c.Commit.ID.String(), models.ListOptions{}) if err != nil { log.Error("GetLatestCommitStatus: %v", err) } else { diff --git a/routers/api/v1/repo/status.go b/routers/api/v1/repo/status.go index cf6f1e42dcc06..2376fabf11d8c 100644 --- a/routers/api/v1/repo/status.go +++ b/routers/api/v1/repo/status.go @@ -276,7 +276,7 @@ func GetCombinedCommitStatusByRef(ctx *context.APIContext) { page := ctx.QueryInt("page") - statuses, err := models.GetLatestCommitStatus(repo, sha, page) + statuses, err := models.GetLatestCommitStatus(repo.ID, sha, utils.GetListOptions(ctx)) if err != nil { ctx.Error(http.StatusInternalServerError, "GetLatestCommitStatus", fmt.Errorf("GetLatestCommitStatus[%s, %s, %d]: %v", repo.FullName(), sha, page, err)) return diff --git a/routers/repo/blame.go b/routers/repo/blame.go index bd11310988eac..514adc3bc1ff3 100644 --- a/routers/repo/blame.go +++ b/routers/repo/blame.go @@ -87,7 +87,7 @@ func RefBlame(ctx *context.Context) { ctx.Data["LatestCommitVerification"] = models.ParseCommitWithSignature(latestCommit) ctx.Data["LatestCommitUser"] = models.ValidateCommitWithEmail(latestCommit) - statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository, ctx.Repo.Commit.ID.String(), 0) + statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository.ID, ctx.Repo.Commit.ID.String(), models.ListOptions{}) if err != nil { log.Error("GetLatestCommitStatus: %v", err) } diff --git a/routers/repo/commit.go b/routers/repo/commit.go index 5bb26ffe41a94..43a95c2adb95d 100644 --- a/routers/repo/commit.go +++ b/routers/repo/commit.go @@ -296,7 +296,7 @@ func Diff(ctx *context.Context) { commitID = commit.ID.String() } - statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository, commitID, 0) + statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository.ID, commitID, models.ListOptions{}) if err != nil { log.Error("GetLatestCommitStatus: %v", err) } diff --git a/routers/repo/pull.go b/routers/repo/pull.go index 0ea6ec33deb7c..bee3bbc1df91e 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -368,7 +368,7 @@ func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.Compare ctx.ServerError(fmt.Sprintf("GetRefCommitID(%s)", pull.GetGitRefName()), err) return nil } - commitStatuses, err := models.GetLatestCommitStatus(repo, sha, 0) + commitStatuses, err := models.GetLatestCommitStatus(repo.ID, sha, models.ListOptions{}) if err != nil { ctx.ServerError("GetLatestCommitStatus", err) return nil @@ -449,7 +449,7 @@ func PrepareViewPullInfo(ctx *context.Context, issue *models.Issue) *git.Compare return nil } - commitStatuses, err := models.GetLatestCommitStatus(repo, sha, 0) + commitStatuses, err := models.GetLatestCommitStatus(repo.ID, sha, models.ListOptions{}) if err != nil { ctx.ServerError("GetLatestCommitStatus", err) return nil diff --git a/routers/repo/view.go b/routers/repo/view.go index 2df5b30ce8f95..79778cb4a06c0 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -353,7 +353,7 @@ func renderDirectory(ctx *context.Context, treeLink string) { ctx.Data["LatestCommitUser"] = models.ValidateCommitWithEmail(latestCommit) - statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository, ctx.Repo.Commit.ID.String(), 0) + statuses, err := models.GetLatestCommitStatus(ctx.Repo.Repository.ID, ctx.Repo.Commit.ID.String(), models.ListOptions{}) if err != nil { log.Error("GetLatestCommitStatus: %v", err) } diff --git a/services/pull/commit_status.go b/services/pull/commit_status.go index 592aad6fc5a7b..b8fb109440be4 100644 --- a/services/pull/commit_status.go +++ b/services/pull/commit_status.go @@ -121,7 +121,7 @@ func GetPullRequestCommitStatusState(pr *models.PullRequest) (structs.CommitStat return "", errors.Wrap(err, "LoadBaseRepo") } - commitStatuses, err := models.GetLatestCommitStatus(pr.BaseRepo, sha, 0) + commitStatuses, err := models.GetLatestCommitStatus(pr.BaseRepo.ID, sha, models.ListOptions{}) if err != nil { return "", errors.Wrap(err, "GetLatestCommitStatus") } diff --git a/services/pull/pull.go b/services/pull/pull.go index 9cf4abfd54218..f3fb762303c8c 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -665,7 +665,7 @@ func GetLastCommitStatus(pr *models.PullRequest) (status *models.CommitStatus, e return nil, err } - statusList, err := models.GetLatestCommitStatus(pr.BaseRepo, lastCommitID, 0) + statusList, err := models.GetLatestCommitStatus(pr.BaseRepo.ID, lastCommitID, models.ListOptions{}) if err != nil { return nil, err } From 6e684e9f411badb6fc4b1d13cae647ceb755d65d Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 16 Dec 2020 23:14:02 +0100 Subject: [PATCH 07/16] update swagger docs --- routers/api/v1/repo/status.go | 7 +++++-- templates/swagger/v1_json.tmpl | 22 ++++++++++++++-------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/routers/api/v1/repo/status.go b/routers/api/v1/repo/status.go index 2376fabf11d8c..c7d057a81e393 100644 --- a/routers/api/v1/repo/status.go +++ b/routers/api/v1/repo/status.go @@ -258,9 +258,12 @@ func GetCombinedCommitStatusByRef(ctx *context.APIContext) { // required: true // - name: page // in: query - // description: page number of results + // description: page number of results to return (1-based) + // type: integer + // - name: limit + // in: query + // description: page size of results // type: integer - // required: false // responses: // "200": // "$ref": "#/responses/Status" diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 6eb53a3d70e18..b5c4657155dbd 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -2901,9 +2901,15 @@ }, { "type": "integer", - "description": "page number of results", + "description": "page number of results to return (1-based)", "name": "page", "in": "query" + }, + { + "type": "integer", + "description": "page size of results", + "name": "limit", + "in": "query" } ], "responses": { @@ -11558,6 +11564,11 @@ }, "x-go-package": "code.gitea.io/gitea/modules/structs" }, + "CommitStatusState": { + "description": "CommitStatusState holds the state of a Status\nIt can be \"pending\", \"success\", \"error\", \"failure\", and \"warning\"", + "type": "string", + "x-go-package": "code.gitea.io/gitea/modules/structs" + }, "CommitUser": { "type": "object", "title": "CommitUser contains information of a user in the context of a commit.", @@ -12353,7 +12364,7 @@ "x-go-name": "Description" }, "state": { - "$ref": "#/definitions/StatusState" + "$ref": "#/definitions/CommitStatusState" }, "target_url": { "type": "string", @@ -15277,7 +15288,7 @@ "x-go-name": "ID" }, "status": { - "$ref": "#/definitions/StatusState" + "$ref": "#/definitions/CommitStatusState" }, "target_url": { "type": "string", @@ -15295,11 +15306,6 @@ }, "x-go-package": "code.gitea.io/gitea/modules/structs" }, - "StatusState": { - "description": "StatusState holds the state of a Status\nIt can be \"pending\", \"success\", \"error\", \"failure\", and \"warning\"", - "type": "string", - "x-go-package": "code.gitea.io/gitea/modules/structs" - }, "StopWatch": { "description": "StopWatch represent a running stopwatch", "type": "object", From cc0f948793c0b691585627c081a659f37d108d6f Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 16 Dec 2020 23:23:06 +0100 Subject: [PATCH 08/16] fix tests --- integrations/pull_status_test.go | 2 +- integrations/repo_commits_test.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/integrations/pull_status_test.go b/integrations/pull_status_test.go index 0002784792b60..fc4c7ca33da1a 100644 --- a/integrations/pull_status_test.go +++ b/integrations/pull_status_test.go @@ -70,7 +70,7 @@ func TestPullCreate_CommitStatus(t *testing.T) { token := getTokenForLoggedInUser(t, session) req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/user1/repo1/statuses/%s?token=%s", commitID, token), api.CreateStatusOption{ - State: api.StatusState(status), + State: status, TargetURL: "http://test.ci/", Description: "", Context: "testci", diff --git a/integrations/repo_commits_test.go b/integrations/repo_commits_test.go index 2aa0260a16903..fe7ad3ae4d4fb 100644 --- a/integrations/repo_commits_test.go +++ b/integrations/repo_commits_test.go @@ -51,7 +51,7 @@ func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) { // Call API to add status for commit req = NewRequestWithJSON(t, "POST", "/api/v1/repos/user2/repo1/statuses/"+path.Base(commitURL)+"?token="+token, api.CreateStatusOption{ - State: api.StatusState(state), + State: api.CommitStatusState(state), TargetURL: "http://test.ci/", Description: "", Context: "testci", @@ -87,7 +87,7 @@ func testRepoCommitsWithStatus(t *testing.T, resp *httptest.ResponseRecorder, st assert.NoError(t, decoder.Decode(&statuses)) assert.Len(t, statuses, 1) for _, s := range statuses { - assert.Equal(t, api.StatusState(state), s.State) + assert.Equal(t, api.CommitStatusState(state), s.State) assert.Equal(t, setting.AppURL+"api/v1/repos/user2/repo1/statuses/65f1bf27bc3bf70f64657658635e66094edbcb4d", s.URL) assert.Equal(t, "http://test.ci/", s.TargetURL) assert.Equal(t, "", s.Description) From 020f7fd0af8f68125fa75a198be2466e81583749 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 16 Dec 2020 23:34:08 +0100 Subject: [PATCH 09/16] Fix swagger docs --- routers/api/v1/repo/status.go | 2 +- routers/api/v1/swagger/repo.go | 7 ++++++ templates/swagger/v1_json.tmpl | 45 +++++++++++++++++++++++++++++++++- 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/routers/api/v1/repo/status.go b/routers/api/v1/repo/status.go index c7d057a81e393..fc8bdfbef0a49 100644 --- a/routers/api/v1/repo/status.go +++ b/routers/api/v1/repo/status.go @@ -266,7 +266,7 @@ func GetCombinedCommitStatusByRef(ctx *context.APIContext) { // type: integer // responses: // "200": - // "$ref": "#/responses/Status" + // "$ref": "#/responses/CombinedStatus" // "400": // "$ref": "#/responses/error" diff --git a/routers/api/v1/swagger/repo.go b/routers/api/v1/swagger/repo.go index bce9e45c379cf..76c9ac608b387 100644 --- a/routers/api/v1/swagger/repo.go +++ b/routers/api/v1/swagger/repo.go @@ -309,3 +309,10 @@ type swaggerLanguageStatistics struct { // in: body Body map[string]int64 `json:"body"` } + +// CombinedStatus +// swagger:response CombinedStatus +type swaggerCombinedStatus struct { + // in: body + Body api.CombinedStatus `json:"body"` +} diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index b5c4657155dbd..b7360f2472602 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -2914,7 +2914,7 @@ ], "responses": { "200": { - "$ref": "#/responses/Status" + "$ref": "#/responses/CombinedStatus" }, "400": { "$ref": "#/responses/error" @@ -11437,6 +11437,43 @@ }, "x-go-package": "code.gitea.io/gitea/modules/structs" }, + "CombinedStatus": { + "description": "CombinedStatus holds the combined state of several statuses for a single commit", + "type": "object", + "properties": { + "commit_url": { + "type": "string", + "x-go-name": "CommitURL" + }, + "repository": { + "$ref": "#/definitions/Repository" + }, + "sha": { + "type": "string", + "x-go-name": "SHA" + }, + "state": { + "$ref": "#/definitions/CommitStatusState" + }, + "statuses": { + "type": "array", + "items": { + "$ref": "#/definitions/Status" + }, + "x-go-name": "Statuses" + }, + "total_count": { + "type": "integer", + "format": "int64", + "x-go-name": "TotalCount" + }, + "url": { + "type": "string", + "x-go-name": "URL" + } + }, + "x-go-package": "code.gitea.io/gitea/modules/structs" + }, "Comment": { "description": "Comment represents a comment on a commit or issue", "type": "object", @@ -15779,6 +15816,12 @@ } } }, + "CombinedStatus": { + "description": "CombinedStatus", + "schema": { + "$ref": "#/definitions/CombinedStatus" + } + }, "Comment": { "description": "Comment", "schema": { From 0efdbb6d1d95a566650dfa0227aa189ae2c4b0fd Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 17 Dec 2020 00:47:29 +0000 Subject: [PATCH 10/16] rm page --- routers/api/v1/repo/status.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/routers/api/v1/repo/status.go b/routers/api/v1/repo/status.go index fc8bdfbef0a49..df8b9b14183fc 100644 --- a/routers/api/v1/repo/status.go +++ b/routers/api/v1/repo/status.go @@ -277,11 +277,9 @@ func GetCombinedCommitStatusByRef(ctx *context.APIContext) { } repo := ctx.Repo.Repository - page := ctx.QueryInt("page") - statuses, err := models.GetLatestCommitStatus(repo.ID, sha, utils.GetListOptions(ctx)) if err != nil { - ctx.Error(http.StatusInternalServerError, "GetLatestCommitStatus", fmt.Errorf("GetLatestCommitStatus[%s, %s, %d]: %v", repo.FullName(), sha, page, err)) + ctx.Error(http.StatusInternalServerError, "GetLatestCommitStatus", fmt.Errorf("GetLatestCommitStatus[%s, %s]: %v", repo.FullName(), sha, err)) return } From dd49df7b680bed2955dc69cbeb6f0988d7df6615 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 17 Dec 2020 01:54:13 +0100 Subject: [PATCH 11/16] fix swagger docs!!! --- routers/api/v1/repo/status.go | 2 +- templates/swagger/v1_json.tmpl | 83 +++++++++++++++++++++++++++++++++- 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/routers/api/v1/repo/status.go b/routers/api/v1/repo/status.go index df8b9b14183fc..09bdba8fc89b9 100644 --- a/routers/api/v1/repo/status.go +++ b/routers/api/v1/repo/status.go @@ -235,7 +235,7 @@ func getCommitStatuses(ctx *context.APIContext, sha string) { // GetCombinedCommitStatusByRef returns the combined status for any given commit hash func GetCombinedCommitStatusByRef(ctx *context.APIContext) { - // swagger:operation GET /repos/{owner}/{repo}/commits/{ref}/statuses repository repoGetCombinedStatusByRef + // swagger:operation GET /repos/{owner}/{repo}/commits/{ref}/status repository repoGetCombinedStatusByRef // --- // summary: Get a commit's combined status, by branch/tag/commit reference // produces: diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index b7360f2472602..7253e5ff4fb61 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -2867,7 +2867,7 @@ } } }, - "/repos/{owner}/{repo}/commits/{ref}/statuses": { + "/repos/{owner}/{repo}/commits/{ref}/status": { "get": { "produces": [ "application/json" @@ -2922,6 +2922,87 @@ } } }, + "/repos/{owner}/{repo}/commits/{ref}/statuses": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "repository" + ], + "summary": "Get a commit's statuses, by branch/tag/commit reference", + "operationId": "repoListStatusesByRef", + "parameters": [ + { + "type": "string", + "description": "owner of the repo", + "name": "owner", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "name of the repo", + "name": "repo", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "name of branch/tag/commit", + "name": "ref", + "in": "path", + "required": true + }, + { + "enum": [ + "oldest", + "recentupdate", + "leastupdate", + "leastindex", + "highestindex" + ], + "type": "string", + "description": "type of sort", + "name": "sort", + "in": "query" + }, + { + "enum": [ + "pending", + "success", + "error", + "failure", + "warning" + ], + "type": "string", + "description": "type of state", + "name": "state", + "in": "query" + }, + { + "type": "integer", + "description": "page number of results to return (1-based)", + "name": "page", + "in": "query" + }, + { + "type": "integer", + "description": "page size of results", + "name": "limit", + "in": "query" + } + ], + "responses": { + "200": { + "$ref": "#/responses/StatusList" + }, + "400": { + "$ref": "#/responses/error" + } + } + } + }, "/repos/{owner}/{repo}/contents": { "get": { "produces": [ From 9fdd73d068f6edacc4197162e2591e8d6273238a Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 17 Dec 2020 02:34:04 +0100 Subject: [PATCH 12/16] return json null --- routers/api/v1/repo/status.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/api/v1/repo/status.go b/routers/api/v1/repo/status.go index 09bdba8fc89b9..b4bf1937b6ae9 100644 --- a/routers/api/v1/repo/status.go +++ b/routers/api/v1/repo/status.go @@ -284,7 +284,7 @@ func GetCombinedCommitStatusByRef(ctx *context.APIContext) { } if len(statuses) == 0 { - ctx.Status(http.StatusOK) + ctx.JSON(http.StatusOK, nil) return } From d324da9670f89bf6e69ec2fbdbbf005a8d3e61ec Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 17 Dec 2020 02:39:44 +0100 Subject: [PATCH 13/16] always return json --- routers/api/v1/repo/status.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/api/v1/repo/status.go b/routers/api/v1/repo/status.go index b4bf1937b6ae9..082c387da5211 100644 --- a/routers/api/v1/repo/status.go +++ b/routers/api/v1/repo/status.go @@ -284,7 +284,7 @@ func GetCombinedCommitStatusByRef(ctx *context.APIContext) { } if len(statuses) == 0 { - ctx.JSON(http.StatusOK, nil) + ctx.JSON(http.StatusOK, &api.CombinedStatus{}) return } From aa4c882406feb7d4420f52dc907cc4b9f78be1a6 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 17 Dec 2020 17:30:33 +0100 Subject: [PATCH 14/16] rename api.Status to api.CommitStatus --- integrations/repo_commits_test.go | 2 +- modules/convert/status.go | 8 +- modules/structs/commit_status.go | 12 +-- modules/structs/status.go | 8 +- routers/api/v1/repo/status.go | 4 +- routers/api/v1/swagger/repo.go | 14 ++-- templates/swagger/v1_json.tmpl | 126 +++++++++++++++--------------- 7 files changed, 87 insertions(+), 87 deletions(-) diff --git a/integrations/repo_commits_test.go b/integrations/repo_commits_test.go index fe7ad3ae4d4fb..f341f8026af5a 100644 --- a/integrations/repo_commits_test.go +++ b/integrations/repo_commits_test.go @@ -83,7 +83,7 @@ func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) { func testRepoCommitsWithStatus(t *testing.T, resp *httptest.ResponseRecorder, state string) { decoder := json.NewDecoder(resp.Body) - statuses := []*api.Status{} + statuses := []*api.CommitStatus{} assert.NoError(t, decoder.Decode(&statuses)) assert.Len(t, statuses, 1) for _, s := range statuses { diff --git a/modules/convert/status.go b/modules/convert/status.go index 56fc8d74a8573..f972fc333c133 100644 --- a/modules/convert/status.go +++ b/modules/convert/status.go @@ -9,9 +9,9 @@ import ( api "code.gitea.io/gitea/modules/structs" ) -// ToCommitStatus converts models.CommitStatus to api.Status -func ToCommitStatus(status *models.CommitStatus) *api.Status { - apiStatus := &api.Status{ +// ToCommitStatus converts models.CommitStatus to api.CommitStatus +func ToCommitStatus(status *models.CommitStatus) *api.CommitStatus { + apiStatus := &api.CommitStatus{ Created: status.CreatedUnix.AsTime(), Updated: status.CreatedUnix.AsTime(), State: status.State, @@ -44,7 +44,7 @@ func ToCombinedStatus(statuses []*models.CommitStatus, repo *api.Repository) *ap URL: "", } - retStatus.Statuses = make([]*api.Status, 0, len(statuses)) + retStatus.Statuses = make([]*api.CommitStatus, 0, len(statuses)) for _, status := range statuses { retStatus.Statuses = append(retStatus.Statuses, ToCommitStatus(status)) if status.State.NoBetterThan(retStatus.State) { diff --git a/modules/structs/commit_status.go b/modules/structs/commit_status.go index 397356b133413..23e0c383b8b42 100644 --- a/modules/structs/commit_status.go +++ b/modules/structs/commit_status.go @@ -4,20 +4,20 @@ package structs -// CommitStatusState holds the state of a Status +// CommitStatusState holds the state of a CommitStatus // It can be "pending", "success", "error", "failure", and "warning" type CommitStatusState string const ( - // CommitStatusPending is for when the Status is Pending + // CommitStatusPending is for when the CommitStatus is Pending CommitStatusPending CommitStatusState = "pending" - // CommitStatusSuccess is for when the Status is Success + // CommitStatusSuccess is for when the CommitStatus is Success CommitStatusSuccess CommitStatusState = "success" - // CommitStatusError is for when the Status is Error + // CommitStatusError is for when the CommitStatus is Error CommitStatusError CommitStatusState = "error" - // CommitStatusFailure is for when the Status is Failure + // CommitStatusFailure is for when the CommitStatus is Failure CommitStatusFailure CommitStatusState = "failure" - // CommitStatusWarning is for when the Status is Warning + // CommitStatusWarning is for when the CommitStatus is Warning CommitStatusWarning CommitStatusState = "warning" ) diff --git a/modules/structs/status.go b/modules/structs/status.go index 6d6bdb4df3be7..ed42b24be81bd 100644 --- a/modules/structs/status.go +++ b/modules/structs/status.go @@ -8,8 +8,8 @@ import ( "time" ) -// Status holds a single Status of a single Commit -type Status struct { +// CommitStatus holds a single status of a single Commit +type CommitStatus struct { ID int64 `json:"id"` State CommitStatusState `json:"status"` TargetURL string `json:"target_url"` @@ -28,13 +28,13 @@ type CombinedStatus struct { State CommitStatusState `json:"state"` SHA string `json:"sha"` TotalCount int `json:"total_count"` - Statuses []*Status `json:"statuses"` + Statuses []*CommitStatus `json:"statuses"` Repository *Repository `json:"repository"` CommitURL string `json:"commit_url"` URL string `json:"url"` } -// CreateStatusOption holds the information needed to create a new Status for a Commit +// CreateStatusOption holds the information needed to create a new CommitStatus for a Commit type CreateStatusOption struct { State CommitStatusState `json:"state"` TargetURL string `json:"target_url"` diff --git a/routers/api/v1/repo/status.go b/routers/api/v1/repo/status.go index 082c387da5211..b41d78d7ec154 100644 --- a/routers/api/v1/repo/status.go +++ b/routers/api/v1/repo/status.go @@ -45,7 +45,7 @@ func NewCommitStatus(ctx *context.APIContext, form api.CreateStatusOption) { // "$ref": "#/definitions/CreateStatusOption" // responses: // "201": - // "$ref": "#/responses/Status" + // "$ref": "#/responses/CommitStatus" // "400": // "$ref": "#/responses/error" @@ -221,7 +221,7 @@ func getCommitStatuses(ctx *context.APIContext, sha string) { return } - apiStatuses := make([]*api.Status, 0, len(statuses)) + apiStatuses := make([]*api.CommitStatus, 0, len(statuses)) for _, status := range statuses { apiStatuses = append(apiStatuses, convert.ToCommitStatus(status)) } diff --git a/routers/api/v1/swagger/repo.go b/routers/api/v1/swagger/repo.go index 76c9ac608b387..d539bcb9feabb 100644 --- a/routers/api/v1/swagger/repo.go +++ b/routers/api/v1/swagger/repo.go @@ -169,18 +169,18 @@ type swaggerResponsePullReviewCommentList struct { Body []api.PullReviewComment `json:"body"` } -// Status -// swagger:response Status +// CommitStatus +// swagger:response CommitStatus type swaggerResponseStatus struct { // in:body - Body api.Status `json:"body"` + Body api.CommitStatus `json:"body"` } -// StatusList -// swagger:response StatusList -type swaggerResponseStatusList struct { +// CommitStatusList +// swagger:response CommitStatusList +type swaggerResponseCommitStatusList struct { // in:body - Body []api.Status `json:"body"` + Body []api.CommitStatus `json:"body"` } // WatchInfo diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 7253e5ff4fb61..6a1a8f6782ed1 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -8597,7 +8597,7 @@ ], "responses": { "201": { - "$ref": "#/responses/Status" + "$ref": "#/responses/CommitStatus" }, "400": { "$ref": "#/responses/error" @@ -11539,7 +11539,7 @@ "statuses": { "type": "array", "items": { - "$ref": "#/definitions/Status" + "$ref": "#/definitions/CommitStatus" }, "x-go-name": "Statuses" }, @@ -11682,8 +11682,52 @@ }, "x-go-package": "code.gitea.io/gitea/modules/structs" }, + "CommitStatus": { + "description": "CommitStatus holds a single CommitStatus of a single Commit", + "type": "object", + "properties": { + "context": { + "type": "string", + "x-go-name": "Context" + }, + "created_at": { + "type": "string", + "format": "date-time", + "x-go-name": "Created" + }, + "creator": { + "$ref": "#/definitions/User" + }, + "description": { + "type": "string", + "x-go-name": "Description" + }, + "id": { + "type": "integer", + "format": "int64", + "x-go-name": "ID" + }, + "status": { + "$ref": "#/definitions/CommitStatusState" + }, + "target_url": { + "type": "string", + "x-go-name": "TargetURL" + }, + "updated_at": { + "type": "string", + "format": "date-time", + "x-go-name": "Updated" + }, + "url": { + "type": "string", + "x-go-name": "URL" + } + }, + "x-go-package": "code.gitea.io/gitea/modules/structs" + }, "CommitStatusState": { - "description": "CommitStatusState holds the state of a Status\nIt can be \"pending\", \"success\", \"error\", \"failure\", and \"warning\"", + "description": "CommitStatusState holds the state of a CommitStatus\nIt can be \"pending\", \"success\", \"error\", \"failure\", and \"warning\"", "type": "string", "x-go-package": "code.gitea.io/gitea/modules/structs" }, @@ -12470,7 +12514,7 @@ "x-go-package": "code.gitea.io/gitea/modules/structs" }, "CreateStatusOption": { - "description": "CreateStatusOption holds the information needed to create a new Status for a Commit", + "description": "CreateStatusOption holds the information needed to create a new CommitStatus for a Commit", "type": "object", "properties": { "context": { @@ -15380,50 +15424,6 @@ "type": "string", "x-go-package": "code.gitea.io/gitea/modules/structs" }, - "Status": { - "description": "Status holds a single Status of a single Commit", - "type": "object", - "properties": { - "context": { - "type": "string", - "x-go-name": "Context" - }, - "created_at": { - "type": "string", - "format": "date-time", - "x-go-name": "Created" - }, - "creator": { - "$ref": "#/definitions/User" - }, - "description": { - "type": "string", - "x-go-name": "Description" - }, - "id": { - "type": "integer", - "format": "int64", - "x-go-name": "ID" - }, - "status": { - "$ref": "#/definitions/CommitStatusState" - }, - "target_url": { - "type": "string", - "x-go-name": "TargetURL" - }, - "updated_at": { - "type": "string", - "format": "date-time", - "x-go-name": "Updated" - }, - "url": { - "type": "string", - "x-go-name": "URL" - } - }, - "x-go-package": "code.gitea.io/gitea/modules/structs" - }, "StopWatch": { "description": "StopWatch represent a running stopwatch", "type": "object", @@ -15959,6 +15959,21 @@ } } }, + "CommitStatus": { + "description": "CommitStatus", + "schema": { + "$ref": "#/definitions/CommitStatus" + } + }, + "CommitStatusList": { + "description": "CommitStatusList", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/CommitStatus" + } + } + }, "ContentsListResponse": { "description": "ContentsListResponse", "schema": { @@ -16365,21 +16380,6 @@ "$ref": "#/definitions/ServerVersion" } }, - "Status": { - "description": "Status", - "schema": { - "$ref": "#/definitions/Status" - } - }, - "StatusList": { - "description": "StatusList", - "schema": { - "type": "array", - "items": { - "$ref": "#/definitions/Status" - } - } - }, "StopWatch": { "description": "StopWatch", "schema": { From 636fa5de1ed80b0b1d2f9372ff3957474457f38d Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 17 Dec 2020 17:34:39 +0100 Subject: [PATCH 15/16] fix swagger docs --- routers/api/v1/repo/status.go | 2 +- templates/swagger/v1_json.tmpl | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/routers/api/v1/repo/status.go b/routers/api/v1/repo/status.go index b41d78d7ec154..bc52b3229b71e 100644 --- a/routers/api/v1/repo/status.go +++ b/routers/api/v1/repo/status.go @@ -113,7 +113,7 @@ func GetCommitStatuses(ctx *context.APIContext) { // type: integer // responses: // "200": - // "$ref": "#/responses/StatusList" + // "$ref": "#/responses/CommitStatusList" // "400": // "$ref": "#/responses/error" diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 6a1a8f6782ed1..d3012954ba99a 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -8549,7 +8549,7 @@ ], "responses": { "200": { - "$ref": "#/responses/StatusList" + "$ref": "#/responses/CommitStatusList" }, "400": { "$ref": "#/responses/error" @@ -11683,7 +11683,7 @@ "x-go-package": "code.gitea.io/gitea/modules/structs" }, "CommitStatus": { - "description": "CommitStatus holds a single CommitStatus of a single Commit", + "description": "CommitStatus holds a single status of a single Commit", "type": "object", "properties": { "context": { From 3a26d5de977d28477d691feaf24de05f8b14a890 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 17 Dec 2020 17:45:23 +0100 Subject: [PATCH 16/16] sec swagger fix --- routers/api/v1/repo/status.go | 2 +- templates/swagger/v1_json.tmpl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/routers/api/v1/repo/status.go b/routers/api/v1/repo/status.go index bc52b3229b71e..9c0d902f76b28 100644 --- a/routers/api/v1/repo/status.go +++ b/routers/api/v1/repo/status.go @@ -165,7 +165,7 @@ func GetCommitStatusesByRef(ctx *context.APIContext) { // type: integer // responses: // "200": - // "$ref": "#/responses/StatusList" + // "$ref": "#/responses/CommitStatusList" // "400": // "$ref": "#/responses/error" diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index d3012954ba99a..aa31b3f07892e 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -2995,7 +2995,7 @@ ], "responses": { "200": { - "$ref": "#/responses/StatusList" + "$ref": "#/responses/CommitStatusList" }, "400": { "$ref": "#/responses/error"