|
| 1 | +import type { Octokit } from "@octokit/core"; |
| 2 | +import type { Endpoints } from "@octokit/types"; |
| 3 | + |
| 4 | +type TreeParameter = Endpoints["POST /repos/:owner/:repo/git/trees"]["parameters"]["tree"]; |
| 5 | + |
| 6 | +type Options = { |
| 7 | + owner: string; |
| 8 | + repo: string; |
| 9 | + title: string; |
| 10 | + body: string; |
| 11 | + head: string; |
| 12 | + base?: string; |
| 13 | + changes: Changes; |
| 14 | +}; |
| 15 | + |
| 16 | +type Changes = { |
| 17 | + files: { |
| 18 | + [path: string]: string | File; |
| 19 | + }; |
| 20 | + commit: string; |
| 21 | +}; |
| 22 | + |
| 23 | +// https://developer.github.com/v3/git/blobs/#parameters |
| 24 | +type File = { |
| 25 | + content: string; |
| 26 | + encoding: "utf-8" | "base64"; |
| 27 | +}; |
| 28 | + |
| 29 | +export async function octokitCreatePullRequest( |
| 30 | + octokit: Octokit, |
| 31 | + { owner, repo, title, body, base, head, changes }: Options |
| 32 | +) { |
| 33 | + // https://developer.github.com/v3/repos/#get-a-repository |
| 34 | + const { data: repository } = await octokit.request( |
| 35 | + "GET /repos/:owner/:repo", |
| 36 | + { |
| 37 | + owner, |
| 38 | + repo, |
| 39 | + } |
| 40 | + ); |
| 41 | + |
| 42 | + if (!repository.permissions) { |
| 43 | + throw new Error( |
| 44 | + "[octokit-plugin-create-pull-request] Missing authentication" |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + if (!base) { |
| 49 | + base = repository.default_branch; |
| 50 | + } |
| 51 | + |
| 52 | + let fork = owner; |
| 53 | + |
| 54 | + if (!repository.permissions.push) { |
| 55 | + // https://developer.github.com/v3/users/#get-the-authenticated-user |
| 56 | + const user = await octokit.request("GET /user"); |
| 57 | + |
| 58 | + // https://developer.github.com/v3/repos/forks/#list-forks |
| 59 | + const forks = await octokit.request("GET /repos/:owner/:repo/forks", { |
| 60 | + owner, |
| 61 | + repo, |
| 62 | + }); |
| 63 | + const hasFork = forks.data.find( |
| 64 | + (fork) => fork.owner.login === user.data.login |
| 65 | + ); |
| 66 | + |
| 67 | + if (!hasFork) { |
| 68 | + // https://developer.github.com/v3/repos/forks/#create-a-fork |
| 69 | + await octokit.request("POST /repos/:owner/:repo/forks", { |
| 70 | + owner, |
| 71 | + repo, |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + fork = user.data.login; |
| 76 | + } |
| 77 | + |
| 78 | + // https://developer.github.com/v3/repos/commits/#list-commits-on-a-repository |
| 79 | + const { |
| 80 | + data: [firstCommit], |
| 81 | + } = await octokit.request("GET /repos/:owner/:repo/commits", { |
| 82 | + owner, |
| 83 | + repo, |
| 84 | + sha: base, |
| 85 | + per_page: 1, |
| 86 | + }); |
| 87 | + const treeSha = firstCommit.commit.tree.sha; |
| 88 | + |
| 89 | + const tree = ( |
| 90 | + await Promise.all( |
| 91 | + Object.keys(changes.files).map(async (path) => { |
| 92 | + const value = changes.files[path]; |
| 93 | + |
| 94 | + if (value === null) { |
| 95 | + // Deleting a non-existent file from a tree leads to an "GitRPC::BadObjectState" error, |
| 96 | + // so we only attempt to delete the file if it exists. |
| 97 | + try { |
| 98 | + // https://developer.github.com/v3/repos/contents/#get-contents |
| 99 | + await octokit.request("HEAD /repos/:owner/:repo/contents/:path", { |
| 100 | + owner: fork, |
| 101 | + repo, |
| 102 | + ref: firstCommit.sha, |
| 103 | + path, |
| 104 | + }); |
| 105 | + |
| 106 | + return { |
| 107 | + path, |
| 108 | + mode: "100644", |
| 109 | + sha: null, |
| 110 | + }; |
| 111 | + } catch (error) { |
| 112 | + return; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // Text files can be changed through the .content key |
| 117 | + if (typeof value === "string") { |
| 118 | + return { |
| 119 | + path, |
| 120 | + mode: "100644", |
| 121 | + content: value, |
| 122 | + }; |
| 123 | + } |
| 124 | + |
| 125 | + // Binary files need to be created first using the git blob API, |
| 126 | + // then changed by referencing in the .sha key |
| 127 | + const { data } = await octokit.request( |
| 128 | + "POST /repos/:owner/:repo/git/blobs", |
| 129 | + { |
| 130 | + owner, |
| 131 | + repo, |
| 132 | + ...value, |
| 133 | + } |
| 134 | + ); |
| 135 | + const blobSha = data.sha; |
| 136 | + return { |
| 137 | + path, |
| 138 | + mode: "100644", |
| 139 | + sha: blobSha, |
| 140 | + }; |
| 141 | + }) |
| 142 | + ) |
| 143 | + ).filter(Boolean) as TreeParameter; |
| 144 | + |
| 145 | + // https://developer.github.com/v3/git/trees/#create-a-tree |
| 146 | + const { |
| 147 | + data: { sha: newTreeSha }, |
| 148 | + } = await octokit.request("POST /repos/:owner/:repo/git/trees", { |
| 149 | + owner: fork, |
| 150 | + repo, |
| 151 | + base_tree: treeSha, |
| 152 | + tree, |
| 153 | + }); |
| 154 | + |
| 155 | + // https://developer.github.com/v3/git/commits/#create-a-commit |
| 156 | + const { |
| 157 | + data: { sha: latestCommitSha }, |
| 158 | + } = await octokit.request("POST /repos/:owner/:repo/git/commits", { |
| 159 | + owner: fork, |
| 160 | + repo, |
| 161 | + message: changes.commit, |
| 162 | + tree: newTreeSha, |
| 163 | + parents: [firstCommit.sha], |
| 164 | + }); |
| 165 | + |
| 166 | + // https://developer.github.com/v3/git/refs/#create-a-reference |
| 167 | + await octokit.request("POST /repos/:owner/:repo/git/refs", { |
| 168 | + owner: fork, |
| 169 | + repo, |
| 170 | + sha: latestCommitSha, |
| 171 | + ref: `refs/heads/${head}`, |
| 172 | + }); |
| 173 | + |
| 174 | + // https://developer.github.com/v3/pulls/#create-a-pull-request |
| 175 | + return await octokit.request("POST /repos/:owner/:repo/pulls", { |
| 176 | + owner, |
| 177 | + repo, |
| 178 | + head: `${fork}:${head}`, |
| 179 | + base, |
| 180 | + title, |
| 181 | + body, |
| 182 | + }); |
| 183 | +} |
0 commit comments