diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a852dcc..afdb74c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,6 +7,7 @@ on: permissions: contents: write + pull-requests: write jobs: ci-checks: diff --git a/src/test/integration/node.test.ts b/src/test/integration/node.test.ts index 304bfa3..9d8e6cf 100644 --- a/src/test/integration/node.test.ts +++ b/src/test/integration/node.test.ts @@ -93,6 +93,21 @@ describe("node", () => { } }; + const createPullRequestForBranch = async (branch: string, title: string) => { + const pullRequest = await octokit.rest.pulls.create({ + ...REPO, + title, + head: branch, + base: "main", + body: `CI-created integration test PR for \`${branch}\`.`, + }); + + expect(pullRequest.data.state).toEqual("open"); + expect(pullRequest.data.head.ref).toEqual(branch); + + return pullRequest.data.html_url; + }; + let testTargetCommit: string; /** * For tests, important that this commit is not an ancestor of TEST_TARGET_COMMIT, @@ -263,6 +278,107 @@ describe("node", () => { }); }); + it("can create a commit using the REST git API", async () => { + const branch = `${TEST_BRANCH_PREFIX}-rest-git-${Date.now()}`; + const filePath = "rest-git-api-test.txt"; + + const baseRef = await octokit.rest.git.getRef({ + ...REPO, + ref: "heads/main", + }); + const baseOid = baseRef.data.object.sha; + + const baseCommit = await octokit.rest.git.getCommit({ + ...REPO, + commit_sha: baseOid, + }); + + const tree = await octokit.rest.git.createTree({ + ...REPO, + base_tree: baseCommit.data.tree.sha, + tree: [ + { + path: filePath, + mode: "100644", + type: "blob", + content: "Hello from the REST git API!\n", + }, + ], + }); + + const commit = await octokit.rest.git.createCommit({ + ...REPO, + message: "Test REST git commit", + tree: tree.data.sha, + parents: [baseOid], + }); + + await octokit.rest.git.createRef({ + ...REPO, + ref: `refs/heads/${branch}`, + sha: commit.data.sha, + }); + + await waitForGitHubToBeReady(); + + const branchRef = await octokit.rest.git.getRef({ + ...REPO, + ref: `heads/${branch}`, + }); + + expect(branchRef.data.object.sha).toEqual(commit.data.sha); + + const pullRequestUrl = await createPullRequestForBranch( + branch, + `test: REST git API branch ${branch}`, + ); + + expect(pullRequestUrl).toContain(`/pull/`); + }); + + it("can create a commit using the GraphQL commit path", async () => { + const branch = `${TEST_BRANCH_PREFIX}-graphql-git-${Date.now()}`; + const filePath = "graphql-git-api-test.txt"; + + await commitFilesFromBuffers({ + octokit, + ...REPO, + branch, + base: { + branch: "main", + }, + message: { + headline: "Test GraphQL git commit", + body: "Created through commitFilesFromBuffers", + }, + fileChanges: { + additions: [ + { + path: filePath, + contents: Buffer.from("Hello from the GraphQL commit path!\n"), + }, + ], + }, + log, + }); + + await waitForGitHubToBeReady(); + + const branchRef = await octokit.rest.git.getRef({ + ...REPO, + ref: `heads/${branch}`, + }); + + expect(branchRef.data.object.sha).toBeTruthy(); + + const pullRequestUrl = await createPullRequestForBranch( + branch, + `test: GraphQL commit path branch ${branch}`, + ); + + expect(pullRequestUrl).toContain(`/pull/`); + }); + describe("existing branches", () => { it("can commit to existing branch when force is true", async () => { const branch = `${TEST_BRANCH_PREFIX}-existing-branch-force`;