Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot get local plugin to load #2019

Open
jwodder opened this issue Jun 22, 2021 · 3 comments
Open

Cannot get local plugin to load #2019

jwodder opened this issue Jun 22, 2021 · 3 comments
Labels
bug Something isn't working

Comments

@jwodder
Copy link
Contributor

jwodder commented Jun 22, 2021

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 jwodder added the bug Something isn't working label Jun 22, 2021
@jwodder
Copy link
Contributor Author

jwodder commented Sep 10, 2021

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

@yarikoptic
Copy link

yes, fix would be very much appreciated

@Maxim-Durand
Copy link

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)
      }
    })
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants