Search in sources :

Example 11 with ProjectModel

use of com.gitblit.models.ProjectModel in project gitblit by gitblit.

the class RepositoryPage method setupPage.

@Override
protected void setupPage(String repositoryName, String pageName) {
    //It may be attempted to be called again if an info or error message is displayed.
    if (get("projectTitle") != null) {
        return;
    }
    String projectName = StringUtils.getFirstPathElement(repositoryName);
    ProjectModel project = app().projects().getProjectModel(projectName);
    if (project.isUserProject()) {
        // user-as-project
        add(new LinkPanel("projectTitle", null, project.getDisplayName(), UserPage.class, WicketUtils.newUsernameParameter(project.name.substring(1))));
    } else {
        // project
        add(new LinkPanel("projectTitle", null, project.name, ProjectPage.class, WicketUtils.newProjectParameter(project.name)));
    }
    String name = StringUtils.stripDotGit(repositoryName);
    if (!StringUtils.isEmpty(projectName) && name.startsWith(projectName)) {
        name = name.substring(projectName.length() + 1);
    }
    add(new LinkPanel("repositoryName", null, name, SummaryPage.class, WicketUtils.newRepositoryParameter(repositoryName)));
    UserModel user = GitBlitWebSession.get().getUser();
    if (user == null) {
        user = UserModel.ANONYMOUS;
    }
    // indicate origin repository
    RepositoryModel model = getRepositoryModel();
    if (StringUtils.isEmpty(model.originRepository)) {
        if (model.isMirror) {
            add(new Fragment("repoIcon", "mirrorIconFragment", this));
            Fragment mirrorFrag = new Fragment("originRepository", "mirrorFragment", this);
            Label lbl = new Label("originRepository", MessageFormat.format(getString("gb.mirrorOf"), "<b>" + model.origin + "</b>"));
            mirrorFrag.add(lbl.setEscapeModelStrings(false));
            add(mirrorFrag);
        } else {
            if (model.isBare) {
                add(new Fragment("repoIcon", "repoIconFragment", this));
            } else {
                add(new Fragment("repoIcon", "cloneIconFragment", this));
            }
            add(new Label("originRepository", Optional.of(model.description).or("")));
        }
    } else {
        RepositoryModel origin = app().repositories().getRepositoryModel(model.originRepository);
        if (origin == null) {
            // no origin repository, show description if available
            if (model.isBare) {
                add(new Fragment("repoIcon", "repoIconFragment", this));
            } else {
                add(new Fragment("repoIcon", "cloneIconFragment", this));
            }
            add(new Label("originRepository", Optional.of(model.description).or("")));
        } else if (!user.canView(origin)) {
            // show origin repository without link
            add(new Fragment("repoIcon", "forkIconFragment", this));
            Fragment forkFrag = new Fragment("originRepository", "originFragment", this);
            forkFrag.add(new Label("originRepository", StringUtils.stripDotGit(model.originRepository)));
            add(forkFrag);
        } else {
            // link to origin repository
            add(new Fragment("repoIcon", "forkIconFragment", this));
            Fragment forkFrag = new Fragment("originRepository", "originFragment", this);
            forkFrag.add(new LinkPanel("originRepository", null, StringUtils.stripDotGit(model.originRepository), SummaryPage.class, WicketUtils.newRepositoryParameter(model.originRepository)));
            add(forkFrag);
        }
    }
    // new ticket button
    if (user.isAuthenticated && app().tickets().isAcceptingNewTickets(getRepositoryModel())) {
        String newTicketUrl = getRequestCycle().urlFor(NewTicketPage.class, WicketUtils.newRepositoryParameter(repositoryName)).toString();
        addToolbarButton("newTicketLink", "fa fa-ticket", getString("gb.new"), newTicketUrl);
    } else {
        add(new Label("newTicketLink").setVisible(false));
    }
    // (un)star link allows a user to star a repository
    if (user.isAuthenticated && model.hasCommits) {
        PageParameters starParams = DeepCopier.copy(getPageParameters());
        starParams.put(PARAM_STAR, !user.getPreferences().isStarredRepository(model.name));
        String toggleStarUrl = getRequestCycle().urlFor(getClass(), starParams).toString();
        if (user.getPreferences().isStarredRepository(model.name)) {
            // show unstar button
            add(new Label("starLink").setVisible(false));
            addToolbarButton("unstarLink", "icon-star-empty", getString("gb.unstar"), toggleStarUrl);
        } else {
            // show star button
            addToolbarButton("starLink", "icon-star", getString("gb.star"), toggleStarUrl);
            add(new Label("unstarLink").setVisible(false));
        }
    } else {
        // anonymous user
        add(new Label("starLink").setVisible(false));
        add(new Label("unstarLink").setVisible(false));
    }
    // fork controls
    if (!allowForkControls() || !user.isAuthenticated) {
        // must be logged-in to fork, hide all fork controls
        add(new ExternalLink("forkLink", "").setVisible(false));
        add(new ExternalLink("myForkLink", "").setVisible(false));
    } else {
        String fork = app().repositories().getFork(user.username, model.name);
        String userRepo = ModelUtils.getPersonalPath(user.username) + "/" + StringUtils.stripDotGit(StringUtils.getLastPathElement(model.name));
        boolean hasUserRepo = app().repositories().hasRepository(userRepo);
        boolean hasFork = fork != null;
        boolean canFork = user.canFork(model) && model.hasCommits && !hasUserRepo;
        if (hasFork || !canFork) {
            // user not allowed to fork or fork already exists or repo forbids forking
            add(new ExternalLink("forkLink", "").setVisible(false));
            if (hasFork && !fork.equals(model.name)) {
                // user has fork, view my fork link
                String url = getRequestCycle().urlFor(SummaryPage.class, WicketUtils.newRepositoryParameter(fork)).toString();
                add(new ExternalLink("myForkLink", url));
            } else {
                // no fork, hide view my fork link
                add(new ExternalLink("myForkLink", "").setVisible(false));
            }
        } else if (canFork) {
            // can fork and we do not have one
            add(new ExternalLink("myForkLink", "").setVisible(false));
            String url = getRequestCycle().urlFor(ForkPage.class, WicketUtils.newRepositoryParameter(model.name)).toString();
            add(new ExternalLink("forkLink", url));
        }
    }
    if (showAdmin || isOwner) {
        String url = getRequestCycle().urlFor(EditRepositoryPage.class, WicketUtils.newRepositoryParameter(model.name)).toString();
        add(new ExternalLink("editLink", url));
    } else {
        add(new Label("editLink").setVisible(false));
    }
    super.setupPage(repositoryName, pageName);
}
Also used : Label(org.apache.wicket.markup.html.basic.Label) RepositoryModel(com.gitblit.models.RepositoryModel) PageParameters(org.apache.wicket.PageParameters) Fragment(org.apache.wicket.markup.html.panel.Fragment) ExternalLink(org.apache.wicket.markup.html.link.ExternalLink) LinkPanel(com.gitblit.wicket.panels.LinkPanel) UserModel(com.gitblit.models.UserModel) ProjectModel(com.gitblit.models.ProjectModel)

