From 1e02efcd4a769e4e2825d0d55c570100e8f567d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Eahin=20Akkaya?= Date: Fri, 16 Feb 2024 15:51:56 +0300 Subject: [PATCH 01/10] Add initial recent commits page --- options/locale/locale_en-US.ini | 3 +++ routers/web/repo/recent_commits.go | 35 ++++++++++++++++++++++++++++++ routers/web/web.go | 4 ++++ templates/repo/activity.tmpl | 1 + templates/repo/navbar.tmpl | 3 +++ templates/repo/recent_commits.tmpl | 3 +++ 6 files changed, 49 insertions(+) create mode 100644 routers/web/repo/recent_commits.go create mode 100644 templates/repo/recent_commits.tmpl diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 5f34bc4c1d74..d0ad090c3f8e 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1914,6 +1914,7 @@ wiki.original_git_entry_tooltip = View original Git file instead of using friend activity = Activity activity.navbar.pulse = Pulse activity.navbar.contributors = Contributors +activity.navbar.recent_commits = Recent Commits activity.period.filter_label = Period: activity.period.daily = 1 day activity.period.halfweekly = 3 days @@ -1989,6 +1990,8 @@ contributors.loading_title_failed = Could not load contributions contributors.loading_info = This might take a bit… contributors.component_failed_to_load = An unexpected error happened. +recent_commits = Recent Commits + search = Search search.search_repo = Search repository search.type.tooltip = Search type diff --git a/routers/web/repo/recent_commits.go b/routers/web/repo/recent_commits.go new file mode 100644 index 000000000000..b1960dfcdc5f --- /dev/null +++ b/routers/web/repo/recent_commits.go @@ -0,0 +1,35 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package repo + +import ( + "net/http" + + "code.gitea.io/gitea/modules/base" + "code.gitea.io/gitea/modules/context" + contributors_service "code.gitea.io/gitea/services/repository" +) + +const ( + tplRecentCommits base.TplName = "repo/activity" +) + +// RecentCommits renders the page to show recent commit frequency on repository +func RecentCommits(ctx *context.Context) { + ctx.Data["Title"] = ctx.Tr("repo.recent_commits") + + ctx.Data["PageIsActivity"] = true + ctx.Data["PageIsRecentCommits"] = true + + ctx.HTML(http.StatusOK, tplRecentCommits) +} + +// RecentCommitsData returns JSON of recent commits data +func RecentCommitsData(ctx *context.Context) { + if contributorStats, err := contributors_service.GetContributorStats(ctx, ctx.Cache, ctx.Repo.Repository, ctx.Repo.CommitID); err != nil { + ctx.ServerError("GetCodeFrequencyData", err) + } else { + ctx.JSON(http.StatusOK, contributorStats["total"].Weeks) + } +} diff --git a/routers/web/web.go b/routers/web/web.go index 0528b20328eb..5c7eaadef8ea 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -1397,6 +1397,10 @@ func registerRoutes(m *web.Route) { m.Get("", repo.Contributors) m.Get("/data", repo.ContributorsData) }) + m.Group("/recent-commits", func() { + m.Get("", repo.RecentCommits) + m.Get("/data", repo.RecentCommitsData) + }) }, context.RepoRef(), repo.MustBeNotEmpty, context.RequireRepoReaderOr(unit.TypePullRequests, unit.TypeIssues, unit.TypeReleases)) m.Group("/activity_author_data", func() { diff --git a/templates/repo/activity.tmpl b/templates/repo/activity.tmpl index 960083d2fbd7..7b9f2964a537 100644 --- a/templates/repo/activity.tmpl +++ b/templates/repo/activity.tmpl @@ -8,6 +8,7 @@
{{if .PageIsPulse}}{{template "repo/pulse" .}}{{end}} {{if .PageIsContributors}}{{template "repo/contributors" .}}{{end}} + {{if .PageIsRecentCommits}}{{template "repo/recent_commits" .}}{{end}}
diff --git a/templates/repo/navbar.tmpl b/templates/repo/navbar.tmpl index a9042ee30d0b..80a561829da4 100644 --- a/templates/repo/navbar.tmpl +++ b/templates/repo/navbar.tmpl @@ -5,4 +5,7 @@ {{ctx.Locale.Tr "repo.activity.navbar.contributors"}} + + {{ctx.Locale.Tr "repo.activity.navbar.recent_commits"}} + diff --git a/templates/repo/recent_commits.tmpl b/templates/repo/recent_commits.tmpl new file mode 100644 index 000000000000..8674265a60e6 --- /dev/null +++ b/templates/repo/recent_commits.tmpl @@ -0,0 +1,3 @@ +{{if .Permission.CanRead $.UnitTypeCode}} +

Recent Commits

+{{end}} From 55da039412e56d188728f2c225d074069ebf192b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Eahin=20Akkaya?= Date: Sat, 17 Feb 2024 13:37:26 +0300 Subject: [PATCH 02/10] Implement recent commits page --- options/locale/locale_en-US.ini | 4 + routers/web/repo/recent_commits.go | 8 +- templates/repo/recent_commits.tmpl | 8 +- web_src/js/components/RepoRecentCommits.vue | 183 ++++++++++++++++++++ web_src/js/features/recent-commits.js | 21 +++ web_src/js/index.js | 2 + 6 files changed, 224 insertions(+), 2 deletions(-) create mode 100644 web_src/js/components/RepoRecentCommits.vue create mode 100644 web_src/js/features/recent-commits.js diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index d0ad090c3f8e..d6486d661da0 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1991,6 +1991,10 @@ contributors.loading_info = This might take a bit… contributors.component_failed_to_load = An unexpected error happened. recent_commits = Recent Commits +recent_commits.loading_title = Loading recent commits... +recent_commits.loading_title_failed = Could not load recent commits +recent_commits.loading_info = This might take a bit… +recent_commits.component_failed_to_load = An unexpected error happened. search = Search search.search_repo = Search repository diff --git a/routers/web/repo/recent_commits.go b/routers/web/repo/recent_commits.go index b1960dfcdc5f..f87191e763ab 100644 --- a/routers/web/repo/recent_commits.go +++ b/routers/web/repo/recent_commits.go @@ -4,6 +4,7 @@ package repo import ( + "errors" "net/http" "code.gitea.io/gitea/modules/base" @@ -21,6 +22,7 @@ func RecentCommits(ctx *context.Context) { ctx.Data["PageIsActivity"] = true ctx.Data["PageIsRecentCommits"] = true + ctx.PageData["repoLink"] = ctx.Repo.RepoLink ctx.HTML(http.StatusOK, tplRecentCommits) } @@ -28,7 +30,11 @@ func RecentCommits(ctx *context.Context) { // RecentCommitsData returns JSON of recent commits data func RecentCommitsData(ctx *context.Context) { if contributorStats, err := contributors_service.GetContributorStats(ctx, ctx.Cache, ctx.Repo.Repository, ctx.Repo.CommitID); err != nil { - ctx.ServerError("GetCodeFrequencyData", err) + if errors.Is(err, contributors_service.ErrAwaitGeneration) { + ctx.Status(http.StatusAccepted) + return + } + ctx.ServerError("RecentCommitsData", err) } else { ctx.JSON(http.StatusOK, contributorStats["total"].Weeks) } diff --git a/templates/repo/recent_commits.tmpl b/templates/repo/recent_commits.tmpl index 8674265a60e6..6e6e772a25d1 100644 --- a/templates/repo/recent_commits.tmpl +++ b/templates/repo/recent_commits.tmpl @@ -1,3 +1,9 @@ {{if .Permission.CanRead $.UnitTypeCode}} -

Recent Commits

+
+
{{end}} diff --git a/web_src/js/components/RepoRecentCommits.vue b/web_src/js/components/RepoRecentCommits.vue new file mode 100644 index 000000000000..8e4a8dbf026c --- /dev/null +++ b/web_src/js/components/RepoRecentCommits.vue @@ -0,0 +1,183 @@ + + + diff --git a/web_src/js/features/recent-commits.js b/web_src/js/features/recent-commits.js new file mode 100644 index 000000000000..ded10d39be49 --- /dev/null +++ b/web_src/js/features/recent-commits.js @@ -0,0 +1,21 @@ +import {createApp} from 'vue'; + +export async function initRepoRecentCommits() { + const el = document.getElementById('repo-recent-commits-chart'); + if (!el) return; + + const {default: RepoRecentCommits} = await import(/* webpackChunkName: "recent-commits-graph" */'../components/RepoRecentCommits.vue'); + try { + const View = createApp(RepoRecentCommits, { + locale: { + loadingTitle: el.getAttribute('data-locale-loading-title'), + loadingTitleFailed: el.getAttribute('data-locale-loading-title-failed'), + loadingInfo: el.getAttribute('data-locale-loading-info'), + } + }); + View.mount(el); + } catch (err) { + console.error('RepoRecentCommits failed to load', err); + el.textContent = el.getAttribute('data-locale-component-failed-to-load'); + } +} diff --git a/web_src/js/index.js b/web_src/js/index.js index 078f9fc9df41..d7740d7590a4 100644 --- a/web_src/js/index.js +++ b/web_src/js/index.js @@ -84,6 +84,7 @@ import {onDomReady} from './utils/dom.js'; import {initRepoIssueList} from './features/repo-issue-list.js'; import {initCommonIssueListQuickGoto} from './features/common-issue-list.js'; import {initRepoContributors} from './features/contributors.js'; +import {initRepoRecentCommits} from './features/recent-commits.js'; import {initRepoDiffCommitBranchesAndTags} from './features/repo-diff-commit.js'; import {initDirAuto} from './modules/dirauto.js'; @@ -174,6 +175,7 @@ onDomReady(() => { initRepository(); initRepositoryActionView(); initRepoContributors(); + initRepoRecentCommits(); initCommitStatuses(); initCaptcha(); From 16e5260eb037113ff5fbb9b04085c070de393c67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Eahin=20Akkaya?= Date: Sat, 17 Feb 2024 14:01:26 +0300 Subject: [PATCH 03/10] Fix linting error --- web_src/js/components/RepoRecentCommits.vue | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/web_src/js/components/RepoRecentCommits.vue b/web_src/js/components/RepoRecentCommits.vue index 8e4a8dbf026c..002e46eb2813 100644 --- a/web_src/js/components/RepoRecentCommits.vue +++ b/web_src/js/components/RepoRecentCommits.vue @@ -22,7 +22,6 @@ import { fillEmptyStartDaysWithZeroes, } from '../utils/time.js'; import 'chartjs-adapter-dayjs-4/dist/chartjs-adapter-dayjs-4.esm'; -import $ from 'jquery'; const {pageData} = window.config; @@ -108,8 +107,8 @@ export default { datasets: [ { data: data.map((i) => ({x: i.week, y: i.commits})), - label: "Commits", - backgroundColor: colors["commits"], + label: 'Commits', + backgroundColor: colors['commits'], borderWidth: 0, tension: 0.3, }, @@ -117,7 +116,6 @@ export default { }; }, - getOptions() { return { responsive: true, @@ -152,7 +150,7 @@ export default { }, }, }; - + From f8771d5a9ee189bd6754f04e89329c2848a69101 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Eahin=20Akkaya?= Date: Sat, 24 Feb 2024 12:50:17 +0300 Subject: [PATCH 09/10] Apply suggestions from code review --- routers/web/repo/recent_commits.go | 1 - templates/repo/recent_commits.tmpl | 1 + web_src/js/components/RepoRecentCommits.vue | 9 +++++---- web_src/js/features/recent-commits.js | 3 ++- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/routers/web/repo/recent_commits.go b/routers/web/repo/recent_commits.go index 3507cb875211..b4de1919a67e 100644 --- a/routers/web/repo/recent_commits.go +++ b/routers/web/repo/recent_commits.go @@ -22,7 +22,6 @@ func RecentCommits(ctx *context.Context) { ctx.Data["PageIsActivity"] = true ctx.Data["PageIsRecentCommits"] = true - ctx.PageData["repoLink"] = ctx.Repo.RepoLink ctx.HTML(http.StatusOK, tplRecentCommits) } diff --git a/templates/repo/recent_commits.tmpl b/templates/repo/recent_commits.tmpl index 5c241d635cd9..04757338d42e 100644 --- a/templates/repo/recent_commits.tmpl +++ b/templates/repo/recent_commits.tmpl @@ -4,6 +4,7 @@ data-locale-loading-title-failed="{{ctx.Locale.Tr "graphs.component_loading_failed" (ctx.Locale.Tr "graphs.recent_commits.what")}}" data-locale-loading-info="{{ctx.Locale.Tr "graphs.component_loading_info"}}" data-locale-component-failed-to-load="{{ctx.Locale.Tr "graphs.component_failed_to_load"}}" + data-repo-link="{{ctx.Repo.RepoLink}}" > {{end}} diff --git a/web_src/js/components/RepoRecentCommits.vue b/web_src/js/components/RepoRecentCommits.vue index 77697cd413f9..4a1bbcd79175 100644 --- a/web_src/js/components/RepoRecentCommits.vue +++ b/web_src/js/components/RepoRecentCommits.vue @@ -18,8 +18,6 @@ import {chartJsColors} from '../utils/color.js'; import {sleep} from '../utils.js'; import 'chartjs-adapter-dayjs-4/dist/chartjs-adapter-dayjs-4.esm'; -const {pageData} = window.config; - Chart.defaults.color = chartJsColors.text; Chart.defaults.borderColor = chartJsColors.border; @@ -37,11 +35,14 @@ export default { type: Object, required: true }, + repoLink: { + type: String, + required: true + } }, data: () => ({ isLoading: false, errorText: '', - repoLink: pageData.repoLink || [], data: [], }), mounted() { @@ -53,7 +54,7 @@ export default { try { let response; do { - response = await GET(`${this.repoLink}/activity/recent-commits/data`); + response = await GET(`${repoLink}/activity/recent-commits/data`); if (response.status === 202) { await sleep(1000); // wait for 1 second before retrying } diff --git a/web_src/js/features/recent-commits.js b/web_src/js/features/recent-commits.js index ded10d39be49..a32670782665 100644 --- a/web_src/js/features/recent-commits.js +++ b/web_src/js/features/recent-commits.js @@ -11,7 +11,8 @@ export async function initRepoRecentCommits() { loadingTitle: el.getAttribute('data-locale-loading-title'), loadingTitleFailed: el.getAttribute('data-locale-loading-title-failed'), loadingInfo: el.getAttribute('data-locale-loading-info'), - } + }, + repoLink: el.getAttribute('data-repo-link') }); View.mount(el); } catch (err) { From bc0294d7521dcb838b260f9dc89d5f6976892737 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Eahin=20Akkaya?= Date: Sat, 24 Feb 2024 12:57:13 +0300 Subject: [PATCH 10/10] Revert "Apply suggestions from code review" This reverts commit f8771d5a9ee189bd6754f04e89329c2848a69101. --- routers/web/repo/recent_commits.go | 1 + templates/repo/recent_commits.tmpl | 1 - web_src/js/components/RepoRecentCommits.vue | 9 ++++----- web_src/js/features/recent-commits.js | 3 +-- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/routers/web/repo/recent_commits.go b/routers/web/repo/recent_commits.go index b4de1919a67e..3507cb875211 100644 --- a/routers/web/repo/recent_commits.go +++ b/routers/web/repo/recent_commits.go @@ -22,6 +22,7 @@ func RecentCommits(ctx *context.Context) { ctx.Data["PageIsActivity"] = true ctx.Data["PageIsRecentCommits"] = true + ctx.PageData["repoLink"] = ctx.Repo.RepoLink ctx.HTML(http.StatusOK, tplRecentCommits) } diff --git a/templates/repo/recent_commits.tmpl b/templates/repo/recent_commits.tmpl index 04757338d42e..5c241d635cd9 100644 --- a/templates/repo/recent_commits.tmpl +++ b/templates/repo/recent_commits.tmpl @@ -4,7 +4,6 @@ data-locale-loading-title-failed="{{ctx.Locale.Tr "graphs.component_loading_failed" (ctx.Locale.Tr "graphs.recent_commits.what")}}" data-locale-loading-info="{{ctx.Locale.Tr "graphs.component_loading_info"}}" data-locale-component-failed-to-load="{{ctx.Locale.Tr "graphs.component_failed_to_load"}}" - data-repo-link="{{ctx.Repo.RepoLink}}" > {{end}} diff --git a/web_src/js/components/RepoRecentCommits.vue b/web_src/js/components/RepoRecentCommits.vue index 4a1bbcd79175..77697cd413f9 100644 --- a/web_src/js/components/RepoRecentCommits.vue +++ b/web_src/js/components/RepoRecentCommits.vue @@ -18,6 +18,8 @@ import {chartJsColors} from '../utils/color.js'; import {sleep} from '../utils.js'; import 'chartjs-adapter-dayjs-4/dist/chartjs-adapter-dayjs-4.esm'; +const {pageData} = window.config; + Chart.defaults.color = chartJsColors.text; Chart.defaults.borderColor = chartJsColors.border; @@ -35,14 +37,11 @@ export default { type: Object, required: true }, - repoLink: { - type: String, - required: true - } }, data: () => ({ isLoading: false, errorText: '', + repoLink: pageData.repoLink || [], data: [], }), mounted() { @@ -54,7 +53,7 @@ export default { try { let response; do { - response = await GET(`${repoLink}/activity/recent-commits/data`); + response = await GET(`${this.repoLink}/activity/recent-commits/data`); if (response.status === 202) { await sleep(1000); // wait for 1 second before retrying } diff --git a/web_src/js/features/recent-commits.js b/web_src/js/features/recent-commits.js index a32670782665..ded10d39be49 100644 --- a/web_src/js/features/recent-commits.js +++ b/web_src/js/features/recent-commits.js @@ -11,8 +11,7 @@ export async function initRepoRecentCommits() { loadingTitle: el.getAttribute('data-locale-loading-title'), loadingTitleFailed: el.getAttribute('data-locale-loading-title-failed'), loadingInfo: el.getAttribute('data-locale-loading-info'), - }, - repoLink: el.getAttribute('data-repo-link') + } }); View.mount(el); } catch (err) {