From f690392689c883ac49f5488125cc8be091df75aa Mon Sep 17 00:00:00 2001
From: Anya Helene Bagge <anya@ii.uib.no>
Date: Sun, 5 Mar 2023 19:37:44 +0100
Subject: [PATCH] skip wip tests

---
 find_gitlab_users.py     | 11 +++++++++--
 run-tests                | 26 +++++++++++++++++--------
 student_project_setup.py | 41 ++++++++++++++++++++++++++++++++--------
 3 files changed, 60 insertions(+), 18 deletions(-)

diff --git a/find_gitlab_users.py b/find_gitlab_users.py
index b130ddb..304be24 100755
--- a/find_gitlab_users.py
+++ b/find_gitlab_users.py
@@ -33,7 +33,7 @@ for row in rows:
     name = row['sortable_name']
     (lastname, firstname) = [s.strip() for s in name.split(',', 1)]
     (first_firstname,*more_firstnames) = firstname.split()
-    if row.get('userid','') == '':
+    if row.get('gitid','') == '':
         row['gituser'] = ''
 
         users = gl.users.list(username=row['email'].replace('@uib.no','').replace('@student.uib.no',''))
@@ -41,13 +41,18 @@ for row in rows:
         if len(users) == 1:
             user = users[0]
         else:
-            more_users = gl.users.list(username=f'{first_firstname}.{lastname}')
+            more_users = gl.users.list(username=row['login_id'])
             if len(more_users) == 1:
                 user = more_users[0]
+            else:
+                more_users = gl.users.list(username=f'{first_firstname}.{lastname}')
+                if len(more_users) == 1:
+                    user = more_users[0]
 
         if user:
             row['gituser'] = user.username
             row['gitid'] = user.id
+            print("Found", format_user(row))
         elif len(users) == 0:
             if 'uib.no' in row['email']:
                 missing.append(row)            
@@ -76,6 +81,8 @@ if 'gituser' not in fieldnames:
     fieldnames.append('gituser')
 if 'gitid' not in fieldnames:
     fieldnames.append('gitid')
+if 'gitoverride' not in fieldnames:
+    fieldnames.append('gitoverride')
 
 with open('students.csv','w') as csvfile:
     writer = csv.DictWriter(csvfile, fieldnames)
diff --git a/run-tests b/run-tests
index 4f358e8..b1de4ee 100755
--- a/run-tests
+++ b/run-tests
@@ -6,6 +6,8 @@
 # Usage: run-tests [compile] [test]
 #
 
+BUILD_SUCCESSES=0
+BUILD_FAILURES=0
 SUCCESSES=0
 FAILURES=0
 
@@ -15,18 +17,21 @@ compile()
     echo "==========Compiling Test Files=========="
     echo "----------------------------------------"
 
-    for i in $(cd "$TEST_DIR"; find . -name '*_Test*.hs'); do
+    [ -d "$REPORT_DIR" ] || mkdir -p "$REPORT_DIR"
+
+    for i in $(cd "$TEST_DIR"; find . -name '*_Test*.hs' -o -name 'wip*' -prune); do
         i="${i#./}"   # strip leading ./
         o="${i%.hs}"  # strip trailing .hs
+        r=$(echo "$o" | sed -e 's;/;_;g')    #  / → _
 
         mkdir -p "$BUILD_DIR/$(dirname $o)"
 
         echo "--Compiling $i to $o---"
 
-        if ghc -i"$STUDENT_DIR" --make "$TEST_DIR/$i" -o "$BUILD_DIR/$o"; then
-            SUCCESSES=$(( SUCCESSES + 1 ))
+        if ghc -i"$STUDENT_DIR" --make "$TEST_DIR/$i" -o "$BUILD_DIR/$o" | tee "$REPORT_DIR/${r}_ghc.log"; then
+            BUILD_SUCCESSES=$(( BUILD_SUCCESSES + 1 ))
         else
-            FAILURES=$(( FAILURES + 1 ))
+            BUILD_FAILURES=$(( BUILD_FAILURES + 1 ))
         fi
     done
 
