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

505 lines
14 KiB

9 years ago
8 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package repo
  5. import (
  6. "strings"
  7. "time"
  8. "github.com/gogits/git-module"
  9. "github.com/gogits/gogs/models"
  10. "github.com/gogits/gogs/modules/auth"
  11. "github.com/gogits/gogs/modules/base"
  12. "github.com/gogits/gogs/modules/context"
  13. "github.com/gogits/gogs/modules/log"
  14. "github.com/gogits/gogs/modules/mailer"
  15. "github.com/gogits/gogs/modules/setting"
  16. )
  17. const (
  18. SETTINGS_OPTIONS base.TplName = "repo/settings/options"
  19. COLLABORATION base.TplName = "repo/settings/collaboration"
  20. GITHOOKS base.TplName = "repo/settings/githooks"
  21. GITHOOK_EDIT base.TplName = "repo/settings/githook_edit"
  22. DEPLOY_KEYS base.TplName = "repo/settings/deploy_keys"
  23. )
  24. func Settings(ctx *context.Context) {
  25. ctx.Data["Title"] = ctx.Tr("repo.settings")
  26. ctx.Data["PageIsSettingsOptions"] = true
  27. ctx.HTML(200, SETTINGS_OPTIONS)
  28. }
  29. func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
  30. ctx.Data["Title"] = ctx.Tr("repo.settings")
  31. ctx.Data["PageIsSettingsOptions"] = true
  32. repo := ctx.Repo.Repository
  33. switch ctx.Query("action") {
  34. case "update":
  35. if ctx.HasError() {
  36. ctx.HTML(200, SETTINGS_OPTIONS)
  37. return
  38. }
  39. isNameChanged := false
  40. oldRepoName := repo.Name
  41. newRepoName := form.RepoName
  42. // Check if repository name has been changed.
  43. if repo.LowerName != strings.ToLower(newRepoName) {
  44. isNameChanged = true
  45. if err := models.ChangeRepositoryName(ctx.Repo.Owner, repo.Name, newRepoName); err != nil {
  46. ctx.Data["Err_RepoName"] = true
  47. switch {
  48. case models.IsErrRepoAlreadyExist(err):
  49. ctx.RenderWithErr(ctx.Tr("form.repo_name_been_taken"), SETTINGS_OPTIONS, &form)
  50. case models.IsErrNameReserved(err):
  51. ctx.RenderWithErr(ctx.Tr("repo.form.name_reserved", err.(models.ErrNameReserved).Name), SETTINGS_OPTIONS, &form)
  52. case models.IsErrNamePatternNotAllowed(err):
  53. ctx.RenderWithErr(ctx.Tr("repo.form.name_pattern_not_allowed", err.(models.ErrNamePatternNotAllowed).Pattern), SETTINGS_OPTIONS, &form)
  54. default:
  55. ctx.Handle(500, "ChangeRepositoryName", err)
  56. }
  57. return
  58. }
  59. log.Trace("Repository name changed: %s/%s -> %s", ctx.Repo.Owner.Name, repo.Name, newRepoName)
  60. }
  61. // In case it's just a case change.
  62. repo.Name = newRepoName
  63. repo.LowerName = strings.ToLower(newRepoName)
  64. if ctx.Repo.GitRepo.IsBranchExist(form.Branch) &&
  65. repo.DefaultBranch != form.Branch {
  66. repo.DefaultBranch = form.Branch
  67. if err := ctx.Repo.GitRepo.SetDefaultBranch(form.Branch); err != nil {
  68. if !git.IsErrUnsupportedVersion(err) {
  69. ctx.Handle(500, "SetDefaultBranch", err)
  70. return
  71. }
  72. }
  73. }
  74. repo.Description = form.Description
  75. repo.Website = form.Website
  76. // Visibility of forked repository is forced sync with base repository.
  77. if repo.IsFork {
  78. form.Private = repo.BaseRepo.IsPrivate
  79. }
  80. visibilityChanged := repo.IsPrivate != form.Private
  81. repo.IsPrivate = form.Private
  82. if err := models.UpdateRepository(repo, visibilityChanged); err != nil {
  83. ctx.Handle(500, "UpdateRepository", err)
  84. return
  85. }
  86. log.Trace("Repository basic settings updated: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  87. if isNameChanged {
  88. if err := models.RenameRepoAction(ctx.User, oldRepoName, repo); err != nil {
  89. log.Error(4, "RenameRepoAction: %v", err)
  90. }
  91. }
  92. if repo.IsMirror {
  93. if form.Interval > 0 {
  94. ctx.Repo.Mirror.Interval = form.Interval
  95. ctx.Repo.Mirror.NextUpdate = time.Now().Add(time.Duration(form.Interval) * time.Hour)
  96. if err := models.UpdateMirror(ctx.Repo.Mirror); err != nil {
  97. ctx.Handle(500, "UpdateMirror", err)
  98. return
  99. }
  100. }
  101. if err := ctx.Repo.Mirror.SaveAddress(form.MirrorAddress); err != nil {
  102. ctx.Handle(500, "SaveAddress", err)
  103. return
  104. }
  105. }
  106. ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
  107. ctx.Redirect(repo.RepoLink() + "/settings")
  108. case "advanced":
  109. repo.EnableWiki = form.EnableWiki
  110. repo.EnableExternalWiki = form.EnableExternalWiki
  111. repo.ExternalWikiURL = form.ExternalWikiURL
  112. repo.EnableIssues = form.EnableIssues
  113. repo.EnableExternalTracker = form.EnableExternalTracker
  114. repo.ExternalTrackerFormat = form.TrackerURLFormat
  115. repo.EnablePulls = form.EnablePulls
  116. if err := models.UpdateRepository(repo, false); err != nil {
  117. ctx.Handle(500, "UpdateRepository", err)
  118. return
  119. }
  120. log.Trace("Repository advanced settings updated: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  121. ctx.Flash.Success(ctx.Tr("repo.settings.update_settings_success"))
  122. ctx.Redirect(ctx.Repo.RepoLink + "/settings")
  123. case "convert":
  124. if !ctx.Repo.IsOwner() {
  125. ctx.Error(404)
  126. return
  127. }
  128. if repo.Name != form.RepoName {
  129. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  130. return
  131. }
  132. if ctx.Repo.Owner.IsOrganization() {
  133. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.Id) {
  134. ctx.Error(404)
  135. return
  136. }
  137. }
  138. if !repo.IsMirror {
  139. ctx.Error(404)
  140. return
  141. }
  142. repo.IsMirror = false
  143. if _, err := models.CleanUpMigrateInfo(repo, models.RepoPath(ctx.Repo.Owner.Name, repo.Name)); err != nil {
  144. ctx.Handle(500, "CleanUpMigrateInfo", err)
  145. return
  146. } else if err = models.DeleteMirrorByRepoID(ctx.Repo.Repository.ID); err != nil {
  147. ctx.Handle(500, "DeleteMirrorByRepoID", err)
  148. return
  149. }
  150. log.Trace("Repository converted from mirror to regular: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  151. ctx.Flash.Success(ctx.Tr("repo.settings.convert_succeed"))
  152. ctx.Redirect(setting.AppSubUrl + "/" + ctx.Repo.Owner.Name + "/" + repo.Name)
  153. case "transfer":
  154. if !ctx.Repo.IsOwner() {
  155. ctx.Error(404)
  156. return
  157. }
  158. if repo.Name != form.RepoName {
  159. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  160. return
  161. }
  162. if ctx.Repo.Owner.IsOrganization() {
  163. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.Id) {
  164. ctx.Error(404)
  165. return
  166. }
  167. }
  168. newOwner := ctx.Query("new_owner_name")
  169. isExist, err := models.IsUserExist(0, newOwner)
  170. if err != nil {
  171. ctx.Handle(500, "IsUserExist", err)
  172. return
  173. } else if !isExist {
  174. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_owner_name"), SETTINGS_OPTIONS, nil)
  175. return
  176. }
  177. if err = models.TransferOwnership(ctx.User, newOwner, repo); err != nil {
  178. if models.IsErrRepoAlreadyExist(err) {
  179. ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), SETTINGS_OPTIONS, nil)
  180. } else {
  181. ctx.Handle(500, "TransferOwnership", err)
  182. }
  183. return
  184. }
  185. log.Trace("Repository transfered: %s/%s -> %s", ctx.Repo.Owner.Name, repo.Name, newOwner)
  186. ctx.Flash.Success(ctx.Tr("repo.settings.transfer_succeed"))
  187. ctx.Redirect(setting.AppSubUrl + "/" + newOwner + "/" + repo.Name)
  188. case "delete":
  189. if !ctx.Repo.IsOwner() {
  190. ctx.Error(404)
  191. return
  192. }
  193. if repo.Name != form.RepoName {
  194. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  195. return
  196. }
  197. if ctx.Repo.Owner.IsOrganization() {
  198. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.Id) {
  199. ctx.Error(404)
  200. return
  201. }
  202. }
  203. if err := models.DeleteRepository(ctx.Repo.Owner.Id, repo.ID); err != nil {
  204. ctx.Handle(500, "DeleteRepository", err)
  205. return
  206. }
  207. log.Trace("Repository deleted: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  208. ctx.Flash.Success(ctx.Tr("repo.settings.deletion_success"))
  209. ctx.Redirect(ctx.Repo.Owner.DashboardLink())
  210. case "delete-wiki":
  211. if !ctx.Repo.IsOwner() {
  212. ctx.Error(404)
  213. return
  214. }
  215. if repo.Name != form.RepoName {
  216. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  217. return
  218. }
  219. if ctx.Repo.Owner.IsOrganization() {
  220. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.Id) {
  221. ctx.Error(404)
  222. return
  223. }
  224. }
  225. repo.DeleteWiki()
  226. log.Trace("Repository wiki deleted: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  227. repo.EnableWiki = false
  228. if err := models.UpdateRepository(repo, false); err != nil {
  229. ctx.Handle(500, "UpdateRepository", err)
  230. return
  231. }
  232. ctx.Flash.Success(ctx.Tr("repo.settings.wiki_deletion_success"))
  233. ctx.Redirect(ctx.Repo.RepoLink + "/settings")
  234. }
  235. }
  236. func Collaboration(ctx *context.Context) {
  237. ctx.Data["Title"] = ctx.Tr("repo.settings")
  238. ctx.Data["PageIsSettingsCollaboration"] = true
  239. users, err := ctx.Repo.Repository.GetCollaborators()
  240. if err != nil {
  241. ctx.Handle(500, "GetCollaborators", err)
  242. return
  243. }
  244. ctx.Data["Collaborators"] = users
  245. ctx.HTML(200, COLLABORATION)
  246. }
  247. func CollaborationPost(ctx *context.Context) {
  248. name := strings.ToLower(ctx.Query("collaborator"))
  249. if len(name) == 0 || ctx.Repo.Owner.LowerName == name {
  250. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  251. return
  252. }
  253. u, err := models.GetUserByName(name)
  254. if err != nil {
  255. if models.IsErrUserNotExist(err) {
  256. ctx.Flash.Error(ctx.Tr("form.user_not_exist"))
  257. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  258. } else {
  259. ctx.Handle(500, "GetUserByName", err)
  260. }
  261. return
  262. }
  263. // Organization is not allowed to be added as a collaborator.
  264. if u.IsOrganization() {
  265. ctx.Flash.Error(ctx.Tr("repo.settings.org_not_allowed_to_be_collaborator"))
  266. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  267. return
  268. }
  269. // Check if user is organization member.
  270. if ctx.Repo.Owner.IsOrganization() && ctx.Repo.Owner.IsOrgMember(u.Id) {
  271. ctx.Flash.Info(ctx.Tr("repo.settings.user_is_org_member"))
  272. ctx.Redirect(ctx.Repo.RepoLink + "/settings/collaboration")
  273. return
  274. }
  275. if err = ctx.Repo.Repository.AddCollaborator(u); err != nil {
  276. ctx.Handle(500, "AddCollaborator", err)
  277. return
  278. }
  279. if setting.Service.EnableNotifyMail {
  280. if err = mailer.SendCollaboratorMail(ctx.Render, u, ctx.User, ctx.Repo.Repository); err != nil {
  281. ctx.Handle(500, "SendCollaboratorMail", err)
  282. return
  283. }
  284. }
  285. ctx.Flash.Success(ctx.Tr("repo.settings.add_collaborator_success"))
  286. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  287. }
  288. func ChangeCollaborationAccessMode(ctx *context.Context) {
  289. if err := ctx.Repo.Repository.ChangeCollaborationAccessMode(
  290. ctx.QueryInt64("uid"),
  291. models.AccessMode(ctx.QueryInt("mode"))); err != nil {
  292. log.Error(4, "ChangeCollaborationAccessMode: %v", err)
  293. }
  294. }
  295. func DeleteCollaboration(ctx *context.Context) {
  296. if err := ctx.Repo.Repository.DeleteCollaboration(ctx.QueryInt64("id")); err != nil {
  297. ctx.Flash.Error("DeleteCollaboration: " + err.Error())
  298. } else {
  299. ctx.Flash.Success(ctx.Tr("repo.settings.remove_collaborator_success"))
  300. }
  301. ctx.JSON(200, map[string]interface{}{
  302. "redirect": ctx.Repo.RepoLink + "/settings/collaboration",
  303. })
  304. }
  305. func parseOwnerAndRepo(ctx *context.Context) (*models.User, *models.Repository) {
  306. owner, err := models.GetUserByName(ctx.Params(":username"))
  307. if err != nil {
  308. if models.IsErrUserNotExist(err) {
  309. ctx.Handle(404, "GetUserByName", err)
  310. } else {
  311. ctx.Handle(500, "GetUserByName", err)
  312. }
  313. return nil, nil
  314. }
  315. repo, err := models.GetRepositoryByName(owner.Id, ctx.Params(":reponame"))
  316. if err != nil {
  317. if models.IsErrRepoNotExist(err) {
  318. ctx.Handle(404, "GetRepositoryByName", err)
  319. } else {
  320. ctx.Handle(500, "GetRepositoryByName", err)
  321. }
  322. return nil, nil
  323. }
  324. return owner, repo
  325. }
  326. func GitHooks(ctx *context.Context) {
  327. ctx.Data["Title"] = ctx.Tr("repo.settings.githooks")
  328. ctx.Data["PageIsSettingsGitHooks"] = true
  329. hooks, err := ctx.Repo.GitRepo.Hooks()
  330. if err != nil {
  331. ctx.Handle(500, "Hooks", err)
  332. return
  333. }
  334. ctx.Data["Hooks"] = hooks
  335. ctx.HTML(200, GITHOOKS)
  336. }
  337. func GitHooksEdit(ctx *context.Context) {
  338. ctx.Data["Title"] = ctx.Tr("repo.settings.githooks")
  339. ctx.Data["PageIsSettingsGitHooks"] = true
  340. name := ctx.Params(":name")
  341. hook, err := ctx.Repo.GitRepo.GetHook(name)
  342. if err != nil {
  343. if err == git.ErrNotValidHook {
  344. ctx.Handle(404, "GetHook", err)
  345. } else {
  346. ctx.Handle(500, "GetHook", err)
  347. }
  348. return
  349. }
  350. ctx.Data["Hook"] = hook
  351. ctx.HTML(200, GITHOOK_EDIT)
  352. }
  353. func GitHooksEditPost(ctx *context.Context) {
  354. name := ctx.Params(":name")
  355. hook, err := ctx.Repo.GitRepo.GetHook(name)
  356. if err != nil {
  357. if err == git.ErrNotValidHook {
  358. ctx.Handle(404, "GetHook", err)
  359. } else {
  360. ctx.Handle(500, "GetHook", err)
  361. }
  362. return
  363. }
  364. hook.Content = ctx.Query("content")
  365. if err = hook.Update(); err != nil {
  366. ctx.Handle(500, "hook.Update", err)
  367. return
  368. }
  369. ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks/git")
  370. }
  371. func DeployKeys(ctx *context.Context) {
  372. ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
  373. ctx.Data["PageIsSettingsKeys"] = true
  374. keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
  375. if err != nil {
  376. ctx.Handle(500, "ListDeployKeys", err)
  377. return
  378. }
  379. ctx.Data["Deploykeys"] = keys
  380. ctx.HTML(200, DEPLOY_KEYS)
  381. }
  382. func DeployKeysPost(ctx *context.Context, form auth.AddSSHKeyForm) {
  383. ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
  384. ctx.Data["PageIsSettingsKeys"] = true
  385. keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
  386. if err != nil {
  387. ctx.Handle(500, "ListDeployKeys", err)
  388. return
  389. }
  390. ctx.Data["Deploykeys"] = keys
  391. if ctx.HasError() {
  392. ctx.HTML(200, DEPLOY_KEYS)
  393. return
  394. }
  395. content, err := models.CheckPublicKeyString(form.Content)
  396. if err != nil {
  397. if models.IsErrKeyUnableVerify(err) {
  398. ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
  399. } else {
  400. ctx.Data["HasError"] = true
  401. ctx.Data["Err_Content"] = true
  402. ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
  403. ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
  404. return
  405. }
  406. }
  407. key, err := models.AddDeployKey(ctx.Repo.Repository.ID, form.Title, content)
  408. if err != nil {
  409. ctx.Data["HasError"] = true
  410. switch {
  411. case models.IsErrKeyAlreadyExist(err):
  412. ctx.Data["Err_Content"] = true
  413. ctx.RenderWithErr(ctx.Tr("repo.settings.key_been_used"), DEPLOY_KEYS, &form)
  414. case models.IsErrKeyNameAlreadyUsed(err):
  415. ctx.Data["Err_Title"] = true
  416. ctx.RenderWithErr(ctx.Tr("repo.settings.key_name_used"), DEPLOY_KEYS, &form)
  417. default:
  418. ctx.Handle(500, "AddDeployKey", err)
  419. }
  420. return
  421. }
  422. log.Trace("Deploy key added: %d", ctx.Repo.Repository.ID)
  423. ctx.Flash.Success(ctx.Tr("repo.settings.add_key_success", key.Name))
  424. ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
  425. }
  426. func DeleteDeployKey(ctx *context.Context) {
  427. if err := models.DeleteDeployKey(ctx.User, ctx.QueryInt64("id")); err != nil {
  428. ctx.Flash.Error("DeleteDeployKey: " + err.Error())
  429. } else {
  430. ctx.Flash.Success(ctx.Tr("repo.settings.deploy_key_deletion_success"))
  431. }
  432. ctx.JSON(200, map[string]interface{}{
  433. "redirect": ctx.Repo.RepoLink + "/settings/keys",
  434. })
  435. }