Created
June 29, 2018 06:46
-
-
Save wangzuo/282b2cfe7143bbadd4a0f81e789caa51 to your computer and use it in GitHub Desktop.
Github bot pr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GitHub.prototype.writeFile = function (filePath, data, branch, commitTitle) { | |
branch = branch || this.options.branch | |
commitTitle = commitTitle || 'Add Staticman file' | |
return this.api.repos.createFile({ | |
user: this.options.username, | |
repo: this.options.repository, | |
path: filePath, | |
content: Buffer.from(data).toString('base64'), | |
message: commitTitle, | |
branch: branch | |
}).catch(err => { | |
try { | |
const message = err && err.message | |
if (message) { | |
const parsedError = JSON.parse(message) | |
if ( | |
parsedError && | |
parsedError.message && | |
parsedError.message.includes('"sha" wasn\'t supplied') | |
) { | |
return Promise.reject(errorHandler('GITHUB_FILE_ALREADY_EXISTS', {err})) | |
} | |
} | |
} catch (err) {} // eslint-disable-line no-empty | |
return Promise.reject(errorHandler('GITHUB_WRITING_FILE', {err})) | |
}) | |
} | |
GitHub.prototype.writeFileAndSendPR = function (filePath, data, branch, commitTitle, commitBody) { | |
commitTitle = commitTitle || 'Add Staticman file' | |
commitBody = commitBody || '' | |
return this.api.repos.getBranch({ | |
user: this.options.username, | |
repo: this.options.repository, | |
branch: this.options.branch | |
}).then(res => { | |
return this.api.gitdata.createReference({ | |
user: this.options.username, | |
repo: this.options.repository, | |
ref: 'refs/heads/' + branch, | |
sha: res.commit.sha | |
}) | |
}).then(res => { | |
return this.writeFile(filePath, data, branch, commitTitle) | |
}).then(res => { | |
return this.api.pullRequests.create({ | |
user: this.options.username, | |
repo: this.options.repository, | |
title: commitTitle, | |
head: branch, | |
base: this.options.branch, | |
body: commitBody | |
}) | |
}).catch(err => { | |
return Promise.reject(errorHandler('GITHUB_CREATING_PR', {err})) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment