Exemplo n.º 1
0
func CreateBuildFromCircleCI(circleci *model.CircleCI) (*model.Build, error) {
	var build model.Build

	err := Transaction(func(tx *gorm.DB) error {
		var err error

		repo, err := CreateRepositoryByOwnerAndName(circleci.Username, circleci.Reponame)
		if err != nil {
			return err
		}

		var commit model.Commit
		err = tx.Where(model.Commit{RepositoryID: repo.ID, Rev: circleci.VCSRevision}).
			Attrs(model.Commit{Branch: circleci.Branch}).
			FirstOrCreate(&commit).
			Error
		if err != nil {
			return err
		}
		commit.Repository = *repo

		err = tx.
			Where(model.Build{CommitID: commit.ID, Service: model.BUILD_SERVICE_CIRCLE_CI}).
			Assign(model.Build{TotalTime: circleci.TotalTime(), Status: circleci.BuildStatus(), ServiceBuildNum: circleci.ServiceBuildNum()}).
			FirstOrInit(&build).
			Error
		if err != nil {
			return err
			return err
		}
		build.Commit = commit

		err = tx.Save(&build).Error
		if err != nil {
			return err
		}

		return nil
	})

	return &build, err
}
Exemplo n.º 2
0
func CreateBuildFromWercker(wercker *model.Wercker) (*model.Build, error) {
	var build model.Build

	err := Transaction(func(tx *gorm.DB) error {
		var err error

		repo, err := CreateRepositoryByOwnerAndName(wercker.Application.Owner.Name, wercker.Application.Name)
		if err != nil {
			return err
		}

		var commit model.Commit
		err = tx.Where(model.Commit{RepositoryID: repo.ID, Rev: wercker.CommitHash}).
			Attrs(model.Commit{Branch: wercker.Branch}).
			FirstOrCreate(&commit).
			Error
		if err != nil {
			return err
		}
		commit.Repository = *repo

		err = tx.
			Where(model.Build{CommitID: commit.ID, Service: model.BUILD_SERVICE_WERCKER}).
			Assign(model.Build{TotalTime: wercker.TotalTime(), Status: wercker.BuildStatus(), ServiceBuildNum: wercker.ServiceBuildNum()}).
			FirstOrInit(&build).
			Error
		if err != nil {
			return err
		}
		build.Commit = commit

		err = tx.Save(&build).Error
		if err != nil {
			return err
		}

		return nil
	})

	return &build, err
}