// PostUserSync accepts a request to post user sync // // POST /api/user/sync // func PostUserSync(c web.C, w http.ResponseWriter, r *http.Request) { var ctx = context.FromC(c) var user = ToUser(c) if user == nil { w.WriteHeader(http.StatusUnauthorized) return } var remote = remote.Lookup(user.Remote) if remote == nil { w.WriteHeader(http.StatusNotFound) return } if user.Syncing { w.WriteHeader(http.StatusConflict) return } user.Syncing = true if err := datastore.PutUser(ctx, user); err != nil { w.WriteHeader(http.StatusNotFound) return } go sync.SyncUser(ctx, user, remote) w.WriteHeader(http.StatusNoContent) return }
// PostHook accepts a post-commit hook and parses the payload // in order to trigger a build. The payload is specified to the // remote system (ie GitHub) and will therefore get parsed by // the appropriate remote plugin. // // POST /api/repos/{host}/{owner}/{name}/branches/{branch}/commits/{commit} // func PostCommit(c web.C, w http.ResponseWriter, r *http.Request) { var ctx = context.FromC(c) var ( branch = c.URLParams["branch"] hash = c.URLParams["commit"] host = c.URLParams["host"] repo = ToRepo(c) remote = remote.Lookup(host) ) commit, err := datastore.GetCommitSha(ctx, repo, branch, hash) if err != nil { w.WriteHeader(http.StatusNotFound) return } if commit.Status == model.StatusStarted || commit.Status == model.StatusEnqueue { w.WriteHeader(http.StatusConflict) return } commit.Status = model.StatusEnqueue commit.Started = 0 commit.Finished = 0 commit.Duration = 0 if err := datastore.PutCommit(ctx, commit); err != nil { w.WriteHeader(http.StatusInternalServerError) return } owner, err := datastore.GetUser(ctx, repo.UserID) if err != nil { w.WriteHeader(http.StatusBadRequest) return } // Request a new token and update user_token, err := remote.GetToken(owner) if user_token != nil { owner.Access = user_token.AccessToken owner.Secret = user_token.RefreshToken owner.TokenExpiry = user_token.Expiry datastore.PutUser(ctx, owner) } else if err != nil { w.WriteHeader(http.StatusBadRequest) return } // drop the items on the queue go worker.Do(ctx, &worker.Work{ User: owner, Repo: repo, Commit: commit, Host: httputil.GetURL(r), }) w.WriteHeader(http.StatusOK) }
// PostUser accepts a request to create a new user in the // system. The created user account is returned in JSON // format if successful. // // POST /api/users/:host/:login // func PostUser(c web.C, w http.ResponseWriter, r *http.Request) { var ctx = context.FromC(c) var ( host = c.URLParams["host"] login = c.URLParams["login"] ) var remote = remote.Lookup(host) if remote == nil { w.WriteHeader(http.StatusNotFound) return } // not sure I love this, but POST now flexibly accepts the oauth_token for // GitHub as either application/x-www-form-urlencoded OR as applcation/json // with this format: // { "oauth_token": "...." } var oauthToken string switch cnttype := r.Header.Get("Content-Type"); cnttype { case "application/json": var out interface{} err := json.NewDecoder(r.Body).Decode(&out) if err == nil { if val, ok := out.(map[string]interface{})["oauth_token"]; ok { oauthToken = val.(string) } } case "application/x-www-form-urlencoded": oauthToken = r.PostForm.Get("oauth_token") default: // we don't recognize the content-type, but it isn't worth it // to error here log.Printf("PostUser(%s) Unknown 'Content-Type': %s)", r.URL, cnttype) } account := model.NewUser(host, login, "", oauthToken) if err := datastore.PostUser(ctx, account); err != nil { w.WriteHeader(http.StatusBadRequest) return } // borrowed this concept from login.go. upon first creation we // may trying syncing the user's repositories. account.Syncing = account.IsStale() if err := datastore.PutUser(ctx, account); err != nil { log.Println(err) w.WriteHeader(http.StatusBadRequest) return } if account.Syncing { log.Println("sync user account.", account.Login) // sync inside a goroutine go sync.SyncUser(ctx, account, remote) } json.NewEncoder(w).Encode(account) }
// PostRepo accapets a request to activate the named repository // in the datastore. It returns a 201 status created if successful // // POST /api/repos/:host/:owner/:name // func PostRepo(c web.C, w http.ResponseWriter, r *http.Request) { var ctx = context.FromC(c) var repo = ToRepo(c) var user = ToUser(c) // update the repo active flag and fields repo.Active = true repo.PullRequest = true repo.PostCommit = true repo.UserID = user.ID repo.Timeout = 3600 // default to 1 hour // generate a secret key for post-commit hooks if len(repo.Token) == 0 { repo.Token = model.GenerateToken() } // generates the rsa key if len(repo.PublicKey) == 0 || len(repo.PrivateKey) == 0 { key, err := sshutil.GeneratePrivateKey() if err != nil { w.WriteHeader(http.StatusInternalServerError) return } repo.PublicKey = sshutil.MarshalPublicKey(&key.PublicKey) repo.PrivateKey = sshutil.MarshalPrivateKey(key) } var remote = remote.Lookup(repo.Host) if remote == nil { w.WriteHeader(http.StatusNotFound) return } // setup the post-commit hook with the remote system and // if necessary, register the public key var hook = fmt.Sprintf("%s/api/hook/%s/%s", httputil.GetURL(r), repo.Remote, repo.Token) if err := remote.Activate(user, repo, hook); err != nil { w.WriteHeader(http.StatusInternalServerError) return } if err := datastore.PutRepo(ctx, repo); err != nil { w.WriteHeader(http.StatusInternalServerError) return } w.WriteHeader(http.StatusCreated) json.NewEncoder(w).Encode(repo) }
// PostUserSync accepts a request to post user sync // // POST /api/user/sync // func PostUserSync(c web.C, w http.ResponseWriter, r *http.Request) { var ctx = context.FromC(c) var user = ToUser(c) if user == nil { w.WriteHeader(http.StatusUnauthorized) return } var remote = remote.Lookup(user.Remote) if remote == nil { w.WriteHeader(http.StatusNotFound) return } if user.Syncing { w.WriteHeader(http.StatusConflict) return } // Request a new token and update user_token, err := remote.GetToken(user) if user_token != nil { user.Access = user_token.AccessToken user.Secret = user_token.RefreshToken user.TokenExpiry = user_token.Expiry } else if err != nil { w.WriteHeader(http.StatusNotFound) return } user.Syncing = true if err := datastore.PutUser(ctx, user); err != nil { w.WriteHeader(http.StatusNotFound) return } go sync.SyncUser(ctx, user, remote) w.WriteHeader(http.StatusNoContent) return }
// DelRepo accepts a request to delete the named // repository. // // DEL /api/repos/:host/:owner/:name // func DelRepo(c web.C, w http.ResponseWriter, r *http.Request) { var ctx = context.FromC(c) var repo = ToRepo(c) // completely remove the repository from the database var user = ToUser(c) var remote = remote.Lookup(repo.Host) if remote == nil { log.Printf("[ERROR] no remote for host '%s' found", repo.Host) } else { // Request a new token and update user_token, err := remote.GetToken(user) if err != nil { log.Printf("[ERROR] no token for user '%s' on remote '%s' ", user.Email, repo.Host) } else { if user_token != nil { user.Access = user_token.AccessToken user.Secret = user_token.RefreshToken user.TokenExpiry = user_token.Expiry datastore.PutUser(ctx, user) } // setup the post-commit hook with the remote system and // and deactiveate this hook/user on the remote system var hook = fmt.Sprintf("%s/api/hook/%s/%s", httputil.GetURL(r), repo.Remote, repo.Token) if err := remote.Deactivate(user, repo, hook); err != nil { log.Printf("[ERROR] deactivate on remote '%s' failed: %s", repo.Host, err) } } } // fail through: if any of the actions on the remote failed // we try to delete the repo in our datastore anyway if err := datastore.DelRepo(ctx, repo); err != nil { w.WriteHeader(http.StatusInternalServerError) } else { w.WriteHeader(http.StatusNoContent) } }
// PostHook accepts a post-commit hook and parses the payload // in order to trigger a build. The payload is specified to the // remote system (ie GitHub) and will therefore get parsed by // the appropriate remote plugin. // // GET /api/hook/:host // func PostHook(c web.C, w http.ResponseWriter, r *http.Request) { var ctx = context.FromC(c) var host = c.URLParams["host"] var token = c.URLParams["token"] var remote = remote.Lookup(host) if remote == nil { w.WriteHeader(http.StatusNotFound) return } // parse the hook payload hook, err := remote.ParseHook(r) if err != nil { log.Printf("Unable to parse hook. %s\n", err) w.WriteHeader(http.StatusBadRequest) return } // in some cases we have neither a hook nor error. An example // would be GitHub sending a ping request to the URL, in which // case we'll just exit quiely with an 'OK' if hook == nil || strings.Contains(hook.Message, "[CI SKIP]") { w.WriteHeader(http.StatusOK) return } // fetch the repository from the database repo, err := datastore.GetRepoName(ctx, remote.GetHost(), hook.Owner, hook.Repo) if err != nil { w.WriteHeader(http.StatusNotFound) return } // each hook contains a token to verify the sender. If the token // is not provided or does not match, exit if len(repo.Token) == 0 || repo.Token != token { log.Printf("Rejected post commit hook for %s. Token mismatch\n", repo.Name) w.WriteHeader(http.StatusUnauthorized) return } if repo.Active == false || (repo.PostCommit == false && len(hook.PullRequest) == 0) || (repo.PullRequest == false && len(hook.PullRequest) != 0) { w.WriteHeader(http.StatusNotFound) return } // fetch the user from the database that owns this repo user, err := datastore.GetUser(ctx, repo.UserID) if err != nil { w.WriteHeader(http.StatusNotFound) return } // Request a new token and update user_token, err := remote.GetToken(user) if user_token != nil { user.Access = user_token.AccessToken user.Secret = user_token.RefreshToken user.TokenExpiry = user_token.Expiry datastore.PutUser(ctx, user) } else if err != nil { log.Printf("Unable to refresh token. %s\n", err) w.WriteHeader(http.StatusBadRequest) return } // featch the .drone.yml file from the database yml, err := remote.GetScript(user, repo, hook) if err != nil { log.Printf("Unable to fetch .drone.yml file. %s\n", err) w.WriteHeader(http.StatusBadRequest) return } // verify the commit hooks branch matches the list of approved // branches (unless it is a pull request). Note that we don't really // care if parsing the yaml fails here. s, _ := script.ParseBuild(string(yml)) if len(hook.PullRequest) == 0 && !s.MatchBranch(hook.Branch) { w.WriteHeader(http.StatusOK) return } commit := model.Commit{ RepoID: repo.ID, Status: model.StatusEnqueue, Sha: hook.Sha, Branch: hook.Branch, PullRequest: hook.PullRequest, Timestamp: hook.Timestamp, Message: hook.Message, Config: string(yml), } commit.SetAuthor(hook.Author) // inserts the commit into the database if err := datastore.PostCommit(ctx, &commit); err != nil { log.Printf("Unable to persist commit %s@%s. %s\n", commit.Sha, commit.Branch, err) w.WriteHeader(http.StatusBadRequest) return } owner, err := datastore.GetUser(ctx, repo.UserID) if err != nil { log.Printf("Unable to retrieve repository owner. %s.\n", err) w.WriteHeader(http.StatusBadRequest) return } // drop the items on the queue go worker.Do(ctx, &worker.Work{ User: owner, Repo: repo, Commit: &commit, Host: httputil.GetURL(r), }) w.WriteHeader(http.StatusOK) }
// GetLogin accepts a request to authorize the user and to // return a valid OAuth2 access token. The access token is // returned as url segment #access_token // // GET /login/:host // func GetLogin(c web.C, w http.ResponseWriter, r *http.Request) { var ctx = context.FromC(c) var host = c.URLParams["host"] var redirect = "/" var remote = remote.Lookup(host) if remote == nil { w.WriteHeader(http.StatusNotFound) return } // authenticate the user login, err := remote.Authorize(w, r) if err != nil { log.Println(err) w.WriteHeader(http.StatusBadRequest) return } else if login == nil { // in this case we probably just redirected // the user, so we can exit with no error return } // get the user from the database u, err := datastore.GetUserLogin(ctx, host, login.Login) if err != nil { // if self-registration is disabled we should // return a notAuthorized error. the only exception // is if no users exist yet in the system we'll proceed. if capability.Enabled(ctx, capability.Registration) == false { users, err := datastore.GetUserList(ctx) if err != nil || len(users) != 0 { log.Println("Unable to create account. Registration is closed") w.WriteHeader(http.StatusForbidden) return } } // create the user account u = model.NewUser(remote.GetKind(), login.Login, login.Email) u.Name = login.Name u.SetEmail(login.Email) // insert the user into the database if err := datastore.PostUser(ctx, u); err != nil { log.Println(err) w.WriteHeader(http.StatusBadRequest) return } // if this is the first user, they // should be an admin. if u.ID == 1 { u.Admin = true } } // update the user access token // in case it changed in GitHub u.Access = login.Access u.Secret = login.Secret u.Name = login.Name u.SetEmail(login.Email) u.Syncing = u.IsStale() if err := datastore.PutUser(ctx, u); err != nil { log.Println(err) w.WriteHeader(http.StatusBadRequest) return } // look at the last synchronized date to determine if // we need to re-sync the account. // // todo(bradrydzewski) this should move to a server/sync package and // should be injected into this struct, just like the database code. // // todo(bradrydzewski) this login should be a bit more intelligent // than the current implementation. if u.Syncing { redirect = "/sync" log.Println("sync user account.", u.Login) // sync inside a goroutine go sync.SyncUser(ctx, u, remote) } token, err := session.GenerateToken(ctx, r, u) if err != nil { log.Println(err) w.WriteHeader(http.StatusInternalServerError) return } redirect = redirect + "#access_token=" + token http.Redirect(w, r, redirect, http.StatusSeeOther) }
// PostHook accepts a post-commit hook and parses the payload // in order to trigger a build. The payload is specified to the // remote system (ie GitHub) and will therefore get parsed by // the appropriate remote plugin. // // GET /api/hook/:host // func PostHook(c web.C, w http.ResponseWriter, r *http.Request) { var ctx = context.FromC(c) var host = c.URLParams["host"] var remote = remote.Lookup(host) if remote == nil { w.WriteHeader(http.StatusNotFound) return } // parse the hook payload hook, err := remote.ParseHook(r) if err != nil { w.WriteHeader(http.StatusBadRequest) return } // in some cases we have neither a hook nor error. An example // would be GitHub sending a ping request to the URL, in which // case we'll just exit quiely with an 'OK' if hook == nil || strings.Contains(hook.Message, "[CI SKIP]") { w.WriteHeader(http.StatusOK) return } // fetch the repository from the database repo, err := datastore.GetRepoName(ctx, remote.GetHost(), hook.Owner, hook.Repo) if err != nil { w.WriteHeader(http.StatusNotFound) return } if repo.Active == false || (repo.PostCommit == false && len(hook.PullRequest) == 0) || (repo.PullRequest == false && len(hook.PullRequest) != 0) { w.WriteHeader(http.StatusNotFound) return } // fetch the user from the database that owns this repo user, err := datastore.GetUser(ctx, repo.UserID) if err != nil { w.WriteHeader(http.StatusNotFound) return } // featch the .drone.yml file from the database yml, err := remote.GetScript(user, repo, hook) if err != nil { w.WriteHeader(http.StatusBadRequest) return } // verify the commit hooks branch matches the list of approved // branches (unless it is a pull request). Note that we don't really // care if parsing the yaml fails here. s, _ := script.ParseBuild(string(yml)) if len(hook.PullRequest) == 0 && !s.MatchBranch(hook.Branch) { w.WriteHeader(http.StatusOK) return } commit := model.Commit{ RepoID: repo.ID, Status: model.StatusEnqueue, Sha: hook.Sha, Branch: hook.Branch, PullRequest: hook.PullRequest, Timestamp: hook.Timestamp, Message: hook.Message, Config: string(yml), } commit.SetAuthor(hook.Author) // inserts the commit into the database if err := datastore.PostCommit(ctx, &commit); err != nil { w.WriteHeader(http.StatusBadRequest) return } owner, err := datastore.GetUser(ctx, repo.UserID) if err != nil { w.WriteHeader(http.StatusBadRequest) return } // drop the items on the queue go worker.Do(ctx, &worker.Work{ User: owner, Repo: repo, Commit: &commit, Host: httputil.GetURL(r), }) w.WriteHeader(http.StatusOK) }
// GetLogin accepts a request to authorize the user and to // return a valid OAuth2 access token. The access token is // returned as url segment #access_token // // GET /login/:host // func GetLogin(c web.C, w http.ResponseWriter, r *http.Request) { var ctx = context.FromC(c) var host = c.URLParams["host"] var redirect = "/" var remote = remote.Lookup(host) if remote == nil { w.WriteHeader(http.StatusNotFound) return } // authenticate the user login, err := remote.Authorize(w, r) if err != nil { log.Println(err) w.WriteHeader(http.StatusBadRequest) return } else if login == nil { // in this case we probably just redirected // the user, so we can exit with no error return } // get the user from the database u, err := datastore.GetUserLogin(ctx, host, login.Login) if err != nil { // if self-registration is disabled we should // return a notAuthorized error. the only exception // is if no users exist yet in the system we'll proceed. if capability.Enabled(ctx, capability.Registration) == false { users, err := datastore.GetUserList(ctx) if err != nil || len(users) != 0 { log.Println("Unable to create account. Registration is closed") w.WriteHeader(http.StatusForbidden) return } } // create the user account u = model.NewUser(remote.GetKind(), login.Login, login.Email) u.Name = login.Name u.SetEmail(login.Email) // insert the user into the database if err := datastore.PostUser(ctx, u); err != nil { log.Println(err) w.WriteHeader(http.StatusBadRequest) return } // if this is the first user, they // should be an admin. if u.ID == 1 { u.Admin = true } } // update the user access token // in case it changed in GitHub u.Access = login.Access u.Secret = login.Secret u.Name = login.Name u.SetEmail(login.Email) u.Syncing = true //u.IsStale() // todo (badrydzewski) should not always sync if err := datastore.PutUser(ctx, u); err != nil { log.Println(err) w.WriteHeader(http.StatusBadRequest) return } // look at the last synchronized date to determine if // we need to re-sync the account. // // todo(bradrydzewski) this should move to a server/sync package and // should be injected into this struct, just like the database code. // // todo(bradrydzewski) this login should be a bit more intelligent // than the current implementation. if u.Syncing { redirect = "/sync" log.Println("sync user account.", u.Login) // sync inside a goroutine. This should eventually be moved to // its own package / sync utility. go func() { repos, err := remote.GetRepos(u) if err != nil { log.Println("Error syncing user account, listing repositories", u.Login, err) return } // insert all repositories for _, repo := range repos { var role = repo.Role if err := datastore.PostRepo(ctx, repo); err != nil { // typically we see a failure because the repository already exists // in which case, we can retrieve the existing record to get the ID. repo, err = datastore.GetRepoName(ctx, repo.Host, repo.Owner, repo.Name) if err != nil { log.Println("Error adding repo.", u.Login, repo.Name, err) continue } } // add user permissions perm := model.Perm{ UserID: u.ID, RepoID: repo.ID, Read: role.Read, Write: role.Write, Admin: role.Admin, } if err := datastore.PostPerm(ctx, &perm); err != nil { log.Println("Error adding permissions.", u.Login, repo.Name, err) continue } log.Println("Successfully syced repo.", u.Login+"/"+repo.Name) } u.Synced = time.Now().UTC().Unix() u.Syncing = false if err := datastore.PutUser(ctx, u); err != nil { log.Println("Error syncing user account, updating sync date", u.Login, err) return } }() } token, err := session.GenerateToken(ctx, r, u) if err != nil { log.Println(err) w.WriteHeader(http.StatusInternalServerError) return } redirect = redirect + "#access_token=" + token http.Redirect(w, r, redirect, http.StatusSeeOther) }