gatsby-oi-wiki icon indicating copy to clipboard operation
gatsby-oi-wiki copied to clipboard

贡献者名称获取

Open ouuan opened this issue 4 years ago • 20 comments
trafficstars

用 author 肯定是不行的,用 API 依次查询每个 commit 的 author 工作量有点大,我希望是通过 API 查询每个 author 的 username。

伪代码:

const emailToAuthor = Array.from(new Set(commits.map(({ email }) => email))).reduce((map, email) => {
  const match = email.match(/\d+\+(.+)@users\.noreply\.github\.com/);
  if (match) map.set(email, match[1]);
  else map.set(email, get(`https://api.github.com/search/users?q=${email}`).items[0].login);
  return map;
}, new Map());

ouuan avatar Feb 12 '21 09:02 ouuan

在 build 时执行,API 可以缓存结果(到文件),(用 scheduled Actions)定期更新(以防用户名更改)。

ouuan avatar Feb 12 '21 10:02 ouuan

啊,其实可以从 commit 获取 author 而非 search..

ouuan avatar Feb 12 '21 10:02 ouuan

啊,其实可以从 commit 获取 author 而非 search..

// build 时用来获取作者
function getAuthor(map, { email, sha }) {
  if (map.has(email)) return map.get(email);
  const res = get(
    `https://api.github.com/repos/OI-wiki/OI-wiki/commits/${sha}`
  );
  if (res.author) {
    const author = res.author.login;
    map.set(email, author);
    return author;
  }
  return null;
}

// 定期运行
function updateAll() {
  const emailToAuthor = new Map();
  commits.forEach((commit) => getAuthor(emailToAuthor, commit));
}

// build 结束 / updateAll 结束后,将 emailToAuthor 存下来,build 开始时读取

大概这种感觉

ouuan avatar Feb 12 '21 12:02 ouuan

我的想法:

  1. 在遇到文件改动的时候github api是否会失效(比如既改文件名又做出大量改动)
  2. 手动维护 author 实际上是把主动权交给贡献者。如果他想要刘明就会自己去更新字段(相当于甩锅给贡献者),可能在migrate的时候会有点工作量。另外,author字段相比于自动化fetch的容错率更高(虽然也参杂有我懒不想写代码的因素),因此 author 字段并非不可行。

sshwy avatar Feb 13 '21 01:02 sshwy

我是觉得手动修改 author 太麻烦了,而且不是所有人都会知道需要修改。可能一个人想留名但第一次贡献,就改个 typo,根本不会知道什么是 author 字段。

ouuan avatar Feb 13 '21 02:02 ouuan

文件改动的时候再加 author 字段,以前都是这么搞的。

ouuan avatar Feb 13 '21 02:02 ouuan

有道理

我有个方案:让bot来fetch github commit,然后修改author子段,只増不删

这样可以解决新人贡献和文件改动的问题 :new_moon_with_face:

sshwy avatar Feb 13 '21 02:02 sshwy

我是觉得搞个脚本帮助移动文件时添加 author 比较好..

ouuan avatar Feb 13 '21 02:02 ouuan

每次都用 bot 改的话:

  1. bot 的 commit 太多了,通知吵,log 乱。
  2. author 字段是单行的,conflict 很难处理,还是少改为好。
  3. 这并不比发生文件改动时修改好多少。

另外就是 git log --follow 可以解决大多数无需人工干预的文件移动。

ouuan avatar Feb 13 '21 02:02 ouuan

https://github.com/ouuan/github-file-authors

ouuan avatar Feb 13 '21 12:02 ouuan

之前写了一个 VuePress 插件满足同样的需求,用了一个取巧的办法,希望能有所帮助。

如果想要本地解析 Git 日志可以参考 https://github.com/Fischermaen/vuepress-plugin-contributors

kidonng avatar Feb 20 '21 16:02 kidonng

之前写了一个 VuePress 插件满足同样的需求,用了一个取巧的办法,希望能有所帮助。

这个东西不支持文件重命名,也不支持手动设置识别不出来的邮箱的 GitHub 帐号。

如果想要本地解析 Git 日志可以参考 https://github.com/Fischermaen/vuepress-plugin-contributors

本地解析确实高效一些,我写的调用 Git 很有点慢..但是懒得研究了。大约不会比 Mathjax SSR 以及 Gulp minify 慢(

ouuan avatar Feb 20 '21 16:02 ouuan

最近几天整了一个 git-authors-info,从目前干的事情来看和 github-file-authors 好像差不多,除了在实现的功能和性能上有些许差异。

关于贡献者名称的获取,我建议的方式是:类似 sidebar.yml,使用 bootstrap 构建时生成一个缓存了邮件地址和对应贡献者名称的 json 文件,在 changelog 等需要贡献者名称的页面,利用邮件地址对贡献者名称进行替换。带来的改变是每个页面都需要查询对应的 commit log 以记录 Author 的邮件地址和名称,关于这一点我还没试过 gatsby-source-local-git 能不能做到。

如果做不到的话,我写的脚本稍微改一下,也能缓存每个页面的 Author。目前的仓库也可以跑一下 build 生成单独的脚本,可能和 bootstrap 整合起来比较方便。

拿出来想请教一下各位,我不知道我这个思路和脚本各位觉得行不?

SkyeYoung avatar Aug 09 '21 03:08 SkyeYoung

最好不要和bootstrap扯上关系 这个东西只是用来预览全量版本的

diauweb avatar Aug 09 '21 03:08 diauweb

@diauweb 嗯嗯,只要在构建的时候同时跑一下就可以了。

SkyeYoung avatar Aug 09 '21 03:08 SkyeYoung

https://github.com/OI-wiki/oiwiki-migrator/tree/goodauthor

abuse 了一下 GraphQL API, 一次性查询了所有 commit (~4500) 的 authors,且只消耗了 5 个 quota

diauweb avatar Aug 30 '21 14:08 diauweb

https://github.com/OI-wiki/oiwiki-migrator/tree/goodauthor

abuse 了一下 GraphQL API, 一次性查询了所有 commit (~4500) 的 authors,且只消耗了 5 个 quota

这个文件重命名后还能查到作者吗

ouuan avatar Aug 30 '21 14:08 ouuan

这个文件重命名后还能查到作者吗

因为是按文件 git log 的 所以大概 --follow 之后可以

diauweb avatar Aug 30 '21 15:08 diauweb

因为是按文件 git log 的 所以大概 --follow 之后可以

哦,刚刚没看代码..我还以为全是用 API 实现的 🌚

ouuan avatar Aug 30 '21 15:08 ouuan

mgt, [08.09.21 22:22] 或者要不这样, 对于能从 git 里面查到的, 带上链接

mgt, [08.09.21 22:22] author 字段里面的, 不加链接

Enter-tainer avatar Sep 11 '21 04:09 Enter-tainer