Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

贡献者名称获取 #689

Open
ouuan opened this issue Feb 12, 2021 · 20 comments
Open

贡献者名称获取 #689

ouuan opened this issue Feb 12, 2021 · 20 comments
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed

Comments

@ouuan
Copy link
Member

ouuan commented Feb 12, 2021

用 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
Copy link
Member Author

ouuan commented Feb 12, 2021

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

@ouuan
Copy link
Member Author

ouuan commented Feb 12, 2021

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

@Enter-tainer Enter-tainer added enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed labels Feb 12, 2021
@ouuan
Copy link
Member Author

ouuan commented Feb 12, 2021

啊,其实可以从 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 开始时读取

大概这种感觉

@sshwy
Copy link
Member

sshwy commented Feb 13, 2021

我的想法:

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

@ouuan
Copy link
Member Author

ouuan commented Feb 13, 2021

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

@ouuan
Copy link
Member Author

ouuan commented Feb 13, 2021

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

@sshwy
Copy link
Member

sshwy commented Feb 13, 2021

有道理

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

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

@ouuan
Copy link
Member Author

ouuan commented Feb 13, 2021

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

@ouuan
Copy link
Member Author

ouuan commented Feb 13, 2021

每次都用 bot 改的话:

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

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

@ouuan
Copy link
Member Author

ouuan commented Feb 13, 2021

@ouuan ouuan added bug Something isn't working and removed enhancement New feature or request labels Feb 18, 2021
@kidonng
Copy link

kidonng commented Feb 20, 2021

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

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

@ouuan
Copy link
Member Author

ouuan commented Feb 20, 2021

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

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

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

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

@SkyeYoung
Copy link
Member

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

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

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

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

@diauweb diauweb closed this as completed Aug 9, 2021
@diauweb diauweb reopened this Aug 9, 2021
@diauweb
Copy link
Member

diauweb commented Aug 9, 2021

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

@SkyeYoung
Copy link
Member

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

@diauweb
Copy link
Member

diauweb commented Aug 30, 2021

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

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

@ouuan
Copy link
Member Author

ouuan commented Aug 30, 2021

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

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

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

@diauweb
Copy link
Member

diauweb commented Aug 30, 2021

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

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

@ouuan
Copy link
Member Author

ouuan commented Aug 30, 2021

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

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

@Enter-tainer
Copy link
Member

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

6 participants