You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

api.go 27 kB

Add basic integration test infrastructure (and new endpoint `/api/v1/version` for testing it) (#741) * Implement '/api/v1/version' * Cleanup and various fixes * Enhance run.sh * Add install_test.go * Add parameter utils.Config for testing handlers * Re-organize TestVersion.go * Rename functions * handling process cleanup properly * Fix missing function renaming * Cleanup the 'retry' logic * Cleanup * Remove unneeded logging code * Logging messages tweaking * Logging message tweaking * Fix logging messages * Use 'const' instead of hardwired numbers * We don't really need retries anymore * Move constant ServerHttpPort to install_test.go * Restore mistakenly removed constant * Add required comments to make the linter happy. * Fix comments and naming to address linter's complaints * Detect Gitea executale version automatically * Remove tests/run.sh, `go test` suffices. * Make `make build` a prerequisite of `make test` * Do not sleep before trying * Speedup the server pinging loop * Use defined const instead of hardwired numbers * Remove redundant error handling * Use a dedicated target for running code.gitea.io/tests * Do not make 'test' depend on 'build' target * Rectify the excluded package list * Remove redundant 'exit 1' * Change the API to allow passing test.T to test handlers * Make testing.T an embedded field * Use assert.Equal to comparing results * Add copyright info * Parametrized logging output * Use tmpdir instead * Eliminate redundant casting * Remove unneeded variable * Fix last commit * Add missing copyright info * Replace fmt.Fprintf with fmt.Fprint * rename the xtest to integration-test * Use Symlink instead of hard-link for cross-device linking * Turn debugging logs on * Follow the existing framework for APIs * Output logs only if test.v is true * Re-order import statements * Enhance the error message * Fix comment which breaks the linter's rule * Rename 'integration-test' to 'e2e-test' for saving keystrokes * Add comment to avoid possible confusion * Rename tests -> integration-tests Also change back the Makefile to use `make integration-test`. * Use tests/integration for now * tests/integration -> integrations Slightly flattened directory hierarchy is better. * Update Makefile accordingly * Fix a missing change in Makefile * govendor update code.gitea.io/sdk/gitea * Fix comment of struct fields * Fix conditional nonsense * Fix missing updates regarding version string changes * Make variable naming more consistent * Check http status code * Rectify error messages
8 years ago
10 years ago
9 years ago
Feature: Timetracking (#2211) * Added comment's hashtag to url for mail notifications. * Added explanation to return statement + documentation. * Replacing in-line link generation with HTMLURL. (+gofmt) * Replaced action-based model with nil-based model. (+gofmt) * Replaced mailIssueActionToParticipants with mailIssueCommentToParticipants. * Updating comment for mailIssueCommentToParticipants * Added link to comment in "Dashboard" * Deleting feed entry if a comment is going to be deleted * Added migration * Added improved migration to add a CommentID column to action. * Added improved links to comments in feed entries. * Fixes #1956 by filtering for deleted comments that are referenced in actions. * Introducing "IsDeleted" column to action. * Adding design draft (not functional) * Adding database models for stopwatches and trackedtimes * See go-gitea/gitea#967 * Adding design draft (not functional) * Adding translations and improving design * Implementing stopwatch (for timetracking) * Make UI functional * Add hints in timeline for time tracking events * Implementing timetracking feature * Adding "Add time manual" option * Improved stopwatch * Created report of total spent time by user * Only showing total time spent if theire is something to show. * Adding license headers. * Improved error handling for "Add Time Manual" * Adding @sapks 's changes, refactoring * Adding API for feature tracking * Adding unit test * Adding DISABLE/ENABLE option to Repository settings page * Improving translations * Applying @sapk 's changes * Removing repo_unit and using IssuesSetting for disabling/enabling timetracker * Adding DEFAULT_ENABLE_TIMETRACKER to config, installation and admin menu * Improving documentation * Fixing vendor/ folder * Changing timtracking routes by adding subgroups /times and /times/stopwatch (Proposed by @lafriks ) * Restricting write access to timetracking based on the repo settings (Proposed by @lafriks ) * Fixed minor permissions bug. * Adding CanUseTimetracker and IsTimetrackerEnabled in ctx.Repo * Allow assignees and authors to track there time too. * Fixed some build-time-errors + logical errors. * Removing unused Get...ByID functions * Moving IsTimetrackerEnabled from context.Repository to models.Repository * Adding a seperate file for issue related repo functions * Adding license headers * Fixed GetUserByParams return 404 * Moving /users/:username/times to /repos/:username/:reponame/times/:username for security reasons * Adding /repos/:username/times to get all tracked times of the repo * Updating sdk-dependency * Updating swagger.v1.json * Adding warning if user has already a running stopwatch (auto-timetracker) * Replacing GetTrackedTimesBy... with GetTrackedTimes(options FindTrackedTimesOptions) * Changing code.gitea.io/sdk back to code.gitea.io/sdk * Correcting spelling mistake * Updating vendor.json * Changing GET stopwatch/toggle to POST stopwatch/toggle * Changing GET stopwatch/cancel to POST stopwatch/cancel * Added migration for stopwatches/timetracking * Fixed some access bugs for read-only users * Added default allow only contributors to track time value to config * Fixed migration by chaging x.Iterate to x.Find * Resorted imports * Moved Add Time Manually form to repo_form.go * Removed "Seconds" field from Add Time Manually * Resorted imports * Improved permission checking * Fixed some bugs * Added integration test * gofmt * Adding integration test by @lafriks * Added created_unix to comment fixtures * Using last event instead of a fixed event * Adding another integration test by @lafriks * Fixing bug Timetracker enabled causing error 500 at sidebar.tpl * Fixed a refactoring bug that resulted in hiding "HasUserStopwatch" warning. * Returning TrackedTime instead of AddTimeOption at AddTime. * Updating SDK from go-gitea/go-sdk#69 * Resetting Go-SDK back to default repository * Fixing test-vendor by changing ini back to original repository * Adding "tags" to swagger spec * govendor sync * Removed duplicate * Formatting templates * Adding IsTimetrackingEnabled checks to API * Improving translations / english texts * Improving documentation * Updating swagger spec * Fixing integration test caused be translation-changes * Removed encoding issues in local_en-US.ini. * "Added" copyright line * Moved unit.IssuesConfig().EnableTimetracker into a != nil check * Removed some other encoding issues in local_en-US.ini * Improved javascript by checking if data-context exists * Replaced manual comment creation with CreateComment * Removed unnecessary code * Improved error checking * Small cosmetic changes * Replaced int>string>duration parsing with int>duration parsing * Fixed encoding issues * Removed unused imports Signed-off-by: Jonas Franz <info@jonasfranz.software>
8 years ago
10 years ago
Feature: Timetracking (#2211) * Added comment's hashtag to url for mail notifications. * Added explanation to return statement + documentation. * Replacing in-line link generation with HTMLURL. (+gofmt) * Replaced action-based model with nil-based model. (+gofmt) * Replaced mailIssueActionToParticipants with mailIssueCommentToParticipants. * Updating comment for mailIssueCommentToParticipants * Added link to comment in "Dashboard" * Deleting feed entry if a comment is going to be deleted * Added migration * Added improved migration to add a CommentID column to action. * Added improved links to comments in feed entries. * Fixes #1956 by filtering for deleted comments that are referenced in actions. * Introducing "IsDeleted" column to action. * Adding design draft (not functional) * Adding database models for stopwatches and trackedtimes * See go-gitea/gitea#967 * Adding design draft (not functional) * Adding translations and improving design * Implementing stopwatch (for timetracking) * Make UI functional * Add hints in timeline for time tracking events * Implementing timetracking feature * Adding "Add time manual" option * Improved stopwatch * Created report of total spent time by user * Only showing total time spent if theire is something to show. * Adding license headers. * Improved error handling for "Add Time Manual" * Adding @sapks 's changes, refactoring * Adding API for feature tracking * Adding unit test * Adding DISABLE/ENABLE option to Repository settings page * Improving translations * Applying @sapk 's changes * Removing repo_unit and using IssuesSetting for disabling/enabling timetracker * Adding DEFAULT_ENABLE_TIMETRACKER to config, installation and admin menu * Improving documentation * Fixing vendor/ folder * Changing timtracking routes by adding subgroups /times and /times/stopwatch (Proposed by @lafriks ) * Restricting write access to timetracking based on the repo settings (Proposed by @lafriks ) * Fixed minor permissions bug. * Adding CanUseTimetracker and IsTimetrackerEnabled in ctx.Repo * Allow assignees and authors to track there time too. * Fixed some build-time-errors + logical errors. * Removing unused Get...ByID functions * Moving IsTimetrackerEnabled from context.Repository to models.Repository * Adding a seperate file for issue related repo functions * Adding license headers * Fixed GetUserByParams return 404 * Moving /users/:username/times to /repos/:username/:reponame/times/:username for security reasons * Adding /repos/:username/times to get all tracked times of the repo * Updating sdk-dependency * Updating swagger.v1.json * Adding warning if user has already a running stopwatch (auto-timetracker) * Replacing GetTrackedTimesBy... with GetTrackedTimes(options FindTrackedTimesOptions) * Changing code.gitea.io/sdk back to code.gitea.io/sdk * Correcting spelling mistake * Updating vendor.json * Changing GET stopwatch/toggle to POST stopwatch/toggle * Changing GET stopwatch/cancel to POST stopwatch/cancel * Added migration for stopwatches/timetracking * Fixed some access bugs for read-only users * Added default allow only contributors to track time value to config * Fixed migration by chaging x.Iterate to x.Find * Resorted imports * Moved Add Time Manually form to repo_form.go * Removed "Seconds" field from Add Time Manually * Resorted imports * Improved permission checking * Fixed some bugs * Added integration test * gofmt * Adding integration test by @lafriks * Added created_unix to comment fixtures * Using last event instead of a fixed event * Adding another integration test by @lafriks * Fixing bug Timetracker enabled causing error 500 at sidebar.tpl * Fixed a refactoring bug that resulted in hiding "HasUserStopwatch" warning. * Returning TrackedTime instead of AddTimeOption at AddTime. * Updating SDK from go-gitea/go-sdk#69 * Resetting Go-SDK back to default repository * Fixing test-vendor by changing ini back to original repository * Adding "tags" to swagger spec * govendor sync * Removed duplicate * Formatting templates * Adding IsTimetrackingEnabled checks to API * Improving translations / english texts * Improving documentation * Updating swagger spec * Fixing integration test caused be translation-changes * Removed encoding issues in local_en-US.ini. * "Added" copyright line * Moved unit.IssuesConfig().EnableTimetracker into a != nil check * Removed some other encoding issues in local_en-US.ini * Improved javascript by checking if data-context exists * Replaced manual comment creation with CreateComment * Removed unnecessary code * Improved error checking * Small cosmetic changes * Replaced int>string>duration parsing with int>duration parsing * Fixed encoding issues * Removed unused imports Signed-off-by: Jonas Franz <info@jonasfranz.software>
8 years ago
Feature: Timetracking (#2211) * Added comment's hashtag to url for mail notifications. * Added explanation to return statement + documentation. * Replacing in-line link generation with HTMLURL. (+gofmt) * Replaced action-based model with nil-based model. (+gofmt) * Replaced mailIssueActionToParticipants with mailIssueCommentToParticipants. * Updating comment for mailIssueCommentToParticipants * Added link to comment in "Dashboard" * Deleting feed entry if a comment is going to be deleted * Added migration * Added improved migration to add a CommentID column to action. * Added improved links to comments in feed entries. * Fixes #1956 by filtering for deleted comments that are referenced in actions. * Introducing "IsDeleted" column to action. * Adding design draft (not functional) * Adding database models for stopwatches and trackedtimes * See go-gitea/gitea#967 * Adding design draft (not functional) * Adding translations and improving design * Implementing stopwatch (for timetracking) * Make UI functional * Add hints in timeline for time tracking events * Implementing timetracking feature * Adding "Add time manual" option * Improved stopwatch * Created report of total spent time by user * Only showing total time spent if theire is something to show. * Adding license headers. * Improved error handling for "Add Time Manual" * Adding @sapks 's changes, refactoring * Adding API for feature tracking * Adding unit test * Adding DISABLE/ENABLE option to Repository settings page * Improving translations * Applying @sapk 's changes * Removing repo_unit and using IssuesSetting for disabling/enabling timetracker * Adding DEFAULT_ENABLE_TIMETRACKER to config, installation and admin menu * Improving documentation * Fixing vendor/ folder * Changing timtracking routes by adding subgroups /times and /times/stopwatch (Proposed by @lafriks ) * Restricting write access to timetracking based on the repo settings (Proposed by @lafriks ) * Fixed minor permissions bug. * Adding CanUseTimetracker and IsTimetrackerEnabled in ctx.Repo * Allow assignees and authors to track there time too. * Fixed some build-time-errors + logical errors. * Removing unused Get...ByID functions * Moving IsTimetrackerEnabled from context.Repository to models.Repository * Adding a seperate file for issue related repo functions * Adding license headers * Fixed GetUserByParams return 404 * Moving /users/:username/times to /repos/:username/:reponame/times/:username for security reasons * Adding /repos/:username/times to get all tracked times of the repo * Updating sdk-dependency * Updating swagger.v1.json * Adding warning if user has already a running stopwatch (auto-timetracker) * Replacing GetTrackedTimesBy... with GetTrackedTimes(options FindTrackedTimesOptions) * Changing code.gitea.io/sdk back to code.gitea.io/sdk * Correcting spelling mistake * Updating vendor.json * Changing GET stopwatch/toggle to POST stopwatch/toggle * Changing GET stopwatch/cancel to POST stopwatch/cancel * Added migration for stopwatches/timetracking * Fixed some access bugs for read-only users * Added default allow only contributors to track time value to config * Fixed migration by chaging x.Iterate to x.Find * Resorted imports * Moved Add Time Manually form to repo_form.go * Removed "Seconds" field from Add Time Manually * Resorted imports * Improved permission checking * Fixed some bugs * Added integration test * gofmt * Adding integration test by @lafriks * Added created_unix to comment fixtures * Using last event instead of a fixed event * Adding another integration test by @lafriks * Fixing bug Timetracker enabled causing error 500 at sidebar.tpl * Fixed a refactoring bug that resulted in hiding "HasUserStopwatch" warning. * Returning TrackedTime instead of AddTimeOption at AddTime. * Updating SDK from go-gitea/go-sdk#69 * Resetting Go-SDK back to default repository * Fixing test-vendor by changing ini back to original repository * Adding "tags" to swagger spec * govendor sync * Removed duplicate * Formatting templates * Adding IsTimetrackingEnabled checks to API * Improving translations / english texts * Improving documentation * Updating swagger spec * Fixing integration test caused be translation-changes * Removed encoding issues in local_en-US.ini. * "Added" copyright line * Moved unit.IssuesConfig().EnableTimetracker into a != nil check * Removed some other encoding issues in local_en-US.ini * Improved javascript by checking if data-context exists * Replaced manual comment creation with CreateComment * Removed unnecessary code * Improved error checking * Small cosmetic changes * Replaced int>string>duration parsing with int>duration parsing * Fixed encoding issues * Removed unused imports Signed-off-by: Jonas Franz <info@jonasfranz.software>
8 years ago
Add Attachment API (#3478) * Add Attachment API * repos/:owner/:repo/releases (add attachments) * repos/:owner/:repo/releases/:id (add attachments) * repos/:owner/:repo/releases/:id/attachments * repos/:owner/:repo/releases/:id/attachments/:attachment_id Signed-off-by: Jonas Franz <info@jonasfranz.de> * Add unit tests for new attachment functions Fix comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * fix lint * Update vendor.json Signed-off-by: Jonas Franz <info@jonasfranz.software> * remove version of sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit tests Add missing license header Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add CreateReleaseAttachment Add EditReleaseAttachment Add DeleteReleaseAttachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add filename query parameter for choosing another name for an attachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix order of imports Signed-off-by: Jonas Franz <info@jonasfranz.software> * Restricting updatable attachment columns Signed-off-by: Jonas Franz <info@jonasfranz.software> * gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update go-sdk Replace Attachments with Assets Signed-off-by: Jonas Franz <info@jonasfranz.de> * Update go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.de> * Updating go-sdk and regenerating swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add missing file of go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change origin of code.gitea.io/sdk to code.gitea.io/sdk Update code.gitea.io/sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update updateAttachment
7 years ago
Add Attachment API (#3478) * Add Attachment API * repos/:owner/:repo/releases (add attachments) * repos/:owner/:repo/releases/:id (add attachments) * repos/:owner/:repo/releases/:id/attachments * repos/:owner/:repo/releases/:id/attachments/:attachment_id Signed-off-by: Jonas Franz <info@jonasfranz.de> * Add unit tests for new attachment functions Fix comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * fix lint * Update vendor.json Signed-off-by: Jonas Franz <info@jonasfranz.software> * remove version of sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit tests Add missing license header Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add CreateReleaseAttachment Add EditReleaseAttachment Add DeleteReleaseAttachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add filename query parameter for choosing another name for an attachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix order of imports Signed-off-by: Jonas Franz <info@jonasfranz.software> * Restricting updatable attachment columns Signed-off-by: Jonas Franz <info@jonasfranz.software> * gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update go-sdk Replace Attachments with Assets Signed-off-by: Jonas Franz <info@jonasfranz.de> * Update go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.de> * Updating go-sdk and regenerating swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add missing file of go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change origin of code.gitea.io/sdk to code.gitea.io/sdk Update code.gitea.io/sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update updateAttachment
7 years ago
Add Attachment API (#3478) * Add Attachment API * repos/:owner/:repo/releases (add attachments) * repos/:owner/:repo/releases/:id (add attachments) * repos/:owner/:repo/releases/:id/attachments * repos/:owner/:repo/releases/:id/attachments/:attachment_id Signed-off-by: Jonas Franz <info@jonasfranz.de> * Add unit tests for new attachment functions Fix comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * fix lint * Update vendor.json Signed-off-by: Jonas Franz <info@jonasfranz.software> * remove version of sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit tests Add missing license header Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add CreateReleaseAttachment Add EditReleaseAttachment Add DeleteReleaseAttachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add filename query parameter for choosing another name for an attachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix order of imports Signed-off-by: Jonas Franz <info@jonasfranz.software> * Restricting updatable attachment columns Signed-off-by: Jonas Franz <info@jonasfranz.software> * gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update go-sdk Replace Attachments with Assets Signed-off-by: Jonas Franz <info@jonasfranz.de> * Update go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.de> * Updating go-sdk and regenerating swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add missing file of go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change origin of code.gitea.io/sdk to code.gitea.io/sdk Update code.gitea.io/sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update updateAttachment
7 years ago
Add Attachment API (#3478) * Add Attachment API * repos/:owner/:repo/releases (add attachments) * repos/:owner/:repo/releases/:id (add attachments) * repos/:owner/:repo/releases/:id/attachments * repos/:owner/:repo/releases/:id/attachments/:attachment_id Signed-off-by: Jonas Franz <info@jonasfranz.de> * Add unit tests for new attachment functions Fix comments Signed-off-by: Jonas Franz <info@jonasfranz.software> * fix lint * Update vendor.json Signed-off-by: Jonas Franz <info@jonasfranz.software> * remove version of sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix unit tests Add missing license header Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add CreateReleaseAttachment Add EditReleaseAttachment Add DeleteReleaseAttachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add filename query parameter for choosing another name for an attachment Signed-off-by: Jonas Franz <info@jonasfranz.software> * Fix order of imports Signed-off-by: Jonas Franz <info@jonasfranz.software> * Restricting updatable attachment columns Signed-off-by: Jonas Franz <info@jonasfranz.software> * gofmt Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update go-sdk Replace Attachments with Assets Signed-off-by: Jonas Franz <info@jonasfranz.de> * Update go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.de> * Updating go-sdk and regenerating swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Add missing file of go-sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Change origin of code.gitea.io/sdk to code.gitea.io/sdk Update code.gitea.io/sdk Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update swagger Signed-off-by: Jonas Franz <info@jonasfranz.software> * Update updateAttachment
7 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Copyright 2016 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. // Package v1 Gitea API.
  6. //
  7. // This documentation describes the Gitea API.
  8. //
  9. // Schemes: http, https
  10. // BasePath: /api/v1
  11. // Version: 1.1.1
  12. // License: MIT http://opensource.org/licenses/MIT
  13. //
  14. // Consumes:
  15. // - application/json
  16. // - text/plain
  17. //
  18. // Produces:
  19. // - application/json
  20. // - text/html
  21. //
  22. // Security:
  23. // - BasicAuth :
  24. // - Token :
  25. // - AccessToken :
  26. // - AuthorizationHeaderToken :
  27. // - SudoParam :
  28. // - SudoHeader :
  29. //
  30. // SecurityDefinitions:
  31. // BasicAuth:
  32. // type: basic
  33. // Token:
  34. // type: apiKey
  35. // name: token
  36. // in: query
  37. // AccessToken:
  38. // type: apiKey
  39. // name: access_token
  40. // in: query
  41. // AuthorizationHeaderToken:
  42. // type: apiKey
  43. // name: Authorization
  44. // in: header
  45. // SudoParam:
  46. // type: apiKey
  47. // name: sudo
  48. // in: query
  49. // description: Sudo API request as the user provided as the key. Admin privileges are required.
  50. // SudoHeader:
  51. // type: apiKey
  52. // name: Sudo
  53. // in: header
  54. // description: Sudo API request as the user provided as the key. Admin privileges are required.
  55. //
  56. // swagger:meta
  57. package v1
  58. import (
  59. "strings"
  60. "code.gitea.io/gitea/models"
  61. "code.gitea.io/gitea/modules/auth"
  62. "code.gitea.io/gitea/modules/context"
  63. "code.gitea.io/gitea/modules/log"
  64. "code.gitea.io/gitea/modules/setting"
  65. "code.gitea.io/gitea/routers/api/v1/admin"
  66. "code.gitea.io/gitea/routers/api/v1/misc"
  67. "code.gitea.io/gitea/routers/api/v1/org"
  68. "code.gitea.io/gitea/routers/api/v1/repo"
  69. _ "code.gitea.io/gitea/routers/api/v1/swagger" // for swagger generation
  70. "code.gitea.io/gitea/routers/api/v1/user"
  71. api "code.gitea.io/sdk/gitea"
  72. "github.com/go-macaron/binding"
  73. "gopkg.in/macaron.v1"
  74. )
  75. func sudo() macaron.Handler {
  76. return func(ctx *context.APIContext) {
  77. sudo := ctx.Query("sudo")
  78. if len(sudo) == 0 {
  79. sudo = ctx.Req.Header.Get("Sudo")
  80. }
  81. if len(sudo) > 0 {
  82. if ctx.IsSigned && ctx.User.IsAdmin {
  83. user, err := models.GetUserByName(sudo)
  84. if err != nil {
  85. if models.IsErrUserNotExist(err) {
  86. ctx.NotFound()
  87. } else {
  88. ctx.Error(500, "GetUserByName", err)
  89. }
  90. return
  91. }
  92. log.Trace("Sudo from (%s) to: %s", ctx.User.Name, user.Name)
  93. ctx.User = user
  94. } else {
  95. ctx.JSON(403, map[string]string{
  96. "message": "Only administrators allowed to sudo.",
  97. })
  98. return
  99. }
  100. }
  101. }
  102. }
  103. func repoAssignment() macaron.Handler {
  104. return func(ctx *context.APIContext) {
  105. userName := ctx.Params(":username")
  106. repoName := ctx.Params(":reponame")
  107. var (
  108. owner *models.User
  109. err error
  110. )
  111. // Check if the user is the same as the repository owner.
  112. if ctx.IsSigned && ctx.User.LowerName == strings.ToLower(userName) {
  113. owner = ctx.User
  114. } else {
  115. owner, err = models.GetUserByName(userName)
  116. if err != nil {
  117. if models.IsErrUserNotExist(err) {
  118. ctx.NotFound()
  119. } else {
  120. ctx.Error(500, "GetUserByName", err)
  121. }
  122. return
  123. }
  124. }
  125. ctx.Repo.Owner = owner
  126. // Get repository.
  127. repo, err := models.GetRepositoryByName(owner.ID, repoName)
  128. if err != nil {
  129. if models.IsErrRepoNotExist(err) {
  130. redirectRepoID, err := models.LookupRepoRedirect(owner.ID, repoName)
  131. if err == nil {
  132. context.RedirectToRepo(ctx.Context, redirectRepoID)
  133. } else if models.IsErrRepoRedirectNotExist(err) {
  134. ctx.NotFound()
  135. } else {
  136. ctx.Error(500, "LookupRepoRedirect", err)
  137. }
  138. } else {
  139. ctx.Error(500, "GetRepositoryByName", err)
  140. }
  141. return
  142. }
  143. repo.Owner = owner
  144. ctx.Repo.Repository = repo
  145. ctx.Repo.Permission, err = models.GetUserRepoPermission(repo, ctx.User)
  146. if err != nil {
  147. ctx.Error(500, "GetUserRepoPermission", err)
  148. return
  149. }
  150. if !ctx.Repo.HasAccess() {
  151. ctx.NotFound()
  152. return
  153. }
  154. }
  155. }
  156. // Contexter middleware already checks token for user sign in process.
  157. func reqToken() macaron.Handler {
  158. return func(ctx *context.APIContext) {
  159. if true == ctx.Data["IsApiToken"] {
  160. return
  161. }
  162. if ctx.Context.IsBasicAuth {
  163. ctx.CheckForOTP()
  164. return
  165. }
  166. if ctx.IsSigned {
  167. ctx.RequireCSRF()
  168. return
  169. }
  170. ctx.Context.Error(401)
  171. }
  172. }
  173. func reqBasicAuth() macaron.Handler {
  174. return func(ctx *context.APIContext) {
  175. if !ctx.Context.IsBasicAuth {
  176. ctx.Context.Error(401)
  177. return
  178. }
  179. ctx.CheckForOTP()
  180. }
  181. }
  182. // reqSiteAdmin user should be the site admin
  183. func reqSiteAdmin() macaron.Handler {
  184. return func(ctx *context.Context) {
  185. if !ctx.IsUserSiteAdmin() {
  186. ctx.Error(403)
  187. return
  188. }
  189. }
  190. }
  191. // reqOwner user should be the owner of the repo or site admin.
  192. func reqOwner() macaron.Handler {
  193. return func(ctx *context.Context) {
  194. if !ctx.IsUserRepoOwner() && !ctx.IsUserSiteAdmin() {
  195. ctx.Error(403)
  196. return
  197. }
  198. }
  199. }
  200. // reqAdmin user should be an owner or a collaborator with admin write of a repository, or site admin
  201. func reqAdmin() macaron.Handler {
  202. return func(ctx *context.Context) {
  203. if !ctx.IsUserRepoAdmin() && !ctx.IsUserSiteAdmin() {
  204. ctx.Error(403)
  205. return
  206. }
  207. }
  208. }
  209. // reqRepoWriter user should have a permission to write to a repo, or be a site admin
  210. func reqRepoWriter(unitTypes ...models.UnitType) macaron.Handler {
  211. return func(ctx *context.Context) {
  212. if !ctx.IsUserRepoWriter(unitTypes) && !ctx.IsUserRepoAdmin() && !ctx.IsUserSiteAdmin() {
  213. ctx.Error(403)
  214. return
  215. }
  216. }
  217. }
  218. // reqRepoReader user should have specific read permission or be a repo admin or a site admin
  219. func reqRepoReader(unitType models.UnitType) macaron.Handler {
  220. return func(ctx *context.Context) {
  221. if !ctx.IsUserRepoReaderSpecific(unitType) && !ctx.IsUserRepoAdmin() && !ctx.IsUserSiteAdmin() {
  222. ctx.Error(403)
  223. return
  224. }
  225. }
  226. }
  227. // reqAnyRepoReader user should have any permission to read repository or permissions of site admin
  228. func reqAnyRepoReader() macaron.Handler {
  229. return func(ctx *context.Context) {
  230. if !ctx.IsUserRepoReaderAny() && !ctx.IsUserSiteAdmin() {
  231. ctx.Error(403)
  232. return
  233. }
  234. }
  235. }
  236. // reqOrgOwnership user should be an organization owner, or a site admin
  237. func reqOrgOwnership() macaron.Handler {
  238. return func(ctx *context.APIContext) {
  239. if ctx.Context.IsUserSiteAdmin() {
  240. return
  241. }
  242. var orgID int64
  243. if ctx.Org.Organization != nil {
  244. orgID = ctx.Org.Organization.ID
  245. } else if ctx.Org.Team != nil {
  246. orgID = ctx.Org.Team.OrgID
  247. } else {
  248. ctx.Error(500, "", "reqOrgOwnership: unprepared context")
  249. return
  250. }
  251. isOwner, err := models.IsOrganizationOwner(orgID, ctx.User.ID)
  252. if err != nil {
  253. ctx.Error(500, "IsOrganizationOwner", err)
  254. return
  255. } else if !isOwner {
  256. if ctx.Org.Organization != nil {
  257. ctx.Error(403, "", "Must be an organization owner")
  258. } else {
  259. ctx.NotFound()
  260. }
  261. return
  262. }
  263. }
  264. }
  265. // reqTeamMembership user should be an team member, or a site admin
  266. func reqTeamMembership() macaron.Handler {
  267. return func(ctx *context.APIContext) {
  268. if ctx.Context.IsUserSiteAdmin() {
  269. return
  270. }
  271. if ctx.Org.Team == nil {
  272. ctx.Error(500, "", "reqTeamMembership: unprepared context")
  273. return
  274. }
  275. var orgID = ctx.Org.Team.OrgID
  276. isOwner, err := models.IsOrganizationOwner(orgID, ctx.User.ID)
  277. if err != nil {
  278. ctx.Error(500, "IsOrganizationOwner", err)
  279. return
  280. } else if isOwner {
  281. return
  282. }
  283. if isTeamMember, err := models.IsTeamMember(orgID, ctx.Org.Team.ID, ctx.User.ID); err != nil {
  284. ctx.Error(500, "IsTeamMember", err)
  285. return
  286. } else if !isTeamMember {
  287. isOrgMember, err := models.IsOrganizationMember(orgID, ctx.User.ID)
  288. if err != nil {
  289. ctx.Error(500, "IsOrganizationMember", err)
  290. } else if isOrgMember {
  291. ctx.Error(403, "", "Must be a team member")
  292. } else {
  293. ctx.NotFound()
  294. }
  295. return
  296. }
  297. }
  298. }
  299. // reqOrgMembership user should be an organization member, or a site admin
  300. func reqOrgMembership() macaron.Handler {
  301. return func(ctx *context.APIContext) {
  302. if ctx.Context.IsUserSiteAdmin() {
  303. return
  304. }
  305. var orgID int64
  306. if ctx.Org.Organization != nil {
  307. orgID = ctx.Org.Organization.ID
  308. } else if ctx.Org.Team != nil {
  309. orgID = ctx.Org.Team.OrgID
  310. } else {
  311. ctx.Error(500, "", "reqOrgMembership: unprepared context")
  312. return
  313. }
  314. if isMember, err := models.IsOrganizationMember(orgID, ctx.User.ID); err != nil {
  315. ctx.Error(500, "IsOrganizationMember", err)
  316. return
  317. } else if !isMember {
  318. if ctx.Org.Organization != nil {
  319. ctx.Error(403, "", "Must be an organization member")
  320. } else {
  321. ctx.NotFound()
  322. }
  323. return
  324. }
  325. }
  326. }
  327. func reqGitHook() macaron.Handler {
  328. return func(ctx *context.APIContext) {
  329. if !ctx.User.CanEditGitHook() {
  330. ctx.Error(403, "", "must be allowed to edit Git hooks")
  331. return
  332. }
  333. }
  334. }
  335. func orgAssignment(args ...bool) macaron.Handler {
  336. var (
  337. assignOrg bool
  338. assignTeam bool
  339. )
  340. if len(args) > 0 {
  341. assignOrg = args[0]
  342. }
  343. if len(args) > 1 {
  344. assignTeam = args[1]
  345. }
  346. return func(ctx *context.APIContext) {
  347. ctx.Org = new(context.APIOrganization)
  348. var err error
  349. if assignOrg {
  350. ctx.Org.Organization, err = models.GetOrgByName(ctx.Params(":orgname"))
  351. if err != nil {
  352. if models.IsErrOrgNotExist(err) {
  353. ctx.NotFound()
  354. } else {
  355. ctx.Error(500, "GetOrgByName", err)
  356. }
  357. return
  358. }
  359. }
  360. if assignTeam {
  361. ctx.Org.Team, err = models.GetTeamByID(ctx.ParamsInt64(":teamid"))
  362. if err != nil {
  363. if models.IsErrUserNotExist(err) {
  364. ctx.NotFound()
  365. } else {
  366. ctx.Error(500, "GetTeamById", err)
  367. }
  368. return
  369. }
  370. }
  371. }
  372. }
  373. func mustEnableIssues(ctx *context.APIContext) {
  374. if !ctx.Repo.CanRead(models.UnitTypeIssues) {
  375. if log.IsTrace() {
  376. if ctx.IsSigned {
  377. log.Trace("Permission Denied: User %-v cannot read %-v in Repo %-v\n"+
  378. "User in Repo has Permissions: %-+v",
  379. ctx.User,
  380. models.UnitTypeIssues,
  381. ctx.Repo.Repository,
  382. ctx.Repo.Permission)
  383. } else {
  384. log.Trace("Permission Denied: Anonymous user cannot read %-v in Repo %-v\n"+
  385. "Anonymous user in Repo has Permissions: %-+v",
  386. models.UnitTypeIssues,
  387. ctx.Repo.Repository,
  388. ctx.Repo.Permission)
  389. }
  390. }
  391. ctx.NotFound()
  392. return
  393. }
  394. }
  395. func mustAllowPulls(ctx *context.APIContext) {
  396. if !(ctx.Repo.Repository.CanEnablePulls() && ctx.Repo.CanRead(models.UnitTypePullRequests)) {
  397. if ctx.Repo.Repository.CanEnablePulls() && log.IsTrace() {
  398. if ctx.IsSigned {
  399. log.Trace("Permission Denied: User %-v cannot read %-v in Repo %-v\n"+
  400. "User in Repo has Permissions: %-+v",
  401. ctx.User,
  402. models.UnitTypePullRequests,
  403. ctx.Repo.Repository,
  404. ctx.Repo.Permission)
  405. } else {
  406. log.Trace("Permission Denied: Anonymous user cannot read %-v in Repo %-v\n"+
  407. "Anonymous user in Repo has Permissions: %-+v",
  408. models.UnitTypePullRequests,
  409. ctx.Repo.Repository,
  410. ctx.Repo.Permission)
  411. }
  412. }
  413. ctx.NotFound()
  414. return
  415. }
  416. }
  417. func mustEnableIssuesOrPulls(ctx *context.APIContext) {
  418. if !ctx.Repo.CanRead(models.UnitTypeIssues) &&
  419. !(ctx.Repo.Repository.CanEnablePulls() && ctx.Repo.CanRead(models.UnitTypePullRequests)) {
  420. if ctx.Repo.Repository.CanEnablePulls() && log.IsTrace() {
  421. if ctx.IsSigned {
  422. log.Trace("Permission Denied: User %-v cannot read %-v and %-v in Repo %-v\n"+
  423. "User in Repo has Permissions: %-+v",
  424. ctx.User,
  425. models.UnitTypeIssues,
  426. models.UnitTypePullRequests,
  427. ctx.Repo.Repository,
  428. ctx.Repo.Permission)
  429. } else {
  430. log.Trace("Permission Denied: Anonymous user cannot read %-v and %-v in Repo %-v\n"+
  431. "Anonymous user in Repo has Permissions: %-+v",
  432. models.UnitTypeIssues,
  433. models.UnitTypePullRequests,
  434. ctx.Repo.Repository,
  435. ctx.Repo.Permission)
  436. }
  437. }
  438. ctx.NotFound()
  439. return
  440. }
  441. }
  442. func mustEnableUserHeatmap(ctx *context.APIContext) {
  443. if !setting.Service.EnableUserHeatmap {
  444. ctx.NotFound()
  445. return
  446. }
  447. }
  448. func mustNotBeArchived(ctx *context.APIContext) {
  449. if ctx.Repo.Repository.IsArchived {
  450. ctx.NotFound()
  451. return
  452. }
  453. }
  454. // RegisterRoutes registers all v1 APIs routes to web application.
  455. // FIXME: custom form error response
  456. func RegisterRoutes(m *macaron.Macaron) {
  457. bind := binding.Bind
  458. if setting.API.EnableSwagger {
  459. m.Get("/swagger", misc.Swagger) //Render V1 by default
  460. }
  461. m.Group("/v1", func() {
  462. // Miscellaneous
  463. if setting.API.EnableSwagger {
  464. m.Get("/swagger", misc.Swagger)
  465. }
  466. m.Get("/version", misc.Version)
  467. m.Post("/markdown", bind(api.MarkdownOption{}), misc.Markdown)
  468. m.Post("/markdown/raw", misc.MarkdownRaw)
  469. // Users
  470. m.Group("/users", func() {
  471. m.Get("/search", user.Search)
  472. m.Group("/:username", func() {
  473. m.Get("", user.GetInfo)
  474. m.Get("/heatmap", mustEnableUserHeatmap, user.GetUserHeatmapData)
  475. m.Get("/repos", user.ListUserRepos)
  476. m.Group("/tokens", func() {
  477. m.Combo("").Get(user.ListAccessTokens).
  478. Post(bind(api.CreateAccessTokenOption{}), user.CreateAccessToken)
  479. m.Combo("/:id").Delete(user.DeleteAccessToken)
  480. }, reqBasicAuth())
  481. })
  482. })
  483. m.Group("/users", func() {
  484. m.Group("/:username", func() {
  485. m.Get("/keys", user.ListPublicKeys)
  486. m.Get("/gpg_keys", user.ListGPGKeys)
  487. m.Get("/followers", user.ListFollowers)
  488. m.Group("/following", func() {
  489. m.Get("", user.ListFollowing)
  490. m.Get("/:target", user.CheckFollowing)
  491. })
  492. m.Get("/starred", user.GetStarredRepos)
  493. m.Get("/subscriptions", user.GetWatchedRepos)
  494. })
  495. }, reqToken())
  496. m.Group("/user", func() {
  497. m.Get("", user.GetAuthenticatedUser)
  498. m.Combo("/emails").Get(user.ListEmails).
  499. Post(bind(api.CreateEmailOption{}), user.AddEmail).
  500. Delete(bind(api.DeleteEmailOption{}), user.DeleteEmail)
  501. m.Get("/followers", user.ListMyFollowers)
  502. m.Group("/following", func() {
  503. m.Get("", user.ListMyFollowing)
  504. m.Combo("/:username").Get(user.CheckMyFollowing).Put(user.Follow).Delete(user.Unfollow)
  505. })
  506. m.Group("/keys", func() {
  507. m.Combo("").Get(user.ListMyPublicKeys).
  508. Post(bind(api.CreateKeyOption{}), user.CreatePublicKey)
  509. m.Combo("/:id").Get(user.GetPublicKey).
  510. Delete(user.DeletePublicKey)
  511. })
  512. m.Group("/gpg_keys", func() {
  513. m.Combo("").Get(user.ListMyGPGKeys).
  514. Post(bind(api.CreateGPGKeyOption{}), user.CreateGPGKey)
  515. m.Combo("/:id").Get(user.GetGPGKey).
  516. Delete(user.DeleteGPGKey)
  517. })
  518. m.Combo("/repos").Get(user.ListMyRepos).
  519. Post(bind(api.CreateRepoOption{}), repo.Create)
  520. m.Group("/starred", func() {
  521. m.Get("", user.GetMyStarredRepos)
  522. m.Group("/:username/:reponame", func() {
  523. m.Get("", user.IsStarring)
  524. m.Put("", user.Star)
  525. m.Delete("", user.Unstar)
  526. }, repoAssignment())
  527. })
  528. m.Get("/times", repo.ListMyTrackedTimes)
  529. m.Get("/subscriptions", user.GetMyWatchedRepos)
  530. m.Get("/teams", org.ListUserTeams)
  531. }, reqToken())
  532. // Repositories
  533. m.Post("/org/:org/repos", reqToken(), bind(api.CreateRepoOption{}), repo.CreateOrgRepo)
  534. m.Group("/repos", func() {
  535. m.Get("/search", repo.Search)
  536. })
  537. m.Combo("/repositories/:id", reqToken()).Get(repo.GetByID)
  538. m.Group("/repos", func() {
  539. m.Post("/migrate", reqToken(), bind(auth.MigrateRepoForm{}), repo.Migrate)
  540. m.Group("/:username/:reponame", func() {
  541. m.Combo("").Get(reqAnyRepoReader(), repo.Get).
  542. Delete(reqToken(), reqOwner(), repo.Delete)
  543. m.Group("/hooks", func() {
  544. m.Combo("").Get(repo.ListHooks).
  545. Post(bind(api.CreateHookOption{}), repo.CreateHook)
  546. m.Group("/:id", func() {
  547. m.Combo("").Get(repo.GetHook).
  548. Patch(bind(api.EditHookOption{}), repo.EditHook).
  549. Delete(repo.DeleteHook)
  550. m.Post("/tests", context.RepoRef(), repo.TestHook)
  551. })
  552. m.Group("/git", func() {
  553. m.Combo("").Get(repo.ListGitHooks)
  554. m.Group("/:id", func() {
  555. m.Combo("").Get(repo.GetGitHook).
  556. Patch(bind(api.EditGitHookOption{}), repo.EditGitHook).
  557. Delete(repo.DeleteGitHook)
  558. })
  559. }, reqGitHook(), context.ReferencesGitRepo(true))
  560. }, reqToken(), reqAdmin())
  561. m.Group("/collaborators", func() {
  562. m.Get("", repo.ListCollaborators)
  563. m.Combo("/:collaborator").Get(repo.IsCollaborator).
  564. Put(bind(api.AddCollaboratorOption{}), repo.AddCollaborator).
  565. Delete(repo.DeleteCollaborator)
  566. }, reqToken(), reqAdmin())
  567. m.Get("/raw/*", context.RepoRefByType(context.RepoRefAny), reqRepoReader(models.UnitTypeCode), repo.GetRawFile)
  568. m.Get("/archive/*", reqRepoReader(models.UnitTypeCode), repo.GetArchive)
  569. m.Combo("/forks").Get(repo.ListForks).
  570. Post(reqToken(), reqRepoReader(models.UnitTypeCode), bind(api.CreateForkOption{}), repo.CreateFork)
  571. m.Group("/branches", func() {
  572. m.Get("", repo.ListBranches)
  573. m.Get("/*", context.RepoRefByType(context.RepoRefBranch), repo.GetBranch)
  574. }, reqRepoReader(models.UnitTypeCode))
  575. m.Group("/tags", func() {
  576. m.Get("", repo.ListTags)
  577. }, reqRepoReader(models.UnitTypeCode))
  578. m.Group("/keys", func() {
  579. m.Combo("").Get(repo.ListDeployKeys).
  580. Post(bind(api.CreateKeyOption{}), repo.CreateDeployKey)
  581. m.Combo("/:id").Get(repo.GetDeployKey).
  582. Delete(repo.DeleteDeploykey)
  583. }, reqToken(), reqAdmin())
  584. m.Group("/times", func() {
  585. m.Combo("").Get(repo.ListTrackedTimesByRepository)
  586. m.Combo("/:timetrackingusername").Get(repo.ListTrackedTimesByUser)
  587. }, mustEnableIssues)
  588. m.Group("/issues", func() {
  589. m.Combo("").Get(repo.ListIssues).
  590. Post(reqToken(), mustNotBeArchived, bind(api.CreateIssueOption{}), repo.CreateIssue)
  591. m.Group("/comments", func() {
  592. m.Get("", repo.ListRepoIssueComments)
  593. m.Combo("/:id", reqToken()).
  594. Patch(mustNotBeArchived, bind(api.EditIssueCommentOption{}), repo.EditIssueComment).
  595. Delete(repo.DeleteIssueComment)
  596. })
  597. m.Group("/:index", func() {
  598. m.Combo("").Get(repo.GetIssue).
  599. Patch(reqToken(), bind(api.EditIssueOption{}), repo.EditIssue)
  600. m.Group("/comments", func() {
  601. m.Combo("").Get(repo.ListIssueComments).
  602. Post(reqToken(), mustNotBeArchived, bind(api.CreateIssueCommentOption{}), repo.CreateIssueComment)
  603. m.Combo("/:id", reqToken()).Patch(bind(api.EditIssueCommentOption{}), repo.EditIssueCommentDeprecated).
  604. Delete(repo.DeleteIssueCommentDeprecated)
  605. })
  606. m.Group("/labels", func() {
  607. m.Combo("").Get(repo.ListIssueLabels).
  608. Post(reqToken(), bind(api.IssueLabelsOption{}), repo.AddIssueLabels).
  609. Put(reqToken(), bind(api.IssueLabelsOption{}), repo.ReplaceIssueLabels).
  610. Delete(reqToken(), repo.ClearIssueLabels)
  611. m.Delete("/:id", reqToken(), repo.DeleteIssueLabel)
  612. })
  613. m.Group("/times", func() {
  614. m.Combo("").Get(repo.ListTrackedTimes).
  615. Post(reqToken(), bind(api.AddTimeOption{}), repo.AddTime)
  616. })
  617. m.Combo("/deadline").Post(reqToken(), bind(api.EditDeadlineOption{}), repo.UpdateIssueDeadline)
  618. m.Group("/stopwatch", func() {
  619. m.Post("/start", reqToken(), repo.StartIssueStopwatch)
  620. m.Post("/stop", reqToken(), repo.StopIssueStopwatch)
  621. })
  622. })
  623. }, mustEnableIssuesOrPulls)
  624. m.Group("/labels", func() {
  625. m.Combo("").Get(repo.ListLabels).
  626. Post(reqToken(), reqRepoWriter(models.UnitTypeIssues, models.UnitTypePullRequests), bind(api.CreateLabelOption{}), repo.CreateLabel)
  627. m.Combo("/:id").Get(repo.GetLabel).
  628. Patch(reqToken(), reqRepoWriter(models.UnitTypeIssues, models.UnitTypePullRequests), bind(api.EditLabelOption{}), repo.EditLabel).
  629. Delete(reqToken(), reqRepoWriter(models.UnitTypeIssues, models.UnitTypePullRequests), repo.DeleteLabel)
  630. })
  631. m.Post("/markdown", bind(api.MarkdownOption{}), misc.Markdown)
  632. m.Post("/markdown/raw", misc.MarkdownRaw)
  633. m.Group("/milestones", func() {
  634. m.Combo("").Get(repo.ListMilestones).
  635. Post(reqToken(), reqRepoWriter(models.UnitTypeIssues, models.UnitTypePullRequests), bind(api.CreateMilestoneOption{}), repo.CreateMilestone)
  636. m.Combo("/:id").Get(repo.GetMilestone).
  637. Patch(reqToken(), reqRepoWriter(models.UnitTypeIssues, models.UnitTypePullRequests), bind(api.EditMilestoneOption{}), repo.EditMilestone).
  638. Delete(reqToken(), reqRepoWriter(models.UnitTypeIssues, models.UnitTypePullRequests), repo.DeleteMilestone)
  639. })
  640. m.Get("/stargazers", repo.ListStargazers)
  641. m.Get("/subscribers", repo.ListSubscribers)
  642. m.Group("/subscription", func() {
  643. m.Get("", user.IsWatching)
  644. m.Put("", reqToken(), user.Watch)
  645. m.Delete("", reqToken(), user.Unwatch)
  646. })
  647. m.Group("/releases", func() {
  648. m.Combo("").Get(repo.ListReleases).
  649. Post(reqToken(), reqRepoWriter(models.UnitTypeReleases), context.ReferencesGitRepo(false), bind(api.CreateReleaseOption{}), repo.CreateRelease)
  650. m.Group("/:id", func() {
  651. m.Combo("").Get(repo.GetRelease).
  652. Patch(reqToken(), reqRepoWriter(models.UnitTypeReleases), context.ReferencesGitRepo(false), bind(api.EditReleaseOption{}), repo.EditRelease).
  653. Delete(reqToken(), reqRepoWriter(models.UnitTypeReleases), repo.DeleteRelease)
  654. m.Group("/assets", func() {
  655. m.Combo("").Get(repo.ListReleaseAttachments).
  656. Post(reqToken(), reqRepoWriter(models.UnitTypeReleases), repo.CreateReleaseAttachment)
  657. m.Combo("/:asset").Get(repo.GetReleaseAttachment).
  658. Patch(reqToken(), reqRepoWriter(models.UnitTypeReleases), bind(api.EditAttachmentOptions{}), repo.EditReleaseAttachment).
  659. Delete(reqToken(), reqRepoWriter(models.UnitTypeReleases), repo.DeleteReleaseAttachment)
  660. })
  661. })
  662. }, reqRepoReader(models.UnitTypeReleases))
  663. m.Post("/mirror-sync", reqToken(), reqRepoWriter(models.UnitTypeCode), repo.MirrorSync)
  664. m.Get("/editorconfig/:filename", context.RepoRef(), reqRepoReader(models.UnitTypeCode), repo.GetEditorconfig)
  665. m.Group("/pulls", func() {
  666. m.Combo("").Get(bind(api.ListPullRequestsOptions{}), repo.ListPullRequests).
  667. Post(reqToken(), mustNotBeArchived, bind(api.CreatePullRequestOption{}), repo.CreatePullRequest)
  668. m.Group("/:index", func() {
  669. m.Combo("").Get(repo.GetPullRequest).
  670. Patch(reqToken(), reqRepoWriter(models.UnitTypePullRequests), bind(api.EditPullRequestOption{}), repo.EditPullRequest)
  671. m.Combo("/merge").Get(repo.IsPullRequestMerged).
  672. Post(reqToken(), mustNotBeArchived, reqRepoWriter(models.UnitTypePullRequests), bind(auth.MergePullRequestForm{}), repo.MergePullRequest)
  673. })
  674. }, mustAllowPulls, reqRepoReader(models.UnitTypeCode), context.ReferencesGitRepo(false))
  675. m.Group("/statuses", func() {
  676. m.Combo("/:sha").Get(repo.GetCommitStatuses).
  677. Post(reqToken(), bind(api.CreateStatusOption{}), repo.NewCommitStatus)
  678. }, reqRepoReader(models.UnitTypeCode))
  679. m.Group("/commits/:ref", func() {
  680. m.Get("/status", repo.GetCombinedCommitStatusByRef)
  681. m.Get("/statuses", repo.GetCommitStatusesByRef)
  682. }, reqRepoReader(models.UnitTypeCode))
  683. m.Group("/git", func() {
  684. m.Group("/commits", func() {
  685. m.Get("/:sha", repo.GetSingleCommit)
  686. })
  687. m.Get("/refs", repo.GetGitAllRefs)
  688. m.Get("/refs/*", repo.GetGitRefs)
  689. m.Get("/trees/:sha", context.RepoRef(), repo.GetTree)
  690. m.Get("/blobs/:sha", context.RepoRef(), repo.GetBlob)
  691. }, reqRepoReader(models.UnitTypeCode))
  692. m.Group("/contents", func() {
  693. m.Get("/*", repo.GetFileContents)
  694. m.Group("/*", func() {
  695. m.Post("", bind(api.CreateFileOptions{}), repo.CreateFile)
  696. m.Put("", bind(api.UpdateFileOptions{}), repo.UpdateFile)
  697. m.Delete("", bind(api.DeleteFileOptions{}), repo.DeleteFile)
  698. }, reqRepoWriter(models.UnitTypeCode), reqToken())
  699. }, reqRepoReader(models.UnitTypeCode))
  700. }, repoAssignment())
  701. })
  702. // Organizations
  703. m.Get("/user/orgs", reqToken(), org.ListMyOrgs)
  704. m.Get("/users/:username/orgs", org.ListUserOrgs)
  705. m.Post("/orgs", reqToken(), bind(api.CreateOrgOption{}), org.Create)
  706. m.Group("/orgs/:orgname", func() {
  707. m.Get("/repos", user.ListOrgRepos)
  708. m.Combo("").Get(org.Get).
  709. Patch(reqToken(), reqOrgOwnership(), bind(api.EditOrgOption{}), org.Edit).
  710. Delete(reqToken(), reqOrgOwnership(), org.Delete)
  711. m.Group("/members", func() {
  712. m.Get("", org.ListMembers)
  713. m.Combo("/:username").Get(org.IsMember).
  714. Delete(reqToken(), reqOrgOwnership(), org.DeleteMember)
  715. })
  716. m.Group("/public_members", func() {
  717. m.Get("", org.ListPublicMembers)
  718. m.Combo("/:username").Get(org.IsPublicMember).
  719. Put(reqToken(), reqOrgMembership(), org.PublicizeMember).
  720. Delete(reqToken(), reqOrgMembership(), org.ConcealMember)
  721. })
  722. m.Combo("/teams", reqToken(), reqOrgMembership()).Get(org.ListTeams).
  723. Post(bind(api.CreateTeamOption{}), org.CreateTeam)
  724. m.Group("/hooks", func() {
  725. m.Combo("").Get(org.ListHooks).
  726. Post(bind(api.CreateHookOption{}), org.CreateHook)
  727. m.Combo("/:id").Get(org.GetHook).
  728. Patch(reqOrgOwnership(), bind(api.EditHookOption{}), org.EditHook).
  729. Delete(reqOrgOwnership(), org.DeleteHook)
  730. }, reqToken(), reqOrgMembership())
  731. }, orgAssignment(true))
  732. m.Group("/teams/:teamid", func() {
  733. m.Combo("").Get(org.GetTeam).
  734. Patch(reqOrgOwnership(), bind(api.EditTeamOption{}), org.EditTeam).
  735. Delete(reqOrgOwnership(), org.DeleteTeam)
  736. m.Group("/members", func() {
  737. m.Get("", org.GetTeamMembers)
  738. m.Combo("/:username").
  739. Get(org.GetTeamMember).
  740. Put(reqOrgOwnership(), org.AddTeamMember).
  741. Delete(reqOrgOwnership(), org.RemoveTeamMember)
  742. })
  743. m.Group("/repos", func() {
  744. m.Get("", org.GetTeamRepos)
  745. m.Combo("/:orgname/:reponame").
  746. Put(org.AddTeamRepository).
  747. Delete(org.RemoveTeamRepository)
  748. })
  749. }, orgAssignment(false, true), reqToken(), reqTeamMembership())
  750. m.Any("/*", func(ctx *context.APIContext) {
  751. ctx.NotFound()
  752. })
  753. m.Group("/admin", func() {
  754. m.Get("/orgs", admin.GetAllOrgs)
  755. m.Group("/users", func() {
  756. m.Get("", admin.GetAllUsers)
  757. m.Post("", bind(api.CreateUserOption{}), admin.CreateUser)
  758. m.Group("/:username", func() {
  759. m.Combo("").Patch(bind(api.EditUserOption{}), admin.EditUser).
  760. Delete(admin.DeleteUser)
  761. m.Group("/keys", func() {
  762. m.Post("", bind(api.CreateKeyOption{}), admin.CreatePublicKey)
  763. m.Delete("/:id", admin.DeleteUserPublicKey)
  764. })
  765. m.Get("/orgs", org.ListUserOrgs)
  766. m.Post("/orgs", bind(api.CreateOrgOption{}), admin.CreateOrg)
  767. m.Post("/repos", bind(api.CreateRepoOption{}), admin.CreateRepo)
  768. })
  769. })
  770. }, reqToken(), reqSiteAdmin())
  771. m.Group("/topics", func() {
  772. m.Get("/search", repo.TopicSearch)
  773. })
  774. }, context.APIContexter(), sudo())
  775. }