예제 #1
0
// deletePublicKeys does the actual key deletion but does not update authorized_keys file.
func deletePublicKeys(e *xorm.Session, keyIDs ...int64) error {
	if len(keyIDs) == 0 {
		return nil
	}

	_, err := e.In("id", strings.Join(base.Int64sToStrings(keyIDs), ",")).Delete(new(PublicKey))
	return err
}
예제 #2
0
파일: org.go 프로젝트: andreynering/gogs
// GetUserRepositories returns a range of repositories in organization
// that the user with the given userID has access to,
// and total number of records based on given condition.
func (org *User) GetUserRepositories(userID int64, page, pageSize int) ([]*Repository, int64, error) {
	teamIDs, err := org.GetUserTeamIDs(userID)
	if err != nil {
		return nil, 0, fmt.Errorf("GetUserTeamIDs: %v", err)
	}
	if len(teamIDs) == 0 {
		// user has no team but "IN ()" is invalid SQL
		teamIDs = []int64{-1} // there is no repo with id=-1
	}

	if page <= 0 {
		page = 1
	}
	repos := make([]*Repository, 0, pageSize)
	// FIXME: use XORM chain operations instead of raw SQL.
	if err = x.Sql(fmt.Sprintf(`SELECT repository.* FROM repository
	INNER JOIN team_repo
	ON team_repo.repo_id = repository.id
	WHERE (repository.owner_id = ? AND repository.is_private = ?) OR team_repo.team_id IN (%s)
	GROUP BY repository.id
	ORDER BY updated_unix DESC
	LIMIT %d OFFSET %d`,
		strings.Join(base.Int64sToStrings(teamIDs), ","), pageSize, (page-1)*pageSize),
		org.ID, false).Find(&repos); err != nil {
		return nil, 0, fmt.Errorf("get repositories: %v", err)
	}

	results, err := x.Query(fmt.Sprintf(`SELECT repository.id FROM repository
	INNER JOIN team_repo
	ON team_repo.repo_id = repository.id
	WHERE (repository.owner_id = ? AND repository.is_private = ?) OR team_repo.team_id IN (%s)
	GROUP BY repository.id
	ORDER BY updated_unix DESC`,
		strings.Join(base.Int64sToStrings(teamIDs), ",")),
		org.ID, false)
	if err != nil {
		log.Error(4, "count user repositories in organization: %v", err)
	}

	return repos, int64(len(results)), nil
}
예제 #3
0
파일: org.go 프로젝트: andreynering/gogs
// GetUserRepositories returns mirror repositories of the organization
// that the user with the given userID has access to.
func (org *User) GetUserMirrorRepositories(userID int64) ([]*Repository, error) {
	teamIDs, err := org.GetUserTeamIDs(userID)
	if err != nil {
		return nil, fmt.Errorf("GetUserTeamIDs: %v", err)
	}
	if len(teamIDs) == 0 {
		teamIDs = []int64{-1}
	}

	repos := make([]*Repository, 0, 10)
	if err = x.Sql(fmt.Sprintf(`SELECT repository.* FROM repository
	INNER JOIN team_repo
	ON team_repo.repo_id = repository.id AND repository.is_mirror = ?
	WHERE (repository.owner_id = ? AND repository.is_private = ?) OR team_repo.team_id IN (%s)
	GROUP BY repository.id
	ORDER BY updated_unix DESC`,
		strings.Join(base.Int64sToStrings(teamIDs), ",")),
		true, org.ID, false).Find(&repos); err != nil {
		return nil, fmt.Errorf("get repositories: %v", err)
	}
	return repos, nil
}
예제 #4
0
파일: issue.go 프로젝트: andreynering/gogs
// Issues returns a list of issues by given conditions.
func Issues(opts *IssuesOptions) ([]*Issue, error) {
	if opts.Page <= 0 {
		opts.Page = 1
	}

	sess := x.Limit(setting.UI.IssuePagingNum, (opts.Page-1)*setting.UI.IssuePagingNum)

	if opts.RepoID > 0 {
		sess.Where("issue.repo_id=?", opts.RepoID).And("issue.is_closed=?", opts.IsClosed)
	} else if opts.RepoIDs != nil {
		// In case repository IDs are provided but actually no repository has issue.
		if len(opts.RepoIDs) == 0 {
			return make([]*Issue, 0), nil
		}
		sess.In("issue.repo_id", base.Int64sToStrings(opts.RepoIDs)).And("issue.is_closed=?", opts.IsClosed)
	} else {
		sess.Where("issue.is_closed=?", opts.IsClosed)
	}

	if opts.AssigneeID > 0 {
		sess.And("issue.assignee_id=?", opts.AssigneeID)
	} else if opts.PosterID > 0 {
		sess.And("issue.poster_id=?", opts.PosterID)
	}

	if opts.MilestoneID > 0 {
		sess.And("issue.milestone_id=?", opts.MilestoneID)
	}

	sess.And("issue.is_pull=?", opts.IsPull)

	switch opts.SortType {
	case "oldest":
		sess.Asc("issue.created_unix")
	case "recentupdate":
		sess.Desc("issue.updated_unix")
	case "leastupdate":
		sess.Asc("issue.updated_unix")
	case "mostcomment":
		sess.Desc("issue.num_comments")
	case "leastcomment":
		sess.Asc("issue.num_comments")
	case "priority":
		sess.Desc("issue.priority")
	default:
		sess.Desc("issue.created_unix")
	}

	if len(opts.Labels) > 0 && opts.Labels != "0" {
		labelIDs := base.StringsToInt64s(strings.Split(opts.Labels, ","))
		if len(labelIDs) > 0 {
			sess.Join("INNER", "issue_label", "issue.id = issue_label.issue_id").In("issue_label.label_id", labelIDs)
		}
	}

	if opts.IsMention {
		sess.Join("INNER", "issue_user", "issue.id = issue_user.issue_id").And("issue_user.is_mentioned = ?", true)

		if opts.UserID > 0 {
			sess.And("issue_user.uid = ?", opts.UserID)
		}
	}

	issues := make([]*Issue, 0, setting.UI.IssuePagingNum)
	if err := sess.Find(&issues); err != nil {
		return nil, fmt.Errorf("Find: %v", err)
	}

	// FIXME: use IssueList to improve performance.
	for i := range issues {
		if err := issues[i].LoadAttributes(); err != nil {
			return nil, fmt.Errorf("LoadAttributes [%d]: %v", issues[i].ID, err)
		}
	}

	return issues, nil
}