Skip to content

Commit d8a2bef

Browse files
committed
feat: update RC file and content files
1 parent 4612f1e commit d8a2bef

File tree

3 files changed

+67
-25
lines changed

3 files changed

+67
-25
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
},
2525
"dependencies": {
2626
"@probot/serverless-lambda": "^0.2.0",
27-
"all-contributors-cli": "^5.6.1",
27+
"all-contributors-cli": "^5.7.0",
2828
"probot": "^7.2.0"
2929
},
3030
"devDependencies": {

src/index.js

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,60 @@
1-
const GIHUB_BOT_NAME = '@AllContributorsBot'
2-
const ALL_CONTRIBUTORS_RC = '.all-contributorsrc'
1+
const {
2+
addContributorWithDetails,
3+
generate: generateContentFile,
4+
} = require('all-contributors-cli')
35

4-
const { Repository, ResourceNotFoundError} = require('./repository')
6+
const { Repository, ResourceNotFoundError } = require('./repository')
57
// const parseComment = require('./parse-comment')
68

9+
const GIHUB_BOT_NAME = '@AllContributorsBot'
10+
const ALL_CONTRIBUTORS_RC = '.all-contributorsrc'
11+
712
async function createComment({ context, body }) {
813
const issueComment = context.issue({ body })
914
return context.github.issues.createComment(issueComment)
1015
}
1116

12-
async function getReadmeFileContentsByPath({ repository, rcFileContent }) {
13-
if (Array.isArray(rcFileContent.files)) {
14-
return repository.getMultipleFileContents(rcFileContent.files)
17+
async function getReadmeFileContentsByPath({ repository, files }) {
18+
if (Array.isArray(files)) {
19+
return repository.getMultipleFileContents(files)
1520
} else {
1621
// default 'files' is ['README.md']
1722
return repository.getMultipleFileContents(['README.md'])
1823
}
1924
}
2025

26+
async function addContributor({ options, login, contributions }) {
27+
// TODO: fetch details
28+
const name = 'Jake Bolam'
29+
const avatarUrl = 'https://my-example-avatar.example.com'
30+
const profile = 'https://jakebolam.com'
31+
32+
const newContributorsList = await addContributorWithDetails({
33+
options,
34+
login,
35+
contributions,
36+
name,
37+
avatar_url: avatarUrl,
38+
profile,
39+
})
40+
return { ...options, contributors: newContributorsList }
41+
}
42+
43+
async function generateContentFiles({ options, readmeFileContentsByPath }) {
44+
const newReadmeFileContentsByPath = {}
45+
Object.entires(readmeFileContentsByPath).forEach(
46+
([filePath, fileContents]) => {
47+
const newFileContents = generateContentFile(
48+
options,
49+
options.contributors,
50+
fileContents,
51+
)
52+
newReadmeFileContentsByPath[filePath] = newFileContents
53+
},
54+
)
55+
return newReadmeFileContentsByPath
56+
}
57+
2158
async function processNewIssueComment(context) {
2259
if (context.isBot) {
2360
context.log('From a bot, exiting')
@@ -35,9 +72,9 @@ async function processNewIssueComment(context) {
3572

3673
const repository = new Repository(context)
3774

38-
let rcFileContent
75+
let optionsFileContent
3976
try {
40-
rcFileContent = repository.getFileContents(ALL_CONTRIBUTORS_RC)
77+
optionsFileContent = repository.getFileContents(ALL_CONTRIBUTORS_RC)
4178
} catch (error) {
4279
if (error instanceof ResourceNotFoundError) {
4380
await createComment({
@@ -52,23 +89,27 @@ async function processNewIssueComment(context) {
5289
// TODO parse comment and gain intentions
5390
// const { who, contributions } = parseComment(commentBody)
5491
// We had trouble reading your comment. Basic usage:\n\n\`@${GIHUB_BOT_NAME} please add jakebolam for code\`
55-
const who = 'jakebolam'
92+
const login = 'jakebolam'
5693
const contributions = ['code']
5794

58-
const readmeFileContentsByPath = getReadmeFileContentsByPath({
95+
const newOptionsContent = await addContributor({
96+
options: optionsFileContent,
97+
login,
98+
contributions,
99+
})
100+
context.log(newOptionsContent)
101+
102+
const readmeFileContentsByPath = await getReadmeFileContentsByPath({
59103
repository,
60-
rcFileContent,
104+
files: optionsFileContent.files,
61105
})
106+
context.log(readmeFileContentsByPath)
62107

63-
// // TODO: add PR to allContributorsCLI for node api? (Or refactor out?)
64-
// const {newRcFileContent, newReadmeFileContentsList} = allContributors.addContributor({
65-
// rcFileContent,
66-
// readmeFileContentsByPath,
67-
// username: who,
68-
// contributors: contributions,
69-
// contextToAvoidApiCalls
70-
//
71-
// })
108+
const newReadmeFileContentsByPath = await generateContentFiles({
109+
options: newOptionsContent,
110+
readmeFileContentsByPath,
111+
})
112+
context.log(newReadmeFileContentsByPath)
72113

73114
// TODO: Create branch, update files
74115
// GET master state when we read files
@@ -83,8 +124,9 @@ async function processNewIssueComment(context) {
83124

84125
module.exports = app => {
85126
// issueComment.edited
127+
// Issue comments and PR comments both create issue_comment events
86128
app.on('issue_comment.created', async context => {
87-
app.debug(context)
129+
app.log(context)
88130
try {
89131
await processNewIssueComment(context)
90132
} catch (error) {

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,9 @@ align-text@^0.1.1, align-text@^0.1.3:
236236
longest "^1.0.1"
237237
repeat-string "^1.5.2"
238238

239-
all-contributors-cli@^5.6.1:
240-
version "5.6.1"
241-
resolved "https://registry.yarnpkg.com/all-contributors-cli/-/all-contributors-cli-5.6.1.tgz#18de0c355fe393da19dd2d265f0d2adf2d0bc04b"
239+
all-contributors-cli@^5.7.0:
240+
version "5.7.0"
241+
resolved "https://registry.yarnpkg.com/all-contributors-cli/-/all-contributors-cli-5.7.0.tgz#84d4495845ba5a8194fbbd1394b841dd10546169"
242242
dependencies:
243243
async "^2.0.0-rc.1"
244244
chalk "^2.3.0"

0 commit comments

Comments
 (0)