コード例 #1
0
ファイル: badge.go プロジェクト: quartethealth/drone
// GetCC accepts a request to retrieve the latest build
// status for the given repository from the datastore and
// in CCTray XML format.
//
//     GET /api/badge/:host/:owner/:name/cc.xml
//
func GetCC(c web.C, w http.ResponseWriter, r *http.Request) {
	var ctx = context.FromC(c)
	var (
		host  = c.URLParams["host"]
		owner = c.URLParams["owner"]
		name  = c.URLParams["name"]
	)

	w.Header().Set("Content-Type", "application/xml")

	repo, err := datastore.GetRepoName(ctx, host, owner, name)
	if err != nil {
		w.WriteHeader(http.StatusNotFound)
		return
	}
	commits, err := datastore.GetCommitList(ctx, repo, 1, 0)
	if err != nil || len(commits) == 0 {
		w.WriteHeader(http.StatusNotFound)
		return
	}

	var link = httputil.GetURL(r) + "/" + repo.Host + "/" + repo.Owner + "/" + repo.Name
	var cc = model.NewCC(repo, commits[0], link)
	xml.NewEncoder(w).Encode(cc)
}
コード例 #2
0
ファイル: repo.go プロジェクト: quartethealth/drone
// SetRepo is a middleware function that retrieves
// the repository and stores in the context.
func SetRepo(c *web.C, h http.Handler) http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		var (
			ctx   = context.FromC(*c)
			host  = c.URLParams["host"]
			owner = c.URLParams["owner"]
			name  = c.URLParams["name"]
			user  = ToUser(c)
		)

		repo, err := datastore.GetRepoName(ctx, host, owner, name)
		switch {
		case err != nil && user == nil:
			w.WriteHeader(http.StatusUnauthorized)
			return
		case err != nil && user != nil:
			w.WriteHeader(http.StatusNotFound)
			return
		}
		role, _ := datastore.GetPerm(ctx, user, repo)
		RepoToC(c, repo)
		RoleToC(c, role)
		h.ServeHTTP(w, r)
	}
	return http.HandlerFunc(fn)
}
コード例 #3
0
ファイル: badge.go プロジェクト: quartethealth/drone
// GetBadge accepts a request to retrieve the named
// repo and branhes latest build details from the datastore
// and return an SVG badges representing the build results.
//
//     GET /api/badge/:host/:owner/:name/status.svg
//
func GetBadge(c web.C, w http.ResponseWriter, r *http.Request) {
	var ctx = context.FromC(c)
	var (
		host   = c.URLParams["host"]
		owner  = c.URLParams["owner"]
		name   = c.URLParams["name"]
		branch = r.FormValue("branch")
		style  = r.FormValue("style")
	)

	// an SVG response is always served, even when error, so
	// we can go ahead and set the content type appropriately.
	w.Header().Set("Content-Type", "image/svg+xml")

	badge, ok := badgeStyles[style]
	if !ok {
		badge = defaultBadge
	}

	repo, err := datastore.GetRepoName(ctx, host, owner, name)
	if err != nil {
		w.Write(badge.none)
		return
	}
	if len(branch) == 0 {
		branch = model.DefaultBranch
	}
	commit, _ := datastore.GetCommitLast(ctx, repo, branch)

	// if no commit was found then display
	// the 'none' badge, instead of throwing
	// an error response
	if commit == nil {
		w.Write(badge.none)
		return
	}

	switch commit.Status {
	case model.StatusSuccess:
		w.Write(badge.success)
	case model.StatusFailure:
		w.Write(badge.failure)
	case model.StatusError:
		w.Write(badge.err)
	case model.StatusEnqueue, model.StatusStarted:
		w.Write(badge.started)
	default:
		w.Write(badge.none)
	}
}
コード例 #4
0
ファイル: sync.go プロジェクト: quartethealth/drone
func SyncUser(ctx context.Context, user *model.User, remote remote.Remote) {
	repos, err := remote.GetRepos(user)
	if err != nil {
		log.Println("Error syncing user account, listing repositories", user.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.", user.Login, repo.Name, err)
				continue
			}
		}

		// add user permissions
		perm := model.Perm{
			UserID: user.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.", user.Login, repo.Name, err)
			continue
		}

		log.Printf("Successfully synced repo. %s/%s\n", repo.Owner, repo.Name)
	}

	user.Synced = time.Now().UTC().Unix()
	user.Syncing = false
	if err := datastore.PutUser(ctx, user); err != nil {
		log.Println("Error syncing user account, updating sync date", user.Login, err)
		return
	}
}