Created
July 26, 2016 18:18
-
-
Save carlows/6f4dfe8d67e01dafb110b751e51232b2 to your computer and use it in GitHub Desktop.
long method crap
This file contains hidden or 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
function createInitialCommitWithContents(repoData, ghrepoPromise, files) { | |
var defaultOptions = { | |
headers: { | |
"Authorization": format("token {token}", { token: config.GITHUB_TOKEN }) | |
}, | |
json: true | |
}; | |
// getting the SHA for the latest commit | |
var shaCommitOptions = _.extend({}, defaultOptions, { | |
method: "GET", | |
uri: format("https://api.github.com/repos/{repoName}/git/refs/heads/master", { repoData.full_name }) | |
}); | |
return request(shaCommitOptions) | |
.then(function (result) { | |
return result.object.sha; | |
}) | |
.then(function (shaLatestCommit) { | |
// getting the SHA for the latest tree | |
var shaBaseTreeOptions = _.extend({}, defaultOptions, { | |
method: "GET", | |
uri: format("https://api.github.com/repos/{repoName}/git/commits/{sha}", { repoName: repoData.full_name, sha: shaLatestCommit }) | |
}); | |
return [shaLatestCommit, request(shaBaseTreeOptions) | |
.then(function (result) { | |
return result.tree.sha; | |
})]; | |
}) | |
.spread(function (shaLatestCommit, shaBaseTree) { | |
// creating the tree | |
var createTreeOptions = _.extend({}, defaultOptions, { | |
method: "POST", | |
uri: format("https://api.github.com/repos/{repoName}/git/trees", { repoName: repoData.full_name }), | |
body: { | |
base_tree: shaBaseTree, | |
tree: files.map(function (file) { | |
return { | |
path: file.file.url, | |
mode: "100644", | |
type: "blob", | |
content: file.templatedContents | |
}; | |
}) | |
} | |
}); | |
return [shaLatestCommit, request(createTreeOptions) | |
.then(function (result) { | |
return result.sha; | |
})]; | |
}) | |
.spread(function (shaLatestCommit, shaLatestTree) { | |
// creating the commit | |
var createCommitOptions = _.extend({}, defaultOptions, { | |
method: "POST", | |
uri: format("https://api.github.com/repos/{repoName}/git/commits", { repoName: repoData.full_name }), | |
body: { | |
"message": "Initial project structure", | |
"tree": shaLatestTree, | |
"parents": [shaLatestCommit] | |
} | |
}); | |
return request(createCommitOptions) | |
.then(function (result) { | |
return result.sha; | |
}); | |
}) | |
.then(function (shaNewCommit) { | |
// updating latest reference | |
var updateRefOptions = _.extend({}, defaultOptions, { | |
method: "PATCH", | |
uri: format("https://api.github.com/repos/{repoName}/git/refs/heads/master", { repoName: repoData.full_name }), | |
body: { | |
"sha": shaNewCommit, | |
"force": true | |
} | |
}); | |
return request(updateRefOptions); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment