| // Copyright 2019 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. |
| |
| package main |
| |
| import ( |
| "bytes" |
| "crypto/rand" |
| "encoding/base64" |
| "encoding/json" |
| "flag" |
| "fmt" |
| "io/ioutil" |
| "log" |
| "net/http" |
| "net/rpc" |
| "net/url" |
| "os" |
| "strings" |
| |
| "github.com/google/fmtserver" |
| "github.com/google/fmtserver/gerrit" |
| "github.com/google/slothfs/cookie" |
| ) |
| |
| type wrapJar struct { |
| http.CookieJar |
| } |
| |
| func (w *wrapJar) Cookies(u *url.URL) []*http.Cookie { |
| cs := w.CookieJar.Cookies(u) |
| return cs |
| } |
| |
| func (w *wrapJar) SetCookies(u *url.URL, cs []*http.Cookie) { |
| w.CookieJar.SetCookies(u, cs) |
| cs = w.CookieJar.Cookies(u) |
| } |
| |
| const checkerScheme = "fmt:" |
| |
| func CreateChecker(s *gerrit.Server, repo string) (*gerrit.CheckerInfo, error) { |
| var uuidRandom [20]byte |
| rand.Reader.Read(uuidRandom[:]) |
| |
| uuid := fmt.Sprintf("%s%x", checkerScheme, uuidRandom) |
| in := gerrit.CheckerInput{ |
| UUID: uuid, |
| Name: "fmtserver", |
| Repository: repo, |
| Description: "check source code formatting.", |
| Status: "ENABLED", |
| // TODO: should list all file extensions in the query? |
| } |
| |
| body, err := json.Marshal(&in) |
| if err != nil { |
| return nil, err |
| } |
| |
| content, err := s.PostPath("a/plugins/checks/checkers/", "application/json", body) |
| if err != nil { |
| return nil, err |
| } |
| |
| out := gerrit.CheckerInfo{} |
| if err := gerrit.Unmarshal(content, &out); err != nil { |
| return nil, err |
| } |
| |
| log.Printf("return value: %s", string(content)) |
| if err := json.Unmarshal(content, &out); err != nil { |
| return nil, fmt.Errorf("Unmarshal: %v", err) |
| } |
| |
| log.Printf("created checker %#v", out) |
| return &out, nil |
| } |
| |
| func ListCheckers(g *gerrit.Server) ([]*gerrit.CheckerInfo, error) { |
| c, err := g.GetPath("plugins/checks/checkers/") |
| if err != nil { |
| log.Fatalf("ListCheckers: %v", err) |
| } |
| |
| var out []*gerrit.CheckerInfo |
| if err := gerrit.Unmarshal(c, &out); err != nil { |
| return nil, err |
| } |
| |
| filtered := out[:0] |
| for _, o := range out { |
| if !strings.HasPrefix(o.UUID, checkerScheme) { |
| continue |
| } |
| |
| filtered = append(filtered, o) |
| } |
| return filtered, nil |
| } |
| |
| type gerritChecker struct { |
| server *gerrit.Server |
| fmtClient *rpc.Client |
| } |
| |
| func (c *gerritChecker) checkChange(changeID string) error { |
| ch, err := c.server.GetChange(changeID, "current") |
| if err != nil { |
| return err |
| } |
| req := fmtserver.FormatRequest{} |
| for n, f := range ch.Files { |
| req.Files = append(req.Files, |
| fmtserver.File{ |
| Name: n, |
| Content: f.Content, |
| }) |
| } |
| rep := fmtserver.FormatReply{} |
| if err := c.fmtClient.Call("Server.Format", &req, &rep); err != nil { |
| _, ok := err.(rpc.ServerError) |
| if ok { |
| return fmt.Errorf("server returned: %s", err) |
| } |
| return err |
| } |
| |
| for _, f := range rep.Files { |
| orig := ch.Files[f.Name] |
| if orig == nil { |
| return fmt.Errorf("result had unknown file %q", f.Name) |
| } |
| if !bytes.Equal(f.Content, orig.Content) { |
| msg := f.Message |
| if msg == "" { |
| msg = "needs formatting" |
| } |
| log.Printf("file %s: %s", f.Name, f.Message) |
| } |
| } |
| |
| return nil |
| } |
| |
| func main() { |
| gerritURL := flag.String("gerrit", "", "URL to gerrit host") |
| addr := flag.String("addr", "", "Address of the fmtserver") |
| register := flag.Bool("register", false, "Register with the host") |
| list := flag.Bool("list", false, "List pending checks") |
| agent := flag.String("agent", "fmtserver", "user-agent for the fmtserver.") |
| cookieJar := flag.String("cookies", "", "comma separated paths to cURL-style cookie jar file.") |
| authFile := flag.String("auth_file", "", "file containing user:password") |
| repo := flag.String("repo", "", "the repository (project) name to apply the checker to.") |
| flag.Parse() |
| if *gerritURL == "" { |
| log.Fatal("must set --gerrit") |
| } |
| |
| u, err := url.Parse(*gerritURL) |
| if err != nil { |
| log.Fatalf("url.Parse: %v", err) |
| } |
| |
| g := gerrit.New(*u) |
| |
| if nm := *cookieJar; nm != "" { |
| jar, err := cookie.NewJar(nm) |
| if err != nil { |
| log.Fatal("NewJar(%s): %v", nm, err) |
| } |
| if err := cookie.WatchJar(jar, nm); err != nil { |
| log.Printf("WatchJar: %v", err) |
| log.Println("continuing despite WatchJar failure", err) |
| } |
| g.Client.Jar = &wrapJar{jar} |
| } |
| g.UserAgent = *agent |
| |
| if *authFile == "" { |
| log.Fatal("must set --auth_file") |
| } |
| if content, err := ioutil.ReadFile(*authFile); err != nil { |
| log.Fatal(err) |
| } else { |
| auth := bytes.TrimSpace(content) |
| encoded := make([]byte, base64.StdEncoding.EncodedLen(len(auth))) |
| base64.StdEncoding.Encode(encoded, auth) |
| g.BasicAuth = string(encoded) |
| } |
| |
| // Do a GET first to complete any cookie dance, because POST aren't redirected properly. |
| if _, err := g.GetPath("a/accounts/self"); err != nil { |
| log.Fatalf("accounts/self: %v", err) |
| } |
| |
| if *list { |
| if out, err := ListCheckers(g); err != nil { |
| log.Fatalf("Lits: %v", err) |
| } else { |
| log.Println(out) |
| } |
| |
| os.Exit(0) |
| } |
| |
| if *register { |
| if *repo == "" { |
| log.Fatalf("need to set --repo") |
| } |
| ch, err := CreateChecker(g, *repo) |
| if err != nil { |
| log.Fatalf("CreateChecker: %v", err) |
| } |
| log.Printf("CreateChecker result: %v", ch) |
| os.Exit(0) |
| } |
| |
| if *addr == "" { |
| log.Fatal("must set --addr") |
| } |
| |
| client, err := rpc.DialHTTP("tcp", *addr) |
| if err != nil { |
| log.Fatalf("DialHTTP(%s): %v", *addr, err) |
| } |
| |
| gc := gerritChecker{ |
| server: g, |
| fmtClient: client, |
| } |
| |
| if len(flag.Args()) < 1 { |
| log.Fatal("pass change IDs on command line") |
| } |
| for _, a := range flag.Args() { |
| if err := gc.checkChange(a); err != nil { |
| log.Printf("change %s: %v", a, err) |
| } |
| } |
| } |