|
| 1 | +var path = require('path'); |
| 2 | +var url = require('url'); |
| 3 | + |
| 4 | +var GitHubApi = require('github'); |
| 5 | + |
| 6 | +var h = require('../helper'); |
| 7 | +var log = require('../log'); |
| 8 | +var Plugin = require('../plugin'); |
| 9 | + |
| 10 | +// [prerequisite] |
| 11 | +// |
| 12 | +// 1. create a new access token on your github repo setting. (TBD) |
| 13 | +// |
| 14 | +// [config] |
| 15 | +// |
| 16 | +// "PLUGINS": { |
| 17 | +// "github": { |
| 18 | +// "repo": "https://github.com/<your_name>/<your_repo>/<folder_you_like>", |
| 19 | +// "token": "<token created above>" |
| 20 | +// } |
| 21 | +// } |
| 22 | +var plugin = new Plugin(100, 'github', '2017.08.09', |
| 23 | + 'Plugin to commit accepted code to your own github repo.'); |
| 24 | + |
| 25 | +var github = new GitHubApi({ |
| 26 | + host: 'api.github.com' |
| 27 | +}); |
| 28 | + |
| 29 | +var ctx = {}; |
| 30 | + |
| 31 | +plugin.submitProblem = function(problem, cb) { |
| 32 | + var parts = url.parse(this.config.repo).pathname.split('/'); |
| 33 | + var filename = path.basename(problem.file); |
| 34 | + parts.push(filename); |
| 35 | + |
| 36 | + if (parts[0] === '') parts.shift(); |
| 37 | + ctx.owner = parts.shift(); |
| 38 | + ctx.repo = parts.shift(); |
| 39 | + ctx.path = parts.join('/'); |
| 40 | + |
| 41 | + github.authenticate({type: 'token', token: this.config.token}); |
| 42 | + plugin.next.submitProblem(problem, function(_e, results) { |
| 43 | + if (_e || !results[0].ok) return cb(_e, results); |
| 44 | + |
| 45 | + log.debug('running github.getContent: ' + filename); |
| 46 | + github.repos.getContent(ctx, function(e, res) { |
| 47 | + if (e && e.code !== 404) { |
| 48 | + log.error('[github] failed: ' + e.message); |
| 49 | + return cb(_e, results); |
| 50 | + } |
| 51 | + |
| 52 | + ctx.message = 'update ' + filename; |
| 53 | + ctx.content = new Buffer(h.getFileData(problem.file)).toString('base64'); |
| 54 | + |
| 55 | + var onFileDone = function(e, res) { |
| 56 | + if (e) { |
| 57 | + log.error('[github] failed: ' + e.message); |
| 58 | + return cb(_e, results); |
| 59 | + } |
| 60 | + |
| 61 | + log.debug(res.meta.status); |
| 62 | + log.debug('updated current file version = ' + res.data.content.sha); |
| 63 | + log.debug('updated current commit = ' + res.data.commit.sha); |
| 64 | + |
| 65 | + log.info('[github] successfully committed to ' + plugin.config.repo); |
| 66 | + return cb(_e, results); |
| 67 | + }; |
| 68 | + |
| 69 | + if (e) { |
| 70 | + log.debug('no previous file version found'); |
| 71 | + |
| 72 | + log.debug('running github.createFile'); |
| 73 | + github.repos.createFile(ctx, onFileDone); |
| 74 | + } else { |
| 75 | + ctx.sha = res.data.sha; |
| 76 | + log.debug('found previous file version = ' + ctx.sha); |
| 77 | + |
| 78 | + log.debug('running github.updateFile'); |
| 79 | + github.repos.updateFile(ctx, onFileDone); |
| 80 | + } |
| 81 | + }); |
| 82 | + }); |
| 83 | +}; |
| 84 | + |
| 85 | +module.exports = plugin; |
0 commit comments