Skip to content

Commit a44adcc

Browse files
committed
fix: owners.txt names display first. filter robot such as github-actions
1 parent f6323c1 commit a44adcc

2 files changed

Lines changed: 104 additions & 23 deletions

File tree

lib/git-contributor.js

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,22 @@ const getInfoFromGithubNewAPI = info => {
5959
return requestGithub(uri);
6060
};
6161

62+
const isBotUser = (login) => {
63+
const botPatterns = [
64+
'github-actions',
65+
'dependabot',
66+
'renovate',
67+
'greenkeeper',
68+
'snyk-bot',
69+
'codecov',
70+
'coveralls'
71+
];
72+
const lowerLogin = login.toLowerCase();
73+
return botPatterns.some(pattern => lowerLogin.includes(pattern)) || lowerLogin.endsWith('[bot]');
74+
};
75+
6276
const format = list => {
63-
return list.filter(item => item).map(item => {
77+
return list.filter(item => item && !isBotUser(item.login)).map(item => {
6478
return {
6579
login: item.login,
6680
avatar_url: item.avatar_url,
@@ -103,7 +117,9 @@ exports.getAuthor = async (options = {}) => {
103117
const ownersPath = options.owners ? path.resolve(cwd, options.owners) : null;
104118

105119
let authorList = [];
120+
let contributorList = [];
106121

122+
// First, get contributors from GitHub or git log
107123
if (_.isExistedFile(originPkg)) {
108124
try {
109125
const pkg = require(originPkg);
@@ -119,49 +135,53 @@ exports.getAuthor = async (options = {}) => {
119135
if (repoUrl) {
120136
const info = await getRepoInfo(repoUrl);
121137
const infoList = await getInfoFromGithubNewAPI(info);
122-
authorList = format(infoList);
138+
contributorList = format(infoList);
123139
}
124140
}
125141
} catch (e) {
126142
}
127143
} else if (pointGithubRepoUrl) {
128144
const info = await getRepoInfo(pointGithubRepoUrl);
129145
const infoList = await getInfoFromGithubNewAPI(info);
130-
authorList = format(infoList);
146+
contributorList = format(infoList);
131147
}
132148

133-
if (!authorList.length) {
149+
if (!contributorList.length) {
134150
if (!_.isExistedDir(dotGitDir)) {
135-
authorList = [];
151+
contributorList = [];
136152
} else {
137153
const mailList = gitLog2MailList();
138154
const infoList = await getInfoFromGithub(_.uniq(mailList));
139-
authorList = format(infoList);
155+
contributorList = format(infoList);
140156
}
141157
}
142158

159+
// Process owners first (they get priority)
143160
if (ownersPath) {
144161
const owners = parseOwnersFile(ownersPath);
145162
if (owners.length) {
146163
owners.forEach(login => {
147-
authorList.push({
148-
login,
149-
avatar_url: `https://avatars.githubusercontent.com/${login}?v=4`,
150-
html_url: `https://github.com/${login}`
151-
});
164+
// Skip bot users
165+
if (!isBotUser(login)) {
166+
authorList.push({
167+
login,
168+
avatar_url: `https://avatars.githubusercontent.com/${login}?v=4`,
169+
html_url: `https://github.com/${login}`
170+
});
171+
}
152172
});
153173
}
154174
}
155175

156-
// Deduplicate by login
157-
const uniqueAuthors = new Map();
158-
authorList.forEach(author => {
159-
if (!uniqueAuthors.has(author.login)) {
160-
uniqueAuthors.set(author.login, author);
176+
// Then append contributors that are not already in the owners list
177+
const ownersLoginSet = new Set(authorList.map(author => author.login));
178+
contributorList.forEach(contributor => {
179+
if (!ownersLoginSet.has(contributor.login)) {
180+
authorList.push(contributor);
161181
}
162182
});
163183

164-
return Array.from(uniqueAuthors.values());
184+
return authorList;
165185
};
166186

167187
const ifHasZh = (readMeContext) => {

test/git-contributor.test.js

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,24 +266,85 @@ describe('git-contributor', () => {
266266
owners: 'owners.txt'
267267
});
268268

269-
// Should have 3 users (alpha, gamma from API, beta from owners)
269+
// Should have 3 users with owners taking priority
270270
// alpha should appear only once (not duplicated)
271+
// Order: owners first (alpha, beta), then contributors not in owners (gamma)
271272
assert.equal(list.length, 3);
272273
assert.deepEqual(list, [
273274
{
274275
login: 'alpha',
275-
avatar_url: 'avatar-a-api', // Should keep the first occurrence (API version)
276-
html_url: 'html-a'
276+
avatar_url: 'https://avatars.githubusercontent.com/alpha?v=4', // Should keep owners version
277+
html_url: 'https://github.com/alpha'
278+
},
279+
{
280+
login: 'beta',
281+
avatar_url: 'https://avatars.githubusercontent.com/beta?v=4',
282+
html_url: 'https://github.com/beta'
277283
},
278284
{
279285
login: 'gamma',
280286
avatar_url: 'avatar-g',
281287
html_url: 'html-g'
288+
}
289+
]);
290+
});
291+
292+
it('filters out bot users', async () => {
293+
const request = async (uri) => {
294+
return {
295+
data: [
296+
{
297+
login: 'realuser',
298+
avatar_url: 'avatar-r',
299+
html_url: 'html-r'
300+
},
301+
{
302+
login: 'github-actions[bot]',
303+
avatar_url: 'avatar-bot1',
304+
html_url: 'html-bot1'
305+
},
306+
{
307+
login: 'dependabot[bot]',
308+
avatar_url: 'avatar-bot2',
309+
html_url: 'html-bot2'
310+
},
311+
{
312+
login: 'renovate[bot]',
313+
avatar_url: 'avatar-bot3',
314+
html_url: 'html-bot3'
315+
},
316+
{
317+
login: 'anotheruser',
318+
avatar_url: 'avatar-a',
319+
html_url: 'html-a'
320+
}
321+
]
322+
};
323+
};
324+
325+
const contributor = loadContributor({
326+
request,
327+
isExistedFile: () => false,
328+
isExistedDir: () => false
329+
});
330+
331+
const list = await contributor.getAuthor({
332+
cwd: makeTempDir(),
333+
url: 'https://github.com/foo/bar'
334+
});
335+
336+
// Should only have real users, bot users filtered out
337+
assert.equal(list.length, 2);
338+
assert.deepEqual(list, [
339+
{
340+
login: 'realuser',
341+
avatar_url: 'avatar-r',
342+
html_url: 'html-r'
282343
},
283344
{
284-
login: 'beta',
285-
avatar_url: 'https://avatars.githubusercontent.com/beta?v=4',
286-
html_url: 'https://github.com/beta'
345+
login: 'anotheruser',
346+
avatar_url: 'avatar-a',
347+
html_url: 'html-a'
287348
}
288349
]);
289350
});

0 commit comments

Comments
 (0)