@@ -13,6 +13,14 @@ module.exports = function(octokit, opts) {
13
13
return reject ( "No changes provided" ) ;
14
14
}
15
15
16
+ if ( ! opts . batchSize ) {
17
+ opts . batchSize = 1 ;
18
+ }
19
+
20
+ if ( typeof opts . batchSize !== "number" ) {
21
+ return reject ( `batchSize must be a number` ) ;
22
+ }
23
+
16
24
// Destructuring for easier access later
17
25
let {
18
26
owner,
@@ -22,7 +30,8 @@ module.exports = function(octokit, opts) {
22
30
createBranch,
23
31
committer,
24
32
author,
25
- changes
33
+ changes,
34
+ batchSize
26
35
} = opts ;
27
36
28
37
let branchAlreadyExists = true ;
@@ -81,60 +90,68 @@ module.exports = function(octokit, opts) {
81
90
const treeItems = [ ] ;
82
91
// Handle file deletions
83
92
if ( hasFilesToDelete ) {
84
- for ( const fileName of change . filesToDelete ) {
85
- const exists = await fileExistsInRepo (
86
- octokit ,
87
- owner ,
88
- repo ,
89
- fileName ,
90
- baseTree
93
+ for ( const batch of chunk ( change . filesToDelete , batchSize ) ) {
94
+ await Promise . all (
95
+ batch . map ( async fileName => {
96
+ const exists = await fileExistsInRepo (
97
+ octokit ,
98
+ owner ,
99
+ repo ,
100
+ fileName ,
101
+ baseTree
102
+ ) ;
103
+
104
+ // If it doesn't exist, and we're not ignoring missing files
105
+ // reject the promise
106
+ if ( ! exists && ! change . ignoreDeletionFailures ) {
107
+ return reject (
108
+ `The file ${ fileName } could not be found in the repo`
109
+ ) ;
110
+ }
111
+
112
+ // At this point it either exists, or we're ignoring failures
113
+ if ( exists ) {
114
+ treeItems . push ( {
115
+ path : fileName ,
116
+ sha : null , // sha as null implies that the file should be deleted
117
+ mode : "100644" ,
118
+ type : "commit"
119
+ } ) ;
120
+ }
121
+ } )
91
122
) ;
123
+ }
124
+ }
92
125
93
- // If it doesn't exist, and we're not ignoring missing files
94
- // reject the promise
95
- if ( ! exists && ! change . ignoreDeletionFailures ) {
96
- return reject (
97
- `The file ${ fileName } could not be found in the repo`
126
+ for ( const batch of chunk ( Object . keys ( change . files ) , batchSize ) ) {
127
+ await Promise . all (
128
+ batch . map ( async fileName => {
129
+ const properties = change . files [ fileName ] || "" ;
130
+
131
+ const contents = properties . contents || properties ;
132
+ const mode = properties . mode || "100644" ;
133
+ const type = properties . type || "blob" ;
134
+
135
+ if ( ! contents ) {
136
+ return reject ( `No file contents provided for ${ fileName } ` ) ;
137
+ }
138
+
139
+ const fileSha = await createBlob (
140
+ octokit ,
141
+ owner ,
142
+ repo ,
143
+ contents ,
144
+ type
98
145
) ;
99
- }
100
146
101
- // At this point it either exists, or we're ignoring failures
102
- if ( exists ) {
103
147
treeItems . push ( {
104
148
path : fileName ,
105
- sha : null , // sha as null implies that the file should be deleted
106
- mode : "100644" ,
107
- type : "commit"
149
+ sha : fileSha ,
150
+ mode : mode ,
151
+ type : type
108
152
} ) ;
109
- }
110
- }
111
- }
112
-
113
- for ( const fileName in change . files ) {
114
- const properties = change . files [ fileName ] || "" ;
115
-
116
- const contents = properties . contents || properties ;
117
- const mode = properties . mode || "100644" ;
118
- const type = properties . type || "blob" ;
119
-
120
- if ( ! contents ) {
121
- return reject ( `No file contents provided for ${ fileName } ` ) ;
122
- }
123
-
124
- const fileSha = await createBlob (
125
- octokit ,
126
- owner ,
127
- repo ,
128
- contents ,
129
- type
153
+ } )
130
154
) ;
131
-
132
- treeItems . push ( {
133
- path : fileName ,
134
- sha : fileSha ,
135
- mode : mode ,
136
- type : type
137
- } ) ;
138
155
}
139
156
140
157
// no need to issue further requests if there are no updates, creations and deletions
@@ -278,3 +295,11 @@ async function loadRef(octokit, owner, repo, ref) {
278
295
// console.log(e);
279
296
}
280
297
}
298
+
299
+ const chunk = ( input , size ) => {
300
+ return input . reduce ( ( arr , item , idx ) => {
301
+ return idx % size === 0
302
+ ? [ ...arr , [ item ] ]
303
+ : [ ...arr . slice ( 0 , - 1 ) , [ ...arr . slice ( - 1 ) [ 0 ] , item ] ] ;
304
+ } , [ ] ) ;
305
+ } ;
0 commit comments