Exemple #1
0
// ManualCITriggerHandler is a http handler for manually triggering test builds.
func ManualCITriggerHandler(w http.ResponseWriter, r *http.Request) {
	// Checks if the user is signed in and a teacher.
	member, err := checkMemberApproval(w, r, false)
	if err != nil {
		http.Error(w, err.Error(), 404)
		log.Println(err)
		return
	}

	course := r.FormValue("course")
	user := r.FormValue("user")
	lab := r.FormValue("lab")

	if !git.HasOrganization(course) {
		http.Error(w, "Unknown organization", 404)
		return
	}

	org, err := git.NewOrganization(course, true)
	if err != nil {
		http.Error(w, "Organization Error", 404)
		return
	}

	// Defaults back to username or group name for the user if not a teacher.
	if !org.IsTeacher(member) {
		if org.IsMember(member) {
			if strings.Contains(user, "group") {
				if member.Courses[org.Name].IsGroupMember {
					user = "******" + strconv.Itoa(member.Courses[org.Name].GroupNum)
				} else {
					http.Error(w, "Not a group member", 404)
					return
				}
			} else {
				user = member.Username
			}
		} else {
			http.Error(w, "Not a member of the course", 404)
			return
		}
	}

	groupid := -1
	labnum := -1
	if strings.Contains(user, "group") {
		groupid, err = strconv.Atoi(user[len("group"):])
		if err != nil {
			http.Error(w, err.Error(), 500)
			return
		}

		for i, name := range org.GroupLabFolders {
			if name == lab {
				labnum = i
				break
			}
		}
	} else {
		for i, name := range org.IndividualLabFolders {
			if name == lab {
				labnum = i
				break
			}
		}
	}

	var repo string
	var destfolder string
	if _, ok := org.Members[user]; ok {
		repo = user + "-" + git.StandardRepoName
		destfolder = git.StandardRepoName
	} else if _, ok := org.Groups[user]; ok {
		repo = user
		destfolder = git.GroupsRepoName
	} else {
		http.Error(w, "Unknown user", 404)
		return
	}

	opt := ci.DaemonOptions{
		Org:   org.Name,
		User:  user,
		Group: groupid,

		Repo:       repo,
		BaseFolder: org.CI.Basepath,
		LabFolder:  lab,
		LabNumber:  labnum,
		AdminToken: org.AdminToken,
		DestFolder: destfolder,
		Secret:     org.CI.Secret,
		IsPush:     false,
	}

	log.Println(opt)

	ci.StartTesterDaemon(opt)
}
Exemple #2
0
// StartTestBuildProcess will use the payload from github to start the ci build.
func StartTestBuildProcess(load github.PushPayload) (err error) {
	userlogin := *load.Pusher.Name
	reponame := *load.Repo.Name
	orgname := *load.Organization.Login

	if !git.HasMember(userlogin) {
		log.Println("Not a valid user: "******"Not a valid org: ", orgname)
		return errors.New("Not a valid org: " + orgname)
	}

	org, err := git.NewOrganization(orgname, true)
	user, err := git.NewMemberFromUsername(userlogin, true)

	isgroup := !strings.Contains(reponame, "-"+git.StandardRepoName)

	var labfolder string
	var destfolder string
	var labnum int
	var username string
	var gnum = -1
	if isgroup {
		gnum, err = strconv.Atoi(reponame[len("group"):])
		if err != nil {
			log.Println(err)
			return err
		}

		group, err := git.NewGroup(org.Name, gnum, true)
		if err != nil {
			log.Println(err)
			return err
		}

		labnum = group.CurrentLabNum
		if labnum > org.GroupAssignments {
			labnum = org.GroupAssignments
		}
		labfolder = org.GroupLabFolders[labnum]
		username = reponame
		destfolder = git.GroupsRepoName
	} else {
		labnum = user.Courses[org.Name].CurrentLabNum
		if labnum > org.IndividualAssignments {
			labnum = org.IndividualAssignments
		}
		labfolder = org.IndividualLabFolders[labnum]
		username = strings.TrimRight(reponame, "-"+git.StandardRepoName)
		destfolder = git.StandardRepoName
	}

	opt := ci.DaemonOptions{
		Org:        org.Name,
		User:       username,
		Group:      gnum,
		Repo:       reponame,
		BaseFolder: org.CI.Basepath,
		LabFolder:  labfolder,
		LabNumber:  labnum,
		AdminToken: org.AdminToken,
		DestFolder: destfolder,
		IsPush:     true,
		Secret:     org.CI.Secret,
	}

	go ci.StartTesterDaemon(opt)

	return
}