| /* |
| Copyright 2013 Google Inc. All Rights Reserved. |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| */ |
| |
| /* |
| Presubmitter polls GWT's Gerrit server and runs presubmit checks when |
| new changes are detected. |
| |
| To use presubmitter, you need to first setup a JSON-formatted creds file |
| like: |
| |
| { |
| "GerritUsername": "git-azurediamond.bash.org", |
| "GerritPassword": "hunter2", |
| "JenkinsToken": "opensesame" |
| } |
| |
| By default, presubmitter looks for this file at $HOME/.gwtcreds. This |
| can be changed using the -creds flag. |
| |
| Additionally, presubmitter expects to be run from the top-level |
| directory of a GWT git repository that it can use for checking out |
| uploaded changes. |
| |
| Lastly, presubmitter expects to find the checkstyle JAR at |
| ../tools/antlib/checkstyle-all-4.2.jar, but this can also be changed using |
| the -checkstylejar flag. |
| */ |
| package main |
| |
| import ( |
| "flag" |
| "log" |
| "sync" |
| "time" |
| |
| "gwt.googlesource.com/buildglue.git/gerrit" |
| "gwt.googlesource.com/buildglue.git/jenkins" |
| "gwt.googlesource.com/buildglue.git/lint" |
| ) |
| |
| const query = "(project:gwt status:open -label:Verified=1 -label:Verified=-1 -label:Code-Review=-1 -label:Code-Review=-2 ownerin:\"Jenkins Trusted\" -age:1w) OR (project:gwt status:open -label:Verified=1 -label:Verified=-1 label:Code-Review=2 -age:1w)" |
| |
| const timeout = 90 * time.Minute |
| |
| // Running lint mutates the current directory, so we need to ensure we serialize it. |
| var lintLock sync.Mutex |
| |
| func main() { |
| flag.Parse() |
| |
| nextRetry := make(map[string]time.Time) |
| for { |
| refs, err := gerrit.Query(query) |
| if err != nil { |
| log.Println("error querying gerrit:", err) |
| time.Sleep(time.Minute) |
| continue |
| } |
| log.Println("found refs:", refs) |
| |
| now := time.Now() |
| for _, ref := range refs { |
| if now.Before(nextRetry[ref]) { |
| continue |
| } |
| // TODO(mdempsky): Exponential back off. |
| nextRetry[ref] = now.Add(timeout) |
| |
| log.Println("verifying ref:", ref) |
| go func(ref string) { |
| lintLock.Lock() |
| if err := lint.Lint(ref); err != nil { |
| lintLock.Unlock() |
| log.Println("lint error:", err) |
| return |
| } |
| lintLock.Unlock() |
| |
| if err := jenkins.RunPresubmit(ref); err != nil { |
| log.Println("presubmit error:", err) |
| return |
| } |
| }(ref) |
| } |
| |
| time.Sleep(time.Minute) |
| } |
| } |