npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

octokit-plugin-create-pull-request

v6.0.0

Published

Octokit plugin to create a pull request with multiple file changes

Downloads

281,469

Readme

octokit-plugin-create-pull-request

Octokit plugin to create a pull request with multiple file changes

@latest Build Status

Features

  • Retrieves the repository’s default branch unless base branch is set
  • Makes multiple file changes using a single commit
  • Creates a fork if the authenticated user does not have write access to the repository
  • Can update existing pull request

Usage

Load octokit-plugin-create-pull-request and @octokit/core (or core-compatible module) directly from esm.sh

<script type="module">
  import { Octokit } from "https://esm.sh/@octokit/core";
  import { createPullRequest } from "https://esm.sh/octokit-plugin-create-pull-request";
</script>

Install with npm install @octokit/core octokit-plugin-create-pull-request. Optionally replace @octokit/core with a core-compatible module

import { Octokit } from "@octokit/core";
import {
  createPullRequest,
  DELETE_FILE,
} from "octokit-plugin-create-pull-request";

[!IMPORTANT] As we use conditional exports, you will need to adapt your tsconfig.json by setting "moduleResolution": "node16", "module": "node16".

See the TypeScript docs on package.json "exports". See this helpful guide on transitioning to ESM from @sindresorhus

const MyOctokit = Octokit.plugin(createPullRequest);

const TOKEN = "secret123"; // create token at https://github.com/settings/tokens/new?scopes=repo
const octokit = new MyOctokit({
  auth: TOKEN,
});

// Returns a normal Octokit PR response
// See https://octokit.github.io/rest.js/#octokit-routes-pulls-create
octokit
  .createPullRequest({
    owner: "user-or-org-login",
    repo: "repo-name",
    title: "pull request title",
    body: "pull request description",
    head: "pull-request-branch-name",
    base: "main" /* optional: defaults to default branch */,
    update: false /* optional: set to `true` to enable updating existing pull requests */,
    forceFork: false /* optional: force creating fork even when user has write rights */,
    labels: [
      "bug",
    ] /* optional: applies the given labels when user has permissions. When updating an existing pull request, already present labels will not be deleted. */
    changes: [
      {
        /* optional: if `files` is not passed, an empty commit is created instead */
        files: {
          "path/to/file1.txt": "Content for file1",
          "path/to/file2.png": {
            content: "_base64_encoded_content_",
            encoding: "base64",
          },
          // deletes file if it exists,
          "path/to/file3.txt": DELETE_FILE,
          // updates file based on current content
          "path/to/file4.txt": ({ exists, encoding, content }) => {
            // do not create the file if it does not exist
            if (!exists) return null;

            return Buffer.from(content, encoding)
              .toString("utf-8")
              .toUpperCase();
          },
          "path/to/file5.sh": {
            content: "echo Hello World",
            encoding: "utf-8",
            // one of the modes supported by the git tree object
            // https://developer.github.com/v3/git/trees/#tree-object
            mode: "100755",
          },
          "path/to/file6.txt": ({ exists, encoding, content }) => {
            // do nothing if it does not exist
            if (!exists) return null;

            const fileContent = Buffer.from(content, encoding).toString(
              "utf-8"
            );

            if (fileContent.includes("octomania")) {
              // delete file
              return DELETE_FILE;
            }

            // keep file
            return null;
          },
        },
        commit:
          "creating file1.txt, file2.png, deleting file3.txt, updating file4.txt (if it exists), file5.sh",
        /* optional: if not passed, will be the authenticated user and the current date */
        author: {
          name: "Author LastName",
          email: "[email protected]",
          date: new Date().toISOString(), // must be ISO date string
        },
        /* optional: if not passed, will use the information set in author */
        committer: {
          name: "Committer LastName",
          email: "[email protected]",
          date: new Date().toISOString(), // must be ISO date string
        },
        /* optional: if not passed, commit won't be signed*/
        signature: async function (commitPayload) {
          // import { createSignature } from 'github-api-signature'
          //
          // return createSignature(
          //   commitPayload,
          //   privateKey,
          //   passphrase
          // );
        },
      },
    ],
  })
  .then((pr) => console.log(pr.data.number));

By default, a pull request is created, even if no files have been changed. To prevent an empty pull request, set options.createWhenEmpty to false. If no pull request has been created, octokit.createPullRequest() resolves with null.

By default, commits are always created, even if no files have been updated. To prevent empty commits, set options.changes[].emptyCommit to false. To set a custom commit message for empty commits, set emptyCommit to a string.

For using this plugin with another plugin, you can import the composeCreatePullRequest function, which accepts an octokit instance as first argument, and the same options as octokit.createPullRequest as second argument.

import { Octokit } from "@octokit/core";
import { composeCreatePullRequest } from "octokit-plugin-create-pull-request";

export function myPlugin(octokit) {
  return {
    async myFunction(options) {
      // custom code here

      const response = await composeCreatePullRequest(octokit, options);

      // more custom code here

      return response;
    },
  };
}

LICENSE

MIT