Exemplo n.º 1
0
func (a *ABF) makeBuildList(list map[string]interface{}) (*models.List, error) {
	pkg := a.makePkgList(list)

	changelog := ""

	var logs []interface{}
	dig.Get(&list, &logs, "logs")

	for _, v := range logs {
		asserted := v.(map[string]interface{})
		if dig.String(&asserted, "file_name") == "changelog.log" {
			changelog = dig.String(&asserted, "url")
			break
		}
	}

	handleID := to.String(dig.Uint64(&list, "id"))
	handleProject := dig.String(&list, "project", "fullname")
	platform := dig.String(&list, "save_to_repository", "platform", "name")

	bl := models.List{
		Name:     dig.String(&list, "project", "name"),
		Platform: platform,
		Channel:  dig.String(&list, "save_to_repository", "name"),
		Variants: dig.String(&list, "arch", "name"),

		Artifacts: pkg,
		Links: []models.ListLink{
			{
				Name: models.LinkMain,
				URL:  fmt.Sprintf("%s/build_lists/%v", abfURL, handleID),
			},
			{
				Name: models.LinkChangelog,
				URL:  changelog,
			},
			{
				Name: models.LinkSCM,
				URL:  fmt.Sprintf("%s/%v/commits/%v", abfURL, handleProject, platform),
			},
		},
		Changes:   changelog,
		BuildDate: time.Unix(dig.Int64(&list, "updated_at"), 0),

		StageCurrent: models.ListStageNotStarted,
		StageResult:  models.ListRunning,
		Activity: []models.ListActivity{
			{
				UserID:   models.FindUser(models.UserSystem).ID,
				Activity: "Imported this build list from ABF.",
			},
		},

		IntegrationName: "abf",
		IntegrationOne:  "[" + handleID + "]",
		IntegrationTwo:  dig.String(&list, "commit_hash"),
	}

	return &bl, nil
}
Exemplo n.º 2
0
// AdminPostHandler manipulates the central dashboard for kahinah
func AdminPostHandler(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
	adminCheck(r)

	user := r.FormValue("username")
	action := r.FormValue("action")
	permission := r.FormValue("permission")

	if user == "" || (action != "add" && action != "rm") || permission == "" {
		panic(ErrBadRequest)
	}

	modelUser := models.FindUser(user)

	if action == "add" {
		if err := models.DB.Model(modelUser).Association("Permissions").Append(models.UserPermission{
			Permission: permission,
		}).Error; err != nil {
			panic(err)
		}
	} else {
		if err := models.DB.Model(modelUser).Association("Permissions").Delete(models.UserPermission{
			Permission: permission,
		}).Error; err != nil {
			panic(err)
		}
	}

	http.Redirect(rw, r, render.ConvertURL("/admin"), http.StatusTemporaryRedirect)
}
Exemplo n.º 3
0
func PermCheck(r *http.Request, perm string) bool {
	user := Authenticated(r)
	if user != "" {

		model := models.FindUser(user)
		for _, v := range model.Permissions {
			if v.Permission == perm {
				return true
			}
		}

	}
	return false
}
Exemplo n.º 4
0
func PermAbortCheck(r *http.Request, perm string) {
	user := Authenticated(r)
	if user != "" {

		model := models.FindUser(user)
		for _, v := range model.Permissions {
			if v.Permission == perm {
				return
			}
		}

	}

	panic(ErrPermission)
}
Exemplo n.º 5
0
// AdminGetHandler controls the central dashboard for Kahinah.
func AdminGetHandler(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
	adminCheck(r)

	dataRenderer := data.FromContext(ctx)

	toRender := map[string]interface{}{
		"Loc":   0,
		"Tab":   -1,
		"Title": "Admin",
	}

	user := r.FormValue("username")
	if user != "" {
		userModel := models.FindUser(user)
		toRender["User"] = userModel
	}

	var perms []models.UserPermission
	if err := models.DB.Find(&perms).Error; err != nil && err != gorm.ErrRecordNotFound {
		panic(err)
	}

	rendered := map[string][]string{}

	for _, perm := range perms {
		if _, ok := rendered[perm.Permission]; !ok {
			rendered[perm.Permission] = []string{}
		}
		rendered[perm.Permission] = append(rendered[perm.Permission], models.FindUserByID(perm.UserID).Username)
	}

	toRender["Permissions"] = rendered

	dataRenderer.Data = toRender
	dataRenderer.Template = "admin"
}
Exemplo n.º 6
0
// BuildPostHandler handles post actions that occur to the current active stage.
func BuildPostHandler(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
	// check for authentication
	user := models.FindUser(MustAuthenticate(r))

	// setup
	dataRenderer := data.FromContext(ctx)

	// read parameters
	id := to.Uint64(pat.Param(ctx, "id"))
	target := r.FormValue("target") // either activity or process
	name := r.FormValue("name")     // activity (ignored), process - find process
	action := r.FormValue("action") // activity (ignored), process passed on
	value := r.FormValue("value")   // activity (comment), process passed on

	// find the build list
	var pkg models.List
	if err := models.DB.Where("id = ?", id).First(&pkg).Related(&pkg.Stages, "Stages").Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			panic(ErrNotFound)
		} else {
			panic(err)
		}
	}

	var result interface{}

	// act based on target
	switch target {
	case "activity":
		if value == "" {
			panic(ErrBadRequest)
		}
		pkg.AddActivity(user, value)
		result = map[string]interface{}{
			"success": true,
		}
	case "process":
		// load up the stage & process
		if err := models.DB.Related(&pkg.Stages, "Stages").Error; err != nil {
			panic(err)
		}
		var currentStage *models.ListStage
		for _, v := range pkg.Stages {
			if v.Name == pkg.StageCurrent {
				currentStage = &v
				break
			}
		}

		if currentStage == nil {
			panic(ErrNoCurrentStage)
		}

		if err := models.DB.Related(&currentStage.Processes).Error; err != nil {
			panic(err)
		}

		var selectedProcess *models.ListStageProcess
		for _, v := range currentStage.Processes {
			if v.Name == name {
				selectedProcess = &v
				break
			}
		}

		if selectedProcess == nil {
			panic(ErrBadRequest)
		}

		// initialise the process
		process, err := processes.BuildProcess(selectedProcess)
		if err != nil {
			panic(err)
		}

		r, err := process.APIRequest(user, action, value)
		if err != nil {
			result = map[string]interface{}{
				"error":   true,
				"message": err.Error(),
				"result":  r,
			}
		} else {
			result = r
		}
	default:
		panic(ErrBadRequest)
	}

	dataRenderer.Type = data.DataJSON
	dataRenderer.Data = result

	//http.Redirect(rw, r, r.URL.String(), http.StatusTemporaryRedirect)
}