Example 12 with ProjectModel

use of com.gitblit.models.ProjectModel in project gitblit by gitblit.

the class FilterableProjectList method onInitialize.

@Override
protected void onInitialize() {
    super.onInitialize();
    String id = getId();
    String ngCtrl = id + "Ctrl";
    String ngList = id + "List";
    Map<String, Object> values = new HashMap<String, Object>();
    values.put("ngCtrl", ngCtrl);
    values.put("ngList", ngList);
    // use Freemarker to setup an AngularJS/Wicket html snippet
    FreemarkerPanel panel = new FreemarkerPanel("listComponent", "FilterableProjectList.fm", values);
    panel.setParseGeneratedMarkup(true);
    panel.setRenderBodyOnly(true);
    add(panel);
    // add the Wicket controls that are referenced in the snippet
    String listTitle = StringUtils.isEmpty(title) ? getString("gb.projects") : title;
    panel.add(new Label(ngList + "Title", MessageFormat.format("{0} ({1})", listTitle, projects.size())));
    if (StringUtils.isEmpty(iconClass)) {
        panel.add(new Label(ngList + "Icon").setVisible(false));
    } else {
        Label icon = new Label(ngList + "Icon");
        WicketUtils.setCssClass(icon, iconClass);
        panel.add(icon);
    }
    String format = app().settings().getString(Keys.web.datestampShortFormat, "MM/dd/yy");
    final DateFormat df = new SimpleDateFormat(format);
    df.setTimeZone(getTimeZone());
    Collections.sort(projects, new Comparator<ProjectModel>() {

        @Override
        public int compare(ProjectModel o1, ProjectModel o2) {
            return o2.lastChange.compareTo(o1.lastChange);
        }
    });
    List<ProjectListItem> list = new ArrayList<ProjectListItem>();
    for (ProjectModel proj : projects) {
        if (proj.isUserProject() || proj.repositories.isEmpty()) {
            // exclude user projects from list
            continue;
        }
        ProjectListItem item = new ProjectListItem();
        item.p = proj.name;
        item.n = StringUtils.isEmpty(proj.title) ? proj.name : proj.title;
        item.i = proj.description;
        item.t = getTimeUtils().timeAgo(proj.lastChange);
        item.d = df.format(proj.lastChange);
        item.c = proj.repositories.size();
        list.add(item);
    }
    // inject an AngularJS controller with static data
    NgController ctrl = new NgController(ngCtrl);
    ctrl.addVariable(ngList, list);
    add(new HeaderContributor(ctrl));
}
Also used : NgController(com.gitblit.wicket.ng.NgController) HashMap(java.util.HashMap) Label(org.apache.wicket.markup.html.basic.Label) ArrayList(java.util.ArrayList) FreemarkerPanel(com.gitblit.wicket.freemarker.FreemarkerPanel) HeaderContributor(org.apache.wicket.behavior.HeaderContributor) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ProjectModel(com.gitblit.models.ProjectModel) SimpleDateFormat(java.text.SimpleDateFormat)

