| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | |
| 3 // that can be found in the LICENSE file. | |
| 4 | |
| 5 package main | |
| 6 | |
| 7 import ( | |
| 8 "fmt" | |
| 9 "io/ioutil" | |
| 10 "os" | |
| 11 | |
| 12 "github.com/luci/gae/impl/prod" | |
| 13 log "github.com/luci/luci-go/common/logging" | |
| 14 "github.com/luci/luci-go/common/logging/gologger" | |
| 15 "github.com/luci/luci-go/milo/appengine/backend/git" | |
| 16 "github.com/luci/luci-go/milo/appengine/model" | |
| 17 "github.com/maruel/subcommands" | |
| 18 "golang.org/x/net/context" | |
| 19 // Imported so you can upload this as a "module" to app engine, and acce
ss | |
| 20 // the remote API. | |
| 21 _ "google.golang.org/appengine/remote_api" | |
| 22 ) | |
| 23 | |
| 24 var application = &subcommands.DefaultApplication{ | |
| 25 Name: "backfill", | |
| 26 Title: "Backfill Build and Revision data into the milo backend from vari
ous data sources.", | |
| 27 Commands: []*subcommands.Command{ | |
| 28 gitCmd, | |
| 29 subcommands.CmdHelp, | |
| 30 }, | |
| 31 } | |
| 32 | |
| 33 func main() { | |
| 34 os.Exit(subcommands.Run(application, nil)) | |
| 35 } | |
| 36 | |
| 37 // commandRunBase is the base of all backfill subcommands. It defines the common | |
| 38 // flags dryRun and remoteURL | |
| 39 type commandRunBase struct { | |
| 40 subcommands.CommandRunBase | |
| 41 dryRun bool | |
| 42 remoteURL string | |
| 43 } | |
| 44 | |
| 45 func (c *commandRunBase) Init() { | |
| 46 c.Flags.StringVar(&c.remoteURL, "remote-url", "luci-milo.appspot.com", "
the URL of the server to connect to via the remote API. Do NOT include the proto
col (\"https://\")") | |
| 47 c.Flags.BoolVar(&c.dryRun, "dryrun", true, "if this run is a dryrun, and
should make no modifications to the datastore.") | |
| 48 } | |
| 49 | |
| 50 //////////////////////////////////////////////////////////////////////////////// | |
| 51 // Git | |
| 52 | |
| 53 var gitCmd = &subcommands.Command{ | |
| 54 UsageLine: "git", | |
| 55 | |
| 56 ShortDesc: "runs the local git backfiller", | |
| 57 | |
| 58 LongDesc: "Runs the local git backfiller. Reads history from a local git
repo and uploads revision info to cloud datastore at remoteURL.", | |
| 59 | |
| 60 CommandRun: func() subcommands.CommandRun { | |
| 61 c := &cmdGitRun{} | |
| 62 c.Init() | |
| 63 c.Flags.StringVar(&c.gitLog, "git-log", "", "file containing out
put of git log --topo-order --reverse -z --format=format:'%H,%P,%ae,%ct,%b'") | |
| 64 c.Flags.StringVar(&c.repoURL, "repo-url", "", "the repo URL in t
he Revision entity group") | |
| 65 return c | |
| 66 }, | |
| 67 } | |
| 68 | |
| 69 type cmdGitRun struct { | |
| 70 commandRunBase | |
| 71 gitLog string | |
| 72 repoURL string | |
| 73 } | |
| 74 | |
| 75 func (c *cmdGitRun) Run(a subcommands.Application, args []string, _ subcommands.
Env) int { | |
| 76 cfg := gologger.LoggerConfig{ | |
| 77 Format: "%{message}", | |
| 78 Out: os.Stdout, | |
| 79 } | |
| 80 ctx := cfg.Use(context.Background()) | |
| 81 | |
| 82 if c.gitLog == "" || c.remoteURL == "" || c.repoURL == "" { | |
| 83 log.Errorf(ctx, "Flags -git-log, -remote-url, and -repo-url must
all be set.") | |
| 84 return 1 | |
| 85 } | |
| 86 | |
| 87 // We need a gae context even in --dry-run mode to get the repository da
tastore key. | |
| 88 if err := prod.UseRemote(&ctx, c.remoteURL, nil); err != nil { | |
| 89 log.Errorf(ctx, "%s (remote URL: %s)", err, c.remoteURL) | |
| 90 return 1 | |
| 91 } | |
| 92 | |
| 93 contents, err := ioutil.ReadFile(c.gitLog) | |
| 94 if err != nil { | |
| 95 log.Errorf(ctx, "%s", err) | |
| 96 return 1 | |
| 97 } | |
| 98 | |
| 99 revisions, err := git.GetRevisions(string(contents)) | |
| 100 if err != nil { | |
| 101 log.Errorf(ctx, "%s", err) | |
| 102 return 1 | |
| 103 } | |
| 104 | |
| 105 for _, revision := range revisions { | |
| 106 revision.Repository = &model.GetRepository(ctx, c.repoURL).Key | |
| 107 } | |
| 108 | |
| 109 if c.dryRun { | |
| 110 log.Infof(ctx, "Running in dry run mode, writing to stdout.") | |
| 111 for _, r := range revisions { | |
| 112 fmt.Printf("%+v\n", *r) | |
| 113 } | |
| 114 return 0 | |
| 115 } | |
| 116 | |
| 117 log.Infof(ctx, "Saving %d revisions.", len(revisions)) | |
| 118 if err := git.SaveRevisions(ctx, revisions); err != nil { | |
| 119 log.Errorf(ctx, "%s", err) | |
| 120 return 1 | |
| 121 } | |
| 122 | |
| 123 return 0 | |
| 124 } | |
| OLD | NEW |