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

git-auto-patch

v1.0.3

Published

Script to automate github repository patching

Downloads

8

Readme

git-auto-patch

Script to automate github repository patching : it automates connection, cloning for manipulating files, branching and pull request creation.

Usage

  • Use git-auto-patch --help to see option details
  • git-auto-patch -a github_personal_token -s ./your_patch_script to execute patch script with the given authentication token

WARNING : it is recommended to use short lived authentication tokens as they are serialized and can be retreived.

API

The github object passed to the script offers the following methods :

  • async get (url) : triggers an API REST GET request to github, full response is returned
  • repository (name) : returns an object representing the repository, name might be {org}/{repo} or {user}/{repo}

The repository object exposes :

  • async createBranch (name, from = 'main') : creates a branch (the branch is created using github API, clone the repository after creating the branch or you won't get it)
  • cloned : true if the repository is cloned locally
  • async clone () : clones the repository locally (in a working folder)
  • async git (...args) : (⏬) execute the git command
  • async hasChanges () : (⏬) true if the repository has changes (based on git status)
  • async pushStash () : (⏬) stash the changes
  • async popStash () : (⏬) restore the stashed changes (ignore the error if no stash exists)
  • async checkout (branchName = 'main') : (⏬ & fetch) switch to the given branch
  • async exists (filename) : (⏬📂) true if the repository file (or folder) exists
  • async readFile (filename) : (⏬📂) read the repository text file
  • async writeFile (filename, content) : (⏬📂) overwrite the repository text file with the given content
  • async commitAllAndPush (message) : (⏬) stage all changed files, commit them (with the given message) and push
  • async createPullRequest (title, body, head, base = 'main') : create a pull request

⏬ : Before executing the command, the repository is cloned locally (if not already cloned) 📂 : Filename is relative to the root of the repository

Sample patch scripts

  • In this example, a change is made (because of the file concatenation), hence the branch is created first.
module.exports = async (github, ...customParameters) => {
  const repository = github.repository('ArnaudBuchholz/SampleProject')
  const branchName = `patch-${new Date().toISOString().replace(/-|T|:|\.|z/ig, '')}`
  await repository.createBranch(branchName, 'main')
  await repository.checkout(branchName)
  const sampleContent = await repository.readFile('sample.txt')
  await repository.writeFile('sample.txt', sampleContent + `\n${branchName}`)
  await repository.commitAllAndPush('This is a sample message')
  await repository.createPullRequest('Pull request title', 'pull request description', branchName, 'main')
}
  • In this example, hasChanges() checks if the patch generates a change. A stash saves and restores the change while switching to the new branch. The value parameter is specified on the command line using -c.
module.exports = async (github, value) => {
  if (value === undefined) {
    console.error('Use -c <value>')
    return
  }
  const repository = github.repository('ArnaudBuchholz/git-auto-patch-sample')
  await repository.writeFile('value.txt', value)
  if (await repository.hasChanges()) {
    await repository.pushStash() // save changes (not if we created a new file)
    const branchName = `patch-${new Date().toISOString().replace(/-|T|:|\.|z/ig, '')}`
    await repository.createBranch(branchName, 'main')
    await repository.checkout(branchName) // Will fetch first
    await repository.popStash() // restore changes (not if it was a new file)
    await repository.commitAllAndPush('Update of value')
    await repository.createPullRequest('Example of value update', `**value** : ${value}`, branchName, 'main')
  } else {
    console.log('No change !')
  }
}