| 129 | /* Currently only used for prepending discussion posts to README */ |
| 130 | /* callback cb is called on success if it is defined */ |
| 131 | const update = ( |
| 132 | token, |
| 133 | hook, |
| 134 | addition, |
| 135 | directory, |
| 136 | msg, |
| 137 | prepend, |
| 138 | cb = undefined, |
| 139 | ) => { |
| 140 | const URL = `https://api.github.com/repos/${hook}/contents/${directory}/README.md`; |
| 141 | |
| 142 | /* Read from existing file on GitHub */ |
| 143 | const xhr = new XMLHttpRequest(); |
| 144 | xhr.addEventListener('readystatechange', function () { |
| 145 | if (xhr.readyState === 4) { |
| 146 | if (xhr.status === 200 || xhr.status === 201) { |
| 147 | const response = JSON.parse(xhr.responseText); |
| 148 | const existingContent = decodeURIComponent( |
| 149 | escape(atob(response.content)), |
| 150 | ); |
| 151 | let newContent = ''; |
| 152 | |
| 153 | /* Discussion posts prepended at top of README */ |
| 154 | /* Future implementations may require appending to bottom of file */ |
| 155 | if (prepend) { |
| 156 | newContent = btoa( |
| 157 | unescape(encodeURIComponent(addition + existingContent)), |
| 158 | ); |
| 159 | } |
| 160 | |
| 161 | /* Write file with new content to GitHub */ |
| 162 | upload( |
| 163 | token, |
| 164 | hook, |
| 165 | newContent, |
| 166 | directory, |
| 167 | 'README.md', |
| 168 | response.sha, |
| 169 | msg, |
| 170 | cb, |
| 171 | ); |
| 172 | } |
| 173 | } |
| 174 | }); |
| 175 | xhr.open('GET', URL, true); |
| 176 | xhr.setRequestHeader('Authorization', `token ${token}`); |
| 177 | xhr.setRequestHeader('Accept', 'application/vnd.github.v3+json'); |
| 178 | xhr.send(); |
| 179 | }; |
| 180 | |
| 181 | function uploadGit( |
| 182 | code, |