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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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. Alias: opt.Alias,
  221. Description: opt.Description,
  222. IssueLabels: opt.IssueLabels,
  223. Gitignores: opt.Gitignores,
  224. License: opt.License,
  225. Readme: opt.Readme,
  226. IsPrivate: opt.Private,
  227. AutoInit: opt.AutoInit,
  228. DefaultBranch: opt.DefaultBranch,
  229. })
  230. if err != nil {
  231. if models.IsErrRepoAlreadyExist(err) {
  232. ctx.Error(http.StatusConflict, "", "The repository with the same name already exists.")
  233. } else if models.IsErrNameReserved(err) ||
  234. models.IsErrNamePatternNotAllowed(err) {
  235. ctx.Error(http.StatusUnprocessableEntity, "", err)
  236. } else {
  237. ctx.Error(http.StatusInternalServerError, "CreateRepository", err)
  238. }
  239. return
  240. }
  241. ctx.JSON(http.StatusCreated, repo.APIFormat(models.AccessModeOwner))
  242. }
  243. // Create one repository of mine
  244. func Create(ctx *context.APIContext, opt api.CreateRepoOption) {
  245. // swagger:operation POST /user/repos repository user createCurrentUserRepo
  246. // ---
  247. // summary: Create a repository
  248. // consumes:
  249. // - application/json
  250. // produces:
  251. // - application/json
  252. // parameters:
  253. // - name: body
  254. // in: body
  255. // schema:
  256. // "$ref": "#/definitions/CreateRepoOption"
  257. // responses:
  258. // "201":
  259. // "$ref": "#/responses/Repository"
  260. // "409":
  261. // description: The repository with the same name already exists.
  262. // "422":
  263. // "$ref": "#/responses/validationError"
  264. if ctx.User.IsOrganization() {
  265. // Shouldn't reach this condition, but just in case.
  266. ctx.Error(http.StatusUnprocessableEntity, "", "not allowed creating repository for organization")
  267. return
  268. }
  269. CreateUserRepo(ctx, ctx.User, opt)
  270. }
  271. // CreateOrgRepoDeprecated create one repository of the organization
  272. func CreateOrgRepoDeprecated(ctx *context.APIContext, opt api.CreateRepoOption) {
  273. // swagger:operation POST /org/{org}/repos organization createOrgRepoDeprecated
  274. // ---
  275. // summary: Create a repository in an organization
  276. // deprecated: true
  277. // consumes:
  278. // - application/json
  279. // produces:
  280. // - application/json
  281. // parameters:
  282. // - name: org
  283. // in: path
  284. // description: name of organization
  285. // type: string
  286. // required: true
  287. // - name: body
  288. // in: body
  289. // schema:
  290. // "$ref": "#/definitions/CreateRepoOption"
  291. // responses:
  292. // "201":
  293. // "$ref": "#/responses/Repository"
  294. // "422":
  295. // "$ref": "#/responses/validationError"
  296. // "403":
  297. // "$ref": "#/responses/forbidden"
  298. CreateOrgRepo(ctx, opt)
  299. }
  300. // CreateOrgRepo create one repository of the organization
  301. func CreateOrgRepo(ctx *context.APIContext, opt api.CreateRepoOption) {
  302. // swagger:operation POST /orgs/{org}/repos organization createOrgRepo
  303. // ---
  304. // summary: Create a repository in an organization
  305. // consumes:
  306. // - application/json
  307. // produces:
  308. // - application/json
  309. // parameters:
  310. // - name: org
  311. // in: path
  312. // description: name of organization
  313. // type: string
  314. // required: true
  315. // - name: body
  316. // in: body
  317. // schema:
  318. // "$ref": "#/definitions/CreateRepoOption"
  319. // responses:
  320. // "201":
  321. // "$ref": "#/responses/Repository"
  322. // "404":
  323. // "$ref": "#/responses/notFound"
  324. // "403":
  325. // "$ref": "#/responses/forbidden"
  326. org, err := models.GetOrgByName(ctx.Params(":org"))
  327. if err != nil {
  328. if models.IsErrOrgNotExist(err) {
  329. ctx.Error(http.StatusUnprocessableEntity, "", err)
  330. } else {
  331. ctx.Error(http.StatusInternalServerError, "GetOrgByName", err)
  332. }
  333. return
  334. }
  335. if !models.HasOrgVisible(org, ctx.User) {
  336. ctx.NotFound("HasOrgVisible", nil)
  337. return
  338. }
  339. if !ctx.User.IsAdmin {
  340. canCreate, err := org.CanCreateOrgRepo(ctx.User.ID)
  341. if err != nil {
  342. ctx.ServerError("CanCreateOrgRepo", err)
  343. return
  344. } else if !canCreate {
  345. ctx.Error(http.StatusForbidden, "", "Given user is not allowed to create repository in organization.")
  346. return
  347. }
  348. }
  349. CreateUserRepo(ctx, org, opt)
  350. }
  351. // Get one repository
  352. func Get(ctx *context.APIContext) {
  353. // swagger:operation GET /repos/{owner}/{repo} repository repoGet
  354. // ---
  355. // summary: Get a repository
  356. // produces:
  357. // - application/json
  358. // parameters:
  359. // - name: owner
  360. // in: path
  361. // description: owner of the repo
  362. // type: string
  363. // required: true
  364. // - name: repo
  365. // in: path
  366. // description: name of the repo
  367. // type: string
  368. // required: true
  369. // responses:
  370. // "200":
  371. // "$ref": "#/responses/Repository"
  372. ctx.JSON(http.StatusOK, ctx.Repo.Repository.APIFormat(ctx.Repo.AccessMode))
  373. }
  374. // GetByID returns a single Repository
  375. func GetByID(ctx *context.APIContext) {
  376. // swagger:operation GET /repositories/{id} repository repoGetByID
  377. // ---
  378. // summary: Get a repository by id
  379. // produces:
  380. // - application/json
  381. // parameters:
  382. // - name: id
  383. // in: path
  384. // description: id of the repo to get
  385. // type: integer
  386. // format: int64
  387. // required: true
  388. // responses:
  389. // "200":
  390. // "$ref": "#/responses/Repository"
  391. repo, err := models.GetRepositoryByID(ctx.ParamsInt64(":id"))
  392. if err != nil {
  393. if models.IsErrRepoNotExist(err) {
  394. ctx.NotFound()
  395. } else {
  396. ctx.Error(http.StatusInternalServerError, "GetRepositoryByID", err)
  397. }
  398. return
  399. }
  400. perm, err := models.GetUserRepoPermission(repo, ctx.User)
  401. if err != nil {
  402. ctx.Error(http.StatusInternalServerError, "AccessLevel", err)
  403. return
  404. } else if !perm.HasAccess() {
  405. ctx.NotFound()
  406. return
  407. }
  408. ctx.JSON(http.StatusOK, repo.APIFormat(perm.AccessMode))
  409. }
  410. // Edit edit repository properties
  411. func Edit(ctx *context.APIContext, opts api.EditRepoOption) {
  412. // swagger:operation PATCH /repos/{owner}/{repo} repository repoEdit
  413. // ---
  414. // summary: Edit a repository's properties. Only fields that are set will be changed.
  415. // produces:
  416. // - application/json
  417. // parameters:
  418. // - name: owner
  419. // in: path
  420. // description: owner of the repo to edit
  421. // type: string
  422. // required: true
  423. // - name: repo
  424. // in: path
  425. // description: name of the repo to edit
  426. // type: string
  427. // required: true
  428. // required: true
  429. // - name: body
  430. // in: body
  431. // description: "Properties of a repo that you can edit"
  432. // schema:
  433. // "$ref": "#/definitions/EditRepoOption"
  434. // responses:
  435. // "200":
  436. // "$ref": "#/responses/Repository"
  437. // "403":
  438. // "$ref": "#/responses/forbidden"
  439. // "422":
  440. // "$ref": "#/responses/validationError"
  441. if err := updateBasicProperties(ctx, opts); err != nil {
  442. return
  443. }
  444. if err := updateRepoUnits(ctx, opts); err != nil {
  445. return
  446. }
  447. if opts.Archived != nil {
  448. if err := updateRepoArchivedState(ctx, opts); err != nil {
  449. return
  450. }
  451. }
  452. ctx.JSON(http.StatusOK, ctx.Repo.Repository.APIFormat(ctx.Repo.AccessMode))
  453. }
  454. // updateBasicProperties updates the basic properties of a repo: Name, Description, Website and Visibility
  455. func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) error {
  456. owner := ctx.Repo.Owner
  457. repo := ctx.Repo.Repository
  458. newRepoName := repo.Name
  459. if opts.Name != nil {
  460. newRepoName = *opts.Name
  461. }
  462. // Check if repository name has been changed and not just a case change
  463. if repo.LowerName != strings.ToLower(newRepoName) {
  464. if err := repo_service.ChangeRepositoryName(ctx.User, repo, newRepoName); err != nil {
  465. switch {
  466. case models.IsErrRepoAlreadyExist(err):
  467. ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("repo name is already taken [name: %s]", newRepoName), err)
  468. case models.IsErrNameReserved(err):
  469. ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("repo name is reserved [name: %s]", newRepoName), err)
  470. case models.IsErrNamePatternNotAllowed(err):
  471. ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("repo name's pattern is not allowed [name: %s, pattern: %s]", newRepoName, err.(models.ErrNamePatternNotAllowed).Pattern), err)
  472. default:
  473. ctx.Error(http.StatusUnprocessableEntity, "ChangeRepositoryName", err)
  474. }
  475. return err
  476. }
  477. log.Trace("Repository name changed: %s/%s -> %s", ctx.Repo.Owner.Name, repo.Name, newRepoName)
  478. }
  479. // Update the name in the repo object for the response
  480. repo.Name = newRepoName
  481. repo.LowerName = strings.ToLower(newRepoName)
  482. if opts.Description != nil {
  483. repo.Description = *opts.Description
  484. }
  485. if opts.Website != nil {
  486. repo.Website = *opts.Website
  487. }
  488. visibilityChanged := false
  489. if opts.Private != nil {
  490. // Visibility of forked repository is forced sync with base repository.
  491. if repo.IsFork {
  492. *opts.Private = repo.BaseRepo.IsPrivate
  493. }
  494. visibilityChanged = repo.IsPrivate != *opts.Private
  495. // when ForcePrivate enabled, you could change public repo to private, but only admin users can change private to public
  496. if visibilityChanged && setting.Repository.ForcePrivate && !*opts.Private && !ctx.User.IsAdmin {
  497. err := fmt.Errorf("cannot change private repository to public")
  498. ctx.Error(http.StatusUnprocessableEntity, "Force Private enabled", err)
  499. return err
  500. }
  501. repo.IsPrivate = *opts.Private
  502. }
  503. if opts.Template != nil {
  504. repo.IsTemplate = *opts.Template
  505. }
  506. // Default branch only updated if changed and exist
  507. if opts.DefaultBranch != nil && repo.DefaultBranch != *opts.DefaultBranch && ctx.Repo.GitRepo.IsBranchExist(*opts.DefaultBranch) {
  508. if err := ctx.Repo.GitRepo.SetDefaultBranch(*opts.DefaultBranch); err != nil {
  509. if !git.IsErrUnsupportedVersion(err) {
  510. ctx.Error(http.StatusInternalServerError, "SetDefaultBranch", err)
  511. return err
  512. }
  513. }
  514. repo.DefaultBranch = *opts.DefaultBranch
  515. }
  516. if err := models.UpdateRepository(repo, visibilityChanged); err != nil {
  517. ctx.Error(http.StatusInternalServerError, "UpdateRepository", err)
  518. return err
  519. }
  520. log.Trace("Repository basic settings updated: %s/%s", owner.Name, repo.Name)
  521. return nil
  522. }
  523. // updateRepoUnits updates repo units: Issue settings, Wiki settings, PR settings
  524. func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
  525. owner := ctx.Repo.Owner
  526. repo := ctx.Repo.Repository
  527. var units []models.RepoUnit
  528. var deleteUnitTypes []models.UnitType
  529. if opts.HasIssues != nil {
  530. if *opts.HasIssues && opts.ExternalTracker != nil && !models.UnitTypeExternalTracker.UnitGlobalDisabled() {
  531. // Check that values are valid
  532. if !validation.IsValidExternalURL(opts.ExternalTracker.ExternalTrackerURL) {
  533. err := fmt.Errorf("External tracker URL not valid")
  534. ctx.Error(http.StatusUnprocessableEntity, "Invalid external tracker URL", err)
  535. return err
  536. }
  537. if len(opts.ExternalTracker.ExternalTrackerFormat) != 0 && !validation.IsValidExternalTrackerURLFormat(opts.ExternalTracker.ExternalTrackerFormat) {
  538. err := fmt.Errorf("External tracker URL format not valid")
  539. ctx.Error(http.StatusUnprocessableEntity, "Invalid external tracker URL format", err)
  540. return err
  541. }
  542. units = append(units, models.RepoUnit{
  543. RepoID: repo.ID,
  544. Type: models.UnitTypeExternalTracker,
  545. Config: &models.ExternalTrackerConfig{
  546. ExternalTrackerURL: opts.ExternalTracker.ExternalTrackerURL,
  547. ExternalTrackerFormat: opts.ExternalTracker.ExternalTrackerFormat,
  548. ExternalTrackerStyle: opts.ExternalTracker.ExternalTrackerStyle,
  549. },
  550. })
  551. deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeIssues)
  552. } else if *opts.HasIssues && opts.ExternalTracker == nil && !models.UnitTypeIssues.UnitGlobalDisabled() {
  553. // Default to built-in tracker
  554. var config *models.IssuesConfig
  555. if opts.InternalTracker != nil {
  556. config = &models.IssuesConfig{
  557. EnableTimetracker: opts.InternalTracker.EnableTimeTracker,
  558. AllowOnlyContributorsToTrackTime: opts.InternalTracker.AllowOnlyContributorsToTrackTime,
  559. EnableDependencies: opts.InternalTracker.EnableIssueDependencies,
  560. }
  561. } else if unit, err := repo.GetUnit(models.UnitTypeIssues); err != nil {
  562. // Unit type doesn't exist so we make a new config file with default values
  563. config = &models.IssuesConfig{
  564. EnableTimetracker: true,
  565. AllowOnlyContributorsToTrackTime: true,
  566. EnableDependencies: true,
  567. }
  568. } else {
  569. config = unit.IssuesConfig()
  570. }
  571. units = append(units, models.RepoUnit{
  572. RepoID: repo.ID,
  573. Type: models.UnitTypeIssues,
  574. Config: config,
  575. })
  576. deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeExternalTracker)
  577. } else if !*opts.HasIssues {
  578. if !models.UnitTypeExternalTracker.UnitGlobalDisabled() {
  579. deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeExternalTracker)
  580. }
  581. if !models.UnitTypeIssues.UnitGlobalDisabled() {
  582. deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeIssues)
  583. }
  584. }
  585. }
  586. if opts.HasWiki != nil {
  587. if *opts.HasWiki && opts.ExternalWiki != nil && !models.UnitTypeExternalWiki.UnitGlobalDisabled() {
  588. // Check that values are valid
  589. if !validation.IsValidExternalURL(opts.ExternalWiki.ExternalWikiURL) {
  590. err := fmt.Errorf("External wiki URL not valid")
  591. ctx.Error(http.StatusUnprocessableEntity, "", "Invalid external wiki URL")
  592. return err
  593. }
  594. units = append(units, models.RepoUnit{
  595. RepoID: repo.ID,
  596. Type: models.UnitTypeExternalWiki,
  597. Config: &models.ExternalWikiConfig{
  598. ExternalWikiURL: opts.ExternalWiki.ExternalWikiURL,
  599. },
  600. })
  601. deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeWiki)
  602. } else if *opts.HasWiki && opts.ExternalWiki == nil && !models.UnitTypeWiki.UnitGlobalDisabled() {
  603. config := &models.UnitConfig{}
  604. units = append(units, models.RepoUnit{
  605. RepoID: repo.ID,
  606. Type: models.UnitTypeWiki,
  607. Config: config,
  608. })
  609. deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeExternalWiki)
  610. } else if !*opts.HasWiki {
  611. if !models.UnitTypeExternalWiki.UnitGlobalDisabled() {
  612. deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeExternalWiki)
  613. }
  614. if !models.UnitTypeWiki.UnitGlobalDisabled() {
  615. deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeWiki)
  616. }
  617. }
  618. }
  619. if opts.HasPullRequests != nil {
  620. if *opts.HasPullRequests && !models.UnitTypePullRequests.UnitGlobalDisabled() {
  621. // We do allow setting individual PR settings through the API, so
  622. // we get the config settings and then set them
  623. // if those settings were provided in the opts.
  624. unit, err := repo.GetUnit(models.UnitTypePullRequests)
  625. var config *models.PullRequestsConfig
  626. if err != nil {
  627. // Unit type doesn't exist so we make a new config file with default values
  628. config = &models.PullRequestsConfig{
  629. IgnoreWhitespaceConflicts: false,
  630. AllowMerge: true,
  631. AllowRebase: true,
  632. AllowRebaseMerge: true,
  633. AllowSquash: true,
  634. }
  635. } else {
  636. config = unit.PullRequestsConfig()
  637. }
  638. if opts.IgnoreWhitespaceConflicts != nil {
  639. config.IgnoreWhitespaceConflicts = *opts.IgnoreWhitespaceConflicts
  640. }
  641. if opts.AllowMerge != nil {
  642. config.AllowMerge = *opts.AllowMerge
  643. }
  644. if opts.AllowRebase != nil {
  645. config.AllowRebase = *opts.AllowRebase
  646. }
  647. if opts.AllowRebaseMerge != nil {
  648. config.AllowRebaseMerge = *opts.AllowRebaseMerge
  649. }
  650. if opts.AllowSquash != nil {
  651. config.AllowSquash = *opts.AllowSquash
  652. }
  653. units = append(units, models.RepoUnit{
  654. RepoID: repo.ID,
  655. Type: models.UnitTypePullRequests,
  656. Config: config,
  657. })
  658. } else if !*opts.HasPullRequests && !models.UnitTypePullRequests.UnitGlobalDisabled() {
  659. deleteUnitTypes = append(deleteUnitTypes, models.UnitTypePullRequests)
  660. }
  661. }
  662. if err := models.UpdateRepositoryUnits(repo, units, deleteUnitTypes); err != nil {
  663. ctx.Error(http.StatusInternalServerError, "UpdateRepositoryUnits", err)
  664. return err
  665. }
  666. log.Trace("Repository advanced settings updated: %s/%s", owner.Name, repo.Name)
  667. return nil
  668. }
  669. // updateRepoArchivedState updates repo's archive state
  670. func updateRepoArchivedState(ctx *context.APIContext, opts api.EditRepoOption) error {
  671. repo := ctx.Repo.Repository
  672. // archive / un-archive
  673. if opts.Archived != nil {
  674. if repo.IsMirror {
  675. err := fmt.Errorf("repo is a mirror, cannot archive/un-archive")
  676. ctx.Error(http.StatusUnprocessableEntity, err.Error(), err)
  677. return err
  678. }
  679. if *opts.Archived {
  680. if err := repo.SetArchiveRepoState(*opts.Archived); err != nil {
  681. log.Error("Tried to archive a repo: %s", err)
  682. ctx.Error(http.StatusInternalServerError, "ArchiveRepoState", err)
  683. return err
  684. }
  685. log.Trace("Repository was archived: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  686. } else {
  687. if err := repo.SetArchiveRepoState(*opts.Archived); err != nil {
  688. log.Error("Tried to un-archive a repo: %s", err)
  689. ctx.Error(http.StatusInternalServerError, "ArchiveRepoState", err)
  690. return err
  691. }
  692. log.Trace("Repository was un-archived: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  693. }
  694. }
  695. return nil
  696. }
  697. // Delete one repository
  698. func Delete(ctx *context.APIContext) {
  699. // swagger:operation DELETE /repos/{owner}/{repo} repository repoDelete
  700. // ---
  701. // summary: Delete a repository
  702. // produces:
  703. // - application/json
  704. // parameters:
  705. // - name: owner
  706. // in: path
  707. // description: owner of the repo to delete
  708. // type: string
  709. // required: true
  710. // - name: repo
  711. // in: path
  712. // description: name of the repo to delete
  713. // type: string
  714. // required: true
  715. // responses:
  716. // "204":
  717. // "$ref": "#/responses/empty"
  718. // "403":
  719. // "$ref": "#/responses/forbidden"
  720. owner := ctx.Repo.Owner
  721. repo := ctx.Repo.Repository
  722. canDelete, err := repo.CanUserDelete(ctx.User)
  723. if err != nil {
  724. ctx.Error(http.StatusInternalServerError, "CanUserDelete", err)
  725. return
  726. } else if !canDelete {
  727. ctx.Error(http.StatusForbidden, "", "Given user is not owner of organization.")
  728. return
  729. }
  730. if err := repo_service.DeleteRepository(ctx.User, repo); err != nil {
  731. ctx.Error(http.StatusInternalServerError, "DeleteRepository", err)
  732. return
  733. }
  734. log.Trace("Repository deleted: %s/%s", owner.Name, repo.Name)
  735. ctx.Status(http.StatusNoContent)
  736. }