func (hub *DeploymentEventHub) buildDeploymentEvent(s models.DeploymentState, d *models.Deployment) (*DeploymentEvent, error) {
	user, err := getUser(hub.db, d.UserId)
	if err != nil {
		return nil, err
	}
	d.User = user

	application, err := findApplication(d.ApplicationName)
	if err != nil {
		return nil, err
	}

	target, err := findTarget(application, d.TargetName)
	if err != nil {
		return nil, err
	}

	event := &DeploymentEvent{
		State:       s,
		Deployment:  d,
		Application: application,
		Target:      target,
		User:        user,
	}

	return event, nil
}
예제 #2
0
func updateDeploymentState(db *sql.DB, d *models.Deployment, state models.DeploymentState) error {
	_, err := db.Exec(deploymentUpdateStateStmt, string(state), d.Id)
	if err != nil {
		return err
	}

	d.State = state
	return nil
}
예제 #3
0
func createDeployment(db *sql.DB, d *models.Deployment) error {
	var id int64
	var state models.DeploymentState = models.DEPLOYMENT_NEW
	var createdAt time.Time = time.Now()

	tx, err := db.Begin()
	if err != nil {
		return err
	}
	exists, err := activeDeploymentExists(tx, d.TargetName)
	if err != nil {
		tx.Rollback()
		return err
	}
	if exists {
		tx.Rollback()
		return ErrDeployInProgress
	}

	result, err := tx.Exec(deploymentInsertStmt, d.UserId, d.ApplicationName,
		d.TargetName, d.CommitSha, d.Branch, d.Comment, string(state), createdAt)
	if err != nil {
		tx.Rollback()
		return err
	}

	id, err = result.LastInsertId()
	if err != nil {
		tx.Rollback()
		return err
	}

	d.Id = int(id)
	d.State = state
	d.CreatedAt = createdAt

	tx.Commit()

	return nil
}