func main() { token := os.Getenv("TOKEN") host := os.Getenv("HOST") repo := os.Getenv("REPO") branch := os.Getenv("BRANCH") client := drone.NewClientToken(host, token) owner, name := parseRepo(repo) log.Println(token) log.Println(host) log.Println(repo) log.Println(branch) // get the latest build for the specified repository build, err := client.BuildLast(owner, name, branch) if err != nil { log.Printf("Error: unable to get latest build for %s.\n", repo) os.Exit(1) } // start a new build _, err = client.BuildFork(owner, name, build.Number) if err != nil { log.Printf("Error: unable to trigger a new build for %s.\n", repo) log.Panic(err) } log.Printf("Starting new build %d for %s\n", build.Number, repo) }
func appInit(c *cli.Context) { droneToken := c.String("drone-token") if droneToken == "" { logrus.Fatal("Drone API token is missing") } droneURL := c.String("drone-url") if droneURL == "" { logrus.Fatal("Drone CI server URL is missing") } if len(c.Args()) < 0 { logrus.Fatal("No path to watch found") } path := c.Args()[0] droneClient := &Drone{drone.NewClientToken(droneURL, droneToken)} poolClient, err := NewPoolClient(metadataURL) if err != nil { logrus.Fatal(err) } run(droneClient, poolClient, path, c.Int("poll-interval")) }
// Exec runs the plugin func (p *Plugin) Exec() error { if len(p.Token) == 0 { return fmt.Errorf("Error: you must provide your Drone access token.") } if len(p.Server) == 0 { return fmt.Errorf("Error: you must provide your Drone server.") } client := drone.NewClientToken(p.Server, p.Token) for _, entry := range p.Repos { // parses the repository name in owner/name@branch format owner, name, branch := parseRepoBranch(entry) if len(owner) == 0 || len(name) == 0 { return fmt.Errorf("Error: unable to parse repository name %s.\n", entry) } if p.Fork { // get the latest build for the specified repository build, err := client.BuildLast(owner, name, branch) if err != nil { return fmt.Errorf("Error: unable to get latest build for %s.\n", entry) } // start a new build _, err = client.BuildFork(owner, name, build.Number) if err != nil { return fmt.Errorf("Error: unable to trigger a new build for %s.\n", entry) } fmt.Printf("Starting new build %d for %s\n", build.Number, entry) } else { // get the latest build for the specified repository build, err := client.BuildLast(owner, name, branch) if err != nil { return fmt.Errorf("Error: unable to get latest build for %s.\n", entry) } // rebuild the latest build _, err = client.BuildStart(owner, name, build.Number) if err != nil { return fmt.Errorf("Error: unable to trigger build for %s.\n", entry) } fmt.Printf("Restarting build %d for %s\n", build.Number, entry) } } return nil }
func main() { flag.Parse() // create the drone client and get the Drone user client := drone.NewClientToken(*scheme+"://"+*host, *token) user, err := client.Self() if err != nil { log.Fatal(err) } userJson, err := json.Marshal(user) if err != nil { log.Fatal(err) } // serve the static html page http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { index, err := ioutil.ReadFile("index.html") if err != nil { log.Println(err) } out := strings.Replace( string(index), "window.STATE_FROM_SERVER = {}", "window.STATE_FROM_SERVER={user:"******"}", -1) w.Write([]byte(out)) }) // serve static content from the filesystem http.Handle("/static/", http.StripPrefix("/static/", http.FileServer( http.Dir("dist/"), ), ), ) // proxy all requests to beta.drone.io http.Handle("/api/", &httputil.ReverseProxy{ Director: func(req *http.Request) { req.URL.Scheme = *scheme req.URL.Host = *host req.Host = *host req.Header.Set("X-Forwarded-For", *host) req.Header.Set("X-Forwarded-Proto", *scheme) req.Header.Set("Authorization", "Bearer "+*token) }, }) http.ListenAndServe(":9000", nil) }
// handle wraps the command function handlers and // sets up the environment. func handle(c *cli.Context, fn handlerFunc) { var token = c.GlobalString("token") var server = c.GlobalString("server") // if no server url is provided we can default // to the hosted Drone service. if len(server) == 0 { fmt.Println("Error: you must provide the Drone server address.") os.Exit(1) } if len(token) == 0 { fmt.Println("Error: you must provide your Drone access token.") os.Exit(1) } // create the drone client client := drone.NewClientToken(server, token) // handle the function if err := fn(c, client); err != nil { println(err.Error()) os.Exit(1) } }
func main() { fmt.Printf("Drone Downstream Plugin built at %s\n", buildDate) v := new(Params) s := new(drone.System) plugin.Param("system", s) plugin.Param("vargs", v) plugin.MustParse() // if no server url is provided we can default // to the hosted Drone service. if len(v.Token) == 0 { fmt.Println("Error: you must provide your Drone access token.") os.Exit(1) } if v.Server == "" { v.Server = s.Link } // create the drone client client := drone.NewClientToken(v.Server, v.Token) for _, entry := range v.Repos { // parses the repository name in owner/name@branch format owner, name, branch := parseRepoBranch(entry) if len(owner) == 0 || len(name) == 0 { fmt.Printf("Error: unable to parse repository name %s.\n", entry) os.Exit(1) } if v.Fork { // get the latest build for the specified repository build, err := client.BuildLast(owner, name, branch) if err != nil { fmt.Printf("Error: unable to get latest build for %s.\n", entry) os.Exit(1) } // start a new build _, err = client.BuildFork(owner, name, build.Number) if err != nil { fmt.Printf("Error: unable to trigger a new build for %s.\n", entry) os.Exit(1) } fmt.Printf("Starting new build %d for %s\n", build.Number, entry) } else { // get the latest build for the specified repository build, err := client.BuildLast(owner, name, branch) if err != nil { fmt.Printf("Error: unable to get latest build for %s.\n", entry) os.Exit(1) } // rebuild the latest build _, err = client.BuildStart(owner, name, build.Number) if err != nil { fmt.Printf("Error: unable to trigger build for %s.\n", entry) os.Exit(1) } fmt.Printf("Restarting build %d for %s\n", build.Number, entry) } } }