Skip to content

Add "n commits" link to contributors in contributors graph page #29429

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 5 additions & 3 deletions routers/web/repo/contributors.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ func Contributors(ctx *context.Context) {
ctx.Data["PageIsActivity"] = true
ctx.Data["PageIsContributors"] = true

ctx.PageData["contributionType"] = "commits"

ctx.PageData["repoLink"] = ctx.Repo.RepoLink
ctx.PageData["repoContributorsData"] = map[string]any{
"contributionType": "commits",
"repoLink": ctx.Repo.RepoLink,
"repoDefaultBranch": ctx.Repo.RefName,
}

ctx.HTML(http.StatusOK, tplContributors)
}
Expand Down
14 changes: 10 additions & 4 deletions web_src/js/components/RepoContributors.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script>
import {SvgIcon} from '../svg.js';
import dayjs from 'dayjs';
import {
Chart,
Title,
Expand Down Expand Up @@ -65,13 +66,15 @@ export default {
errorText: '',
totalStats: {},
sortedContributors: {},
repoLink: pageData.repoLink || [],
type: pageData.contributionType,
repoLink: pageData.repoContributorsData.repoLink || [],
repoBranch: pageData.repoContributorsData.repoDefaultBranch || [],
type: pageData.repoContributorsData.contributionType,
contributorsStats: [],
xAxisStart: null,
xAxisEnd: null,
xAxisMin: null,
xAxisMax: null,
searchQuery: '',
}),
mounted() {
this.fetchGraphData();
Expand All @@ -88,6 +91,9 @@ export default {
methods: {
sortContributors() {
const contributors = this.filterContributorWeeksByDateRange();
const min = dayjs(this.xAxisMin).format('YYYY-MM-DD');
const max = dayjs(this.xAxisMax).format('YYYY-MM-DD');
this.searchQuery = `${this.repoLink}/commits/branch/${this.repoBranch}/search?q=after:${min}, before:${max}, author:`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to encodeURIComponent(this.repoBranch)? Try with a branch name that contains a space character.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup.

Actually this.repoBranch might need to use path segment escape.

The q parameter also needs to be escaped (encodeURIComponent)

Copy link
Member

@silverwind silverwind Feb 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this.repoBranch might need to use path segment escape.

There is no path escape function in JS, I think encodeURIComponent is suitable for alle cases, path or search params.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess "org/repo/branch/feature%2Fbranch" doesn't work.

Copy link
Member

@silverwind silverwind Feb 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you can not pass / to encodeURIComponent if it's part of the path, it's meant for individual path segments or search param values.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.searchQuery = `${this.repoLink}/commits/branch/${this.repoBranch}/search?q=after:${min}, before:${max}, author:`;
this.searchQuery = `${this.repoLink}/commits/branch/${this.repoBranch}/search?q=after:${min},before:${max},author:`;

also, it looks really weird to construct the search query like this.
Wouldn't it make more sense to have ?after=…&before=…&author=…?
And is all that functionality already supported?
I would have guessed that you need to add it yourself…

Copy link
Member

@silverwind silverwind Feb 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we should use URLSearchParams to construct search params:

String(new URLSearchParams({foo: "bar", baz: "a b"}))
// 'foo=bar&baz=a+b'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it make more sense to have ?after=…&before=…&author=…?

yeah, it would. But I can't do that way because it is not something I decided. Gitea currently works the other way.

And is all that functionality already supported?

Yes. And it won't work if I accept your code suggestion. The spaces are important. As I said, this is how it works.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's existing API, I guess this weird format is okay, otherwise it would be a breaking change. Still we should ensure the individual paramters and path segments are encoded correctly.

const criteria = `total_${this.type}`;
this.sortedContributors = Object.values(contributors)
.filter((contributor) => contributor[criteria] !== 0)
Expand Down Expand Up @@ -162,7 +168,7 @@ export default {
// for details.
user.max_contribution_type += 1;

filteredData[key] = {...user, weeks: filteredWeeks};
filteredData[key] = {...user, weeks: filteredWeeks, email: key};
}

return filteredData;
Expand Down Expand Up @@ -384,7 +390,7 @@ export default {
{{ contributor.name }}
</h4>
<p class="gt-font-12 gt-df gt-gap-2">
<strong v-if="contributor.total_commits">{{ contributor.total_commits.toLocaleString() }} {{ locale.contributionType.commits }}</strong>
<strong v-if="contributor.total_commits"><a :href="searchQuery + contributor.email">{{ contributor.total_commits.toLocaleString() }} {{ locale.contributionType.commits }}</a></strong>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about users having multiple emails?
For those, only the primary email will be counted if I see that correctly…

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All email addresses of a user are mapped to their primary email address in backend. See:

if u != nil {
// update userEmail with user's primary email address so
// that different mail addresses will linked to same account
userEmail = u.GetEmail()
}

If I don't do that, we will see same user multiple times in the contributor graphs page (for multiple emails). And problem you mentioned will be fixed.

But I would prefer it this way as it looks cleaner. I guess it doesn't have to be perfect all the times. If you really want it to be perfect, you may modify the backend code to include commits of a user with their other email addresses when queried (maybe it already works this way, I didn't check)

<strong v-if="contributor.total_additions" class="text green">{{ contributor.total_additions.toLocaleString() }}++ </strong>
<strong v-if="contributor.total_deletions" class="text red">
{{ contributor.total_deletions.toLocaleString() }}--</strong>
Expand Down