Example #1
0
func viewAdminCreateCommand(w http.ResponseWriter, r *http.Request) {
	template := "command/admin/create.html"
	ctx := make(responseContext)

	vars := mux.Vars(r)
	rid, _ := strconv.Atoi(vars["rid"])

	repo, err := database.GetRepositoryByID(int64(rid))

	if err != nil {
		render(w, r, "403.html", make(responseContext)) // TODO: create 500
		return
	}

	ctx["repository"] = repo
	ctx["kinds"] = []string{
		database.CommandKindBuild,
		database.CommandKindDeploy,
		database.CommandKindTest,
	}

	if r.Method == "POST" {
		err := commandAdminForm{}.create(r, repo)

		if err != nil {
			ctx["error"] = err.Error()
		} else {
			uri := fmt.Sprintf("/admin/repository/%d", rid)
			http.Redirect(w, r, uri, 302)
			return
		}
	}

	render(w, r, template, ctx)
}
Example #2
0
func (p *pullRequestCallback) updatePR() {
	for {
		if p.isCurrent() == false {
			log.Println("not current")
			return
		}

		job := database.GetJobByCommit(p.PR.Head.Commit)

		if job.ID == 0 {
			time.Sleep(30 * time.Second)
			continue
		}

		nilTime := time.Time{}
		if !job.TasksFinished.After(nilTime) {
			time.Sleep(10 * time.Second)
			continue
		}

		repository, err := database.GetRepositoryByID(job.RepositoryID)

		if err != nil {
			log.Println(err)
			return
		}

		if repository.ClosePR && job.Passed() == false {
			closePR(job, repository, p.PR.URL)
		}

		return
	}
}
Example #3
0
// Notify sends all relevant notifications for a job that are configured for
// the jobs repository.
func Notify(job *database.Job, event string) {
	repo, err := database.GetRepositoryByID(job.RepositoryID)

	if err != nil {
		log.Println(err)
		return
	}

	sendWebsocket(job, event)

	for _, notificaiton := range repo.Notifications {
		switch notificaiton.Service {
		case database.NotificationServiceEmail:
			sendEmail(job, event)
		case database.NotificationServiceSlack:
			sendSlack(job, event)
		case database.NotificationServiceHipchat:
			sendHipchat(job, event)
		case database.NotificationServiceCampfire:
			sendCampfire(job, event)
		default:
			continue
		}
	}
}
func viewAdminUpdateNotification(w http.ResponseWriter, r *http.Request) {
	template := "notification/admin/update.html"
	ctx := make(responseContext)

	vars := mux.Vars(r)
	rid, _ := strconv.Atoi(vars["rid"])
	nid, _ := strconv.Atoi(vars["nid"])

	repo, err := database.GetRepositoryByID(int64(rid))

	if err != nil {
		render(w, r, "403.html", make(responseContext)) // TODO: create 500
		return
	}

	not, err := database.GetNotification(int64(nid))

	if err != nil {
		render(w, r, "403.html", make(responseContext)) // TODO: create 500
		return
	}

	ctx["repository"] = repo
	ctx["notification"] = not
	ctx["services"] = []string{
		database.NotificationServiceEmail,
		database.NotificationServiceSlack,
		database.NotificationServiceHipchat,
		database.NotificationServiceCampfire,
	}

	if r.Method == "POST" {
		err := notificationAdminForm{}.update(r, not)

		if err == nil {
			ctx["message"] = "Update successful."
		} else {
			ctx["error"] = err.Error()
		}
	}

	render(w, r, template, ctx)
}
Example #5
0
func viewAdminUpdateCommand(w http.ResponseWriter, r *http.Request) {
	template := "command/admin/update.html"
	ctx := make(responseContext)

	vars := mux.Vars(r)
	rid, _ := strconv.Atoi(vars["rid"])
	cid, _ := strconv.Atoi(vars["cid"])

	repo, err := database.GetRepositoryByID(int64(rid))

	if err != nil {
		render(w, r, "403.html", make(responseContext)) // TODO: create 500
		return
	}

	com, err := database.GetCommand(int64(cid))

	if err != nil {
		render(w, r, "403.html", make(responseContext)) // TODO: create 500
		return
	}

	ctx["repository"] = repo
	ctx["command"] = com
	ctx["kinds"] = []string{
		database.CommandKindBuild,
		database.CommandKindDeploy,
		database.CommandKindTest,
	}

	if r.Method == "POST" {
		err := commandAdminForm{}.update(r, com)

		if err == nil {
			ctx["message"] = "Update successful."
		} else {
			ctx["error"] = err.Error()
		}
	}

	render(w, r, template, ctx)
}
Example #6
0
// Runner waits for jobs to be pushed on RunQueue and runs all commands. It also
// creates the command logs and sends the necessary notifications.
func Runner() {
	for {
		queueJob := <-runQueue

		job := database.GetJob(queueJob.JobID)
		repository, err := database.GetRepositoryByID(job.RepositoryID)

		if job.Cancelled == true {
			log.Println("Job cancelled, not running commands", job.ID)
			continue
		}

		if err != nil {
			log.Println("Could not find repository for", job.Repository.URL)
			return
		}

		job.Started()

		run(job, repository, database.CommandKindTest)
		notification.Notify(job, notification.EventTest)

		if job.Passed() && job.ShouldBuild() {
			run(job, repository, database.CommandKindBuild)
			notification.Notify(job, notification.EventBuild)
		}

		job.TasksDone()

		if queueJob.Status != nil {
			queueJob.Status <- true
		}

		if job.Passed() && job.ShouldDeploy() {
			go deploy(job, repository)
		}
	}
}
func viewAdminCreateNotification(w http.ResponseWriter, r *http.Request) {
	template := "notification/admin/create.html"
	ctx := make(responseContext)

	vars := mux.Vars(r)
	rid, _ := strconv.Atoi(vars["rid"])

	repo, err := database.GetRepositoryByID(int64(rid))

	if err != nil {
		render(w, r, "403.html", make(responseContext)) // TODO: create 500
		return
	}

	ctx["repository"] = repo
	ctx["services"] = []string{
		database.NotificationServiceEmail,
		database.NotificationServiceSlack,
		database.NotificationServiceCampfire,
		database.NotificationServiceHipchat,
	}

	if r.Method == "POST" {
		err := notificationAdminForm{}.create(r, repo)

		if err != nil {
			ctx["error"] = err.Error()
		} else {
			uri := fmt.Sprintf("/admin/repository/%d", rid)
			http.Redirect(w, r, uri, 302)
			return
		}
	}

	render(w, r, template, ctx)
}