Skip to content
Snippets Groups Projects
Commit 78d5c776 authored by Fabian Kongelf's avatar Fabian Kongelf
Browse files

added folder without embedded

parent 2fd50215
No related branches found
No related tags found
No related merge requests found
config.txt
\ No newline at end of file
# Auto git structure
This is a gitlab project containing a script designed to automaticly create or update a gitlab structure of groups and projects. The gitlab structure is created with insperation from NTNU professor Mariusz's design. The script and gitlab structure is part of the result of bachelor project "git da gitt" spring 2022, and is design as an alternativ to established LMS(learning management system).
## How it works
The script will use the variables in config.sh to generate the following gitlab structure.
1. subjectCode
1. -> currentYear
1. -> Projects
1. -> X amount of Tasks
1. -> X amount of student projects in relation to task
1. -> Student-Only-Information
1. -> Private-Information
1. -> Public-Information
The gitlab structure consits of groups and project with various access permitions. The root group is public and named your subject code. Under the root group (first tier of subgroups) are groups named after the year they where generated, this is for archiving purpose.
## Getting started
The script is designed to be run by a NTNU affiliated user, and as of current itterations list of students is retrieved with login.stud.ntnu.no and is only usable for students. The deploy.sh script can be edited to remove the LDAP search as long as the list of students is provided beforehand instead.
1. Create a gitlab token. If you dont have a gitlab user you need to create one. Navigate to your profile and "edit profile", here you can create a gitlab token. The token is needed to use gitlab's api and generate groups and projects.
2. Download the repository. It does not matter to where as you are only goning to run the script.
3. Dependencies:
apt-get install sshpass
4. Edit config.sh to match your data. Insert your gitlab token, change to subject code to match yours and so on.
5. Run the script on an NTNU instance. For instance through this command `ssh YOUR-NTNU-USERNAME@login.stud.ntnu.no`
#!/bin/sh
#Token for access to Gitlab-api
Gitlab_token=""
#User password for login@stud.ntnu.no
ntnu_password=""
#Name of subject, effectively becomes root group in Gitlab course hierarchy
subjectCode="DCSG1005"
#Username for login@stud.ntnu.no
username=""
#Amount of tasks projects to be created for students
amountTasks=1
#Name for output file containing all students
studentList="list_of_students.txt"
#Url for Gitlab API requests are sent to
baseURL="https://gitlab.stud.idi.ntnu.no/api/v4/"
#Name for the first section in URL for subject: for example https://...gitlab/nameOfSubject
path=$emnekode
#Retrieves current year, archive groups are named after the value retreived. For example 2022
groupArchiving=$(date +%Y)
#Name for the group to contain information for the public
groupPublic="Public-Information"
#Name for the group to contain information only available to designated users.
groupPrivate="Private-Information"
#Name of the group to contain information only available to teachers and students
groupStudentOnly="Student-Only-Information"
#Name of group to contain all individual task projects for students
groupProjects="Projects"
#!/bin/sh
######################################################
# Creates a subject group/project hierarchy in Gitlab
# Script is barebones and only meant to serve as an
# example. Furthermore the script currently uses
# login.stud.ntnu.no LDAP server to retrieve a list of
# students from the course defined in ./config.sh
# If the user does not have an account on endpoint
# create the project hierarchy manually as depicted
# in bachelor report. If the list of students, name
# line by line, can be retrieved otherwise simply
# remove the code following "Retrieve list of students
# from NTNU LDAP" comment below.
#
# Author: Nicholas Sellevåg
######################################################
. ./config.sh #Retrieve the configured values in "config.sh" as environment variables
#Retrieve list of students from NTNU LDAP
sshpass -p $ntnu_password ssh $username@login.stud.ntnu.no 'ldapsearch -Z -h at.ntnu.no -D "" -b "ou=groups,dc=ntnu,dc=no" "cn=fs_$subjectCode"' | grep memberUid | awk '{print $2}' > $studentList
#Retrieve all groups user has from gitlab and store their names and id in groups.txt
curl --location --request GET ''${baseURL}'groups?owned=true' --header 'Authorization: Bearer '${Gitlab_token}'' | jq '.[] | "\(.name) \(.id)"' | tr -d '"' > groups.txt
#Read each line in groups and check if the subject code exists already
groups="groups.txt"
while IFS= read -r line;
do
i=$(echo $line | awk '{print $1}')
echo "comparing: ${i} : ${emnekode}"
#Check if the subject code matches retrieved subject groups from gitlab
if [ $i = $subjectCode ]
then
#The subject group exists, store its ID
root_gruppe_id=$(echo $line | awk '{print $2}')
fi
done < "$groups"
#Check if the subject group existed already
if [ -z "$root_gruppe_id" ];
then
#The subject did not exist yet
echo "Root group does not exist yet ${root_gruppe_id}"
#Create the subject
parent_id=$(curl --location --request POST ''${baseURL}'groups?name='${subjectCode}'&path='${path}'&visibility=public' --header 'Authorization: Bearer '${Gitlab_token}'' | jq '.id')
#Create subgroup for archiving
archiving_id=$(curl --location --request POST ''${baseURL}'groups?name='${groupArchiving}'&parent_id='${parent_id}'&path='${groupArchiving}'&visibility=public' --header 'Authorization: Bearer '${Gitlab_token}'' | jq '.id' )
#Create subgroup for Public Info
public_id=$(curl --location --request POST ''${baseURL}'groups?name='${groupPublic}'&parent_id='${archiving_id}'&path='${groupPublic}'&visibility=public' --header 'Authorization: Bearer '${Gitlab_token}'' | jq '.id' )
#Create subgroup for Private Info
private_id=$(curl --location --request POST ''${baseURL}'groups?name='${groupPrivate}'&parent_id='${archiving_id}'&path='${groupPrivate}'&visibility=private' --header 'Authorization: Bearer '${Gitlab_token}'' | jq '.id' )
#Create subgroup for student only information
studentOnly_id=$(curl --location --request POST ''${baseURL}'groups?name='${groupStudentOnly}'&parent_id='${archiving_id}'&path='${groupStudentOnly}'&visibility=private' --header 'Authorization: Bearer '${Gitlab_token}'' | jq '.id' )
#Create subgroup for Projects
projects_id=$(curl --location --request POST ''${baseURL}'groups?name='${groupProjects}'&parent_id='${archiving_id}'&path='${groupProjects}'&visibility=private' --header 'Authorization: Bearer '${Gitlab_token}'' | jq '.id' )
#Create X amount of task groups
i=0
max=$amountTasks
while [ $i -lt $max ]
do
task_id=$(curl --location --request POST ''${baseURL}'groups?name='Task${i}'&parent_id='${projects_id}'&path='Task${i}'&visibility=private' --header 'Authorization: Bearer '${Gitlab_token}'' | jq '.id')
foo=""
while IFS= read -r student;
do
#Create project for student within the task group
studentProject_id=$(curl --location --request POST ''${baseURL}'projects?name='${student}'&visibility=private&namespace_id='${task_id}'' --header 'Authorization: Bearer '${Gitlab_token}'' | jq '.id')
#Retrieve the student's ID
student_id=$(curl --location --request GET ''${baseURL}'users?username='${student}'' --header 'Authorization: Bearer '${Gitlab_token}'' | jq '.[].id')
#Add student to the task project created for them
curl --location --request POST ''${baseURL}'projects/'${studentProject_id}'/members?user_id='${student_id}'&access_level=40' --header 'Authorization: Bearer '${Gitlab_token}''
if [ "$foo" = "" ]
then
foo="$student_id"
else
foo="${foo},$student_id"
fi
done < "$studentList"
true $((i=i+1))
done
#Add student to internal group
curl --location --request POST ''${baseURL}'groups/'${studentOnly_id}'/members?user_id='${foo}'&access_level=40' --header 'Authorization: Bearer '${Gitlab_token}''
else
#The subject does exist already
echo "Subject group already exist ${root_gruppe_id}"
#TODO, verify that the created subject hierarchy was done successfully and matches the following structure:
#SubjectName
# -> currentYear
# -> Projects
# -> x Amount of tasks
# -> x task projects for each student
# -> Private-Information
# -> Public-Information
# -> Student-Only-Information
#Dersom ikke opprett de som mangler/eventuelt endre på de som finnes dersom feil konfigurert, for eks med feil visibility?
fi
# find correct group. {{emnekode}}/projects/{{task}}
# find and save all projects in group
# demote all members in all projects to reporter (20)
# clone all projects (ssh_url_to_repo attribute whene list project)
# conferm clone
# Mabye promote members back to maintainer (40)
#!/bin/sh
source config.sh
# ask for name of task to be submitted
# find group owned
curl --location --request GET 'https://gitlab.stud.idi.ntnu.no/api/v4/groups?owned=true' --header 'Authorization: Bearer '${Gitlab_token}
# match all owned subgroup names to task, NB: year for archiving, task names may be the same
# find all project within group, group id must be found from previous curl
curl --location --request GET 'https://gitlab.stud.idi.ntnu.no/api/v4/groups/'${group_id}'/projects' --header 'Authorization: Bearer '${Gitlab_token}
# for-loop of all the projects
for project in $projects
# list members of a project
curl --location --request GET 'https://gitlab.stud.idi.ntnu.no/api/v4/projects/16022/members' --header 'Authorization: Bearer '${Gitlab_token}
# for all members in project
for member in $project
# demot a member
curl --location --request PUT 'https://gitlab.stud.idi.ntnu.no/api/v4/projects/16022/members/'${member_id}'?access_level=20' --header 'Authorization: Bearer '${Gitlab_token}
# end for-loop of members in project
# clone repo
git clone ${ssh_url_to_repo}
# check if cloned
?
# for all members in project
for member in $project
# promote member
curl --location --request PUT 'https://gitlab.stud.idi.ntnu.no/api/v4/projects/16022/members/'${member_id}'?access_level=40' --header 'Authorization: Bearer '${Gitlab_token}
# end for-loop of members in project
# end for-loop of project in subgroup
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment