Skip to content

Commit c1bdca6

Browse files
committed
fix: apply PR lowlighter#1754 working code for recent languages analyzer
Uses exact implementation from oddstr13's PR lowlighter#1754: - Builds wanted map with repo@ref keys - Fetches commits via REST API directly - Tracks before/head SHAs for early exit - Uses optional chaining to prevent destructuring errors - Fetches full commit details with file patches
1 parent 17de6f6 commit c1bdca6

File tree

1 file changed

+44
-47
lines changed

1 file changed

+44
-47
lines changed

source/plugins/languages/analyzer/recent.mjs

Lines changed: 44 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class RecentAnalyzer extends Analyzer {
3030
async patches() {
3131
//Fetch commits from recent activity
3232
this.debug(`fetching patches from last ${this.days || ""} days up to ${this.load || "∞"} events`)
33-
const events = [], pages = Math.ceil((this.load || Infinity) / 100)
33+
const pages = Math.ceil((this.load || Infinity) / 100)
3434
if (this.context.mode === "repository") {
3535
try {
3636
const {data: {default_branch: branch}} = await this.rest.repos.get(this.context)
@@ -42,6 +42,7 @@ export class RecentAnalyzer extends Analyzer {
4242
this.debug(`failed to get default branch for ${this.context.owner}/${this.context.repo} (${error})`)
4343
}
4444
}
45+
const events = []
4546
try {
4647
for (let page = 1; page <= pages; page++) {
4748
this.debug(`fetching events page ${page}`)
@@ -58,65 +59,62 @@ export class RecentAnalyzer extends Analyzer {
5859
this.debug("no more page to load")
5960
}
6061
this.debug(`fetched ${events.length} events`)
61-
this.results.latest = Math.round((new Date().getTime() - new Date(events.slice(-1).shift()?.created_at).getTime()) / (1000 * 60 * 60 * 24))
62-
this.results.commits = events.length
63-
64-
//Retrieve edited files and filter edited lines (those starting with +/-) from patches
65-
this.debug("fetching patches")
6662

67-
//Extract repository/ref combinations and their commit SHAs from events
68-
const commitsByRepo = new Map()
69-
for (const {payload, repo: {name}} of events) {
70-
const {ref, before, head, commits: eventCommits} = payload
71-
if (!ref || !name) continue
72-
const key = `${name}:${ref}`
73-
if (!commitsByRepo.has(key)) {
74-
commitsByRepo.set(key, {repo: name, ref, before, head, shas: new Set()})
75-
}
76-
const entry = commitsByRepo.get(key)
77-
//Add commit SHAs if available in payload
78-
if (Array.isArray(eventCommits)) {
79-
for (const {sha} of eventCommits) {
80-
entry.shas.add(sha)
81-
}
82-
}
83-
entry.head = head //Update with latest head
84-
}
63+
const wanted = new Map()
64+
events.forEach(event => {
65+
let key = `${event.repo.name}@${event.payload.ref}`
66+
let item = wanted.get(key) ?? { commits: [] }
67+
item.repo = event.repo.name
68+
item.ref = event.payload.ref
69+
item.commits.push(event.payload.before)
70+
item.commits.push(event.payload.head)
71+
wanted.set(key, item)
72+
})
8573

86-
//Fetch commits from repos and filter patches
87-
const commitUrls = []
88-
for (const {repo, ref} of commitsByRepo.values()) {
89-
const [owner, repoName] = repo.split("/")
90-
const branch = ref.replace("refs/heads/", "")
91-
//Fetch commit list for this repo/branch
74+
const commits = []
75+
for (const item of wanted.values()) {
9276
try {
93-
const {data: commits} = await this.rest.request(`GET /repos/{owner}/{repo}/commits`, {
94-
owner,
95-
repo: repoName,
96-
sha: branch,
97-
per_page: 100,
98-
})
99-
//Filter by authoring and collect URLs
100-
for (const commit of commits) {
101-
const email = commit.author?.email || commit.commit.author?.email
102-
if (email && filters.text(email, this.authoring, {debug: false}) && commit.parents?.length <= 1) {
103-
commitUrls.push(commit.url)
77+
for (let page = 1; page <= pages; page++) {
78+
this.debug(`fetching commits page ${page}`)
79+
this.debug(`https://api.github.com/repos/${item.repo}/commits?sha=${item.ref}&per_page=20&page=${page}`)
80+
commits.push(
81+
...(await this.rest.request(`https://api.github.com/repos/${item.repo}/commits?sha=${item.ref}&per_page=20&page=${page}`)).data
82+
.map(x => {
83+
item.commits = item.commits.filter(c => c !== x.sha)
84+
return x
85+
})
86+
.filter(({ committer }) => (this.account === "organization") || (this.context.mode === "repository") ? true : !filters.text(committer?.login, [this.login], { debug: false }))
87+
.filter(({ commit }) => ((!this.days) || (new Date(commit.committer.date) > new Date(Date.now() - this.days * 24 * 60 * 60 * 1000)))),
88+
)
89+
if (item.commits.length < 1) {
90+
this.debug("found expected commits")
91+
break
10492
}
10593
}
106-
} catch (error) {
107-
this.debug(`failed to fetch commits for ${repo}: ${error}`)
94+
}
95+
catch {
96+
this.debug("no more page to load")
10897
}
10998
}
11099

111-
//Fetch full commit details with patches
100+
this.debug(`fetched ${commits.length} commits`)
101+
this.results.latest = Math.round((new Date().getTime() - new Date(events.slice(-1).shift()?.created_at).getTime()) / (1000 * 60 * 60 * 24))
102+
this.results.commits = commits.length
103+
104+
//Retrieve edited files and filter edited lines (those starting with +/-) from patches
105+
this.debug("fetching patches")
112106
const patches = [
113107
...await Promise.allSettled(
114-
commitUrls.map(async url => (await this.rest.request(url)).data),
108+
commits
109+
.filter(({committer}) => filters.text(committer?.email, this.authoring, {debug: false}))
110+
.map(commit => commit.url)
111+
.map(async commit => (await this.rest.request(commit)).data),
115112
),
116113
]
117114
.filter(({status}) => status === "fulfilled")
118115
.map(({value}) => value)
119-
.map(({sha, commit: {message, author}, verification, files}) => ({
116+
.filter(({parents}) => parents.length <= 1)
117+
.map(({ sha, commit: { message, author }, verification, files }) => ({
120118
sha,
121119
name: `${message} (authored by ${author.name} on ${author.date})`,
122120
verified: verification?.verified ?? null,
@@ -141,7 +139,6 @@ export class RecentAnalyzer extends Analyzer {
141139
return edition
142140
}),
143141
}))
144-
145142
return patches
146143
}
147144

0 commit comments

Comments
 (0)