@@ -83,6 +88,9 @@ while [ $# -gt 0 ]; do
     elif [ "$1" = "test" ]; then
         run_tests
         shift
+    elif [ "$1" = "clean" ]; then
+         rm -rf "$BUILD_DIR"
+         shift
     elif [ "$1" = "-s" ]; then
         shift
         STUDENT_DIR="$1"
@@ -93,14 +101,16 @@ while [ $# -gt 0 ]; do
         shift
     else
         echo >> /dev/stderr Bad argument: "$1"
-        echo >> /dev/stderr usage: run-tests [-t TEST_DIR] [-s STUDENT_DIR] [compile] [test]
+        echo >> /dev/stderr usage: run-tests [-t TEST_DIR] [-s STUDENT_DIR] [compile] [test] [clean]
         exit 1
     fi
 done
-
-echo $SUCCESSES successful tests, $FAILURES failures
+if [ $BUILD_FAILURES -gt 0 ]; then
+    echo $BUILD_FAILURES tests failed to build
+fi
+echo $SUCCESSES successful tests, $FAILURES test failures
 echo "========================================"
 echo "@-----------------DONE-----------------@"
 echo "========================================"
-[ $FAILURES -eq 0 ]  # set exit code
+[ $FAILURES -eq 0 ]  && [ $BUILD_FAILURES -eq 0 ] # set exit code
 
diff --git a/student_project_setup.py b/student_project_setup.py
index 8f63746..c5f62d2 100755
--- a/student_project_setup.py
+++ b/student_project_setup.py
@@ -119,6 +119,7 @@ class AssignmentFork:
 
     def check_membership(self, resource: Project | Group, user: User | CurrentUser | int):
         user_id = getattr(user, 'id', user)
+        #logger.debug('checking membership for %s %s %s', user, resource, user_id)
         try:
             membership = resource.members.get(user_id)
             if membership.access_level != gitlab.const.OWNER_ACCESS and membership.access_level != self.access_level:
@@ -238,23 +239,24 @@ class AssignmentFork:
         except gitlab.GitlabError as e:
             self.__failed(e, self.assignment)
 
-    def check_user_project(self, user: User | CurrentUser | str | int):
+    def check_user_project(self, user: User | CurrentUser | str | int, details = False, override_project_username = None):
         try:
-            return self.__check_user_project(self.get_user(user))
+            return self.__check_user_project(self.get_user(user), details,override_project_username)
         except gitlab.GitlabError as e:
             self.__failed(e, user)
 
-    def __check_user_project(self, user: User | CurrentUser):
+    def __check_user_project(self, user: User | CurrentUser, details:bool, override_project_username:str|None):
         config = self.gitlab_config.copy()
 
         try:
             config['name'] = f'{user.username} – {self.assignment.name}'
-            project_slug = f'{user.username}_{self.assignment.path}'
+            project_slug = f'{override_project_username or user.username}_{self.assignment.path}'
             self._last_user = user
             self._last_project = project = self.gl.projects.get(
                 f'{self.namespace["full_path"]}/{project_slug}')
             logger.debug('Found project %s for user %s', project.path_with_namespace, user.username)
         except GitlabGetError:
+            details = True
             url = self.assignment.ssh_url_to_repo.replace(
                 f'/{self.assignment.path}.git', f'/{project_slug}.git')
             desc = self.assignment.description
@@ -277,7 +279,7 @@ class AssignmentFork:
             else:
                 project = None
             self.__change('add_fork', project,source=self.assignment)
-        if project != None:
+        if project != None and details:
             self.check_project_setup(user, project, config)
         return project
 
@@ -308,16 +310,29 @@ if __name__ == '__main__':
     args = sys.argv[1:]
     file = None
     assignment = None
-    while len(args) > 1:
+    details = False
+    skip = 0
+    while len(args) > 0:
         if args[0] == '-f':
             file = args[1]
+            print('Students file:', file)
             args = args[2:]
         elif args[0] == '-a':
             assignment = args[1]
+            print('Assignment:', assignment)
+            args = args[2:]
+        elif args[0] == '-d':
+            print('Details enabled!')
+            details = True
+            args = args[1:]
+        elif args[0] == '-s':
+            skip = int(args[1])
+            print('Skipping', skip)
             args = args[2:]
         else:
             raise ValueError(args[0])
 
+    projects : list[tuple[str, Project|None]] = []
     if file == None:
         print("Missing input file")
     elif assignment == None:
@@ -331,9 +346,19 @@ if __name__ == '__main__':
                 n = 0
                 for row in reader:
                     if row['gitid'].isdigit():
-                        assignment.check_user_project(int(row['gitid']))
-                        time.sleep(1)
+                        print(n, row)
+                        if n >= skip:
+                            proj = assignment.check_user_project(int(row['gitid']), details, row.get('gitoverride'))
+                            projects.append((row.get('gitoverride') or row['gituser'], proj))
+                            if details:
+                                time.sleep(1)
+                            else:
+                                time.sleep(0.01)
                         n = n + 1
+                with open('project-links.txt', 'w') as f:
+                    for u,p in projects:
+                        if p:
+                            f.write(f'{u} {p.ssh_url_to_repo}\n')
         finally:
             with open('changes.txt', 'w') as f:
                 f.write(pformat(assignment.changelog, sort_dicts=False))
-- 
GitLab