| OLD | NEW |
| 1 package buildbot | 1 package buildbot |
| 2 | 2 |
| 3 import ( | 3 import ( |
| 4 "database/sql" |
| 4 "encoding/json" | 5 "encoding/json" |
| 5 "fmt" | 6 "fmt" |
| 6 | 7 |
| 7 "github.com/jmoiron/sqlx" | 8 "github.com/jmoiron/sqlx" |
| 8 ) | 9 ) |
| 9 | 10 |
| 10 // replaceIntoDB is a helper function for pushing a BuildStep into the database | 11 // replaceIntoDB is a helper function for pushing a BuildStep into the database |
| 11 // which is used by both BuildStep.ReplaceIntoDB and Build.ReplaceIntoDB, so | 12 // which is used by both BuildStep.ReplaceIntoDB and Build.ReplaceIntoDB, so |
| 12 // that the latter can be done in a single transaction. | 13 // that the latter can be done in a single transaction. |
| 13 func (b BuildStep) replaceIntoDB(tx *sqlx.Tx) error { | 14 func (b BuildStep) replaceIntoDB(tx *sqlx.Tx) error { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 34 // replaceIntoDB does the rollback if needed. | 35 // replaceIntoDB does the rollback if needed. |
| 35 return err | 36 return err |
| 36 } | 37 } |
| 37 if err = tx.Commit(); err != nil { | 38 if err = tx.Commit(); err != nil { |
| 38 tx.Rollback() | 39 tx.Rollback() |
| 39 return fmt.Errorf("Failed to commit the transaction: %v", err) | 40 return fmt.Errorf("Failed to commit the transaction: %v", err) |
| 40 } | 41 } |
| 41 return nil | 42 return nil |
| 42 } | 43 } |
| 43 | 44 |
| 45 // GetCommitsForBuild retrieves the list of commits first built in the given |
| 46 // build from the database. |
| 47 func GetCommitsForBuild(master, builder string, buildNumber int) ([]string, erro
r) { |
| 48 stmt, err := DB.Preparex(fmt.Sprintf("SELECT revision FROM %s WHERE mast
er = ? AND builder = ? AND number = ?", TABLE_BUILD_REVISIONS)) |
| 49 if err != nil { |
| 50 return nil, fmt.Errorf("Unable to retrieve build revisions from
database - failed to prepare query: %v", err) |
| 51 } |
| 52 commits := []string{} |
| 53 if err := stmt.Select(&commits, master, builder, buildNumber); err != ni
l { |
| 54 return nil, fmt.Errorf("Unable to retrieve build revisions from
database: %v", err) |
| 55 } |
| 56 return commits, nil |
| 57 } |
| 58 |
| 59 // GetBuildForCommit retrieves the build number of the build which first |
| 60 // included the given commit. |
| 61 func GetBuildForCommit(master, builder, commit string) (int, error) { |
| 62 stmt, err := DB.Preparex(fmt.Sprintf("SELECT number FROM %s WHERE master
= ? AND builder = ? AND revision = ?", TABLE_BUILD_REVISIONS)) |
| 63 if err != nil { |
| 64 return -1, fmt.Errorf("Unable to retrieve build number from data
base - failed to repare query: %v", err) |
| 65 } |
| 66 n := -1 |
| 67 if err := stmt.Get(&n, master, builder, commit); err != nil { |
| 68 if err == sql.ErrNoRows { |
| 69 // No build includes this commit. |
| 70 return -1, nil |
| 71 } |
| 72 return -1, fmt.Errorf("Unable to retrieve build number from data
base: %v", err) |
| 73 } |
| 74 return n, nil |
| 75 } |
| 76 |
| 44 // GetBuildFromDB retrieves the given build from the database as specified by | 77 // GetBuildFromDB retrieves the given build from the database as specified by |
| 45 // the given master, builder, and build number. | 78 // the given master, builder, and build number. |
| 46 func GetBuildFromDB(master, builder string, buildNumber int) (*Build, error) { | 79 func GetBuildFromDB(master, builder string, buildNumber int) (*Build, error) { |
| 47 // Get the build itself. | 80 // Get the build itself. |
| 48 stmt, err := DB.Preparex(fmt.Sprintf("SELECT * FROM %s WHERE master = ?
AND builder = ? AND number = ?", TABLE_BUILDS)) | 81 stmt, err := DB.Preparex(fmt.Sprintf("SELECT * FROM %s WHERE master = ?
AND builder = ? AND number = ?", TABLE_BUILDS)) |
| 49 if err != nil { | 82 if err != nil { |
| 50 return nil, fmt.Errorf("Unable to retrieve build from database -
failed to prepare query: %v", err) | 83 return nil, fmt.Errorf("Unable to retrieve build from database -
failed to prepare query: %v", err) |
| 51 } | 84 } |
| 52 build := Build{} | 85 build := Build{} |
| 53 if err = stmt.Get(&build, master, builder, buildNumber); err != nil { | 86 if err = stmt.Get(&build, master, builder, buildNumber); err != nil { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 75 if err := stepStmt.Select(&steps, master, builder, buildNumber); err !=
nil { | 108 if err := stepStmt.Select(&steps, master, builder, buildNumber); err !=
nil { |
| 76 return nil, fmt.Errorf("Unable to retrieve build steps from data
base: %v", err) | 109 return nil, fmt.Errorf("Unable to retrieve build steps from data
base: %v", err) |
| 77 } | 110 } |
| 78 for _, s := range steps { | 111 for _, s := range steps { |
| 79 s.Times = []float64{s.Started, s.Finished} | 112 s.Times = []float64{s.Started, s.Finished} |
| 80 s.ResultsRaw = []interface{}{float64(s.Results), []interface{}{}
} | 113 s.ResultsRaw = []interface{}{float64(s.Results), []interface{}{}
} |
| 81 } | 114 } |
| 82 build.Steps = steps | 115 build.Steps = steps |
| 83 | 116 |
| 84 // Get the commits associated with this build. | 117 // Get the commits associated with this build. |
| 85 » cmtStmt, err := DB.Preparex(fmt.Sprintf("SELECT revision FROM %s where m
aster = ? and builder = ? and number = ?", TABLE_BUILD_REVISIONS)) | 118 » build.Commits, err = GetCommitsForBuild(master, builder, buildNumber) |
| 86 if err != nil { | 119 if err != nil { |
| 87 » » return nil, fmt.Errorf("Unable to retrieve build revisions from
database - failed to prepare query: %v", err) | 120 » » return nil, fmt.Errorf("Could not retrieve commits for build: %v
", err) |
| 88 } | 121 } |
| 89 commits := []string{} | |
| 90 if err := cmtStmt.Select(&commits, master, builder, buildNumber); err !=
nil { | |
| 91 return nil, fmt.Errorf("Unable to retrieve build revisions from
database: %v", err) | |
| 92 } | |
| 93 build.Commits = commits | |
| 94 | 122 |
| 95 return &build, nil | 123 return &build, nil |
| 96 } | 124 } |
| 97 | 125 |
| 98 // ReplaceIntoDB inserts or updates the Build in the database. | 126 // ReplaceIntoDB inserts or updates the Build in the database. |
| 99 func (b Build) ReplaceIntoDB() error { | 127 func (b Build) ReplaceIntoDB() error { |
| 100 // Insert the build itself. | 128 // Insert the build itself. |
| 101 tx, err := DB.Beginx() | 129 tx, err := DB.Beginx() |
| 102 if err != nil { | 130 if err != nil { |
| 103 return fmt.Errorf("Unable to push build into database - Could no
t begin transaction: %v", err) | 131 return fmt.Errorf("Unable to push build into database - Could no
t begin transaction: %v", err) |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 stmt, err := DB.Preparex(fmt.Sprintf("SELECT master, builder, number FRO
M %s WHERE finished = 0;", TABLE_BUILDS)) | 223 stmt, err := DB.Preparex(fmt.Sprintf("SELECT master, builder, number FRO
M %s WHERE finished = 0;", TABLE_BUILDS)) |
| 196 if err != nil { | 224 if err != nil { |
| 197 return nil, fmt.Errorf("Unable to retrieve unfinished builds - C
ould not prepare statement: %v", err) | 225 return nil, fmt.Errorf("Unable to retrieve unfinished builds - C
ould not prepare statement: %v", err) |
| 198 } | 226 } |
| 199 b := []*Build{} | 227 b := []*Build{} |
| 200 if err = stmt.Select(&b); err != nil { | 228 if err = stmt.Select(&b); err != nil { |
| 201 return nil, fmt.Errorf("Unable to retrieve unfinished builds: %v
", err) | 229 return nil, fmt.Errorf("Unable to retrieve unfinished builds: %v
", err) |
| 202 } | 230 } |
| 203 return b, nil | 231 return b, nil |
| 204 } | 232 } |
| OLD | NEW |