auto icon indicating copy to clipboard operation
auto copied to clipboard

Cannot get local plugin to load

Open jwodder opened this issue 3 years ago • 3 comments

I am attempting to get this workaround for the E2BIG error to work, but I am unable to get auto (v10.29.3) to load my local plugin. I've tried setting the path to both tools/exec-hooks.js and ./tools/exec-hooks.js, but both times auto just reports "warning Could not find plugin: ./tools/exec-hooks.js".

My .autorc:

{
    "onlyPublishWithReleaseLabel": true,
    "baseBranch": "master",
    "author": "auto <auto@nil>",
    "noVersionPrefix": true,
    "plugins": [
        "git-tag",
        #[
        #    "exec",
        #    {
        #        "beforeCommitChangelog": "mkdir -p env && env | grep -vP '^(GH_TOKEN|TWINE_PASSWORD)' > env/beforeCommitChangelog.env && git add env",
        #        "afterChangelog": "env | grep -vP '^(GH_TOKEN|TWINE_PASSWORD)' > env/afterChangelog.env && git add env && git commit -m afterChangelog && bump2version \"$(printf '%s\n' \"$ARG_0\" | jq -r .bump)\"",
        #        "afterRelease": "python -m build && twine upload dist/*"
        #    }
        #]
        "./tools/exec-hooks.js"
    ]
}

My tools/exec-hooks.js:

const { SEMVER, execPromise, getCurrentBranch } = require("@auto-it/core");

module.exports = class ExecE2BIGWorkaround {
  constructor() {
    this.name = "Custom exec commands";
  }

  /**
   * Setup the plugin
   *
   * @param {import('@auto-canary/core').default} auto
   */
  apply(auto) {
    auto.hooks.beforeCommitChangelog.tapPromise(this.name, async (config) => {
      await execPromise("mkdir", ["-p", "env"]);
      await execPromise("sh", ["env | grep -vP '^(GH_TOKEN|TWINE_PASSWORD)' > env/beforeCommitChangelog.env"]);
      await execPromise("git", ["add", "env"]);
    });

    auto.hooks.afterChangelog.tapPromise(this.name, async ({bump}) => {
      await execPromise("sh", ["env | grep -vP '^(GH_TOKEN|TWINE_PASSWORD)' > env/afterChangelog.env"]);
      await execPromise("git", ["add", "env"]);
      await execPromise("git", ["commit", "-m", "afterChangelog"]);
      await execPromise("bump2version", [bump]);
    });

    auto.hooks.afterRelease.tapPromise(this.name, async (config) => {
      await execPromise("python", ["-m", "build"]);
      // Thankfully, twine expands globs!
      await execPromise("twine", ["upload", "dist/*"]);
    });
  }
};

(If I've made any mistakes in writing the plugin, I'd appreciate them being pointed out; I am not a JavaScript person.)

jwodder avatar Jun 22 '21 13:06 jwodder

Ping @hipstersmoothie. The well-established problems with exec are breaking our pipeline, and we need a workaround.

jwodder avatar Sep 10 '21 15:09 jwodder

yes, fix would be very much appreciated

yarikoptic avatar Sep 10 '21 15:09 yarikoptic

It seems this bug is still there on v11.1.1.

I'm using auto in the non-npm usage and trying to load a local plugin to no avail.

ℹ  info      Found plugin using: /snapshot/auto/packages/core/dist/plugins/filter-non-pull-request.js
ℹ  info      Found plugin using: /snapshot/auto/packages/core/dist/plugins/filter-non-pull-request.js
ℹ  info      Found plugin using: /snapshot/auto/plugins/git-tag/dist/index.js
ℹ  info      Found plugin using: /snapshot/auto/plugins/git-tag/dist/index.js
⚠  warning   Could not find plugin: ./vendor/auto/project_filtering.js

Here is project_filtering.js

const execSync = require('child_process');
const path = require('path');
const { SEMVER, inFolder } = require('@auto-it/core');
const inc = require('semver');

function shouldOmitCommit(currentDir, currentWorkspace, commit, logger) {
  if (!commit.pullRequest) {
    return true
  }

  // auto adds the current path to the file paths reported from git, so we need to undo this
  const fixedFiles = commit.files.map(file => path.relative(currentDir, file))
  console.log(fixedFiles)
  const wsDir = path.join(currentWorkspace, path.sep)

  const atLeastOneFileInCurrentDir = fixedFiles.find(file =>
    inFolder(wsDir, file)
  )

  if (!atLeastOneFileInCurrentDir) {
    logger.verbose.log(
      `All files are outside the current workspace directory ('${wsDir}'). Omitting commit '${commit.hash}'.`
    )
    return true
  } else {
    if (
      commit.labels?.includes("skip-release") ||
      commit.subject?.includes("[skip ci]")
    ) {
      logger.verbose.log(
        "Skipping commit because it is marked as skip-release of [skip-ci]:",
        commit.hash,
        commit.labels,
        commit.subject
      )
      return true
    }

    logger.verbose.log(
      `At least one file is in the current workspace ('${wsDir}'). Including commit '${commit.hash}'.`
    )
    return false
  }
}

class FilterByWorkspacePathPlugin {
  /** The name of the plugin */
  name = "project_filtering"

  /** Initialize the plugin with it's options */
  constructor(options = {}) {
    this.options = options
  }

  apply(auto) {
    const currentDir = path.resolve(".")
    let currentWorkspace = currentDir
    if (!this.options.no_npm) {
      const npmResult = execSync("npm ls --omit=dev --depth 1 -json", {
        encoding: "utf-8",
        stdio: ["pipe", "pipe", "ignore"]
      })
      const workspaceDeps = JSON.parse(npmResult).dependencies
      // eslint-disable-next-line @typescript-eslint/no-magic-numbers
      let currentWorkspace = workspaceDeps[
        Object.keys(workspaceDeps)[0]
      ].resolved.substring(11)
    }

    auto.hooks.onCreateLogParse.tap(this.name, logParse => {
      logParse.hooks.omitCommit.tap(this.name, commit =>
        shouldOmitCommit(currentDir, currentWorkspace, commit, auto.logger)
          ? true
          : undefined
      )
    })

    auto.hooks.onCreateRelease.tap(this.name, release => {
      const origGetVersion = release.getSemverBump.bind(release)
      release.getSemverBump = async (from, to) => {
        const commits = await release.getCommits(from, to)
        if (commits.length === 0) {
          auto.logger.verbose.log("No commits found. Skipping release.")
          return SEMVER.noVersion
        }
        return origGetVersion(from, to)
      }

      release.calcNextVersion = async lastTag => {
        const bump = await release.getSemverBump(lastTag)
        const matches = lastTag.match(/(\d+\.\d+\.\d+)/)
        const lastVersion = matches ? matches[0] : lastTag

        return inc(lastVersion, bump)
      }
    })
  }
}

Maxim-Durand avatar Mar 06 '24 12:03 Maxim-Durand