Example 13 with ProjectModel

use of com.gitblit.models.ProjectModel in project gitblit by gitblit.

the class BasePage method getProjects.

protected List<ProjectModel> getProjects(PageParameters params) {
    if (params == null) {
        return getProjectModels();
    }
    boolean hasParameter = false;
    String regex = WicketUtils.getRegEx(params);
    String team = WicketUtils.getTeam(params);
    int daysBack = params.getInt("db", 0);
    int maxDaysBack = app().settings().getInteger(Keys.web.activityDurationMaximum, 30);
    List<ProjectModel> availableModels = getProjectModels();
    Set<ProjectModel> models = new HashSet<ProjectModel>();
    if (!StringUtils.isEmpty(regex)) {
        // filter the projects by the regex
        hasParameter = true;
        Pattern pattern = Pattern.compile(regex);
        for (ProjectModel model : availableModels) {
            if (pattern.matcher(model.name).find()) {
                models.add(model);
            }
        }
    }
    if (!StringUtils.isEmpty(team)) {
        // filter the projects by the specified teams
        hasParameter = true;
        List<String> teams = StringUtils.getStringsFromValue(team, ",");
        // need TeamModels first
        List<TeamModel> teamModels = new ArrayList<TeamModel>();
        for (String name : teams) {
            TeamModel teamModel = app().users().getTeamModel(name);
            if (teamModel != null) {
                teamModels.add(teamModel);
            }
        }
        // brute-force our way through finding the matching models
        for (ProjectModel projectModel : availableModels) {
            for (String repositoryName : projectModel.repositories) {
                for (TeamModel teamModel : teamModels) {
                    if (teamModel.hasRepositoryPermission(repositoryName)) {
                        models.add(projectModel);
                    }
                }
            }
        }
    }
    if (!hasParameter) {
        models.addAll(availableModels);
    }
    // time-filter the list
    if (daysBack > 0) {
        if (maxDaysBack > 0 && daysBack > maxDaysBack) {
            daysBack = maxDaysBack;
        }
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.add(Calendar.DATE, -1 * daysBack);
        Date threshold = cal.getTime();
        Set<ProjectModel> timeFiltered = new HashSet<ProjectModel>();
        for (ProjectModel model : models) {
            if (model.lastChange.after(threshold)) {
                timeFiltered.add(model);
            }
        }
        models = timeFiltered;
    }
    List<ProjectModel> list = new ArrayList<ProjectModel>(models);
    Collections.sort(list);
    return list;
}
Also used : Pattern(java.util.regex.Pattern) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) Date(java.util.Date) TeamModel(com.gitblit.models.TeamModel) ProjectModel(com.gitblit.models.ProjectModel) HashSet(java.util.HashSet)

