github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/model/project_ref.go (about) 1 package model 2 3 import ( 4 "fmt" 5 6 "github.com/evergreen-ci/evergreen/db" 7 "github.com/evergreen-ci/evergreen/db/bsonutil" 8 "github.com/pkg/errors" 9 "gopkg.in/mgo.v2" 10 "gopkg.in/mgo.v2/bson" 11 ) 12 13 // The ProjectRef struct contains general information, independent of any 14 // revision control system, needed to track a given project 15 type ProjectRef struct { 16 Owner string `bson:"owner_name" json:"owner_name" yaml:"owner"` 17 Repo string `bson:"repo_name" json:"repo_name" yaml:"repo"` 18 Branch string `bson:"branch_name" json:"branch_name" yaml:"branch"` 19 RepoKind string `bson:"repo_kind" json:"repo_kind" yaml:"repokind"` 20 Enabled bool `bson:"enabled" json:"enabled" yaml:"enabled"` 21 Private bool `bson:"private" json:"private" yaml:"private"` 22 BatchTime int `bson:"batch_time" json:"batch_time" yaml:"batchtime"` 23 RemotePath string `bson:"remote_path" json:"remote_path" yaml:"remote_path"` 24 Identifier string `bson:"identifier" json:"identifier" yaml:"identifier"` 25 DisplayName string `bson:"display_name" json:"display_name" yaml:"display_name"` 26 LocalConfig string `bson:"local_config" json:"local_config" yaml:"local_config"` 27 DeactivatePrevious bool `bson:"deactivate_previous" json:"deactivate_previous" yaml:"deactivate_previous"` 28 //Tracked determines whether or not the project is discoverable in the UI 29 Tracked bool `bson:"tracked" json:"tracked"` 30 31 // Admins contain a list of users who are able to access the projects page. 32 Admins []string `bson:"admins" json:"admins"` 33 34 // The "Alerts" field is a map of trigger (e.g. 'task-failed') to 35 // the set of alert deliveries to be processed for that trigger. 36 Alerts map[string][]AlertConfig `bson:"alert_settings" json:"alert_config,omitempty"` 37 38 // RepoDetails contain the details of the status of the consistency 39 // between what is in GitHub and what is in Evergreen 40 RepotrackerError *RepositoryErrorDetails `bson:"repotracker_error" json:"repotracker_error"` 41 } 42 43 // RepositoryErrorDetails indicates whether or not there is an invalid revision and if there is one, 44 // what the guessed merge base revision is. 45 type RepositoryErrorDetails struct { 46 Exists bool `bson:"exists" json:"exists"` 47 InvalidRevision string `bson:"invalid_revision" json:"invalid_revision"` 48 MergeBaseRevision string `bson:"merge_base_revision" json:"merge_base_revision"` 49 } 50 51 type AlertConfig struct { 52 Provider string `bson:"provider" json:"provider"` //e.g. e-mail, flowdock, SMS 53 54 // Data contains provider-specific on how a notification should be delivered. 55 // Typed as bson.M so that the appropriate provider can parse out necessary details 56 Settings bson.M `bson:"settings" json:"settings"` 57 } 58 59 type EmailAlertData struct { 60 Recipients []string `bson:"recipients"` 61 } 62 63 var ( 64 // bson fields for the ProjectRef struct 65 ProjectRefOwnerKey = bsonutil.MustHaveTag(ProjectRef{}, "Owner") 66 ProjectRefRepoKey = bsonutil.MustHaveTag(ProjectRef{}, "Repo") 67 ProjectRefBranchKey = bsonutil.MustHaveTag(ProjectRef{}, "Branch") 68 ProjectRefRepoKindKey = bsonutil.MustHaveTag(ProjectRef{}, "RepoKind") 69 ProjectRefEnabledKey = bsonutil.MustHaveTag(ProjectRef{}, "Enabled") 70 ProjectRefPrivateKey = bsonutil.MustHaveTag(ProjectRef{}, "Private") 71 ProjectRefBatchTimeKey = bsonutil.MustHaveTag(ProjectRef{}, "BatchTime") 72 ProjectRefIdentifierKey = bsonutil.MustHaveTag(ProjectRef{}, "Identifier") 73 ProjectRefDisplayNameKey = bsonutil.MustHaveTag(ProjectRef{}, "DisplayName") 74 ProjectRefDeactivatePreviousKey = bsonutil.MustHaveTag(ProjectRef{}, "DeactivatePrevious") 75 ProjectRefRemotePathKey = bsonutil.MustHaveTag(ProjectRef{}, "RemotePath") 76 ProjectRefTrackedKey = bsonutil.MustHaveTag(ProjectRef{}, "Tracked") 77 ProjectRefLocalConfig = bsonutil.MustHaveTag(ProjectRef{}, "LocalConfig") 78 ProjectRefAlertsKey = bsonutil.MustHaveTag(ProjectRef{}, "Alerts") 79 ProjectRefRepotrackerError = bsonutil.MustHaveTag(ProjectRef{}, "RepotrackerError") 80 ProjectRefAdminsKey = bsonutil.MustHaveTag(ProjectRef{}, "Admins") 81 ) 82 83 const ( 84 ProjectRefCollection = "project_ref" 85 ) 86 87 func (projectRef *ProjectRef) Insert() error { 88 return db.Insert(ProjectRefCollection, projectRef) 89 } 90 91 // FindOneProjectRef gets a project ref given the owner name, the repo 92 // name and the project name 93 func FindOneProjectRef(identifier string) (*ProjectRef, error) { 94 projectRef := &ProjectRef{} 95 err := db.FindOne( 96 ProjectRefCollection, 97 bson.M{ 98 ProjectRefIdentifierKey: identifier, 99 }, 100 db.NoProjection, 101 db.NoSort, 102 projectRef, 103 ) 104 if err == mgo.ErrNotFound { 105 return nil, nil 106 } 107 return projectRef, err 108 } 109 110 // FindAllTrackedProjectRefs returns all project refs in the db 111 // that are currently being tracked (i.e. their project files 112 // still exist) 113 func FindAllTrackedProjectRefs() ([]ProjectRef, error) { 114 projectRefs := []ProjectRef{} 115 err := db.FindAll( 116 ProjectRefCollection, 117 bson.M{ProjectRefTrackedKey: true}, 118 db.NoProjection, 119 db.NoSort, 120 db.NoSkip, 121 db.NoLimit, 122 &projectRefs, 123 ) 124 return projectRefs, err 125 } 126 127 // FindAllProjectRefs returns all project refs in the db 128 func FindAllProjectRefs() ([]ProjectRef, error) { 129 projectRefs := []ProjectRef{} 130 err := db.FindAll( 131 ProjectRefCollection, 132 bson.M{}, 133 db.NoProjection, 134 db.NoSort, 135 db.NoSkip, 136 db.NoLimit, 137 &projectRefs, 138 ) 139 return projectRefs, err 140 } 141 142 // UntrackStaleProjectRefs sets all project_refs in the db not in the array 143 // of project identifiers to "untracked." 144 func UntrackStaleProjectRefs(activeProjects []string) error { 145 _, err := db.UpdateAll( 146 ProjectRefCollection, 147 bson.M{ProjectRefIdentifierKey: bson.M{ 148 "$nin": activeProjects, 149 }}, 150 bson.M{"$set": bson.M{ 151 ProjectRefTrackedKey: false, 152 }}, 153 ) 154 return err 155 } 156 157 // Upsert updates the project ref in the db if an entry already exists, 158 // overwriting the existing ref. If no project ref exists, one is created 159 func (projectRef *ProjectRef) Upsert() error { 160 _, err := db.Upsert( 161 ProjectRefCollection, 162 bson.M{ 163 ProjectRefIdentifierKey: projectRef.Identifier, 164 }, 165 bson.M{ 166 "$set": bson.M{ 167 ProjectRefRepoKindKey: projectRef.RepoKind, 168 ProjectRefEnabledKey: projectRef.Enabled, 169 ProjectRefPrivateKey: projectRef.Private, 170 ProjectRefBatchTimeKey: projectRef.BatchTime, 171 ProjectRefOwnerKey: projectRef.Owner, 172 ProjectRefRepoKey: projectRef.Repo, 173 ProjectRefBranchKey: projectRef.Branch, 174 ProjectRefDisplayNameKey: projectRef.DisplayName, 175 ProjectRefDeactivatePreviousKey: projectRef.DeactivatePrevious, 176 ProjectRefTrackedKey: projectRef.Tracked, 177 ProjectRefRemotePathKey: projectRef.RemotePath, 178 ProjectRefTrackedKey: projectRef.Tracked, 179 ProjectRefLocalConfig: projectRef.LocalConfig, 180 ProjectRefAlertsKey: projectRef.Alerts, 181 ProjectRefRepotrackerError: projectRef.RepotrackerError, 182 ProjectRefAdminsKey: projectRef.Admins, 183 }, 184 }, 185 ) 186 return err 187 } 188 189 // ProjectRef returns a string representation of a ProjectRef 190 func (projectRef *ProjectRef) String() string { 191 return projectRef.Identifier 192 } 193 194 // GetBatchTime returns the Batch Time of the ProjectRef 195 func (p *ProjectRef) GetBatchTime(variant *BuildVariant) int { 196 if variant.BatchTime != nil { 197 return *variant.BatchTime 198 } 199 return p.BatchTime 200 } 201 202 // Location generates and returns the ssh hostname and path to the repo. 203 func (projectRef *ProjectRef) Location() (string, error) { 204 if projectRef.Owner == "" { 205 return "", errors.Errorf("No owner in project ref: %v", projectRef.Identifier) 206 } 207 if projectRef.Repo == "" { 208 return "", errors.Errorf("No repo in project ref: %v", projectRef.Identifier) 209 } 210 return fmt.Sprintf("git@github.com:%v/%v.git", projectRef.Owner, projectRef.Repo), nil 211 }