|
1 | | -import type { MaybeRefOrGetter } from 'vue' |
| 1 | +import type { MaybeRefOrGetter, Ref } from 'vue' |
2 | 2 | import type { Contributor } from '../../types' |
3 | | -import { Octokit } from '@octokit/rest' |
4 | | -import { computedAsync, isClient } from '@vueuse/core' |
5 | | -import gravatar from 'gravatar' |
6 | | -import { useFrontmatter } from 'valaxy' |
7 | | -import { computed, toValue } from 'vue' |
8 | | -import { useAddonGitLogConfig } from '..' |
| 3 | +import { computedAsync } from '@vueuse/core' |
| 4 | +import { toRef, toValue } from 'vue' |
9 | 5 | import { parseGithubUrl } from '../../utils' |
| 6 | +import { useAddonGitLogConfig } from '../options' |
| 7 | +import { fetchContributors } from '../services' |
| 8 | +import { useGitLog } from './gitlog' |
10 | 9 |
|
11 | | -const octokit = new Octokit() |
12 | | -export function useAddonGitLogContributor(userPath?: MaybeRefOrGetter<string>) { |
13 | | - if (!isClient) |
14 | | - return |
15 | | - |
16 | | - const frontmatter = useFrontmatter() |
| 10 | +export function useContributor(path?: MaybeRefOrGetter<string>): Ref<Contributor[]> { |
| 11 | + const gitLog = useGitLog() |
17 | 12 | const gitLogOptions = useAddonGitLogConfig() |
18 | 13 |
|
19 | | - const gitLog = computed(() => frontmatter.value.git_log || { |
20 | | - contributors: [], |
21 | | - }) |
22 | | - |
23 | 14 | if (gitLogOptions.value.contributor?.mode !== 'api') |
24 | | - return |
25 | | - |
26 | | - const { owner, repo } = parseGithubUrl(gitLogOptions.value.repositoryUrl!) |
27 | | - |
28 | | - const autoPath = gitLog.value.path |
| 15 | + return toRef([]) |
29 | 16 |
|
30 | 17 | const contributors = computedAsync<Contributor[]>( |
31 | 18 | async () => { |
32 | | - const path = toValue(userPath || autoPath) |
33 | | - return await fetchCommits(owner, repo, path) |
| 19 | + const { owner, repo } = parseGithubUrl(gitLogOptions.value.repositoryUrl!) |
| 20 | + const _path = toValue(path || gitLog.value.path) |
| 21 | + return await fetchContributors(owner, repo, _path) |
34 | 22 | }, |
35 | 23 | gitLog.value.contributors, |
36 | 24 | { lazy: true }, |
37 | 25 | ) |
38 | 26 |
|
39 | 27 | return contributors |
40 | 28 | } |
41 | | - |
42 | | -async function fetchCommits(owner: string, repo: string, path: string): Promise<Contributor[]> { |
43 | | - let contributors: Contributor[] = [] |
44 | | - |
45 | | - try { |
46 | | - const { data } = await octokit.repos.listCommits({ owner, repo, path }) |
47 | | - const contributorMap: { [key: string]: Contributor } = {} |
48 | | - |
49 | | - data.forEach(({ author, commit }) => { |
50 | | - const name = author?.name || author?.login || commit.author?.name || 'Unknown Contributor' |
51 | | - const email = author?.email || commit.author?.email |
52 | | - |
53 | | - if (!email) |
54 | | - return |
55 | | - |
56 | | - const github = author?.login ? `https://github.com/${author?.login}` : null |
57 | | - const avatar = author?.avatar_url || gravatar.url(email, { d: 'wavatar' }) |
58 | | - |
59 | | - if (contributorMap[name]) |
60 | | - contributorMap[name].count += 1 |
61 | | - else |
62 | | - contributorMap[name] = { count: 1, name, email, avatar, github } |
63 | | - }) |
64 | | - |
65 | | - contributors = Object.values(contributorMap) |
66 | | - |
67 | | - // sort by commit count |
68 | | - contributors.sort((a: any, b: any) => b.count - a.count) |
69 | | - } |
70 | | - catch (error) { |
71 | | - console.error(`valaxy-addon-git-log: ${error}`) |
72 | | - } |
73 | | - |
74 | | - return contributors |
75 | | -} |
0 commit comments