Skip to content

Commit

Permalink
fix: case-insensitive language comparison (#204)
Browse files Browse the repository at this point in the history
Made a fix to make language comparisons case-insenstitive for excluded language
Added few test cases
  • Loading branch information
gauravnadkarni authored Dec 3, 2024
1 parent 2eac6ef commit 88bc12b
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 7 deletions.
3 changes: 2 additions & 1 deletion api/cards/most-commit-language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export default async (req: VercelRequest, res: VercelResponse) => {
}
let excludeArr = <string[]>[];
exclude.split(',').forEach(function (val) {
excludeArr.push(translateLanguage(val));
const translatedLanguage = translateLanguage(val);
excludeArr.push(translatedLanguage.toLowerCase());
});

try {
Expand Down
3 changes: 2 additions & 1 deletion api/cards/repos-per-language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export default async (req: VercelRequest, res: VercelResponse) => {
}
let excludeArr = <string[]>[];
exclude.split(',').forEach(function (val) {
excludeArr.push(translateLanguage(val));
const translatedLanguage = translateLanguage(val);
excludeArr.push(translatedLanguage.toLowerCase());
});

try {
Expand Down
5 changes: 3 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const execCmd = (cmd: string, args: string[] = []) =>
app.on('error', reject);
});

//ProfileSummaryCardsTemplate
// ProfileSummaryCardsTemplate
const commitFile = async () => {
await execCmd('git', ['config', '--global', 'user.email', '[email protected]']);
await execCmd('git', ['config', '--global', 'user.name', 'profile-summary-cards[bot]']);
Expand Down Expand Up @@ -144,7 +144,8 @@ if (process.argv.length == 2) {
const exclude: Array<string> = [];
if (process.argv[4]) {
process.argv[4].split(',').forEach(function (val) {
exclude.push(translateLanguage(val));
const translatedLanguage = translateLanguage(val);
exclude.push(translatedLanguage.toLowerCase());
});
}
main(username, utcOffset, exclude);
Expand Down
2 changes: 1 addition & 1 deletion src/github-api/commits-per-language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export async function getCommitLanguage(username: string, exclude: Array<string>
const langName = node.repository.primaryLanguage.name;
const langColor = node.repository.primaryLanguage.color;
const totalCount = node.contributions.totalCount;
if (!exclude.includes(langName)) {
if (!exclude.includes(langName.toLowerCase())) {
commitLanguages.addLanguageCount(langName, langColor, totalCount);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/github-api/repos-per-language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export async function getRepoLanguages(username: string, exclude: Array<string>)
if (node.primaryLanguage) {
const langName = node.primaryLanguage.name;
const langColor = node.primaryLanguage.color;
if (!exclude.includes(langName)) {
if (!exclude.includes(langName.toLowerCase())) {
repoLanguages.addLanguage(langName, langColor);
}
}
Expand Down
26 changes: 25 additions & 1 deletion tests/github-api/commits-per-language.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ const data = {
totalCount: 100
}
},
{
repository: {
primaryLanguage: {
name: 'Jupyter Notebook',
color: '#f18e33'
}
},
contributions: {
totalCount: 75
}
},
{
repository: {
primaryLanguage: null
Expand Down Expand Up @@ -77,7 +88,8 @@ describe('commit contributions on github', () => {
expect(totalContributions).toEqual({
languageMap: new Map([
['Rust', {color: '#dea584', count: 199, name: 'Rust'}],
['JavaScript', {color: '#f1e05a', count: 84, name: 'JavaScript'}]
['JavaScript', {color: '#f1e05a', count: 84, name: 'JavaScript'}],
['Jupyter Notebook', {color: '#f18e33', count: 75, name: 'Jupyter Notebook'}]
])
});
});
Expand All @@ -86,4 +98,16 @@ describe('commit contributions on github', () => {
mock.onPost('https://api.github.com/graphql').reply(200, error);
await expect(getCommitLanguage('vn7n24fzkq', [])).rejects.toThrow('GitHub api failed');
});

it('should do a case-insensitive comparison for language exclusion', async () => {
mock.onPost('https://api.github.com/graphql')
.reply(200, data);
const repoData = await getCommitLanguage('vn7n24fzkq', ['jupyter notebook']);
expect(repoData).toEqual({
languageMap: new Map([
['Rust', {color: '#dea584', count: 199, name: 'Rust'}],
['JavaScript', {color: '#f1e05a', count: 84, name: 'JavaScript'}]
])
});
});
});
51 changes: 51 additions & 0 deletions tests/github-api/repos-per-language.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,45 @@ const error = {
]
};

const dataContainingLanguageWithWhiteSpace = {
data: {
user: {
repositories: {
nodes: [
{
primaryLanguage: {
color: '#b07219',
name: 'Rust'
}
},
{
primaryLanguage: {
color: '#f18e33',
name: 'Kotlin'
}
},
{
primaryLanguage: {
color: '#f1948a',
name: 'Jupyter Notebook'
}
},
{
primaryLanguage: {
color: '#f9e79f',
name: 'Java'
}
}
],
pageInfo: {
endCursor: null,
hasNextPage: false
}
}
}
}
}

afterEach(() => {
mock.reset();
});
Expand All @@ -92,4 +131,16 @@ describe('repos per language on github', () => {
mock.onPost('https://api.github.com/graphql').reply(200, error);
await expect(getRepoLanguages('vn7n24fzkq', [])).rejects.toThrow('GitHub api failed');
});

it('should do a case-insensitive comparison for language exclusion', async () => {
mock.onPost('https://api.github.com/graphql')
.reply(200, dataContainingLanguageWithWhiteSpace);
const repoData = await getRepoLanguages('vn7n24fzkq', ['rust','jupyter notebook']);
expect(repoData).toEqual({
languageMap: new Map([
['Kotlin', {color: '#f18e33', count: 1, name: 'Kotlin'}],
['Java', {color: '#f9e79f', count: 1, name: 'Java'}]
])
});
});
});

0 comments on commit 88bc12b

Please sign in to comment.