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' )
3
5
4
- const { Repository, ResourceNotFoundError} = require ( './repository' )
6
+ const { Repository, ResourceNotFoundError } = require ( './repository' )
5
7
// const parseComment = require('./parse-comment')
6
8
9
+ const GIHUB_BOT_NAME = '@AllContributorsBot'
10
+ const ALL_CONTRIBUTORS_RC = '.all-contributorsrc'
11
+
7
12
async function createComment ( { context, body } ) {
8
13
const issueComment = context . issue ( { body } )
9
14
return context . github . issues . createComment ( issueComment )
10
15
}
11
16
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 )
15
20
} else {
16
21
// default 'files' is ['README.md']
17
22
return repository . getMultipleFileContents ( [ 'README.md' ] )
18
23
}
19
24
}
20
25
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
+
21
58
async function processNewIssueComment ( context ) {
22
59
if ( context . isBot ) {
23
60
context . log ( 'From a bot, exiting' )
@@ -35,9 +72,9 @@ async function processNewIssueComment(context) {
35
72
36
73
const repository = new Repository ( context )
37
74
38
- let rcFileContent
75
+ let optionsFileContent
39
76
try {
40
- rcFileContent = repository . getFileContents ( ALL_CONTRIBUTORS_RC )
77
+ optionsFileContent = repository . getFileContents ( ALL_CONTRIBUTORS_RC )
41
78
} catch ( error ) {
42
79
if ( error instanceof ResourceNotFoundError ) {
43
80
await createComment ( {
@@ -52,23 +89,27 @@ async function processNewIssueComment(context) {
52
89
// TODO parse comment and gain intentions
53
90
// const { who, contributions } = parseComment(commentBody)
54
91
// 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'
56
93
const contributions = [ 'code' ]
57
94
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 ( {
59
103
repository,
60
- rcFileContent ,
104
+ files : optionsFileContent . files ,
61
105
} )
106
+ context . log ( readmeFileContentsByPath )
62
107
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 )
72
113
73
114
// TODO: Create branch, update files
74
115
// GET master state when we read files
@@ -83,8 +124,9 @@ async function processNewIssueComment(context) {
83
124
84
125
module . exports = app => {
85
126
// issueComment.edited
127
+ // Issue comments and PR comments both create issue_comment events
86
128
app . on ( 'issue_comment.created' , async context => {
87
- app . debug ( context )
129
+ app . log ( context )
88
130
try {
89
131
await processNewIssueComment ( context )
90
132
} catch ( error ) {
0 commit comments