Example 14 with ProjectModel

use of com.gitblit.models.ProjectModel in project gitblit by gitblit.

the class GitblitWicketFilter method getLastModified.

/**
	 * Determines the last-modified date of the requested resource.
	 *
	 * @param servletRequest
	 * @return The last modified time stamp
	 */
@Override
protected long getLastModified(final HttpServletRequest servletRequest) {
    final String pathInfo = getRelativePath(servletRequest);
    if (Strings.isEmpty(pathInfo)) {
        return -1;
    }
    long lastModified = super.getLastModified(servletRequest);
    if (lastModified > -1) {
        return lastModified;
    }
    // try to match request against registered CacheControl pages
    String[] paths = pathInfo.split("/");
    String page = paths[0];
    String repo = "";
    String commitId = "";
    if (paths.length >= 2) {
        repo = paths[1];
    }
    if (paths.length >= 3) {
        commitId = paths[2];
    }
    if (!StringUtils.isEmpty(servletRequest.getParameter("r"))) {
        repo = servletRequest.getParameter("r");
    }
    if (!StringUtils.isEmpty(servletRequest.getParameter("h"))) {
        commitId = servletRequest.getParameter("h");
    }
    repo = repo.replace("%2f", "/").replace("%2F", "/").replace(settings.getChar(Keys.web.forwardSlashCharacter, '/'), '/');
    GitBlitWebApp app = (GitBlitWebApp) getWebApplication();
    int expires = settings.getInteger(Keys.web.pageCacheExpires, 0);
    if (!StringUtils.isEmpty(page) && app.isCacheablePage(page) && expires > 0) {
        // page can be cached by the browser
        CacheControl cacheControl = app.getCacheControl(page);
        Date bootDate = runtimeManager.getBootDate();
        switch(cacheControl.value()) {
            case ACTIVITY:
                // returns the last activity date of the server
                Date activityDate = repositoryManager.getLastActivityDate();
                if (activityDate != null) {
                    return activityDate.after(bootDate) ? activityDate.getTime() : bootDate.getTime();
                }
                return bootDate.getTime();
            case BOOT:
                // return the boot date of the server
                return bootDate.getTime();
            case PROJECT:
                // return the latest change date for the project OR the boot date
                ProjectModel project = projectManager.getProjectModel(StringUtils.getRootPath(repo));
                if (project != null) {
                    return project.lastChange.after(bootDate) ? project.lastChange.getTime() : bootDate.getTime();
                }
                break;
            case REPOSITORY:
                // return the lastest change date for the repository OR the boot
                // date, whichever is latest
                RepositoryModel repository = repositoryManager.getRepositoryModel(repo);
                if (repository != null && repository.lastChange != null) {
                    return repository.lastChange.after(bootDate) ? repository.lastChange.getTime() : bootDate.getTime();
                }
                break;
            case COMMIT:
                // get the date of the specified commit
                if (StringUtils.isEmpty(commitId)) {
                    // no commit id, use boot date
                    return bootDate.getTime();
                } else {
                    // last modified date is the commit date
                    Repository r = null;
                    try {
                        // return the timestamp of the associated commit
                        r = repositoryManager.getRepository(repo);
                        if (r != null) {
                            RevCommit commit = JGitUtils.getCommit(r, commitId);
                            if (commit != null) {
                                Date date = JGitUtils.getCommitDate(commit);
                                return date.after(bootDate) ? date.getTime() : bootDate.getTime();
                            }
                        }
                    } finally {
                        if (r != null) {
                            r.close();
                        }
                    }
                }
                break;
            default:
                break;
        }
    }
    return -1;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) RepositoryModel(com.gitblit.models.RepositoryModel) ProjectModel(com.gitblit.models.ProjectModel) Date(java.util.Date) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 15 with ProjectModel

