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 24 kB

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>
6 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>
6 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>
6 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>
6 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>
6 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
11 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>
6 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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: template
  77. // in: query
  78. // description: include template repositories this user has access to (defaults to true)
  79. // type: boolean
  80. // - name: mode
  81. // in: query
  82. // description: type of repository to search for. Supported values are
  83. // "fork", "source", "mirror" and "collaborative"
  84. // type: string
  85. // - name: exclusive
  86. // in: query
  87. // description: if `uid` is given, search only for repos that the user owns
  88. // type: boolean
  89. // - name: sort
  90. // in: query
  91. // description: sort repos by attribute. Supported values are
  92. // "alpha", "created", "updated", "size", and "id".
  93. // Default is "alpha"
  94. // type: string
  95. // - name: order
  96. // in: query
  97. // description: sort order, either "asc" (ascending) or "desc" (descending).
  98. // Default is "asc", ignored if "sort" is not specified.
  99. // type: string
  100. // - name: page
  101. // in: query
  102. // description: page number of results to return (1-based)
  103. // type: integer
  104. // - name: limit
  105. // in: query
  106. // description: page size of results, maximum page size is 50
  107. // type: integer
  108. // responses:
  109. // "200":
  110. // "$ref": "#/responses/SearchResults"
  111. // "422":
  112. // "$ref": "#/responses/validationError"
  113. opts := &models.SearchRepoOptions{
  114. ListOptions: utils.GetListOptions(ctx),
  115. Actor: ctx.User,
  116. Keyword: strings.Trim(ctx.Query("q"), " "),
  117. OwnerID: ctx.QueryInt64("uid"),
  118. PriorityOwnerID: ctx.QueryInt64("priority_owner_id"),
  119. TopicOnly: ctx.QueryBool("topic"),
  120. Collaborate: util.OptionalBoolNone,
  121. Private: ctx.IsSigned && (ctx.Query("private") == "" || ctx.QueryBool("private")),
  122. Template: util.OptionalBoolNone,
  123. StarredByID: ctx.QueryInt64("starredBy"),
  124. IncludeDescription: ctx.QueryBool("includeDesc"),
  125. }
  126. if ctx.Query("template") != "" {
  127. opts.Template = util.OptionalBoolOf(ctx.QueryBool("template"))
  128. }
  129. if ctx.QueryBool("exclusive") {
  130. opts.Collaborate = util.OptionalBoolFalse
  131. }
  132. var mode = ctx.Query("mode")
  133. switch mode {
  134. case "source":
  135. opts.Fork = util.OptionalBoolFalse
  136. opts.Mirror = util.OptionalBoolFalse
  137. case "fork":
  138. opts.Fork = util.OptionalBoolTrue
  139. case "mirror":
  140. opts.Mirror = util.OptionalBoolTrue
  141. case "collaborative":
  142. opts.Mirror = util.OptionalBoolFalse
  143. opts.Collaborate = util.OptionalBoolTrue
  144. case "":
  145. default:
  146. ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("Invalid search mode: \"%s\"", mode))
  147. return
  148. }
  149. var sortMode = ctx.Query("sort")
  150. if len(sortMode) > 0 {
  151. var sortOrder = ctx.Query("order")
  152. if len(sortOrder) == 0 {
  153. sortOrder = "asc"
  154. }
  155. if searchModeMap, ok := searchOrderByMap[sortOrder]; ok {
  156. if orderBy, ok := searchModeMap[sortMode]; ok {
  157. opts.OrderBy = orderBy
  158. } else {
  159. ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("Invalid sort mode: \"%s\"", sortMode))
  160. return
  161. }
  162. } else {
  163. ctx.Error(http.StatusUnprocessableEntity, "", fmt.Errorf("Invalid sort order: \"%s\"", sortOrder))
  164. return
  165. }
  166. }
  167. var err error
  168. repos, count, err := models.SearchRepository(opts)
  169. if err != nil {
  170. ctx.JSON(http.StatusInternalServerError, api.SearchError{
  171. OK: false,
  172. Error: err.Error(),
  173. })
  174. return
  175. }
  176. results := make([]*api.Repository, len(repos))
  177. for i, repo := range repos {
  178. if err = repo.GetOwner(); err != nil {
  179. ctx.JSON(http.StatusInternalServerError, api.SearchError{
  180. OK: false,
  181. Error: err.Error(),
  182. })
  183. return
  184. }
  185. accessMode, err := models.AccessLevel(ctx.User, repo)
  186. if err != nil {
  187. ctx.JSON(http.StatusInternalServerError, api.SearchError{
  188. OK: false,
  189. Error: err.Error(),
  190. })
  191. }
  192. results[i] = repo.APIFormat(accessMode)
  193. }
  194. ctx.SetLinkHeader(int(count), opts.PageSize)
  195. ctx.Header().Set("X-Total-Count", fmt.Sprintf("%d", count))
  196. ctx.JSON(http.StatusOK, api.SearchResults{
  197. OK: true,
  198. Data: results,
  199. })
  200. }
  201. // CreateUserRepo create a repository for a user
  202. func CreateUserRepo(ctx *context.APIContext, owner *models.User, opt api.CreateRepoOption) {
  203. if opt.AutoInit && opt.Readme == "" {
  204. opt.Readme = "Default"
  205. }
  206. repo, err := repo_service.CreateRepository(ctx.User, owner, models.CreateRepoOptions{
  207. Name: opt.Name,
  208. Description: opt.Description,
  209. IssueLabels: opt.IssueLabels,
  210. Gitignores: opt.Gitignores,
  211. License: opt.License,
  212. Readme: opt.Readme,
  213. IsPrivate: opt.Private,
  214. AutoInit: opt.AutoInit,
  215. DefaultBranch: opt.DefaultBranch,
  216. })
  217. if err != nil {
  218. if models.IsErrRepoAlreadyExist(err) {
  219. ctx.Error(http.StatusConflict, "", "The repository with the same name already exists.")
  220. } else if models.IsErrNameReserved(err) ||
  221. models.IsErrNamePatternNotAllowed(err) {
  222. ctx.Error(http.StatusUnprocessableEntity, "", err)
  223. } else {
  224. ctx.Error(http.StatusInternalServerError, "CreateRepository", err)
  225. }
  226. return
  227. }
  228. ctx.JSON(http.StatusCreated, repo.APIFormat(models.AccessModeOwner))
  229. }
  230. // Create one repository of mine
  231. func Create(ctx *context.APIContext, opt api.CreateRepoOption) {
  232. // swagger:operation POST /user/repos repository user createCurrentUserRepo
  233. // ---
  234. // summary: Create a repository
  235. // consumes:
  236. // - application/json
  237. // produces:
  238. // - application/json
  239. // parameters:
  240. // - name: body
  241. // in: body
  242. // schema:
  243. // "$ref": "#/definitions/CreateRepoOption"
  244. // responses:
  245. // "201":
  246. // "$ref": "#/responses/Repository"
  247. // "409":
  248. // description: The repository with the same name already exists.
  249. // "422":
  250. // "$ref": "#/responses/validationError"
  251. if ctx.User.IsOrganization() {
  252. // Shouldn't reach this condition, but just in case.
  253. ctx.Error(http.StatusUnprocessableEntity, "", "not allowed creating repository for organization")
  254. return
  255. }
  256. CreateUserRepo(ctx, ctx.User, opt)
  257. }
  258. // CreateOrgRepoDeprecated create one repository of the organization
  259. func CreateOrgRepoDeprecated(ctx *context.APIContext, opt api.CreateRepoOption) {
  260. // swagger:operation POST /org/{org}/repos organization createOrgRepoDeprecated
  261. // ---
  262. // summary: Create a repository in an organization
  263. // deprecated: true
  264. // consumes:
  265. // - application/json
  266. // produces:
  267. // - application/json
  268. // parameters:
  269. // - name: org
  270. // in: path
  271. // description: name of organization
  272. // type: string
  273. // required: true
  274. // - name: body
  275. // in: body
  276. // schema:
  277. // "$ref": "#/definitions/CreateRepoOption"
  278. // responses:
  279. // "201":
  280. // "$ref": "#/responses/Repository"
  281. // "422":
  282. // "$ref": "#/responses/validationError"
  283. // "403":
  284. // "$ref": "#/responses/forbidden"
  285. CreateOrgRepo(ctx, opt)
  286. }
  287. // CreateOrgRepo create one repository of the organization
  288. func CreateOrgRepo(ctx *context.APIContext, opt api.CreateRepoOption) {
  289. // swagger:operation POST /orgs/{org}/repos organization createOrgRepo
  290. // ---
  291. // summary: Create a repository in an organization
  292. // consumes:
  293. // - application/json
  294. // produces:
  295. // - application/json
  296. // parameters:
  297. // - name: org
  298. // in: path
  299. // description: name of organization
  300. // type: string
  301. // required: true
  302. // - name: body
  303. // in: body
  304. // schema:
  305. // "$ref": "#/definitions/CreateRepoOption"
  306. // responses:
  307. // "201":
  308. // "$ref": "#/responses/Repository"
  309. // "404":
  310. // "$ref": "#/responses/notFound"
  311. // "403":
  312. // "$ref": "#/responses/forbidden"
  313. org, err := models.GetOrgByName(ctx.Params(":org"))
  314. if err != nil {
  315. if models.IsErrOrgNotExist(err) {
  316. ctx.Error(http.StatusUnprocessableEntity, "", err)
  317. } else {
  318. ctx.Error(http.StatusInternalServerError, "GetOrgByName", err)
  319. }
  320. return
  321. }
  322. if !models.HasOrgVisible(org, ctx.User) {
  323. ctx.NotFound("HasOrgVisible", nil)
  324. return
  325. }
  326. if !ctx.User.IsAdmin {
  327. canCreate, err := org.CanCreateOrgRepo(ctx.User.ID)
  328. if err != nil {
  329. ctx.ServerError("CanCreateOrgRepo", err)
  330. return
  331. } else if !canCreate {
  332. ctx.Error(http.StatusForbidden, "", "Given user is not allowed to create repository in organization.")
  333. return
  334. }
  335. }
  336. CreateUserRepo(ctx, org, opt)
  337. }
  338. // Get one repository
  339. func Get(ctx *context.APIContext) {
  340. // swagger:operation GET /repos/{owner}/{repo} repository repoGet
  341. // ---
  342. // summary: Get a repository
  343. // produces:
  344. // - application/json
  345. // parameters:
  346. // - name: owner
  347. // in: path
  348. // description: owner of the repo
  349. // type: string
  350. // required: true
  351. // - name: repo
  352. // in: path
  353. // description: name of the repo
  354. // type: string
  355. // required: true
  356. // responses:
  357. // "200":
  358. // "$ref": "#/responses/Repository"
  359. ctx.JSON(http.StatusOK, ctx.Repo.Repository.APIFormat(ctx.Repo.AccessMode))
  360. }
  361. // GetByID returns a single Repository
  362. func GetByID(ctx *context.APIContext) {
  363. // swagger:operation GET /repositories/{id} repository repoGetByID
  364. // ---
  365. // summary: Get a repository by id
  366. // produces:
  367. // - application/json
  368. // parameters:
  369. // - name: id
  370. // in: path
  371. // description: id of the repo to get
  372. // type: integer
  373. // format: int64
  374. // required: true
  375. // responses:
  376. // "200":
  377. // "$ref": "#/responses/Repository"
  378. repo, err := models.GetRepositoryByID(ctx.ParamsInt64(":id"))
  379. if err != nil {
  380. if models.IsErrRepoNotExist(err) {
  381. ctx.NotFound()
  382. } else {
  383. ctx.Error(http.StatusInternalServerError, "GetRepositoryByID", err)
  384. }
  385. return
  386. }
  387. perm, err := models.GetUserRepoPermission(repo, ctx.User)
  388. if err != nil {
  389. ctx.Error(http.StatusInternalServerError, "AccessLevel", err)
  390. return
  391. } else if !perm.HasAccess() {
  392. ctx.NotFound()
  393. return
  394. }
  395. ctx.JSON(http.StatusOK, repo.APIFormat(perm.AccessMode))
  396. }
  397. // Edit edit repository properties
  398. func Edit(ctx *context.APIContext, opts api.EditRepoOption) {
  399. // swagger:operation PATCH /repos/{owner}/{repo} repository repoEdit
  400. // ---
  401. // summary: Edit a repository's properties. Only fields that are set will be changed.
  402. // produces:
  403. // - application/json
  404. // parameters:
  405. // - name: owner
  406. // in: path
  407. // description: owner of the repo to edit
  408. // type: string
  409. // required: true
  410. // - name: repo
  411. // in: path
  412. // description: name of the repo to edit
  413. // type: string
  414. // required: true
  415. // required: true
  416. // - name: body
  417. // in: body
  418. // description: "Properties of a repo that you can edit"
  419. // schema:
  420. // "$ref": "#/definitions/EditRepoOption"
  421. // responses:
  422. // "200":
  423. // "$ref": "#/responses/Repository"
  424. // "403":
  425. // "$ref": "#/responses/forbidden"
  426. // "422":
  427. // "$ref": "#/responses/validationError"
  428. if err := updateBasicProperties(ctx, opts); err != nil {
  429. return
  430. }
  431. if err := updateRepoUnits(ctx, opts); err != nil {
  432. return
  433. }
  434. if opts.Archived != nil {
  435. if err := updateRepoArchivedState(ctx, opts); err != nil {
  436. return
  437. }
  438. }
  439. ctx.JSON(http.StatusOK, ctx.Repo.Repository.APIFormat(ctx.Repo.AccessMode))
  440. }
  441. // updateBasicProperties updates the basic properties of a repo: Name, Description, Website and Visibility
  442. func updateBasicProperties(ctx *context.APIContext, opts api.EditRepoOption) error {
  443. owner := ctx.Repo.Owner
  444. repo := ctx.Repo.Repository
  445. newRepoName := repo.Name
  446. if opts.Name != nil {
  447. newRepoName = *opts.Name
  448. }
  449. // Check if repository name has been changed and not just a case change
  450. if repo.LowerName != strings.ToLower(newRepoName) {
  451. if err := repo_service.ChangeRepositoryName(ctx.User, repo, newRepoName); err != nil {
  452. switch {
  453. case models.IsErrRepoAlreadyExist(err):
  454. ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("repo name is already taken [name: %s]", newRepoName), err)
  455. case models.IsErrNameReserved(err):
  456. ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("repo name is reserved [name: %s]", newRepoName), err)
  457. case models.IsErrNamePatternNotAllowed(err):
  458. ctx.Error(http.StatusUnprocessableEntity, fmt.Sprintf("repo name's pattern is not allowed [name: %s, pattern: %s]", newRepoName, err.(models.ErrNamePatternNotAllowed).Pattern), err)
  459. default:
  460. ctx.Error(http.StatusUnprocessableEntity, "ChangeRepositoryName", err)
  461. }
  462. return err
  463. }
  464. log.Trace("Repository name changed: %s/%s -> %s", ctx.Repo.Owner.Name, repo.Name, newRepoName)
  465. }
  466. // Update the name in the repo object for the response
  467. repo.Name = newRepoName
  468. repo.LowerName = strings.ToLower(newRepoName)
  469. if opts.Description != nil {
  470. repo.Description = *opts.Description
  471. }
  472. if opts.Website != nil {
  473. repo.Website = *opts.Website
  474. }
  475. visibilityChanged := false
  476. if opts.Private != nil {
  477. // Visibility of forked repository is forced sync with base repository.
  478. if repo.IsFork {
  479. *opts.Private = repo.BaseRepo.IsPrivate
  480. }
  481. visibilityChanged = repo.IsPrivate != *opts.Private
  482. // when ForcePrivate enabled, you could change public repo to private, but only admin users can change private to public
  483. if visibilityChanged && setting.Repository.ForcePrivate && !*opts.Private && !ctx.User.IsAdmin {
  484. err := fmt.Errorf("cannot change private repository to public")
  485. ctx.Error(http.StatusUnprocessableEntity, "Force Private enabled", err)
  486. return err
  487. }
  488. repo.IsPrivate = *opts.Private
  489. }
  490. if opts.Template != nil {
  491. repo.IsTemplate = *opts.Template
  492. }
  493. // Default branch only updated if changed and exist
  494. if opts.DefaultBranch != nil && repo.DefaultBranch != *opts.DefaultBranch && ctx.Repo.GitRepo.IsBranchExist(*opts.DefaultBranch) {
  495. if err := ctx.Repo.GitRepo.SetDefaultBranch(*opts.DefaultBranch); err != nil {
  496. if !git.IsErrUnsupportedVersion(err) {
  497. ctx.Error(http.StatusInternalServerError, "SetDefaultBranch", err)
  498. return err
  499. }
  500. }
  501. repo.DefaultBranch = *opts.DefaultBranch
  502. }
  503. if err := models.UpdateRepository(repo, visibilityChanged); err != nil {
  504. ctx.Error(http.StatusInternalServerError, "UpdateRepository", err)
  505. return err
  506. }
  507. log.Trace("Repository basic settings updated: %s/%s", owner.Name, repo.Name)
  508. return nil
  509. }
  510. // updateRepoUnits updates repo units: Issue settings, Wiki settings, PR settings
  511. func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
  512. owner := ctx.Repo.Owner
  513. repo := ctx.Repo.Repository
  514. var units []models.RepoUnit
  515. var deleteUnitTypes []models.UnitType
  516. if opts.HasIssues != nil {
  517. if *opts.HasIssues && opts.ExternalTracker != nil && !models.UnitTypeExternalTracker.UnitGlobalDisabled() {
  518. // Check that values are valid
  519. if !validation.IsValidExternalURL(opts.ExternalTracker.ExternalTrackerURL) {
  520. err := fmt.Errorf("External tracker URL not valid")
  521. ctx.Error(http.StatusUnprocessableEntity, "Invalid external tracker URL", err)
  522. return err
  523. }
  524. if len(opts.ExternalTracker.ExternalTrackerFormat) != 0 && !validation.IsValidExternalTrackerURLFormat(opts.ExternalTracker.ExternalTrackerFormat) {
  525. err := fmt.Errorf("External tracker URL format not valid")
  526. ctx.Error(http.StatusUnprocessableEntity, "Invalid external tracker URL format", err)
  527. return err
  528. }
  529. units = append(units, models.RepoUnit{
  530. RepoID: repo.ID,
  531. Type: models.UnitTypeExternalTracker,
  532. Config: &models.ExternalTrackerConfig{
  533. ExternalTrackerURL: opts.ExternalTracker.ExternalTrackerURL,
  534. ExternalTrackerFormat: opts.ExternalTracker.ExternalTrackerFormat,
  535. ExternalTrackerStyle: opts.ExternalTracker.ExternalTrackerStyle,
  536. },
  537. })
  538. deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeIssues)
  539. } else if *opts.HasIssues && opts.ExternalTracker == nil && !models.UnitTypeIssues.UnitGlobalDisabled() {
  540. // Default to built-in tracker
  541. var config *models.IssuesConfig
  542. if opts.InternalTracker != nil {
  543. config = &models.IssuesConfig{
  544. EnableTimetracker: opts.InternalTracker.EnableTimeTracker,
  545. AllowOnlyContributorsToTrackTime: opts.InternalTracker.AllowOnlyContributorsToTrackTime,
  546. EnableDependencies: opts.InternalTracker.EnableIssueDependencies,
  547. }
  548. } else if unit, err := repo.GetUnit(models.UnitTypeIssues); err != nil {
  549. // Unit type doesn't exist so we make a new config file with default values
  550. config = &models.IssuesConfig{
  551. EnableTimetracker: true,
  552. AllowOnlyContributorsToTrackTime: true,
  553. EnableDependencies: true,
  554. }
  555. } else {
  556. config = unit.IssuesConfig()
  557. }
  558. units = append(units, models.RepoUnit{
  559. RepoID: repo.ID,
  560. Type: models.UnitTypeIssues,
  561. Config: config,
  562. })
  563. deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeExternalTracker)
  564. } else if !*opts.HasIssues {
  565. if !models.UnitTypeExternalTracker.UnitGlobalDisabled() {
  566. deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeExternalTracker)
  567. }
  568. if !models.UnitTypeIssues.UnitGlobalDisabled() {
  569. deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeIssues)
  570. }
  571. }
  572. }
  573. if opts.HasWiki != nil {
  574. if *opts.HasWiki && opts.ExternalWiki != nil && !models.UnitTypeExternalWiki.UnitGlobalDisabled() {
  575. // Check that values are valid
  576. if !validation.IsValidExternalURL(opts.ExternalWiki.ExternalWikiURL) {
  577. err := fmt.Errorf("External wiki URL not valid")
  578. ctx.Error(http.StatusUnprocessableEntity, "", "Invalid external wiki URL")
  579. return err
  580. }
  581. units = append(units, models.RepoUnit{
  582. RepoID: repo.ID,
  583. Type: models.UnitTypeExternalWiki,
  584. Config: &models.ExternalWikiConfig{
  585. ExternalWikiURL: opts.ExternalWiki.ExternalWikiURL,
  586. },
  587. })
  588. deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeWiki)
  589. } else if *opts.HasWiki && opts.ExternalWiki == nil && !models.UnitTypeWiki.UnitGlobalDisabled() {
  590. config := &models.UnitConfig{}
  591. units = append(units, models.RepoUnit{
  592. RepoID: repo.ID,
  593. Type: models.UnitTypeWiki,
  594. Config: config,
  595. })
  596. deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeExternalWiki)
  597. } else if !*opts.HasWiki {
  598. if !models.UnitTypeExternalWiki.UnitGlobalDisabled() {
  599. deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeExternalWiki)
  600. }
  601. if !models.UnitTypeWiki.UnitGlobalDisabled() {
  602. deleteUnitTypes = append(deleteUnitTypes, models.UnitTypeWiki)
  603. }
  604. }
  605. }
  606. if opts.HasPullRequests != nil {
  607. if *opts.HasPullRequests && !models.UnitTypePullRequests.UnitGlobalDisabled() {
  608. // We do allow setting individual PR settings through the API, so
  609. // we get the config settings and then set them
  610. // if those settings were provided in the opts.
  611. unit, err := repo.GetUnit(models.UnitTypePullRequests)
  612. var config *models.PullRequestsConfig
  613. if err != nil {
  614. // Unit type doesn't exist so we make a new config file with default values
  615. config = &models.PullRequestsConfig{
  616. IgnoreWhitespaceConflicts: false,
  617. AllowMerge: true,
  618. AllowRebase: true,
  619. AllowRebaseMerge: true,
  620. AllowSquash: true,
  621. }
  622. } else {
  623. config = unit.PullRequestsConfig()
  624. }
  625. if opts.IgnoreWhitespaceConflicts != nil {
  626. config.IgnoreWhitespaceConflicts = *opts.IgnoreWhitespaceConflicts
  627. }
  628. if opts.AllowMerge != nil {
  629. config.AllowMerge = *opts.AllowMerge
  630. }
  631. if opts.AllowRebase != nil {
  632. config.AllowRebase = *opts.AllowRebase
  633. }
  634. if opts.AllowRebaseMerge != nil {
  635. config.AllowRebaseMerge = *opts.AllowRebaseMerge
  636. }
  637. if opts.AllowSquash != nil {
  638. config.AllowSquash = *opts.AllowSquash
  639. }
  640. units = append(units, models.RepoUnit{
  641. RepoID: repo.ID,
  642. Type: models.UnitTypePullRequests,
  643. Config: config,
  644. })
  645. } else if !*opts.HasPullRequests && !models.UnitTypePullRequests.UnitGlobalDisabled() {
  646. deleteUnitTypes = append(deleteUnitTypes, models.UnitTypePullRequests)
  647. }
  648. }
  649. if err := models.UpdateRepositoryUnits(repo, units, deleteUnitTypes); err != nil {
  650. ctx.Error(http.StatusInternalServerError, "UpdateRepositoryUnits", err)
  651. return err
  652. }
  653. log.Trace("Repository advanced settings updated: %s/%s", owner.Name, repo.Name)
  654. return nil
  655. }
  656. // updateRepoArchivedState updates repo's archive state
  657. func updateRepoArchivedState(ctx *context.APIContext, opts api.EditRepoOption) error {
  658. repo := ctx.Repo.Repository
  659. // archive / un-archive
  660. if opts.Archived != nil {
  661. if repo.IsMirror {
  662. err := fmt.Errorf("repo is a mirror, cannot archive/un-archive")
  663. ctx.Error(http.StatusUnprocessableEntity, err.Error(), err)
  664. return err
  665. }
  666. if *opts.Archived {
  667. if err := repo.SetArchiveRepoState(*opts.Archived); err != nil {
  668. log.Error("Tried to archive a repo: %s", err)
  669. ctx.Error(http.StatusInternalServerError, "ArchiveRepoState", err)
  670. return err
  671. }
  672. log.Trace("Repository was archived: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  673. } else {
  674. if err := repo.SetArchiveRepoState(*opts.Archived); err != nil {
  675. log.Error("Tried to un-archive a repo: %s", err)
  676. ctx.Error(http.StatusInternalServerError, "ArchiveRepoState", err)
  677. return err
  678. }
  679. log.Trace("Repository was un-archived: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  680. }
  681. }
  682. return nil
  683. }
  684. // Delete one repository
  685. func Delete(ctx *context.APIContext) {
  686. // swagger:operation DELETE /repos/{owner}/{repo} repository repoDelete
  687. // ---
  688. // summary: Delete a repository
  689. // produces:
  690. // - application/json
  691. // parameters:
  692. // - name: owner
  693. // in: path
  694. // description: owner of the repo to delete
  695. // type: string
  696. // required: true
  697. // - name: repo
  698. // in: path
  699. // description: name of the repo to delete
  700. // type: string
  701. // required: true
  702. // responses:
  703. // "204":
  704. // "$ref": "#/responses/empty"
  705. // "403":
  706. // "$ref": "#/responses/forbidden"
  707. owner := ctx.Repo.Owner
  708. repo := ctx.Repo.Repository
  709. canDelete, err := repo.CanUserDelete(ctx.User)
  710. if err != nil {
  711. ctx.Error(http.StatusInternalServerError, "CanUserDelete", err)
  712. return
  713. } else if !canDelete {
  714. ctx.Error(http.StatusForbidden, "", "Given user is not owner of organization.")
  715. return
  716. }
  717. if err := repo_service.DeleteRepository(ctx.User, repo); err != nil {
  718. ctx.Error(http.StatusInternalServerError, "DeleteRepository", err)
  719. return
  720. }
  721. log.Trace("Repository deleted: %s/%s", owner.Name, repo.Name)
  722. ctx.Status(http.StatusNoContent)
  723. }