async function collectExecutiveSummary()

in apps/contribution-report/app.ts [42:163]


async function collectExecutiveSummary() {
    const teamStats = await Promise.all(team_members.map(async member => {

        const repositoryStats = await Promise.all(repositories.map(async ({owner, repo}) => {

            const issuesAndPrsAll = await octokit.request('GET /repos/{owner}/{repo}/issues', {
                    owner: owner,
                    repo: repo,
                    creator: member.github,
                    state: "all"
                });

            const issuesAndPrs = issuesAndPrsAll.data.filter(d => 
                DateTime.fromISO(d.created_at) < date_end &&
                DateTime.fromISO(d.created_at) > date_start);
            
            // Get number of issues and prs
            const issues = issuesAndPrs.filter(d => d.node_id.split("_")[0] === "I");
            const prs = issuesAndPrs.filter(d => d.node_id.split("_")[0] === "PR");
            const sumIssues = issues.length;
            const sumPrs = prs.length;
            const issueLinks = issues.map(i => ({
                title: i.title,
                date: DateTime.fromISO(i.updated_at).toLocaleString(DateTime.DATETIME_MED),
                link: i.html_url,
            }));
            const prLinks =  prs.map(p => ({
                title: p.title,
                status: p.state,
                date: DateTime.fromISO(p.updated_at).toLocaleString(DateTime.DATETIME_MED),
                link: p.html_url,
            }));
            
            // Get number of commits merged
            const commits = await Promise.all(prs.map(async (pr) => {
                // Get number of commits
                const patch = (await fetch(pr.pull_request.patch_url));
                const text = await patch.text();
                const numCommits = parseInt((text.split("Subject: [PATCH 1/")[1]?.split("]")[0] ?? "1"));

                // Check if commits were merged
                const timeline = await (await octokit.request(`GET ${pr.events_url}`)).data;
                const mergeEvents = timeline.filter(t => t.event === "merged");
                if (mergeEvents.length === 0) {
                    return { commits: 0 };
                }

                return {
                    pr_title: pr.title,
                    pr_url: pr.html_url,
                    is_merged: mergeEvents.length !== 0,
                    commits: numCommits,
                };
            }));

            const sumCommits = commits.reduce((a,b)=> {
                return a + b.commits;
            }, 0);

            // Get Reviewed PRs
            const crPrsQuery = `is:pr reviewed-by:${member.github} repo:${owner}/${repo} created:${date_start.toISODate()}..${date_end.toISODate()}`;
            const crPrs = await searchRateLimiter(crPrsQuery);
            const crPrLinks = crPrs.data.items.map(cp => cp.html_url);
            const crCount = crPrs.data.total_count;

            return {
                repo: `${owner}/${repo}`,
                count_review: crCount,
                count_commit: sumCommits,
                count_issue: sumIssues,
                count_pr: sumPrs,

                issues: issueLinks,
                prs: prLinks,
                reviews: crPrLinks,
                commits: commits,
            }
        }));

        return {
            team_member: member,
            repository_stats: repositoryStats,
            executive_summary_member: repositoryStats.reduce((a,b) => ({
                total_review: a.total_review + b.count_review,
                total_commit: a.total_commit + b.count_commit,
                total_issue: a.total_issue + b.count_issue,
                total_pr: a.total_pr + b.count_pr,
            }),
            {
                total_review: 0,
                total_commit: 0,
                total_issue: 0,
                total_pr: 0,
            })
        }
    }));

    return {
        date_start: date_start.toLocaleString(DateTime.DATETIME_MED),
        date_end: date_end.toLocaleString(DateTime.DATETIME_MED),
        executive_summary_team: 
            {
                ...teamStats.reduce((a,b) => ({
                    total_review: a.total_review + b.executive_summary_member.total_review,
                    total_commit: a.total_commit + b.executive_summary_member.total_commit,
                    total_issue: a.total_issue + b.executive_summary_member.total_issue,
                    total_pr: a.total_pr + b.executive_summary_member.total_pr,
                }),
                {
                    total_review: 0,
                    total_commit: 0,
                    total_issue: 0,
                    total_pr: 0,
                }),
                executive_summary_members: teamStats.map(t => ({
                    ...t.executive_summary_member,
                    member: t.team_member.name
                })) 
            },
        team_stats: teamStats,
    }
}