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.

repo.go 25 kB

4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
5 years ago
Template Repositories (#8768) * Start work on templates Signed-off-by: jolheiser <john.olheiser@gmail.com> * Continue work Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix IsTemplate vs IsGenerated Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix tabs vs spaces * Tabs vs Spaces * Add templates to API & start adding tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix integration tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Remove unused User Signed-off-by: jolheiser <john.olheiser@gmail.com> * Move template tests to existing repos Signed-off-by: jolheiser <john.olheiser@gmail.com> * Minor re-check updates and cleanup Signed-off-by: jolheiser <john.olheiser@gmail.com> * make fmt Signed-off-by: jolheiser <john.olheiser@gmail.com> * Test cleanup Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix optionalbool Signed-off-by: jolheiser <john.olheiser@gmail.com> * make fmt Signed-off-by: jolheiser <john.olheiser@gmail.com> * Test fixes and icon change Signed-off-by: jolheiser <john.olheiser@gmail.com> * Add new user and repo for tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix tests (finally) Signed-off-by: jolheiser <john.olheiser@gmail.com> * Update meta repo with env variables Signed-off-by: jolheiser <john.olheiser@gmail.com> * Move generation to create page Combine with repo create template Modify API search to prioritize owner for repo Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix tests and coverage Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix swagger and JS lint Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix API searching for own private repos Signed-off-by: jolheiser <john.olheiser@gmail.com> * Change wording Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix repo search test. User had a private repo that didn't show up Signed-off-by: jolheiser <john.olheiser@gmail.com> * Another search test fix Signed-off-by: jolheiser <john.olheiser@gmail.com> * Clarify git content Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Feedback updates Signed-off-by: jolheiser <john.olheiser@gmail.com> * Add topics WIP Signed-off-by: jolheiser <john.olheiser@gmail.com> * Finish adding topics Signed-off-by: jolheiser <john.olheiser@gmail.com> * Update locale Signed-off-by: jolheiser <john.olheiser@gmail.com>
5 years ago
Template Repositories (#8768) * Start work on templates Signed-off-by: jolheiser <john.olheiser@gmail.com> * Continue work Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix IsTemplate vs IsGenerated Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix tabs vs spaces * Tabs vs Spaces * Add templates to API & start adding tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix integration tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Remove unused User Signed-off-by: jolheiser <john.olheiser@gmail.com> * Move template tests to existing repos Signed-off-by: jolheiser <john.olheiser@gmail.com> * Minor re-check updates and cleanup Signed-off-by: jolheiser <john.olheiser@gmail.com> * make fmt Signed-off-by: jolheiser <john.olheiser@gmail.com> * Test cleanup Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix optionalbool Signed-off-by: jolheiser <john.olheiser@gmail.com> * make fmt Signed-off-by: jolheiser <john.olheiser@gmail.com> * Test fixes and icon change Signed-off-by: jolheiser <john.olheiser@gmail.com> * Add new user and repo for tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix tests (finally) Signed-off-by: jolheiser <john.olheiser@gmail.com> * Update meta repo with env variables Signed-off-by: jolheiser <john.olheiser@gmail.com> * Move generation to create page Combine with repo create template Modify API search to prioritize owner for repo Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix tests and coverage Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix swagger and JS lint Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix API searching for own private repos Signed-off-by: jolheiser <john.olheiser@gmail.com> * Change wording Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix repo search test. User had a private repo that didn't show up Signed-off-by: jolheiser <john.olheiser@gmail.com> * Another search test fix Signed-off-by: jolheiser <john.olheiser@gmail.com> * Clarify git content Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Feedback updates Signed-off-by: jolheiser <john.olheiser@gmail.com> * Add topics WIP Signed-off-by: jolheiser <john.olheiser@gmail.com> * Finish adding topics Signed-off-by: jolheiser <john.olheiser@gmail.com> * Update locale Signed-off-by: jolheiser <john.olheiser@gmail.com>
5 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
5 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
5 years ago
Restricted users (#6274) * Restricted users (#4334): initial implementation * Add User.IsRestricted & UI to edit it * Pass user object instead of user id to places where IsRestricted flag matters * Restricted users: maintain access rows for all referenced repos (incl public) * Take logged in user & IsRestricted flag into account in org/repo listings, searches and accesses * Add basic repo access tests for restricted users Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Mention restricted users in the faq Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert unnecessary change `.isUserPartOfOrg` -> `.IsUserPartOfOrg` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Remove unnecessary `org.IsOrganization()` call Signed-off-by: Manush Dodunekov <manush@stendahls.se> * Revert to an `int64` keyed `accessMap` * Add type `userAccess` * Add convenience func updateUserAccess() * Turn accessMap into a `map[int64]userAccess` Signed-off-by: Manush Dodunekov <manush@stendahls.se> * or even better: `map[int64]*userAccess` * updateUserAccess(): use tighter syntax as suggested by lafriks * even tighter * Avoid extra loop * Don't disclose limited orgs to unauthenticated users * Don't assume block only applies to orgs * Use an array of `VisibleType` for filtering * fix yet another thinko * Ok - no need for u * Revert "Ok - no need for u" This reverts commit 5c3e886aabd5acd997a3b35687d322439732c200. Co-authored-by: Antoine GIRARD <sapk@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv>
5 years ago
Template Repositories (#8768) * Start work on templates Signed-off-by: jolheiser <john.olheiser@gmail.com> * Continue work Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix IsTemplate vs IsGenerated Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix tabs vs spaces * Tabs vs Spaces * Add templates to API & start adding tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix integration tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Remove unused User Signed-off-by: jolheiser <john.olheiser@gmail.com> * Move template tests to existing repos Signed-off-by: jolheiser <john.olheiser@gmail.com> * Minor re-check updates and cleanup Signed-off-by: jolheiser <john.olheiser@gmail.com> * make fmt Signed-off-by: jolheiser <john.olheiser@gmail.com> * Test cleanup Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix optionalbool Signed-off-by: jolheiser <john.olheiser@gmail.com> * make fmt Signed-off-by: jolheiser <john.olheiser@gmail.com> * Test fixes and icon change Signed-off-by: jolheiser <john.olheiser@gmail.com> * Add new user and repo for tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix tests (finally) Signed-off-by: jolheiser <john.olheiser@gmail.com> * Update meta repo with env variables Signed-off-by: jolheiser <john.olheiser@gmail.com> * Move generation to create page Combine with repo create template Modify API search to prioritize owner for repo Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix tests and coverage Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix swagger and JS lint Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix API searching for own private repos Signed-off-by: jolheiser <john.olheiser@gmail.com> * Change wording Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix repo search test. User had a private repo that didn't show up Signed-off-by: jolheiser <john.olheiser@gmail.com> * Another search test fix Signed-off-by: jolheiser <john.olheiser@gmail.com> * Clarify git content Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Feedback updates Signed-off-by: jolheiser <john.olheiser@gmail.com> * Add topics WIP Signed-off-by: jolheiser <john.olheiser@gmail.com> * Finish adding topics Signed-off-by: jolheiser <john.olheiser@gmail.com> * Update locale Signed-off-by: jolheiser <john.olheiser@gmail.com>
5 years ago
Template Repositories (#8768) * Start work on templates Signed-off-by: jolheiser <john.olheiser@gmail.com> * Continue work Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix IsTemplate vs IsGenerated Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix tabs vs spaces * Tabs vs Spaces * Add templates to API & start adding tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix integration tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Remove unused User Signed-off-by: jolheiser <john.olheiser@gmail.com> * Move template tests to existing repos Signed-off-by: jolheiser <john.olheiser@gmail.com> * Minor re-check updates and cleanup Signed-off-by: jolheiser <john.olheiser@gmail.com> * make fmt Signed-off-by: jolheiser <john.olheiser@gmail.com> * Test cleanup Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix optionalbool Signed-off-by: jolheiser <john.olheiser@gmail.com> * make fmt Signed-off-by: jolheiser <john.olheiser@gmail.com> * Test fixes and icon change Signed-off-by: jolheiser <john.olheiser@gmail.com> * Add new user and repo for tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix tests (finally) Signed-off-by: jolheiser <john.olheiser@gmail.com> * Update meta repo with env variables Signed-off-by: jolheiser <john.olheiser@gmail.com> * Move generation to create page Combine with repo create template Modify API search to prioritize owner for repo Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix tests and coverage Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix swagger and JS lint Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix API searching for own private repos Signed-off-by: jolheiser <john.olheiser@gmail.com> * Change wording Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix repo search test. User had a private repo that didn't show up Signed-off-by: jolheiser <john.olheiser@gmail.com> * Another search test fix Signed-off-by: jolheiser <john.olheiser@gmail.com> * Clarify git content Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Feedback updates Signed-off-by: jolheiser <john.olheiser@gmail.com> * Add topics WIP Signed-off-by: jolheiser <john.olheiser@gmail.com> * Finish adding topics Signed-off-by: jolheiser <john.olheiser@gmail.com> * Update locale Signed-off-by: jolheiser <john.olheiser@gmail.com>
5 years ago
Template Repositories (#8768) * Start work on templates Signed-off-by: jolheiser <john.olheiser@gmail.com> * Continue work Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix IsTemplate vs IsGenerated Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix tabs vs spaces * Tabs vs Spaces * Add templates to API & start adding tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix integration tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Remove unused User Signed-off-by: jolheiser <john.olheiser@gmail.com> * Move template tests to existing repos Signed-off-by: jolheiser <john.olheiser@gmail.com> * Minor re-check updates and cleanup Signed-off-by: jolheiser <john.olheiser@gmail.com> * make fmt Signed-off-by: jolheiser <john.olheiser@gmail.com> * Test cleanup Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix optionalbool Signed-off-by: jolheiser <john.olheiser@gmail.com> * make fmt Signed-off-by: jolheiser <john.olheiser@gmail.com> * Test fixes and icon change Signed-off-by: jolheiser <john.olheiser@gmail.com> * Add new user and repo for tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix tests (finally) Signed-off-by: jolheiser <john.olheiser@gmail.com> * Update meta repo with env variables Signed-off-by: jolheiser <john.olheiser@gmail.com> * Move generation to create page Combine with repo create template Modify API search to prioritize owner for repo Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix tests and coverage Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix swagger and JS lint Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix API searching for own private repos Signed-off-by: jolheiser <john.olheiser@gmail.com> * Change wording Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix repo search test. User had a private repo that didn't show up Signed-off-by: jolheiser <john.olheiser@gmail.com> * Another search test fix Signed-off-by: jolheiser <john.olheiser@gmail.com> * Clarify git content Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Feedback updates Signed-off-by: jolheiser <john.olheiser@gmail.com> * Add topics WIP Signed-off-by: jolheiser <john.olheiser@gmail.com> * Finish adding topics Signed-off-by: jolheiser <john.olheiser@gmail.com> * Update locale Signed-off-by: jolheiser <john.olheiser@gmail.com>
5 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
5 years ago
10 years ago
Template Repositories (#8768) * Start work on templates Signed-off-by: jolheiser <john.olheiser@gmail.com> * Continue work Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix IsTemplate vs IsGenerated Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix tabs vs spaces * Tabs vs Spaces * Add templates to API & start adding tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix integration tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Remove unused User Signed-off-by: jolheiser <john.olheiser@gmail.com> * Move template tests to existing repos Signed-off-by: jolheiser <john.olheiser@gmail.com> * Minor re-check updates and cleanup Signed-off-by: jolheiser <john.olheiser@gmail.com> * make fmt Signed-off-by: jolheiser <john.olheiser@gmail.com> * Test cleanup Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix optionalbool Signed-off-by: jolheiser <john.olheiser@gmail.com> * make fmt Signed-off-by: jolheiser <john.olheiser@gmail.com> * Test fixes and icon change Signed-off-by: jolheiser <john.olheiser@gmail.com> * Add new user and repo for tests Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix tests (finally) Signed-off-by: jolheiser <john.olheiser@gmail.com> * Update meta repo with env variables Signed-off-by: jolheiser <john.olheiser@gmail.com> * Move generation to create page Combine with repo create template Modify API search to prioritize owner for repo Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix tests and coverage Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix swagger and JS lint Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix API searching for own private repos Signed-off-by: jolheiser <john.olheiser@gmail.com> * Change wording Signed-off-by: jolheiser <john.olheiser@gmail.com> * Fix repo search test. User had a private repo that didn't show up Signed-off-by: jolheiser <john.olheiser@gmail.com> * Another search test fix Signed-off-by: jolheiser <john.olheiser@gmail.com> * Clarify git content Co-Authored-By: guillep2k <18600385+guillep2k@users.noreply.github.com> * Feedback updates Signed-off-by: jolheiser <john.olheiser@gmail.com> * Add topics WIP Signed-off-by: jolheiser <john.olheiser@gmail.com> * Finish adding topics Signed-off-by: jolheiser <john.olheiser@gmail.com> * Update locale Signed-off-by: jolheiser <john.olheiser@gmail.com>
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 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 repo
  6. import (
  7. "fmt"
  8. "net/http"
  9. "strings"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/git"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/setting"
  15. api "code.gitea.io/gitea/modules/structs"
  16. "code.gitea.io/gitea/modules/util"
  17. "code.gitea.io/gitea/modules/validation"
  18. "code.gitea.io/gitea/routers/api/v1/utils"
  19. repo_service "code.gitea.io/gitea/services/repository"
  20. )
  21. var searchOrderByMap = map[string]map[string]models.SearchOrderBy{
  22. "asc": {
  23. "alpha": models.SearchOrderByAlphabetically,
  24. "created": models.SearchOrderByOldest,
  25. "updated": models.SearchOrderByLeastUpdated,
  26. "size": models.SearchOrderBySize,
  27. "id": models.SearchOrderByID,
  28. },
  29. "desc": {
  30. "alpha": models.SearchOrderByAlphabeticallyReverse,
  31. "created": models.SearchOrderByNewest,
  32. "updated": models.SearchOrderByRecentUpdated,
  33. "size": models.SearchOrderBySizeReverse,
  34. "id": models.SearchOrderByIDReverse,
  35. },
  36. }
  37. // Search repositories via options
  38. func Search(ctx *context.APIContext) {
  39. // swagger:operation GET /repos/search repository repoSearch
  40. // ---
  41. // summary: Search for repositories
  42. // produces:
  43. // - application/json
  44. // parameters:
  45. // - name: q
  46. // in: query
  47. // description: keyword
  48. // type: string
  49. // - name: topic
  50. // in: query
  51. // description: Limit search to repositories with keyword as topic
  52. // type: boolean
  53. // - name: includeDesc
  54. // in: query
  55. // description: include search of keyword within repository description
  56. // type: boolean
  57. // - name: uid
  58. // in: query
  59. // description: search only for repos that the user with the given id owns or contributes to
  60. // type: integer
  61. // format: int64
  62. // - name: priority_owner_id
  63. // in: query
  64. // description: repo owner to prioritize in the results
  65. // type: integer
  66. // format: int64
  67. // - name: starredBy
  68. // in: query
  69. // description: search only for repos that the user with the given id has starred
  70. // type: integer
  71. // format: int64
  72. // - name: private
  73. // in: query
  74. // description: include private repositories this user has access to (defaults to true)
  75. // type: boolean
  76. // - name: onlyPrivate
  77. // in: query
  78. // description: only include private repositories this user has access to (defaults to false)
  79. // type: boolean
  80. // - name: template
  81. // in: query
  82. // description: include template repositories this user has access to (defaults to true)
  83. // type: boolean
  84. // - name: archived
  85. // in: query
  86. // description: show only archived, non-archived or all repositories (defaults to all)
  87. // type: boolean
  88. // - name: mode
  89. // in: query
  90. // description: type of repository to search for. Supported values are
  91. // "fork", "source", "mirror" and "collaborative"
  92. // type: string
  93. // - name: exclusive
  94. // in: query
  95. // description: if `uid` is given, search only for repos that the user owns
  96. // type: boolean
  97. // - name: sort
  98. // in: query
  99. // description: sort repos by attribute. Supported values are
  100. // "alpha", "created", "updated", "size", and "id".
  101. // Default is "alpha"
  102. // type: string
  103. // - name: order
  104. // in: query
  105. // description: sort order, either "asc" (ascending) or "desc" (descending).
  106. // Default is "asc", ignored if "sort" is not specified.
  107. // type: string
  108. // - name: page
  109. // in: query
  110. // description: page number of results to return (1-based)
  111. // type: integer
  112. // - name: limit
  113. // in: query
  114. // description: page size of results, maximum page size is 50
  115. // type: integer
  116. // responses:
  117. // "200":
  118. // "$ref": "#/responses/SearchResults"
  119. // "422":
  120. // "$ref": "#/responses/validationError"
  121. opts := &models.SearchRepoOptions{
  122. ListOptions: utils.GetListOptions(ctx),
  123. Actor: ctx.User,
  124. Keyword: strings.Trim(ctx.Query("q"), " "),
  125. OwnerID: ctx.QueryInt64("uid"),
  126. PriorityOwnerID: ctx.QueryInt64("priority_owner_id"),
  127. TopicOnly: ctx.QueryBool("topic"),
  128. Collaborate: util.OptionalBoolNone,
  129. Private: ctx.IsSigned && (ctx.Query("private") == "" || ctx.QueryBool("private")),
  130. OnlyPrivate: ctx.IsSigned && ctx.QueryBool("onlyPrivate"),
  131. Template: util.OptionalBoolNone,
  132. StarredByID: ctx.QueryInt64("starredBy"),
  133. IncludeDescription: ctx.QueryBool("includeDesc"),
  134. }
  135. if ctx.Query("template") != "" {
  136. opts.Template = util.OptionalBoolOf(ctx.QueryBool("template"))
  137. }
  138. if ctx.QueryBool("exclusive") {
  139. opts.Collaborate = util.OptionalBoolFalse
  140. }
  141. var mode = ctx.Query("mode")
  142. switch mode {
  143. case "source":
  144. opts.Fork = util.OptionalBoolFalse
  145. opts.Mirror = util.OptionalBoolFalse
  146. case "fork":
  147. opts.Fork = util.OptionalBoolTrue
  148. case "mirror":
  149. opts.Mirror = util.OptionalBoolTrue
  150. case "collaborative":
  151. opts.Mirror = util.OptionalBoolFalse
  152. opts.Collaborate = util.OptionalBoolTrue
  153. case "":
  154. default:
  155. ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("Invalid search mode: \"%s\"", mode))
  156. return
  157. }
  158. if ctx.Query("archived") != "" {
  159. opts.Archived = util.OptionalBoolOf(ctx.QueryBool("archived"))
  160. }
  161. var sortMode = ctx.Query("sort")
  162. if len(sortMode) > 0 {
  163. var sortOrder = ctx.Query("order")
  164. if len(sortOrder) == 0 {
  165. sortOrder = "asc"
  166. }
  167. if searchModeMap, ok := searchOrderByMap[sortOrder]; ok {
  168. if orderBy, ok := searchModeMap[sortMode]; ok {
  169. opts.OrderBy = orderBy
  170. } else {
  171. ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("Invalid sort mode: \"%s\"", sortMode))
  172. return
  173. }
  174. } else {
  175. ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("Invalid sort order: \"%s\"", sortOrder))
  176. return
  177. }
  178. }
  179. var err error
  180. repos, count, err := models.SearchRepository(opts)
  181. if err != nil {
  182. ctx.JSON(http.StatusInternalServerError, api.SearchError{
  183. OK: false,
  184. Error: err.Error(),
  185. })
  186. return
  187. }
  188. results := make([]*api.Repository, len(repos))
  189. for i, repo := range repos {
  190. if err = repo.GetOwner(); err != nil {
  191. ctx.JSON(http.StatusInternalServerError, api.SearchError{
  192. OK: false,
  193. Error: err.Error(),
  194. })
  195. return
  196. }
  197. accessMode, err := models.AccessLevel(ctx.User, repo)
  198. if err != nil {
  199. ctx.JSON(http.StatusInternalServerError, api.SearchError{
  200. OK: false,
  201. Error: err.Error(),
  202. })
  203. }
  204. results[i] = repo.APIFormat(accessMode)
  205. }
  206. ctx.SetLinkHeader(int(count), opts.PageSize)
  207. ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
  208. ctx.JSON(http.StatusOK, api.SearchResults{
  209. OK: true,
  210. Data: results,
  211. })
  212. }
  213. // CreateUserRepo create a repository for a user
  214. func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateRepoOption) {
  215. if opt.AutoInit && opt.Readme == "" {
  216. opt.Readme = "Default"
  217. }
  218. repo, err := repo_service.CreateRepository(ctx.User, owner, models.CreateRepoOptions{
  219. Name: opt.Name,
  220. Description: opt.Description,
  221. IssueLabels: opt.IssueLabels,
  222. Gitignores: opt.Gitignores,
  223. License: opt.License,
  224. Readme: opt.Readme,
  225. IsPrivate: opt.Private,
  226. AutoInit: opt.AutoInit,
  227. DefaultBranch: opt.DefaultBranch,
  228. })
  229. if err != nil {
  230. if models.IsErrRepoAlreadyExist(err) {
  231. ctx.Error(http.StatusConflict, "", "The repository with the same name already exists.")
  232. } else if models.IsErrNameReserved(err) ||
  233. models.IsErrNamePatternNotAllowed(err) {
  234. ctx.Error(http.StatusUnprocessableEntity, "", err)
  235. } else {
  236. ctx.Error(http.StatusInternalServerError, "CreateRepository", err)
  237. }
  238. return
  239. }
  240. ctx.JSON(http.StatusCreated, repo.APIFormat(models.AccessModeOwner))
  241. }
  242. // Create one repository of mine
  243. func Create(ctx *context.APIContext, opt api.CreateRepoOption) {
  244. // swagger:operation POST /user/repos repository user createCurrentUserRepo
  245. // ---
  246. // summary: Create a repository
  247. // consumes:
  248. // - application/json
  249. // produces:
  250. // - application/json
  251. // parameters:
  252. // - name: body
  253. // in: body
  254. // schema:
  255. // "$ref": "#/definitions/CreateRepoOption"
  256. // responses:
  257. // "201":
  258. // "$ref": "#/responses/Repository"
  259. // "409":
  260. // description: The repository with the same name already exists.
  261. // "422":
  262. // "$ref": "#/responses/validationError"
  263. if ctx.User.IsOrganization() {
  264. // Shouldn't reach this condition, but just in case.
  265. ctx.Error(http.StatusUnprocessableEntity, "", "not allowed creating repository for organization")
  266. return
  267. }
  268. CreateUserRepo(ctx, ctx.User, opt)
  269. }
  270. // CreateOrgRepoDeprecated create one repository of the organization
  271. func CreateOrgRepoDeprecated(ctx *context.APIContext, opt api.CreateRepoOption) {
  272. // swagger:operation POST /org/{org}/repos organization createOrgRepoDeprecated
  273. // ---
  274. // summary: Create a repository in an organization
  275. // deprecated: true
  276. // consumes:
  277. // - application/json
  278. // produces:
  279. // - application/json
  280. // parameters:
  281. // - name: org
  282. // in: path
  283. // description: name of organization
  284. // type: string
  285. // required: true
  286. // - name: body
  287. // in: body
  288. // schema:
  289. // "$ref": "#/definitions/CreateRepoOption"
  290. // responses:
  291. // "201":
  292. // "$ref": "#/responses/Repository"
  293. // "422":
  294. // "$ref": "#/responses/validationError"
  295. // "403":
  296. // "$ref": "#/responses/forbidden"
  297. CreateOrgRepo(ctx, opt)
  298. }
  299. // CreateOrgRepo create one repository of the organization
  300. func CreateOrgRepo(ctx *context.APIContext, opt api.CreateRepoOption) {
  301. // swagger:operation POST /orgs/{org}/repos organization createOrgRepo
  302. // ---
  303. // summary: Create a repository in an organization
  304. // consumes:
  305. // - application/json
  306. // produces:
  307. // - application/json
  308. // parameters:
  309. // - name: org
  310. // in: path
  311. // description: name of organization
  312. // type: string
  313. // required: true
  314. // - name: body
  315. // in: body
  316. // schema:
  317. // "$ref": "#/definitions/CreateRepoOption"
  318. // responses:
  319. // "201":
  320. // "$ref": "#/responses/Repository"
  321. // "404":
  322. // "$ref": "#/responses/notFound"
  323. // "403":
  324. // "$ref": "#/responses/forbidden"
  325. org, err := models.GetOrgByName(ctx.Params(":org"))
  326. if err != nil {
  327. if models.IsErrOrgNotExist(err) {
  328. ctx.Error(http.StatusUnprocessableEntity, "", err)
  329. } else {
  330. ctx.Error(http.StatusInternalServerError, "GetOrgByName", err)
  331. }
  332. return
  333. }
  334. if !models.HasOrgVisible(org, ctx.User) {
  335. ctx.NotFound("HasOrgVisible", nil)
  336. return
  337. }
  338. if !ctx.User.IsAdmin {
  339. canCreate, err := org.CanCreateOrgRepo(ctx.User.ID)
  340. if err != nil {
  341. ctx.ServerError("CanCreateOrgRepo", err)
  342. return
  343. } else if !canCreate {
  344. ctx.Error(http.StatusForbidden, "", "Given user is not allowed to create repository in organization.")
  345. return
  346. }
  347. }
  348. CreateUserRepo(ctx, org, opt)
  349. }
  350. // Get one repository
  351. func Get(ctx *context.APIContext) {
  352. // swagger:operation GET /repos/{owner}/{repo} repository repoGet
  353. // ---
  354. // summary: Get a repository
  355. // produces:
  356. // - application/json
  357. // parameters:
  358. // - name: owner
  359. // in: path
  360. // description: owner of the repo
  361. // type: string
  362. // required: true
  363. // - name: repo
  364. // in: path
  365. // description: name of the repo
  366. // type: string
  367. // required: true
  368. // responses:
  369. // "200":
  370. // "$ref": "#/responses/Repository"
  371. ctx.JSON(http.StatusOK, ctx.Repo.Repository.APIFormat(ctx.Repo.AccessMode))
  372. }
  373. // GetByID returns a single Repository
  374. func GetByID(ctx *context.APIContext) {
  375. // swagger:operation GET /repositories/{id} repository repoGetByID
  376. // ---
  377. // summary: Get a repository by id
  378. // produces:
  379. // - application/json
  380. // parameters:
  381. // - name: id
  382. // in: path
  383. // description: id of the repo to get
  384. // type: integer
  385. // format: int64
  386. // required: true
  387. // responses:
  388. // "200":
  389. // "$ref": "#/responses/Repository"
  390. repo, err := models.GetRepositoryByID(ctx.ParamsInt64(":id"))
  391. if err != nil {
  392. if models.IsErrRepoNotExist(err) {
  393. ctx.NotFound()
  394. } else {
  395. ctx.Error(http.StatusInternalServerError, "GetRepositoryByID", err)
  396. }
  397. return
  398. }
  399. perm, err := models.GetUserRepoPermission(repo, ctx.User)
  400. if err != nil {
  401. ctx.Error(http.StatusInternalServerError, "AccessLevel", err)
  402. return
  403. } else if !perm.HasAccess() {
  404. ctx.NotFound()
  405. return
  406. }
  407. ctx.JSON(http.StatusOK, repo.APIFormat(perm.AccessMode))
  408. }
  409. // Edit edit repository properties
  410. func Edit(ctx *context.APIContext, opts api.EditRepoOption) {
  411. // swagger:operation PATCH /repos/{owner}/{repo} repository repoEdit
  412. // ---
  413. // summary: Edit a repository's properties. Only fields that are set will be changed.
  414. // produces:
  415. // - application/json
  416. // parameters:
  417. // - name: owner
  418. // in: path
  419. // description: owner of the repo to edit
  420. // type: string
  421. // required: true
  422. // - name: repo
  423. // in: path
  424. // description: name of the repo to edit
  425. // type: string
  426. // required: true
  427. // required: true
  428. // - name: body
  429. // in: body
  430. // description: "Properties of a repo that you can edit"
  431. // schema:
  432. // "$ref": "#/definitions/EditRepoOption"
  433. // responses:
  434. // "200":
  435. // "$ref": "#/responses/Repository"
  436. // "403":
  437. // "$ref": "#/responses/forbidden"
  438. // "422":
  439. // "$ref": "#/responses/validationError"
  440. if err := updateBasicProperties(ctx, opts); err != nil {
  441. return
  442. }
  443. if err := updateRepoUnits(ctx, opts); err != nil {
  444. return
  445. }
  446. if opts.Archived != nil {
  447. if err := updateRepoArchivedState(ctx, opts); err != nil {
  448. return
  449. }
  450. }
  451. ctx.JSON(http.StatusOK, ctx.Repo.Repository.APIFormat(ctx.Repo.AccessMode))
  452. }
  453. // updateBasicProperties updates the basic properties of a repo: Name, Description, Website and Visibility
  454. func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) error {
  455. owner := ctx.Repo.Owner
  456. repo := ctx.Repo.Repository
  457. newRepoName := repo.Name
  458. if opts.Name != nil {
  459. newRepoName = *opts.Name
  460. }
  461. // Check if repository name has been changed and not just a case change
  462. if repo.LowerName != strings.ToLower(newRepoName) {
  463. if err := repo_service.ChangeRepositoryName(ctx.User, repo, newRepoName); err != nil {
  464. switch {
  465. case models.IsErrRepoAlreadyExist(err):
  466. ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("repo name is already taken [name: %s]", newRepoName), err)
  467. case models.IsErrNameReserved(err):
  468. ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("repo name is reserved [name: %s]", newRepoName), err)
  469. case models.IsErrNamePatternNotAllowed(err):
  470. ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("repo name's pattern is not allowed [name: %s, pattern: %s]", newRepoName, err.(models.ErrNamePatternNotAllowed).Pattern), err)
  471. default:
  472. ctx.Error(http.StatusUnprocessableEntity, "ChangeRepositoryName", err)
  473. }
  474. return err
  475. }
  476. log.Trace("Repository name changed: %s/%s -> %s", ctx.Repo.Owner.Name, repo.Name, newRepoName)
  477. }
  478. // Update the name in the repo object for the response
  479. repo.Name = newRepoName
  480. repo.LowerName = strings.ToLower(newRepoName)
  481. if opts.Description != nil {
  482. repo.Description = *opts.Description
  483. }
  484. if opts.Website != nil {
  485. repo.Website = *opts.Website
  486. }
  487. visibilityChanged := false
  488. if opts.Private != nil {
  489. // Visibility of forked repository is forced sync with base repository.
  490. if repo.IsFork {
  491. *opts.Private = repo.BaseRepo.IsPrivate
  492. }
  493. visibilityChanged = repo.IsPrivate != *opts.Private
  494. // when ForcePrivate enabled, you could change public repo to private, but only admin users can change private to public
  495. if visibilityChanged && setting.Repository.ForcePrivate && !*opts.Private && !ctx.User.IsAdmin {
  496. err := fmt.Errorf("cannot change private repository to public")
  497. ctx.Error(http.StatusUnprocessableEntity, "Force Private enabled", err)
  498. return err
  499. }
  500. repo.IsPrivate = *opts.Private
  501. }
  502. if opts.Template != nil {
  503. repo.IsTemplate = *opts.Template
  504. }
  505. // Default branch only updated if changed and exist
  506. if opts.DefaultBranch != nil && repo.DefaultBranch != *opts.DefaultBranch && ctx.Repo.GitRepo.IsBranchExist(*opts.DefaultBranch) {
  507. if err := ctx.Repo.GitRepo.SetDefaultBranch(*opts.DefaultBranch); err != nil {
  508. if !git.IsErrUnsupportedVersion(err) {
  509. ctx.Error(http.StatusInternalServerError, "SetDefaultBranch", err)
  510. return err
  511. }
  512. }
  513. repo.DefaultBranch = *opts.DefaultBranch
  514. }
  515. if err := models.UpdateRepository(repo, visibilityChanged); err != nil {
  516. ctx.Error(http.StatusInternalServerError, "UpdateRepository", err)
  517. return err
  518. }
  519. log.Trace("Repository basic settings updated: %s/%s", owner.Name, repo.Name)
  520. return nil
  521. }
  522. // updateRepoUnits updates repo units: Issue settings, Wiki settings, PR settings
  523. func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
  524. owner := ctx.Repo.Owner
  525. repo := ctx.Repo.Repository
  526. var units []models.RepoUnit
  527. var deleteUnitTypes []models.UnitType
  528. if opts.HasIssues != nil {
  529. if *opts.HasIssues && opts.ExternalTracker != nil && !models.UnitTypeExternalTracker.UnitGlobalDisabled() {
  530. // Check that values are valid
  531. if !validation.IsValidExternalURL(opts.ExternalTracker.ExternalTrackerURL) {
  532. err := fmt.Errorf("External tracker URL not valid")
  533. ctx.Error(http.StatusUnprocessableEntity, "Invalid external tracker URL", err)
  534. return err
  535. }
  536. if len(opts.ExternalTracker.ExternalTrackerFormat) != 0 && !validation.IsValidExternalTrackerURLFormat(opts.ExternalTracker.ExternalTrackerFormat) {
  537. err := fmt.Errorf("External tracker URL format not valid")
  538. ctx.Error(http.StatusUnprocessableEntity, "Invalid external tracker URL format", err)
  539. return err
  540. }
  541. units = append(units, models.RepoUnit{
  542. RepoID: repo.ID,
  543. Type: models.UnitTypeExternalTracker,
  544. Config: &models.ExternalTrackerConfig{
  545. ExternalTrackerURL: opts.ExternalTracker.ExternalTrackerURL,
  546. ExternalTrackerFormat: opts.ExternalTracker.ExternalTrackerFormat,
  547. ExternalTrackerStyle: opts.ExternalTracker.ExternalTrackerStyle,
  548. },
  549. })
  550. deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeIssues)
  551. } else if *opts.HasIssues && opts.ExternalTracker == nil && !models.UnitTypeIssues.UnitGlobalDisabled() {
  552. // Default to built-in tracker
  553. var config *models.IssuesConfig
  554. if opts.InternalTracker != nil {
  555. config = &models.IssuesConfig{
  556. EnableTimetracker: opts.InternalTracker.EnableTimeTracker,
  557. AllowOnlyContributorsToTrackTime: opts.InternalTracker.AllowOnlyContributorsToTrackTime,
  558. EnableDependencies: opts.InternalTracker.EnableIssueDependencies,
  559. }
  560. } else if unit, err := repo.GetUnit(models.UnitTypeIssues); err != nil {
  561. // Unit type doesn't exist so we make a new config file with default values
  562. config = &models.IssuesConfig{
  563. EnableTimetracker: true,
  564. AllowOnlyContributorsToTrackTime: true,
  565. EnableDependencies: true,
  566. }
  567. } else {
  568. config = unit.IssuesConfig()
  569. }
  570. units = append(units, models.RepoUnit{
  571. RepoID: repo.ID,
  572. Type: models.UnitTypeIssues,
  573. Config: config,
  574. })
  575. deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeExternalTracker)
  576. } else if !*opts.HasIssues {
  577. if !models.UnitTypeExternalTracker.UnitGlobalDisabled() {
  578. deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeExternalTracker)
  579. }
  580. if !models.UnitTypeIssues.UnitGlobalDisabled() {
  581. deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeIssues)
  582. }
  583. }
  584. }
  585. if opts.HasWiki != nil {
  586. if *opts.HasWiki && opts.ExternalWiki != nil && !models.UnitTypeExternalWiki.UnitGlobalDisabled() {
  587. // Check that values are valid
  588. if !validation.IsValidExternalURL(opts.ExternalWiki.ExternalWikiURL) {
  589. err := fmt.Errorf("External wiki URL not valid")
  590. ctx.Error(http.StatusUnprocessableEntity, "", "Invalid external wiki URL")
  591. return err
  592. }
  593. units = append(units, models.RepoUnit{
  594. RepoID: repo.ID,
  595. Type: models.UnitTypeExternalWiki,
  596. Config: &models.ExternalWikiConfig{
  597. ExternalWikiURL: opts.ExternalWiki.ExternalWikiURL,
  598. },
  599. })
  600. deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeWiki)
  601. } else if *opts.HasWiki && opts.ExternalWiki == nil && !models.UnitTypeWiki.UnitGlobalDisabled() {
  602. config := &models.UnitConfig{}
  603. units = append(units, models.RepoUnit{
  604. RepoID: repo.ID,
  605. Type: models.UnitTypeWiki,
  606. Config: config,
  607. })
  608. deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeExternalWiki)
  609. } else if !*opts.HasWiki {
  610. if !models.UnitTypeExternalWiki.UnitGlobalDisabled() {
  611. deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeExternalWiki)
  612. }
  613. if !models.UnitTypeWiki.UnitGlobalDisabled() {
  614. deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeWiki)
  615. }
  616. }
  617. }
  618. if opts.HasPullRequests != nil {
  619. if *opts.HasPullRequests && !models.UnitTypePullRequests.UnitGlobalDisabled() {
  620. // We do allow setting individual PR settings through the API, so
  621. // we get the config settings and then set them
  622. // if those settings were provided in the opts.
  623. unit, err := repo.GetUnit(models.UnitTypePullRequests)
  624. var config *models.PullRequestsConfig
  625. if err != nil {
  626. // Unit type doesn't exist so we make a new config file with default values
  627. config = &models.PullRequestsConfig{
  628. IgnoreWhitespaceConflicts: false,
  629. AllowMerge: true,
  630. AllowRebase: true,
  631. AllowRebaseMerge: true,
  632. AllowSquash: true,
  633. }
  634. } else {
  635. config = unit.PullRequestsConfig()
  636. }
  637. if opts.IgnoreWhitespaceConflicts != nil {
  638. config.IgnoreWhitespaceConflicts = *opts.IgnoreWhitespaceConflicts
  639. }
  640. if opts.AllowMerge != nil {
  641. config.AllowMerge = *opts.AllowMerge
  642. }
  643. if opts.AllowRebase != nil {
  644. config.AllowRebase = *opts.AllowRebase
  645. }
  646. if opts.AllowRebaseMerge != nil {
  647. config.AllowRebaseMerge = *opts.AllowRebaseMerge
  648. }
  649. if opts.AllowSquash != nil {
  650. config.AllowSquash = *opts.AllowSquash
  651. }
  652. units = append(units, models.RepoUnit{
  653. RepoID: repo.ID,
  654. Type: models.UnitTypePullRequests,
  655. Config: config,
  656. })
  657. } else if !*opts.HasPullRequests && !models.UnitTypePullRequests.UnitGlobalDisabled() {
  658. deleteUnitTypes = append(deleteUnitTypes, models.UnitTypePullRequests)
  659. }
  660. }
  661. if err := models.UpdateRepositoryUnits(repo, units, deleteUnitTypes); err != nil {
  662. ctx.Error(http.StatusInternalServerError, "UpdateRepositoryUnits", err)
  663. return err
  664. }
  665. log.Trace("Repository advanced settings updated: %s/%s", owner.Name, repo.Name)
  666. return nil
  667. }
  668. // updateRepoArchivedState updates repo's archive state
  669. func updateRepoArchivedState(ctx *context.APIContext, opts api.EditRepoOption) error {
  670. repo := ctx.Repo.Repository
  671. // archive / un-archive
  672. if opts.Archived != nil {
  673. if repo.IsMirror {
  674. err := fmt.Errorf("repo is a mirror, cannot archive/un-archive")
  675. ctx.Error(http.StatusUnprocessableEntity, err.Error(), err)
  676. return err
  677. }
  678. if *opts.Archived {
  679. if err := repo.SetArchiveRepoState(*opts.Archived); err != nil {
  680. log.Error("Tried to archive a repo: %s", err)
  681. ctx.Error(http.StatusInternalServerError, "ArchiveRepoState", err)
  682. return err
  683. }
  684. log.Trace("Repository was archived: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  685. } else {
  686. if err := repo.SetArchiveRepoState(*opts.Archived); err != nil {
  687. log.Error("Tried to un-archive a repo: %s", err)
  688. ctx.Error(http.StatusInternalServerError, "ArchiveRepoState", err)
  689. return err
  690. }
  691. log.Trace("Repository was un-archived: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  692. }
  693. }
  694. return nil
  695. }
  696. // Delete one repository
  697. func Delete(ctx *context.APIContext) {
  698. // swagger:operation DELETE /repos/{owner}/{repo} repository repoDelete
  699. // ---
  700. // summary: Delete a repository
  701. // produces:
  702. // - application/json
  703. // parameters:
  704. // - name: owner
  705. // in: path
  706. // description: owner of the repo to delete
  707. // type: string
  708. // required: true
  709. // - name: repo
  710. // in: path
  711. // description: name of the repo to delete
  712. // type: string
  713. // required: true
  714. // responses:
  715. // "204":
  716. // "$ref": "#/responses/empty"
  717. // "403":
  718. // "$ref": "#/responses/forbidden"
  719. owner := ctx.Repo.Owner
  720. repo := ctx.Repo.Repository
  721. canDelete, err := repo.CanUserDelete(ctx.User)
  722. if err != nil {
  723. ctx.Error(http.StatusInternalServerError, "CanUserDelete", err)
  724. return
  725. } else if !canDelete {
  726. ctx.Error(http.StatusForbidden, "", "Given user is not owner of organization.")
  727. return
  728. }
  729. if err := repo_service.DeleteRepository(ctx.User, repo); err != nil {
  730. ctx.Error(http.StatusInternalServerError, "DeleteRepository", err)
  731. return
  732. }
  733. log.Trace("Repository deleted: %s/%s", owner.Name, repo.Name)
  734. ctx.Status(http.StatusNoContent)
  735. }