mirror of
https://github.com/dolthub/dolt.git
synced 2026-05-20 03:00:43 -05:00
/go/{cmd,libraries}: wip, do nothing if workflow is up to date
This commit is contained in:
@@ -17,8 +17,10 @@ package ci
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/fatih/color"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/dolthub/dolt/go/cmd/dolt/cli"
|
||||
"github.com/dolthub/dolt/go/cmd/dolt/commands"
|
||||
@@ -132,7 +134,14 @@ func (cmd ImportCmd) Exec(ctx context.Context, commandStr string, args []string,
|
||||
|
||||
err = wr.StoreAndCommit(sqlCtx, db, workflowConfig)
|
||||
if err != nil {
|
||||
return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
|
||||
errorText := err.Error()
|
||||
switch {
|
||||
case strings.Contains("nothing to commit", errorText):
|
||||
cli.Println(color.CyanString(fmt.Sprintf("Dolt CI Workflow '%s' up to date.", workflowConfig.Name)))
|
||||
return 0
|
||||
default:
|
||||
return commands.HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), usage)
|
||||
}
|
||||
}
|
||||
|
||||
return 0
|
||||
|
||||
@@ -52,7 +52,7 @@ func init() {
|
||||
// HasDoltPrefix returns a boolean whether or not the provided string is prefixed with the DoltNamespace. Users should
|
||||
// not be able to create tables in this reserved namespace.
|
||||
func HasDoltPrefix(s string) bool {
|
||||
return strings.HasPrefix(strings.ToLower(s), DoltNamespace)
|
||||
return strings.HasPrefix(strings.ToLower(s), DoltNamespace) && !HasDoltCIPrefix(s)
|
||||
}
|
||||
|
||||
// HasDoltCIPrefix returns a boolean whether or not the provided string is prefixed with the DoltCINamespace. Users should
|
||||
|
||||
@@ -161,6 +161,8 @@ func CreateDoltCITables(ctx *sql.Context, db sqle.Database, commiterName, commit
|
||||
return err
|
||||
}
|
||||
|
||||
// todo: refactor to use query func
|
||||
// todo: then after creating all the tables, run delete from workflows to create all indexes on tables
|
||||
err := createDoltCITables(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
package dolt_ci
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
@@ -66,7 +67,40 @@ func ParseWorkflowConfig(r io.Reader) (workflow *WorkflowConfig, err error) {
|
||||
}
|
||||
|
||||
func ValidateWorkflowConfig(workflow *WorkflowConfig) error {
|
||||
// todo: ensure branch names exist only once for each event
|
||||
// todo: ensure activities exist only once for each event
|
||||
if workflow.On.Push != nil {
|
||||
|
||||
branches := make(map[string]bool)
|
||||
for _, branch := range workflow.On.Push.Branches {
|
||||
_, ok := branches[branch]
|
||||
if ok {
|
||||
return fmt.Errorf("invalid config: on push branch duplicated: %s", branch)
|
||||
} else {
|
||||
branches[branch] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if workflow.On.PullRequest != nil {
|
||||
branches := make(map[string]bool)
|
||||
for _, branch := range workflow.On.Push.Branches {
|
||||
_, ok := branches[branch]
|
||||
if ok {
|
||||
return fmt.Errorf("invalid config: on pull request branch duplicated: %s", branch)
|
||||
} else {
|
||||
branches[branch] = true
|
||||
}
|
||||
}
|
||||
|
||||
activities := make(map[string]bool)
|
||||
for _, activity := range workflow.On.PullRequest.Activities {
|
||||
_, ok := activities[activity]
|
||||
if ok {
|
||||
return fmt.Errorf("invalid config: on pull request activities duplicated: %s", activity)
|
||||
} else {
|
||||
activities[activity] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -72,11 +72,11 @@ func (d *doltWorkflowManager) selectAllFromWorkflowEventsTableByWorkflowNameQuer
|
||||
}
|
||||
|
||||
func (d *doltWorkflowManager) selectAllFromWorkflowEventsTableByWorkflowNameWhereEventTypeIsPullRequestQuery(workflowName string) string {
|
||||
return fmt.Sprintf("select * from %s where `%s` = '%s' and `%s` is %d;", doltdb.WorkflowEventsTableName, doltdb.WorkflowEventsWorkflowNameFkColName, workflowName, doltdb.WorkflowEventsEventTypeColName, WorkflowEventTypePullRequest)
|
||||
return fmt.Sprintf("select * from %s where `%s` = '%s' and `%s` = %d;", doltdb.WorkflowEventsTableName, doltdb.WorkflowEventsWorkflowNameFkColName, workflowName, doltdb.WorkflowEventsEventTypeColName, WorkflowEventTypePullRequest)
|
||||
}
|
||||
|
||||
func (d *doltWorkflowManager) selectAllFromWorkflowEventsTableByWorkflowNameWhereEventTypeIsPushQuery(workflowName string) string {
|
||||
return fmt.Sprintf("select * from %s where `%s` = '%s' and `%s` is %d;", doltdb.WorkflowEventsTableName, doltdb.WorkflowEventsWorkflowNameFkColName, workflowName, doltdb.WorkflowEventsEventTypeColName, WorkflowEventTypePush)
|
||||
return fmt.Sprintf("select * from %s where `%s` = '%s' and `%s` = %d;", doltdb.WorkflowEventsTableName, doltdb.WorkflowEventsWorkflowNameFkColName, workflowName, doltdb.WorkflowEventsEventTypeColName, WorkflowEventTypePush)
|
||||
}
|
||||
|
||||
func (d *doltWorkflowManager) selectAllFromWorkflowJobsTableByWorkflowNameQuery(workflowName string) string {
|
||||
@@ -151,7 +151,7 @@ func (d *doltWorkflowManager) insertIntoWorkflowStepsTableQuery(stepName, jobID
|
||||
|
||||
func (d *doltWorkflowManager) insertIntoWorkflowSavedQueryStepsTableQuery(savedQueryName, stepID string, expectedResultsType int) (string, string) {
|
||||
savedQueryStepID := uuid.NewString()
|
||||
return savedQueryStepID, fmt.Sprintf("insert into %s (`%s`, `%s`, `%s`, `%s`) values ('%s', '%s', '%s', %d);", doltdb.WorkflowSavedQueryStepsTableName, doltdb.WorkflowSavedQueryStepsIdPkColName, doltdb.WorkflowSavedQueryStepsWorkflowStepIdFkColName, doltdb.WorkflowSavedQueryStepsSavedQueryNameColName, doltdb.WorkflowSavedQueryStepsExpectedResultsTypeColName, savedQueryStepID, stepID, savedQueryName, expectedResultsType, stepID)
|
||||
return savedQueryStepID, fmt.Sprintf("insert into %s (`%s`, `%s`, `%s`, `%s`) values ('%s', '%s', '%s', %d);", doltdb.WorkflowSavedQueryStepsTableName, doltdb.WorkflowSavedQueryStepsIdPkColName, doltdb.WorkflowSavedQueryStepsWorkflowStepIdFkColName, doltdb.WorkflowSavedQueryStepsSavedQueryNameColName, doltdb.WorkflowSavedQueryStepsExpectedResultsTypeColName, savedQueryStepID, stepID, savedQueryName, expectedResultsType)
|
||||
}
|
||||
|
||||
func (d *doltWorkflowManager) insertIntoWorkflowSavedQueryStepExpectedRowColumnResultsTableQuery(savedQueryStepID string, expectedColumnComparisonType, expectedRowComparisonType int, expectedColumnCount, expectedRowCount int64) (string, string) {
|
||||
@@ -575,7 +575,16 @@ func (d *doltWorkflowManager) validateWorkflowTables(ctx *sql.Context) error {
|
||||
}
|
||||
|
||||
func (d *doltWorkflowManager) commitWorkflow(ctx *sql.Context, workflowName string) error {
|
||||
return d.sqlWriteQuery(ctx, fmt.Sprintf("CALL DOLT_COMMIT('-Am' 'Successfully stored workflow: %s', '--author', '%s <%s>');", workflowName, d.commiterName, d.commiterEmail))
|
||||
// stage table in reverse order so child tables
|
||||
// are staged before parent tables
|
||||
for i := len(ExpectedDoltCITablesOrdered) - 1; i >= 0; i-- {
|
||||
tableName := ExpectedDoltCITablesOrdered[i]
|
||||
err := d.sqlWriteQuery(ctx, fmt.Sprintf("CALL DOLT_ADD('%s');", tableName))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return d.sqlWriteQuery(ctx, fmt.Sprintf("CALL DOLT_COMMIT('-m' 'Successfully stored workflow: %s', '--author', '%s <%s>');", workflowName, d.commiterName, d.commiterEmail))
|
||||
}
|
||||
|
||||
func (d *doltWorkflowManager) sqlWriteQuery(ctx *sql.Context, query string) error {
|
||||
@@ -923,15 +932,32 @@ func (d *doltWorkflowManager) updateExistingWorkflow(ctx *sql.Context, config *W
|
||||
// handle push
|
||||
if config.On.Push != nil {
|
||||
if len(config.On.Push.Branches) == 0 {
|
||||
err := d.deletePushWorkflowEvents(ctx, WorkflowName(config.Name))
|
||||
|
||||
events, err := d.listWorkflowEventsByWorkflowNameWhereEventTypeIsPush(ctx, WorkflowName(config.Name))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = d.writeWorkflowEventRow(ctx, WorkflowName(config.Name), WorkflowEventTypePush)
|
||||
if err != nil {
|
||||
return err
|
||||
if len(events) == 0 {
|
||||
_, err = d.writeWorkflowEventRow(ctx, WorkflowName(config.Name), WorkflowEventTypePush)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
for _, event := range events {
|
||||
triggers, err := d.listWorkflowEventTriggersByEventIdWhereEventTriggerTypeIsBranches(ctx, *event.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, trigger := range triggers {
|
||||
err = d.deleteWorkflowEventTrigger(ctx, *trigger.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
// todo: create a map of config branches
|
||||
configBranches := make(map[string]string)
|
||||
|
||||
Reference in New Issue
Block a user