Skip to content

Expand/Collapse all changed files #23639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Apr 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions modules/git/repo_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,3 +491,19 @@ func (repo *Repository) AddLastCommitCache(cacheKey, fullName, sha string) error
}
return nil
}

// git diff-tree --no-commit-id --name-only -r 4122dc99f61ae52d04bb501ddbf633d07c065967
func (repo *Repository) GetChangedFilesIn(commitID string) ([]string, error) {
stdout, _, err := NewCommand(repo.Ctx, "diff-tree", "--no-commit-id", "--name-only", "-r").AddDynamicArguments(commitID).RunStdString(&RunOpts{Dir: repo.Path})
if err != nil {
return nil, err
}
split := strings.Split(stdout, "\000")

// Because Git will always emit filenames with a terminal NUL ignore the last entry in the split - which will always be empty.
if len(split) > 0 {
split = split[:len(split)-1]
}

return split, err
}
55 changes: 55 additions & 0 deletions routers/web/repo/pull_review.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
pull_model "code.gitea.io/gitea/models/pull"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
Expand Down Expand Up @@ -293,3 +294,57 @@ func UpdateViewedFiles(ctx *context.Context) {
ctx.ServerError("UpdateReview", err)
}
}

type viewOrUnviewAllFilesForm struct {
HeadCommitSHA string `json:"headCommitSHA"`
ViewAll bool `json:"viewAll"`
}

func ViewOrUnviewAllFiles(ctx *context.Context) {
// Find corresponding PR
issue := checkPullInfo(ctx)
if ctx.Written() {
return
}
pull := issue.PullRequest

var data *viewOrUnviewAllFilesForm
err := json.NewDecoder(ctx.Req.Body).Decode(&data)
if err != nil {
log.Warn("Attempted to view/unview all but could not parse request body: %v", err)
ctx.Resp.WriteHeader(http.StatusBadRequest)
return
}

// Expect the review to have been now if no head commit was supplied
if data.HeadCommitSHA == "" {
data.HeadCommitSHA = pull.HeadCommitID
}

gitRepo, err := git.OpenRepository(ctx, pull.HeadRepo.RepoPath())
if err != nil {
log.Error("unable to open repository: %s Error: %v", pull.HeadRepo.RepoPath(), err)
ctx.Resp.WriteHeader(http.StatusBadRequest)
return
}

// get the list of changed files in the commit
files, err := gitRepo.GetChangedFilesIn(data.HeadCommitSHA)
if err != nil {
log.Error("unable to get the list of changed files, commitID: %s Error: %v", data.HeadCommitSHA, err)
ctx.Resp.WriteHeader(http.StatusBadRequest)
return
}
updatedFiles := make(map[string]pull_model.ViewedState, len(files))
state := pull_model.Unviewed
if data.ViewAll {
state = pull_model.Viewed
}
for _, f := range files {
updatedFiles[f] = state
}
if err := pull_model.UpdateReviewState(ctx, ctx.Doer.ID, pull.ID, data.HeadCommitSHA, updatedFiles); err != nil {
ctx.ServerError("UpdateReview", err)
}
// todo redirect
}
1 change: 1 addition & 0 deletions routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,7 @@ func RegisterRoutes(m *web.Route) {
m.Post("/watch", repo.IssueWatch)
m.Post("/ref", repo.UpdateIssueRef)
m.Post("/viewed-files", repo.UpdateViewedFiles)
m.Post("/view-or-unview-all-files", repo.ViewOrUnviewAllFiles)
m.Group("/dependency", func() {
m.Post("/add", repo.AddDependency)
m.Post("/delete", repo.RemoveDependency)
Expand Down
6 changes: 6 additions & 0 deletions templates/repo/diff/box.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
<label for="viewed-files-summary" id="viewed-files-summary-label" class="gt-mr-3 gt-f1" data-text-changed-template="{{.locale.Tr "repo.pulls.viewed_files_label"}}">
{{.locale.Tr "repo.pulls.viewed_files_label" .Diff.NumViewedFiles .Diff.NumFiles}}
</label>
<button id="view-all-btn" class="ui button tiny tooltip basic{{if eq .Diff.NumViewedFiles .Diff.NumFiles}} gt-hidden{{end}}" data-content="View All">
{{svg "octicon-eye"}}
</button>
<button id="unview-all-btn" class="ui button tiny tooltip basic{{if eq .Diff.NumViewedFiles 0}} gt-hidden{{end}}" data-content="Unview All">
{{svg "octicon-eye-closed"}}
</button>
{{end}}
{{template "repo/diff/whitespace_dropdown" .}}
{{template "repo/diff/options_dropdown" .}}
Expand Down
15 changes: 15 additions & 0 deletions web_src/js/features/pull-view-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const {csrfToken, pageData} = window.config;
const prReview = pageData.prReview || {};
const viewedStyleClass = 'viewed-file-checked-form';
const viewedCheckboxSelector = '.viewed-file-form'; // Selector under which all "Viewed" checkbox forms can be found
const viewAllBtnSelector = '#view-all-btn';
const unviewAllBtnSelector = '#unview-all-btn';


// Refreshes the summary of viewed files if present
Expand All @@ -17,12 +19,20 @@ function refreshViewedFilesSummary() {
.replace('%[2]d', prReview.numberOfFiles);
}

function refreshViewedAndUnviewedButton() {
$(viewAllBtnSelector).removeClass('gt-hidden');
$(unviewAllBtnSelector).removeClass('gt-hidden');
if (prReview.numberOfViewedFiles === prReview.numberOfFiles) $(viewAllBtnSelector).addClass('gt-hidden');
else if (prReview.numberOfViewedFiles === 0) $(unviewAllBtnSelector).addClass('gt-hidden');
}

// Explicitly recounts how many files the user has currently reviewed by counting the number of checked "viewed" checkboxes
// Additionally, the viewed files summary will be updated if it exists
export function countAndUpdateViewedFiles() {
// The number of files is constant, but the number of viewed files can change because files can be loaded dynamically
prReview.numberOfViewedFiles = document.querySelectorAll(`${viewedCheckboxSelector} > input[type=checkbox][checked]`).length;
refreshViewedFilesSummary();
refreshViewedAndUnviewedButton();
}

// Initializes a listener for all children of the given html element
Expand All @@ -48,6 +58,7 @@ export function initViewedCheckboxListenerFor() {

// Update viewed-files summary and remove "has changed" label if present
refreshViewedFilesSummary();
refreshViewedAndUnviewedButton();
const hasChangedLabel = form.parentNode.querySelector('.changed-since-last-review');
hasChangedLabel?.parentNode.removeChild(hasChangedLabel);

Expand All @@ -69,3 +80,7 @@ export function initViewedCheckboxListenerFor() {
});
}
}

export function initViewAndUnviewAllButton() {

}