#!/usr/bin/env bash ## prerequisite ## organisation with a team called readaccessteam that has read access to its repositories ## bot is owner of the organisation (Token used is linked to a profile with owner access) set -euo pipefail if (( $# < 1 )); then echo "Please provide a project name to create (one word, no spaces)" exit 1 fi if (( $# > 3 )); then echo "Only one or two arguments is supported: Project name (one word, no spaces), project team, organisation" fi ## Inputs organisation=$1 projectname=$2 projectteam=$2 if (( $# > 2 )); then projectteam=$3 fi ## Variables templaterepo="gin4rri/template" # name of template repository # Token can be created in GIN user settings, under Applications. # An organisation admin account is required to create teams. token="f5058edf6a8e7e6ea2c52af474039902b2aa923b" # Change this to the local installation addresses to test locally. # Include the protocol scheme http:// or https:// and the port if it's not # standard for the protocol. ginhost="https://gin.g-node.org" # Same as above, change to local address to test locally and include port # number if it's not 22. ginssh="gin.g-node.org" post() { data=$1 url=$2 curl -H "Content-Type: application/jsonAuthorization" -H "Authorization: token ${token}" -d "${data}" ${url} } put() { url=$1 curl -X PUT -H "Authorization: token ${token}" -d "${url}" } createrepo() { reponame=$1 data="{\"Name\":\"${reponame}\",\"Description\":\"deleteme\",\"Private\":true}" post ${data} ${ginhost}/api/v1/org/${organisation}/repos } createteam() { teamname=$1 data="{\"name\":\"${teamname}\",\"description\":\"deleteme\",\"permission\":\"admin\"}" post ${data} ${ginhost}/api/v1/orgs/${organisation}/teams echo } addrepototeam (){ reponame=$1 teamname=$2 # TODO https://github.com/gogs/docs-api/blob/master/Administration/Teams.md needs admin rights? # PUT /admin/teams/:teamid/repos/:reponame #data="{\"Name\":\"${teamname}\",\"Description\":\"deleteme\"}" #post ${data} ${ginhost}/api/v1/admin/orgs/${organisation}/teams } issuetrackeroption (){ reponame=$1 enable_issuesopt=$2 # todo https://github.com/gogs/docs-api/tree/master/Repositories # PATCH /repos/:owner/:repo/issue-tracker # enable_issues = false ## maybe link to parent issue tracker instead ? } echo "Creating team ${projectname} if it does not already exist" ##TODO: check team existence: createteam ${projectname} echo "Creating project ${projectname}" createrepo ${projectname} addrepototeam ${projectname} ${projectname} echo "Creating raw submodule ${projectname}.raw" createrepo ${projectname}.raw addrepototeam ${projectname}.raw ${projectname} issuetrackeroption ${projectname}.raw false echo "Creating shared submodule ${projectname}.shared" createrepo ${projectname}.shared addrepototeam ${projectname}.shared ${projectname} addrepototeam ${projectname}.shared readaccessteam # add also to the team with read access in the organisation issuetrackeroption ${projectname}.shared false projectdir=${HOME}/datasets/${projectname} # Clone template echo "Downloading template repository to ${projectdir}" datalad install --recursive --source git@${ginssh}:/${templaterepo} ${projectdir} # change remote URL to project repo and init annex echo "Configuring new project directory" git -C ${projectdir} remote set-url origin git@${ginssh}:/${organisation}/${projectname} git -C ${projectdir} annex init # change submodule URLs and init annex echo "Configuring project directories" git -C ${projectdir} submodule set-url raw git@${ginssh}:/${organisation}/${projectname}.raw git -C ${projectdir} submodule set-url shared git@${ginssh}:/${organisation}/${projectname}.shared git -C ${projectdir} submodule sync git -C ${projectdir} submodule foreach git annex init datalad -C ${projectdir} add --to-git .gitmodules --message "Initialise submodules" datalad -C ${projectdir} publish --recursive