create_researchrepo 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/usr/bin/env bash
  2. ## prerequisite
  3. ## organisation with a team called readaccessteam that has read access to its repositories
  4. ## bot is owner of the organisation (Token used is linked to a profile with owner access)
  5. set -euo pipefail
  6. if (( $# < 1 )); then
  7. echo "Please provide a project name to create (one word, no spaces)"
  8. exit 1
  9. fi
  10. if (( $# > 3 )); then
  11. echo "Only one or two arguments is supported: Project name (one word, no spaces), project team, organisation"
  12. fi
  13. ## Inputs
  14. organisation=$1
  15. projectname=$2
  16. projectteam=$2
  17. if (( $# > 2 )); then
  18. projectteam=$3
  19. fi
  20. ## Variables
  21. templaterepo="gin4rri/template" # name of template repository
  22. # Token can be created in GIN user settings, under Applications.
  23. # An organisation admin account is required to create teams.
  24. token="f5058edf6a8e7e6ea2c52af474039902b2aa923b"
  25. # Change this to the local installation addresses to test locally.
  26. # Include the protocol scheme http:// or https:// and the port if it's not
  27. # standard for the protocol.
  28. ginhost="https://gin.g-node.org"
  29. # Same as above, change to local address to test locally and include port
  30. # number if it's not 22.
  31. ginssh="gin.g-node.org"
  32. post() {
  33. data=$1
  34. url=$2
  35. curl -H "Content-Type: application/jsonAuthorization" -H "Authorization: token ${token}" -d "${data}" ${url}
  36. }
  37. put() {
  38. url=$1
  39. curl -X PUT -H "Authorization: token ${token}" -d "${url}"
  40. }
  41. createrepo() {
  42. reponame=$1
  43. data="{\"Name\":\"${reponame}\",\"Description\":\"deleteme\",\"Private\":true}"
  44. post ${data} ${ginhost}/api/v1/org/${organisation}/repos
  45. }
  46. createteam() {
  47. teamname=$1
  48. data="{\"name\":\"${teamname}\",\"description\":\"deleteme\",\"permission\":\"admin\"}"
  49. post ${data} ${ginhost}/api/v1/orgs/${organisation}/teams
  50. echo
  51. }
  52. addrepototeam (){
  53. reponame=$1
  54. teamname=$2
  55. # TODO https://github.com/gogs/docs-api/blob/master/Administration/Teams.md needs admin rights?
  56. # PUT /admin/teams/:teamid/repos/:reponame
  57. #data="{\"Name\":\"${teamname}\",\"Description\":\"deleteme\"}"
  58. #post ${data} ${ginhost}/api/v1/admin/orgs/${organisation}/teams
  59. }
  60. issuetrackeroption (){
  61. reponame=$1
  62. enable_issuesopt=$2
  63. # todo https://github.com/gogs/docs-api/tree/master/Repositories
  64. # PATCH /repos/:owner/:repo/issue-tracker
  65. # enable_issues = false
  66. ## maybe link to parent issue tracker instead ?
  67. }
  68. echo "Creating team ${projectname} if it does not already exist"
  69. ##TODO: check team existence:
  70. createteam ${projectname}
  71. echo "Creating project ${projectname}"
  72. createrepo ${projectname}
  73. addrepototeam ${projectname} ${projectname}
  74. echo "Creating raw submodule ${projectname}.raw"
  75. createrepo ${projectname}.raw
  76. addrepototeam ${projectname}.raw ${projectname}
  77. issuetrackeroption ${projectname}.raw false
  78. echo "Creating shared submodule ${projectname}.shared"
  79. createrepo ${projectname}.shared
  80. addrepototeam ${projectname}.shared ${projectname}
  81. addrepototeam ${projectname}.shared readaccessteam # add also to the team with read access in the organisation
  82. issuetrackeroption ${projectname}.shared false
  83. projectdir=${HOME}/datasets/${projectname}
  84. # Clone template
  85. echo "Downloading template repository to ${projectdir}"
  86. datalad install --recursive --source git@${ginssh}:/${templaterepo} ${projectdir}
  87. # change remote URL to project repo and init annex
  88. echo "Configuring new project directory"
  89. git -C ${projectdir} remote set-url origin git@${ginssh}:/${organisation}/${projectname}
  90. git -C ${projectdir} annex init
  91. # change submodule URLs and init annex
  92. echo "Configuring project directories"
  93. git -C ${projectdir} submodule set-url raw git@${ginssh}:/${organisation}/${projectname}.raw
  94. git -C ${projectdir} submodule set-url shared git@${ginssh}:/${organisation}/${projectname}.shared
  95. git -C ${projectdir} submodule sync
  96. git -C ${projectdir} submodule foreach git annex init
  97. datalad -C ${projectdir} add --to-git .gitmodules --message "Initialise submodules"
  98. datalad -C ${projectdir} publish --recursive