Skip to content

Commit 483a731

Browse files
committed
feat: fetch user etc
1 parent 675a820 commit 483a731

File tree

5 files changed

+118
-16
lines changed

5 files changed

+118
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ A bot for automatically adding all-contributors. 🤖
1313
2. Please setup your `README.md` and `.all-contributorsrc` using the all-contributors-cli tool
1414

1515
### Adding contributions
16-
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)
16+
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)
1717
2. Bot will look for `.all-contributorsrc` if not found, comments on pr to run setup
1818
3. If user exists, add new contribution, if not add user and add contribution
1919

src/index.js

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable camelcase */
2+
13
const {
24
addContributorWithDetails,
35
generate: generateContentFile,
@@ -23,19 +25,41 @@ async function getReadmeFileContentsByPath({ repository, files }) {
2325
}
2426
}
2527

26-
async function addContributor({ options, login, contributions }) {
27-
// TODO: fetch details
28-
// https://octokit.github.io/rest.js/#api-Users-getByUsername
29-
const name = 'Jake Bolam'
30-
const avatarUrl = 'https://my-example-avatar.example.com'
31-
const profile = 'https://jakebolam.com'
28+
async function getUserDetials({ context, username }) {
29+
// TODO: optimzation, if commenting user is the user we're adding we can avoid an api call
30+
// const commentUser = context.payload.comment.user.login
31+
// if (user === commentUser) {
32+
// return {
33+
// name: context.payload.comment.user.name
34+
// avatarUrl: context.payload.comment.avatar_url
35+
// profile:
36+
// }
37+
// }
38+
39+
const result = await context.github.users.getByUsername({ username })
40+
const { avatar_url, blog, html_url, name } = result.data
41+
42+
return {
43+
name: name || username,
44+
avatar_url,
45+
profile: blog || html_url,
46+
}
47+
}
3248

49+
async function addContributor({
50+
options,
51+
login,
52+
contributions,
53+
name,
54+
avatar_url,
55+
profile,
56+
}) {
3357
const newContributorsList = await addContributorWithDetails({
3458
options,
3559
login,
3660
contributions,
3761
name,
38-
avatar_url: avatarUrl,
62+
avatar_url,
3963
profile,
4064
})
4165
return { ...options, contributors: newContributorsList }
@@ -75,41 +99,62 @@ async function processNewIssueComment(context) {
7599

76100
let optionsFileContent
77101
try {
78-
optionsFileContent = repository.getFileContents(ALL_CONTRIBUTORS_RC)
102+
const rawOptionsFileContent = await repository.getFileContents(
103+
ALL_CONTRIBUTORS_RC,
104+
)
105+
optionsFileContent = JSON.parse(rawOptionsFileContent)
106+
// TODO: if JSON has error report that
79107
} catch (error) {
80108
if (error instanceof ResourceNotFoundError) {
81109
await createComment({
82110
context,
83-
body: `@${fromUser} Please setup ${repository.getFullname()} for all-contributors using the [all-contributors-cli](https://github.com/all-contributors/all-contributors-cli) tool.`,
111+
body: `@${fromUser} This project is not yet setup for [all-contributors](https://github.com/all-contributors/all-contributors).\n
112+
You will need to first setup [${
113+
repository.repo
114+
}](https://github.com/${repository.getFullname()}) using the [all-contributors-cli](https://github.com/all-contributors/all-contributors-cli) tool.`,
84115
})
85116
context.log(error)
86117
return
87118
}
88119
}
120+
context.log('Options Content')
121+
context.log(optionsFileContent)
89122

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

129+
const { name, avatar_url, profile } = await getUserDetials({
130+
context,
131+
username: who,
132+
})
133+
96134
const newOptionsContent = await addContributor({
97135
options: optionsFileContent,
98-
login,
136+
login: who,
99137
contributions,
138+
name,
139+
avatar_url,
140+
profile,
100141
})
142+
context.log('New Options Content')
101143
context.log(newOptionsContent)
102144

103145
const readmeFileContentsByPath = await getReadmeFileContentsByPath({
104146
repository,
105147
files: optionsFileContent.files,
106148
})
149+
150+
context.log('Readme file contents by path')
107151
context.log(readmeFileContentsByPath)
108152

109153
const newReadmeFileContentsByPath = await generateContentFiles({
110154
options: newOptionsContent,
111155
readmeFileContentsByPath,
112156
})
157+
context.log('New readme file contents by path')
113158
context.log(newReadmeFileContentsByPath)
114159

115160
// TODO: Create branch, update files
@@ -127,7 +172,6 @@ module.exports = app => {
127172
// issueComment.edited
128173
// Issue comments and PR comments both create issue_comment events
129174
app.on('issue_comment.created', async context => {
130-
app.log(context)
131175
try {
132176
await processNewIssueComment(context)
133177
} catch (error) {

src/repository/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,13 @@ class Repository {
2727
})
2828

2929
// Returns empty if file not found
30-
if (file.data.content) {
30+
if (!file.data || !file.data.content) {
3131
throw new ResourceNotFoundError(filePath, this.full_name)
3232
}
3333

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

@@ -52,8 +51,12 @@ class Repository {
5251
})
5352

5453
const getFilesMultipleList = await Promise.all(getFilesMultiple)
55-
5654
const multipleFileContentsByPath = {}
55+
getFilesMultipleList.forEach(({ filePath, content }) => {
56+
multipleFileContentsByPath[filePath] = content
57+
})
58+
59+
return
5760
}
5861
}
5962

test/fixtures/repos.getContents.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"data": {
3+
"name": ".all-contributorsrc",
4+
"path": ".all-contributorsrc",
5+
"sha": "dff34f715bca51114c0336a49381456a926806d5",
6+
"size": 507,
7+
"url": "https://api.github.com/repos/all-contributors/all-contributors-bot/contents/.all-contributorsrc?ref=master",
8+
"html_url": "https://github.com/all-contributors/all-contributors-bot/blob/master/.all-contributorsrc",
9+
"git_url": "https://api.github.com/repos/all-contributors/all-contributors-bot/git/blobs/dff34f715bca51114c0336a49381456a926806d5",
10+
"download_url": "https://raw.githubusercontent.com/all-contributors/all-contributors-bot/master/.all-contributorsrc",
11+
"type": "file",
12+
"content": "ewogICJwcm9qZWN0TmFtZSI6ICJib3QiLAogICJwcm9qZWN0T3duZXIiOiAi\nYWxsLWNvbnRyaWJ0dW9ycyIsCiAgInJlcG9UeXBlIjogImdpdGh1YiIsCiAg\nInJlcG9Ib3N0IjogImh0dHBzOi8vZ2l0aHViLmNvbSIsCiAgImZpbGVzIjog\nWwogICAgIlJFQURNRS5tZCIKICBdLAogICJpbWFnZVNpemUiOiAxMDAsCiAg\nImNvbW1pdCI6IGZhbHNlLAogICJjb250cmlidXRvcnMiOiBbCiAgICB7CiAg\nICAgICJsb2dpbiI6ICJqYWtlYm9sYW0iLAogICAgICAibmFtZSI6ICJKYWtl\nIEJvbGFtIiwKICAgICAgImF2YXRhcl91cmwiOiAiaHR0cHM6Ly9hdmF0YXJz\nMi5naXRodWJ1c2VyY29udGVudC5jb20vdS8zNTM0MjM2P3Y9NCIsCiAgICAg\nICJwcm9maWxlIjogImh0dHBzOi8vamFrZWJvbGFtLmNvbSIsCiAgICAgICJj\nb250cmlidXRpb25zIjogWwogICAgICAgICJjb2RlIiwKICAgICAgICAiaWRl\nYXMiLAogICAgICAgICJpbmZyYSIsCiAgICAgICAgInRlc3QiCiAgICAgIF0K\nICAgIH0KICBdCn0K\n",
13+
"encoding": "base64",
14+
"_links": {
15+
"self": "https://api.github.com/repos/all-contributors/all-contributors-bot/contents/.all-contributorsrc?ref=master",
16+
"git": "https://api.github.com/repos/all-contributors/all-contributors-bot/git/blobs/dff34f715bca51114c0336a49381456a926806d5",
17+
"html": "https://github.com/all-contributors/all-contributors-bot/blob/master/.all-contributorsrc"
18+
}
19+
}
20+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"data": {
3+
"login": "jakebolam",
4+
"id": 3534236,
5+
"node_id": "MDQ6VXNlcjM1MzQyMzY=",
6+
"avatar_url": "https://avatars2.githubusercontent.com/u/3534236?v=4",
7+
"gravatar_id": "",
8+
"url": "https://api.github.com/users/jakebolam",
9+
"html_url": "https://github.com/jakebolam",
10+
"followers_url": "https://api.github.com/users/jakebolam/followers",
11+
"following_url": "https://api.github.com/users/jakebolam/following{/other_user}",
12+
"gists_url": "https://api.github.com/users/jakebolam/gists{/gist_id}",
13+
"starred_url": "https://api.github.com/users/jakebolam/starred{/owner}{/repo}",
14+
"subscriptions_url": "https://api.github.com/users/jakebolam/subscriptions",
15+
"organizations_url": "https://api.github.com/users/jakebolam/orgs",
16+
"repos_url": "https://api.github.com/users/jakebolam/repos",
17+
"events_url": "https://api.github.com/users/jakebolam/events{/privacy}",
18+
"received_events_url": "https://api.github.com/users/jakebolam/received_events",
19+
"type": "User",
20+
"site_admin": false,
21+
"name": "Jake Bolam",
22+
"company": "@tophat @lighthouse-labs @bundlewatch",
23+
"blog": "https://jakebolam.com",
24+
"location": "Toronto, Ontario",
25+
"email": null,
26+
"hireable": null,
27+
"bio": "Software Infrastructure @TopHat\r\n@all-contributors Maintainer\r\nOpen Source 🐢",
28+
"public_repos": 14,
29+
"public_gists": 0,
30+
"followers": 13,
31+
"following": 2,
32+
"created_at": "2013-02-11T17:05:21Z",
33+
"updated_at": "2019-01-08T07:04:42Z"
34+
}
35+
}

0 commit comments

Comments
 (0)