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.

454 lines
13 KiB

9 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/log"
  13. "github.com/gogits/gogs/modules/mailer"
  14. "github.com/gogits/gogs/modules/middleware"
  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 *middleware.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 *middleware.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 repo.Name != form.RepoName {
  125. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  126. return
  127. }
  128. if ctx.Repo.Owner.IsOrganization() {
  129. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.Id) {
  130. ctx.Error(404)
  131. return
  132. }
  133. }
  134. if !repo.IsMirror {
  135. ctx.Error(404)
  136. return
  137. }
  138. repo.IsMirror = false
  139. if _, err := models.CleanUpMigrateInfo(repo, models.RepoPath(ctx.Repo.Owner.Name, repo.Name)); err != nil {
  140. ctx.Handle(500, "CleanUpMigrateInfo", err)
  141. return
  142. } else if err = models.DeleteMirrorByRepoID(ctx.Repo.Repository.ID); err != nil {
  143. ctx.Handle(500, "DeleteMirrorByRepoID", err)
  144. return
  145. }
  146. log.Trace("Repository converted from mirror to regular: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  147. ctx.Flash.Success(ctx.Tr("repo.settings.convert_succeed"))
  148. ctx.Redirect(setting.AppSubUrl + "/" + ctx.Repo.Owner.Name + "/" + repo.Name)
  149. case "transfer":
  150. if repo.Name != form.RepoName {
  151. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  152. return
  153. }
  154. if ctx.Repo.Owner.IsOrganization() {
  155. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.Id) {
  156. ctx.Error(404)
  157. return
  158. }
  159. }
  160. newOwner := ctx.Query("new_owner_name")
  161. isExist, err := models.IsUserExist(0, newOwner)
  162. if err != nil {
  163. ctx.Handle(500, "IsUserExist", err)
  164. return
  165. } else if !isExist {
  166. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_owner_name"), SETTINGS_OPTIONS, nil)
  167. return
  168. }
  169. if err = models.TransferOwnership(ctx.User, newOwner, repo); err != nil {
  170. if models.IsErrRepoAlreadyExist(err) {
  171. ctx.RenderWithErr(ctx.Tr("repo.settings.new_owner_has_same_repo"), SETTINGS_OPTIONS, nil)
  172. } else {
  173. ctx.Handle(500, "TransferOwnership", err)
  174. }
  175. return
  176. }
  177. log.Trace("Repository transfered: %s/%s -> %s", ctx.Repo.Owner.Name, repo.Name, newOwner)
  178. ctx.Flash.Success(ctx.Tr("repo.settings.transfer_succeed"))
  179. ctx.Redirect(setting.AppSubUrl + "/" + newOwner + "/" + repo.Name)
  180. case "delete":
  181. if repo.Name != form.RepoName {
  182. ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_repo_name"), SETTINGS_OPTIONS, nil)
  183. return
  184. }
  185. if ctx.Repo.Owner.IsOrganization() {
  186. if !ctx.Repo.Owner.IsOwnedBy(ctx.User.Id) {
  187. ctx.Error(404)
  188. return
  189. }
  190. }
  191. if err := models.DeleteRepository(ctx.Repo.Owner.Id, repo.ID); err != nil {
  192. ctx.Handle(500, "DeleteRepository", err)
  193. return
  194. }
  195. log.Trace("Repository deleted: %s/%s", ctx.Repo.Owner.Name, repo.Name)
  196. ctx.Flash.Success(ctx.Tr("repo.settings.deletion_success"))
  197. ctx.Redirect(ctx.Repo.Owner.DashboardLink())
  198. }
  199. }
  200. func Collaboration(ctx *middleware.Context) {
  201. ctx.Data["Title"] = ctx.Tr("repo.settings")
  202. ctx.Data["PageIsSettingsCollaboration"] = true
  203. if ctx.Req.Method == "POST" {
  204. name := strings.ToLower(ctx.Query("collaborator"))
  205. if len(name) == 0 || ctx.Repo.Owner.LowerName == name {
  206. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  207. return
  208. }
  209. u, err := models.GetUserByName(name)
  210. if err != nil {
  211. if models.IsErrUserNotExist(err) {
  212. ctx.Flash.Error(ctx.Tr("form.user_not_exist"))
  213. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  214. } else {
  215. ctx.Handle(500, "GetUserByName", err)
  216. }
  217. return
  218. }
  219. // Check if user is organization member.
  220. if ctx.Repo.Owner.IsOrganization() && ctx.Repo.Owner.IsOrgMember(u.Id) {
  221. ctx.Flash.Info(ctx.Tr("repo.settings.user_is_org_member"))
  222. ctx.Redirect(ctx.Repo.RepoLink + "/settings/collaboration")
  223. return
  224. }
  225. if err = ctx.Repo.Repository.AddCollaborator(u); err != nil {
  226. ctx.Handle(500, "AddCollaborator", err)
  227. return
  228. }
  229. if setting.Service.EnableNotifyMail {
  230. if err = mailer.SendCollaboratorMail(ctx.Render, u, ctx.User, ctx.Repo.Repository); err != nil {
  231. ctx.Handle(500, "SendCollaboratorMail", err)
  232. return
  233. }
  234. }
  235. ctx.Flash.Success(ctx.Tr("repo.settings.add_collaborator_success"))
  236. ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path)
  237. return
  238. }
  239. // Delete collaborator.
  240. remove := strings.ToLower(ctx.Query("remove"))
  241. if len(remove) > 0 && remove != ctx.Repo.Owner.LowerName {
  242. u, err := models.GetUserByName(remove)
  243. if err != nil {
  244. ctx.Handle(500, "GetUserByName", err)
  245. return
  246. }
  247. if err := ctx.Repo.Repository.DeleteCollaborator(u); err != nil {
  248. ctx.Handle(500, "DeleteCollaborator", err)
  249. return
  250. }
  251. ctx.Flash.Success(ctx.Tr("repo.settings.remove_collaborator_success"))
  252. ctx.Redirect(ctx.Repo.RepoLink + "/settings/collaboration")
  253. return
  254. }
  255. users, err := ctx.Repo.Repository.GetCollaborators()
  256. if err != nil {
  257. ctx.Handle(500, "GetCollaborators", err)
  258. return
  259. }
  260. ctx.Data["Collaborators"] = users
  261. ctx.HTML(200, COLLABORATION)
  262. }
  263. func parseOwnerAndRepo(ctx *middleware.Context) (*models.User, *models.Repository) {
  264. owner, err := models.GetUserByName(ctx.Params(":username"))
  265. if err != nil {
  266. if models.IsErrUserNotExist(err) {
  267. ctx.Handle(404, "GetUserByName", err)
  268. } else {
  269. ctx.Handle(500, "GetUserByName", err)
  270. }
  271. return nil, nil
  272. }
  273. repo, err := models.GetRepositoryByName(owner.Id, ctx.Params(":reponame"))
  274. if err != nil {
  275. if models.IsErrRepoNotExist(err) {
  276. ctx.Handle(404, "GetRepositoryByName", err)
  277. } else {
  278. ctx.Handle(500, "GetRepositoryByName", err)
  279. }
  280. return nil, nil
  281. }
  282. return owner, repo
  283. }
  284. func GitHooks(ctx *middleware.Context) {
  285. ctx.Data["Title"] = ctx.Tr("repo.settings.githooks")
  286. ctx.Data["PageIsSettingsGitHooks"] = true
  287. hooks, err := ctx.Repo.GitRepo.Hooks()
  288. if err != nil {
  289. ctx.Handle(500, "Hooks", err)
  290. return
  291. }
  292. ctx.Data["Hooks"] = hooks
  293. ctx.HTML(200, GITHOOKS)
  294. }
  295. func GitHooksEdit(ctx *middleware.Context) {
  296. ctx.Data["Title"] = ctx.Tr("repo.settings.githooks")
  297. ctx.Data["PageIsSettingsGitHooks"] = true
  298. name := ctx.Params(":name")
  299. hook, err := ctx.Repo.GitRepo.GetHook(name)
  300. if err != nil {
  301. if err == git.ErrNotValidHook {
  302. ctx.Handle(404, "GetHook", err)
  303. } else {
  304. ctx.Handle(500, "GetHook", err)
  305. }
  306. return
  307. }
  308. ctx.Data["Hook"] = hook
  309. ctx.HTML(200, GITHOOK_EDIT)
  310. }
  311. func GitHooksEditPost(ctx *middleware.Context) {
  312. name := ctx.Params(":name")
  313. hook, err := ctx.Repo.GitRepo.GetHook(name)
  314. if err != nil {
  315. if err == git.ErrNotValidHook {
  316. ctx.Handle(404, "GetHook", err)
  317. } else {
  318. ctx.Handle(500, "GetHook", err)
  319. }
  320. return
  321. }
  322. hook.Content = ctx.Query("content")
  323. if err = hook.Update(); err != nil {
  324. ctx.Handle(500, "hook.Update", err)
  325. return
  326. }
  327. ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks/git")
  328. }
  329. func DeployKeys(ctx *middleware.Context) {
  330. ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
  331. ctx.Data["PageIsSettingsKeys"] = true
  332. keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
  333. if err != nil {
  334. ctx.Handle(500, "ListDeployKeys", err)
  335. return
  336. }
  337. ctx.Data["Deploykeys"] = keys
  338. ctx.HTML(200, DEPLOY_KEYS)
  339. }
  340. func DeployKeysPost(ctx *middleware.Context, form auth.AddSSHKeyForm) {
  341. ctx.Data["Title"] = ctx.Tr("repo.settings.deploy_keys")
  342. ctx.Data["PageIsSettingsKeys"] = true
  343. keys, err := models.ListDeployKeys(ctx.Repo.Repository.ID)
  344. if err != nil {
  345. ctx.Handle(500, "ListDeployKeys", err)
  346. return
  347. }
  348. ctx.Data["Deploykeys"] = keys
  349. if ctx.HasError() {
  350. ctx.HTML(200, DEPLOY_KEYS)
  351. return
  352. }
  353. content, err := models.CheckPublicKeyString(form.Content)
  354. if err != nil {
  355. if models.IsErrKeyUnableVerify(err) {
  356. ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
  357. } else {
  358. ctx.Data["HasError"] = true
  359. ctx.Data["Err_Content"] = true
  360. ctx.Flash.Error(ctx.Tr("form.invalid_ssh_key", err.Error()))
  361. ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
  362. return
  363. }
  364. }
  365. key, err := models.AddDeployKey(ctx.Repo.Repository.ID, form.Title, content)
  366. if err != nil {
  367. ctx.Data["HasError"] = true
  368. switch {
  369. case models.IsErrKeyAlreadyExist(err):
  370. ctx.Data["Err_Content"] = true
  371. ctx.RenderWithErr(ctx.Tr("repo.settings.key_been_used"), DEPLOY_KEYS, &form)
  372. case models.IsErrKeyNameAlreadyUsed(err):
  373. ctx.Data["Err_Title"] = true
  374. ctx.RenderWithErr(ctx.Tr("repo.settings.key_name_used"), DEPLOY_KEYS, &form)
  375. default:
  376. ctx.Handle(500, "AddDeployKey", err)
  377. }
  378. return
  379. }
  380. log.Trace("Deploy key added: %d", ctx.Repo.Repository.ID)
  381. ctx.Flash.Success(ctx.Tr("repo.settings.add_key_success", key.Name))
  382. ctx.Redirect(ctx.Repo.RepoLink + "/settings/keys")
  383. }
  384. func DeleteDeployKey(ctx *middleware.Context) {
  385. if err := models.DeleteDeployKey(ctx.User, ctx.QueryInt64("id")); err != nil {
  386. ctx.Flash.Error("DeleteDeployKey: " + err.Error())
  387. } else {
  388. ctx.Flash.Success(ctx.Tr("repo.settings.deploy_key_deletion_success"))
  389. }
  390. ctx.JSON(200, map[string]interface{}{
  391. "redirect": ctx.Repo.RepoLink + "/settings/keys",
  392. })
  393. }