Skip to content

Commit

Permalink
feat: fetch user etc
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebolam committed Jan 9, 2019
1 parent 675a820 commit 483a731
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ A bot for automatically adding all-contributors. 🤖
2. Please setup your `README.md` and `.all-contributorsrc` using the all-contributors-cli tool

### Adding contributions
1. Comment on Issue/PR etc with text: @AllContributorBot please add jakebolam for infrastructure, testing and code (Can also use the short terms, full key coming soon)
1. Comment on Issue/PR etc with text: `@AllContributorBot please add jakebolam for infrastructure, testing and code` (Can also use the short terms, full key coming soon)
2. Bot will look for `.all-contributorsrc` if not found, comments on pr to run setup
3. If user exists, add new contribution, if not add user and add contribution

Expand Down
68 changes: 56 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable camelcase */

const {
addContributorWithDetails,
generate: generateContentFile,
Expand All @@ -23,19 +25,41 @@ async function getReadmeFileContentsByPath({ repository, files }) {
}
}

async function addContributor({ options, login, contributions }) {
// TODO: fetch details
// https://octokit.github.io/rest.js/#api-Users-getByUsername
const name = 'Jake Bolam'
const avatarUrl = 'https://my-example-avatar.example.com'
const profile = 'https://jakebolam.com'
async function getUserDetials({ context, username }) {
// TODO: optimzation, if commenting user is the user we're adding we can avoid an api call
// const commentUser = context.payload.comment.user.login
// if (user === commentUser) {
// return {
// name: context.payload.comment.user.name
// avatarUrl: context.payload.comment.avatar_url
// profile:
// }
// }

const result = await context.github.users.getByUsername({ username })
const { avatar_url, blog, html_url, name } = result.data

return {
name: name || username,
avatar_url,
profile: blog || html_url,
}
}

async function addContributor({
options,
login,
contributions,
name,
avatar_url,
profile,
}) {
const newContributorsList = await addContributorWithDetails({
options,
login,
contributions,
name,
avatar_url: avatarUrl,
avatar_url,
profile,
})
return { ...options, contributors: newContributorsList }
Expand Down Expand Up @@ -75,41 +99,62 @@ async function processNewIssueComment(context) {

let optionsFileContent
try {
optionsFileContent = repository.getFileContents(ALL_CONTRIBUTORS_RC)
const rawOptionsFileContent = await repository.getFileContents(
ALL_CONTRIBUTORS_RC,
)
optionsFileContent = JSON.parse(rawOptionsFileContent)
// TODO: if JSON has error report that
} catch (error) {
if (error instanceof ResourceNotFoundError) {
await createComment({
context,
body: `@${fromUser} Please setup ${repository.getFullname()} for all-contributors using the [all-contributors-cli](https:/all-contributors/all-contributors-cli) tool.`,
body: `@${fromUser} This project is not yet setup for [all-contributors](https:/all-contributors/all-contributors).\n
You will need to first setup [${
repository.repo
}](https:/${repository.getFullname()}) using the [all-contributors-cli](https:/all-contributors/all-contributors-cli) tool.`,
})
context.log(error)
return
}
}
context.log('Options Content')
context.log(optionsFileContent)

// TODO parse comment and gain intentions
// const { who, contributions } = parseComment(commentBody)
// We had trouble reading your comment. Basic usage:\n\n\`@${GIHUB_BOT_NAME} please add jakebolam for code\`
const login = 'jakebolam'
const who = 'jakebolam'
const contributions = ['code']

const { name, avatar_url, profile } = await getUserDetials({
context,
username: who,
})

const newOptionsContent = await addContributor({
options: optionsFileContent,
login,
login: who,
contributions,
name,
avatar_url,
profile,
})
context.log('New Options Content')
context.log(newOptionsContent)

const readmeFileContentsByPath = await getReadmeFileContentsByPath({
repository,
files: optionsFileContent.files,
})

context.log('Readme file contents by path')
context.log(readmeFileContentsByPath)

const newReadmeFileContentsByPath = await generateContentFiles({
options: newOptionsContent,
readmeFileContentsByPath,
})
context.log('New readme file contents by path')
context.log(newReadmeFileContentsByPath)

// TODO: Create branch, update files
Expand All @@ -127,7 +172,6 @@ module.exports = app => {
// issueComment.edited
// Issue comments and PR comments both create issue_comment events
app.on('issue_comment.created', async context => {
app.log(context)
try {
await processNewIssueComment(context)
} catch (error) {
Expand Down
9 changes: 6 additions & 3 deletions src/repository/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ class Repository {
})

// Returns empty if file not found
if (file.data.content) {
if (!file.data || !file.data.content) {
throw new ResourceNotFoundError(filePath, this.full_name)
}

// Contents can be an array if its a directory, should be an edge case, and we can just crash
const contentBinary = file.data.content
const content = Buffer.from(contentBinary, 'base64').toString()
this.context.log(content)
return content
}

Expand All @@ -52,8 +51,12 @@ class Repository {
})

const getFilesMultipleList = await Promise.all(getFilesMultiple)

const multipleFileContentsByPath = {}
getFilesMultipleList.forEach(({ filePath, content }) => {
multipleFileContentsByPath[filePath] = content
})

return
}
}

Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/repos.getContents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"data": {
"name": ".all-contributorsrc",
"path": ".all-contributorsrc",
"sha": "dff34f715bca51114c0336a49381456a926806d5",
"size": 507,
"url": "https://api.github.com/repos/all-contributors/all-contributors-bot/contents/.all-contributorsrc?ref=master",
"html_url": "https:/all-contributors/all-contributors-bot/blob/master/.all-contributorsrc",
"git_url": "https://api.github.com/repos/all-contributors/all-contributors-bot/git/blobs/dff34f715bca51114c0336a49381456a926806d5",
"download_url": "https://raw.githubusercontent.com/all-contributors/all-contributors-bot/master/.all-contributorsrc",
"type": "file",
"content": "ewogICJwcm9qZWN0TmFtZSI6ICJib3QiLAogICJwcm9qZWN0T3duZXIiOiAi\nYWxsLWNvbnRyaWJ0dW9ycyIsCiAgInJlcG9UeXBlIjogImdpdGh1YiIsCiAg\nInJlcG9Ib3N0IjogImh0dHBzOi8vZ2l0aHViLmNvbSIsCiAgImZpbGVzIjog\nWwogICAgIlJFQURNRS5tZCIKICBdLAogICJpbWFnZVNpemUiOiAxMDAsCiAg\nImNvbW1pdCI6IGZhbHNlLAogICJjb250cmlidXRvcnMiOiBbCiAgICB7CiAg\nICAgICJsb2dpbiI6ICJqYWtlYm9sYW0iLAogICAgICAibmFtZSI6ICJKYWtl\nIEJvbGFtIiwKICAgICAgImF2YXRhcl91cmwiOiAiaHR0cHM6Ly9hdmF0YXJz\nMi5naXRodWJ1c2VyY29udGVudC5jb20vdS8zNTM0MjM2P3Y9NCIsCiAgICAg\nICJwcm9maWxlIjogImh0dHBzOi8vamFrZWJvbGFtLmNvbSIsCiAgICAgICJj\nb250cmlidXRpb25zIjogWwogICAgICAgICJjb2RlIiwKICAgICAgICAiaWRl\nYXMiLAogICAgICAgICJpbmZyYSIsCiAgICAgICAgInRlc3QiCiAgICAgIF0K\nICAgIH0KICBdCn0K\n",
"encoding": "base64",
"_links": {
"self": "https://api.github.com/repos/all-contributors/all-contributors-bot/contents/.all-contributorsrc?ref=master",
"git": "https://api.github.com/repos/all-contributors/all-contributors-bot/git/blobs/dff34f715bca51114c0336a49381456a926806d5",
"html": "https:/all-contributors/all-contributors-bot/blob/master/.all-contributorsrc"
}
}
}
35 changes: 35 additions & 0 deletions test/fixtures/users.getByUsername.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"data": {
"login": "jakebolam",
"id": 3534236,
"node_id": "MDQ6VXNlcjM1MzQyMzY=",
"avatar_url": "https://avatars2.githubusercontent.com/u/3534236?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/jakebolam",
"html_url": "https:/jakebolam",
"followers_url": "https://api.github.com/users/jakebolam/followers",
"following_url": "https://api.github.com/users/jakebolam/following{/other_user}",
"gists_url": "https://api.github.com/users/jakebolam/gists{/gist_id}",
"starred_url": "https://api.github.com/users/jakebolam/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/jakebolam/subscriptions",
"organizations_url": "https://api.github.com/users/jakebolam/orgs",
"repos_url": "https://api.github.com/users/jakebolam/repos",
"events_url": "https://api.github.com/users/jakebolam/events{/privacy}",
"received_events_url": "https://api.github.com/users/jakebolam/received_events",
"type": "User",
"site_admin": false,
"name": "Jake Bolam",
"company": "@tophat @lighthouse-labs @bundlewatch",
"blog": "https://jakebolam.com",
"location": "Toronto, Ontario",
"email": null,
"hireable": null,
"bio": "Software Infrastructure @TopHat\r\n@all-contributors Maintainer\r\nOpen Source 🐢",
"public_repos": 14,
"public_gists": 0,
"followers": 13,
"following": 2,
"created_at": "2013-02-11T17:05:21Z",
"updated_at": "2019-01-08T07:04:42Z"
}
}

0 comments on commit 483a731

Please sign in to comment.