|
| 1 | +module.exports = function(octokit, opts) { |
| 2 | + return new Promise(async (resolve, reject) => { |
| 3 | + // Up front validation |
| 4 | + try { |
| 5 | + for (let req of ["owner", "repo", "branch", "message"]) { |
| 6 | + if (!opts[req]) { |
| 7 | + return reject(`'${req}' is a required parameter`); |
| 8 | + } |
| 9 | + } |
| 10 | + |
| 11 | + if (!opts.changes || !opts.changes.length) { |
| 12 | + return reject("No changes provided"); |
| 13 | + } |
| 14 | + |
| 15 | + // Destructuring for easier access later |
| 16 | + let { |
| 17 | + owner, |
| 18 | + repo, |
| 19 | + newBranch, |
| 20 | + branch: branchName, |
| 21 | + overwriteBranch, |
| 22 | + changes, |
| 23 | + message |
| 24 | + } = opts; |
| 25 | + |
| 26 | + if (overwriteBranch && !newBranch) { |
| 27 | + return reject( |
| 28 | + "You can only overwrite a branch if you provide a 'newBranch'" |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + let branchAlreadyExists = true; |
| 33 | + let targetBranch = branchName; |
| 34 | + let baseTree; |
| 35 | + |
| 36 | + // Does the target branch already exist? |
| 37 | + if (newBranch) { |
| 38 | + baseTree = await loadRef(octokit, owner, repo, newBranch); |
| 39 | + targetBranch = newBranch; |
| 40 | + if (!baseTree) { |
| 41 | + branchAlreadyExists = false; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + // If it doesn't exist, or we want to replace the branch then |
| 46 | + // we grab the base sha from our target branch |
| 47 | + if (overwriteBranch || !baseTree) { |
| 48 | + baseTree = await loadRef(octokit, owner, repo, branchName); |
| 49 | + if (!baseTree) { |
| 50 | + return reject( |
| 51 | + `Unable to load branch. '${branchName}' does not exist` |
| 52 | + ); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Create blobs |
| 57 | + const treeItems = []; |
| 58 | + for (let change of changes) { |
| 59 | + if (!change.contents) { |
| 60 | + return reject( |
| 61 | + `No file contents provided for ${change.path || "Un-named file"}` |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + if (!change.path) { |
| 66 | + return reject( |
| 67 | + `No file path provided for the following contents: ${change.contents.substr( |
| 68 | + 0, |
| 69 | + 30 |
| 70 | + )}...` |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + let file = ( |
| 75 | + await octokit.git.createBlob({ |
| 76 | + owner, |
| 77 | + repo, |
| 78 | + content: Buffer.from(change.contents).toString("base64"), |
| 79 | + encoding: "base64" |
| 80 | + }) |
| 81 | + ).data; |
| 82 | + |
| 83 | + treeItems.push({ |
| 84 | + path: change.path, |
| 85 | + sha: file.sha, |
| 86 | + mode: "100644", |
| 87 | + type: "blob" |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + // Add those blobs to a tree |
| 92 | + let tree = ( |
| 93 | + await octokit.git.createTree({ |
| 94 | + owner, |
| 95 | + repo, |
| 96 | + tree: treeItems, |
| 97 | + base_tree: baseTree |
| 98 | + }) |
| 99 | + ).data; |
| 100 | + |
| 101 | + // Create a commit that points to that tree |
| 102 | + let commit = ( |
| 103 | + await octokit.git.createCommit({ |
| 104 | + owner, |
| 105 | + repo, |
| 106 | + message, |
| 107 | + tree: tree.sha, |
| 108 | + parents: [baseTree] |
| 109 | + }) |
| 110 | + ).data; |
| 111 | + |
| 112 | + // Create a ref that points to that tree |
| 113 | + let action = "createRef"; |
| 114 | + let updateRefBase = "refs/"; |
| 115 | + |
| 116 | + // Or if it already exists, we'll update that existing ref |
| 117 | + if (branchAlreadyExists) { |
| 118 | + action = "updateRef"; |
| 119 | + updateRefBase = ""; |
| 120 | + } |
| 121 | + |
| 122 | + const branch = ( |
| 123 | + await octokit.git[action]({ |
| 124 | + owner, |
| 125 | + repo, |
| 126 | + force: true, |
| 127 | + ref: `${updateRefBase}heads/${targetBranch}`, |
| 128 | + sha: commit.sha |
| 129 | + }) |
| 130 | + ).data; |
| 131 | + |
| 132 | + // Return the new branch name so that we can use it later |
| 133 | + // e.g. to create a pull request |
| 134 | + return resolve(targetBranch); |
| 135 | + } catch (e) { |
| 136 | + return reject(e); |
| 137 | + } |
| 138 | + }); |
| 139 | +}; |
| 140 | + |
| 141 | +async function loadRef(octokit, owner, repo, ref) { |
| 142 | + try { |
| 143 | + return ( |
| 144 | + await octokit.git.getRef({ |
| 145 | + owner, |
| 146 | + repo, |
| 147 | + ref: `heads/${ref}` |
| 148 | + }) |
| 149 | + ).data.object.sha; |
| 150 | + } catch (e) {} |
| 151 | +} |
0 commit comments