Skip to content

Commit

Permalink
fix(github sybil): amended the code after PR review
Browse files Browse the repository at this point in the history
Implemented changes to the code to reflect the PR review. In particular, error messages have been
amended to better reflect the error and guide users on how to solve them.
  • Loading branch information
ctrlc03 committed Jan 26, 2023
1 parent f78979d commit 6bbe77b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
13 changes: 9 additions & 4 deletions packages/actions/src/helpers/security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import fetch from "node-fetch"
const getNumberOfPublicRepos = async (user: string, token: string): Promise<number> => {
const response = await fetch(`https://api.github.com/users/${user}/repos`, {
headers: {
Authorization: `token ${token}`
authorization: `token ${token}`
}
})
const repos = await response.json()

if (!repos || repos.length === 0) throw new Error("No public repos found")
if (!repos || repos.length === 0)
throw new Error("It was not possible to retrieve the number of public repositories. Please try again.")
return repos.length
}

Expand All @@ -28,11 +29,15 @@ export const githubReputation = async (token: string) => {
authorization: `token ${token}`
}
})
if (userResponse.status !== 200) throw new Error("Not connected")
if (userResponse.status !== 200)
throw new Error("The token is not valid. Please authenticate via GitHub and try again.")
const user = await userResponse.json()

const following = Number(user.following)
const repos = await getNumberOfPublicRepos(user.login, token)

if (following < 5 || repos === 0) throw new Error("This account is not reputable enough")
if (following < 5 || repos === 0)
throw new Error(
"The user connected does not fit the anti-spam criteria. Please connect with an account that follows at least five users and has at least one public repository"
)
}
4 changes: 2 additions & 2 deletions packages/actions/test/utils/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const getLastGithubVerificationCode = async (
// Fetch messages (emails) and retrieve the id of the last one.
let response = await fetch(`https://gmail.googleapis.com/gmail/v1/users/${gmailUserEmail}/messages`, {
headers: {
Authorization: `Bearer ${token}`
authorization: `Bearer ${token}`
}
})
let body = await response.json()
Expand All @@ -61,7 +61,7 @@ export const getLastGithubVerificationCode = async (
// Read last message using id.
response = await fetch(`https://gmail.googleapis.com/gmail/v1/users/${gmailUserEmail}/messages/${lastMsgId}`, {
headers: {
Authorization: `Bearer ${token}`
authorization: `Bearer ${token}`
}
})
body = await response.json()
Expand Down

0 comments on commit 6bbe77b

Please sign in to comment.