use of com.gitblit.models.ProjectModel in project gitblit by gitblit.

the class ProjectManager method getProjectModels.

/**
	 * Returns a list of project models for the user.
	 *
	 * @param user
	 * @param includeUsers
	 * @return list of projects that are accessible to the user
	 */
@Override
public List<ProjectModel> getProjectModels(UserModel user, boolean includeUsers) {
    Map<String, ProjectModel> configs = getProjectConfigs();
    // per-user project lists, this accounts for security and visibility
    Map<String, ProjectModel> map = new TreeMap<String, ProjectModel>();
    // root project
    map.put("", configs.get(""));
    for (RepositoryModel model : repositoryManager.getRepositoryModels(user)) {
        String projectPath = StringUtils.getRootPath(model.name);
        String projectKey = projectPath.toLowerCase();
        if (!map.containsKey(projectKey)) {
            ProjectModel project;
            if (configs.containsKey(projectKey)) {
                // clone the project model because it's repository list will
                // be tailored for the requesting user
                project = DeepCopier.copy(configs.get(projectKey));
            } else {
                project = new ProjectModel(projectPath);
            }
            map.put(projectKey, project);
        }
        map.get(projectKey).addRepository(model);
    }
    // sort projects, root project first
    List<ProjectModel> projects;
    if (includeUsers) {
        // all projects
        projects = new ArrayList<ProjectModel>(map.values());
        Collections.sort(projects);
        projects.remove(map.get(""));
        projects.add(0, map.get(""));
    } else {
        // all non-user projects
        projects = new ArrayList<ProjectModel>();
        ProjectModel root = map.remove("");
        for (ProjectModel model : map.values()) {
            if (!model.isUserProject()) {
                projects.add(model);
            }
        }
        Collections.sort(projects);
        projects.add(0, root);
    }
    return projects;
}
Also used : RepositoryModel(com.gitblit.models.RepositoryModel) ProjectModel(com.gitblit.models.ProjectModel) TreeMap(java.util.TreeMap)

Aggregations

ProjectModel (com.gitblit.models.ProjectModel)15 RepositoryModel (com.gitblit.models.RepositoryModel)8 UserModel (com.gitblit.models.UserModel)7 ArrayList (java.util.ArrayList)6 Label (org.apache.wicket.markup.html.basic.Label)5 Date (java.util.Date)3 ParameterMenuItem (com.gitblit.models.Menu.ParameterMenuItem)2 FilterableRepositoryList (com.gitblit.wicket.panels.FilterableRepositoryList)2 LinkPanel (com.gitblit.wicket.panels.LinkPanel)2 Calendar (java.util.Calendar)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 LinkedHashMap (java.util.LinkedHashMap)2 Component (org.apache.wicket.Component)2 PageParameters (org.apache.wicket.PageParameters)2 ExternalLink (org.apache.wicket.markup.html.link.ExternalLink)2 Fragment (org.apache.wicket.markup.html.panel.Fragment)2 Repository (org.eclipse.jgit.lib.Repository)2 RevCommit (org.eclipse.jgit.revwalk.RevCommit)2 Constants (com.gitblit.Constants)1