Repository: intuit/auto
Pull request #729 (2019-11-28 00:51:58)

remove old use of prerelease label + add prerelease label to released plugin

<!-- GITHUB_RELEASE PR BODY: canary-version -->
Published PR with canary version: `8.0.0-canary.729.9588.0`
<!-- GITHUB_RELEASE PR BODY: canary-version -->



Patch:
diff --git a/docs/pages/extras/version.md b/docs/pages/extras/version.md
index 79a5fb2e..0cd5d106 100644
--- a/docs/pages/extras/version.md
+++ b/docs/pages/extras/version.md
@@ -1,9 +1,5 @@
 Useful in conjunction with `npm version` to auto-version releases.
 
-## Prerelease
-
-To create a prerelease add the `prerelease` label to your pull request.
-
 ## Skip Release
 
 To not create a release for a pull request add the `skip-release` label. Any pull request with this tag will make `auto version` return `''`.
diff --git a/packages/core/src/auto-rc.json b/packages/core/src/auto-rc.json
index 9012799e..8c13f4e5 100644
--- a/packages/core/src/auto-rc.json
+++ b/packages/core/src/auto-rc.json
@@ -25,10 +25,6 @@
         "release": {
           "type": "string",
           "description": "Only used when in 'onlyPublishWithReleaseLabel' to publish pull requests."
-        },
-        "prerelease": {
-          "type": "string",
-          "description": "Label used to mark a pull request as a prerelease"
         }
       }
     },
diff --git a/packages/core/src/git.ts b/packages/core/src/git.ts
index 93c8dcde..12b8fe6d 100644
--- a/packages/core/src/git.ts
+++ b/packages/core/src/git.ts
@@ -466,6 +466,23 @@ export default class Git {
     return result;
   }
 
+  /** Add a label to and issue or pull request */
+  async removeLabel(pr: number, label: string) {
+    this.logger.verbose.info(`Removing "${label}" from #${pr}`);
+
+    const result = await this.github.issues.removeLabel({
+      issue_number: pr,
+      owner: this.options.owner,
+      repo: this.options.repo,
+      name: label
+    });
+
+    this.logger.veryVerbose.info('Got response from removeLabel\n', result);
+    this.logger.verbose.info('Removed label on Pull Request.');
+
+    return result;
+  }
+
   /** Lock an issue */
   async lockIssue(issue: number) {
     this.logger.verbose.info(`Locking #${issue} issue...`);
diff --git a/packages/core/src/release.ts b/packages/core/src/release.ts
index e362d64b..2f31e896 100644
--- a/packages/core/src/release.ts
+++ b/packages/core/src/release.ts
@@ -27,16 +27,14 @@ export type VersionLabel =
   | SEMVER.minor
   | SEMVER.patch
   | 'skip-release'
-  | 'release'
-  | 'prerelease';
+  | 'release';
 
 export const defaultLabels: VersionLabel[] = [
   SEMVER.major,
   SEMVER.minor,
   SEMVER.patch,
   'skip-release',
-  'release',
-  'prerelease'
+  'release'
 ];
 
 /** Determine if a label is a label used for versioning */
@@ -138,13 +136,6 @@ export const defaultLabelDefinition: ILabelDefinitionMap = {
       description: 'Create a release when this pr is merged'
     }
   ],
-  prerelease: [
-    {
-      name: 'prerelease',
-      title: '🚧 Prerelease',
-      description: 'Create a pre-release version when merged'
-    }
-  ],
   internal: [
     {
       name: 'internal',
diff --git a/packages/core/src/semver.ts b/packages/core/src/semver.ts
index 56f4ff08..d2c786cb 100644
--- a/packages/core/src/semver.ts
+++ b/packages/core/src/semver.ts
@@ -63,12 +63,10 @@ export function calculateSemVerBump(
   });
 
   let skipRelease = false;
-  let isPrerelease = false;
 
   if (labels.length > 0 && labels[0].length > 0) {
-    const prereleaseLabels = labelMap.get('prerelease') || [];
     const releaseLabels = labelMap.get('release') || [];
-    isPrerelease = labels[0].some(label => prereleaseLabels.includes(label));
+
     skipRelease = onlyPublishWithReleaseLabel
       ? !labels[0].some(label => releaseLabels.includes(label))
       : labels[0].some(label => skipReleaseLabels.includes(label));
@@ -80,17 +78,5 @@ export function calculateSemVerBump(
     return SEMVER.noVersion;
   }
 
-  if (isPrerelease) {
-    if (version === SEMVER.major) {
-      return SEMVER.premajor;
-    }
-
-    if (version === SEMVER.minor) {
-      return SEMVER.preminor;
-    }
-
-    return SEMVER.prepatch;
-  }
-
   return version;
 }
diff --git a/plugins/released/README.md b/plugins/released/README.md
index ae36374f..364a6ab6 100644
--- a/plugins/released/README.md
+++ b/plugins/released/README.md
@@ -50,6 +50,23 @@ Customize the label this plugin attaches to merged pull requests.
 }
 ```
 
+### Prerelease Label
+
+Customize the prerelease label this plugin attaches to pull requests merged to prerelease branches.
+
+```json
+{
+  "plugins": [
+    [
+      "released",
+      {
+        "prereleaseLabel": "🚧"
+      }
+    ]
+  ]
+}
+```
+
 ### Message
 
 To customize the message this plugin uses on issues and pull requests use the following format.
diff --git a/plugins/released/src/index.ts b/plugins/released/src/index.ts
index 183cf624..4f589fbd 100644
--- a/plugins/released/src/index.ts
+++ b/plugins/released/src/index.ts
@@ -7,6 +7,8 @@ interface IReleasedLabelPluginOptions {
   message: string;
   /** The label to add to issues and pull requests */
   label: string;
+  /** The label to add to issues and pull requests that are in a prerelease */
+  prereleaseLabel: string;
   /** Whether to lock the issue once the pull request has been released */
   lockIssues: boolean;
 }
@@ -15,6 +17,7 @@ const TYPE = '%TYPE';
 const VERSION = '%VERSION';
 const defaultOptions = {
   label: 'released',
+  prereleaseLabel: 'prerelease',
   lockIssues: false,
   message: `:rocket: ${TYPE} was released in ${VERSION} :rocket:`
 };
@@ -42,8 +45,12 @@ export default class ReleasedLabelPlugin implements IPlugin {
     auto.hooks.modifyConfig.tap(this.name, config => {
       config.labels.released = config.labels.released || [
         {
-          name: 'released',
+          name: this.options.label,
           description: 'This issue/pull request has been released.'
+        },
+        {
+          name: this.options.prereleaseLabel,
+          description: 'This change is available in a prerelease.'
         }
       ];
 
@@ -53,10 +60,6 @@ export default class ReleasedLabelPlugin implements IPlugin {
     auto.hooks.afterRelease.tapPromise(
       this.name,
       async ({ newVersion, commits, response }) => {
-        if (response?.data.prerelease) {
-          return;
-        }
-
         if (!newVersion) {
           return;
         }
@@ -81,7 +84,12 @@ export default class ReleasedLabelPlugin implements IPlugin {
 
         await Promise.all(
           commits.map(async commit =>
-            this.addReleased(auto, commit, newVersion)
+            this.addReleased(
+              auto,
+              commit,
+              newVersion,
+              response?.data.prerelease
+            )
           )
         );
       }
@@ -92,16 +100,18 @@ export default class ReleasedLabelPlugin implements IPlugin {
   private async addReleased(
     auto: Auto,
     commit: IExtendedCommit,
-    newVersion: string
+    newVersion: string,
+    isPrerelease = false
   ) {
     const messages = [commit.subject];
 
     if (commit.pullRequest) {
-      await this.addCommentAndLabel(
+      await this.addCommentAndLabel({
         auto,
         newVersion,
-        commit.pullRequest.number
-      );
+        prOrIssue: commit.pullRequest.number,
+        isPrerelease
+      });
 
       const pr = await auto.git!.getPullRequest(commit.pullRequest.number);
       pr.data.body.split('\n').map(line => messages.push(line));
@@ -122,9 +132,15 @@ export default class ReleasedLabelPlugin implements IPlugin {
 
     await Promise.all(
       issues.map(async issue => {
-        await this.addCommentAndLabel(auto, newVersion, issue, true);
-
-        if (this.options.lockIssues && !isCanary(newVersion)) {
+        await this.addCommentAndLabel({
+          auto,
+          newVersion,
+          prOrIssue: issue,
+          isIssue: true,
+          isPrerelease
+        });
+
+        if (this.options.lockIssues && !isCanary(newVersion) && !isPrerelease) {
           await auto.git!.lockIssue(issue);
         }
       })
@@ -132,12 +148,24 @@ export default class ReleasedLabelPlugin implements IPlugin {
   }
 
   /** Add the templated comment to the pr and attach the "released" label */
-  private async addCommentAndLabel(
-    auto: Auto,
-    newVersion: string,
-    prOrIssue: number,
-    isIssue = false
-  ) {
+  private async addCommentAndLabel({
+    auto,
+    newVersion,
+    prOrIssue,
+    isIssue = false,
+    isPrerelease = false
+  }: {
+    /** Reference to auto instance */
+    auto: Auto;
+    /** The version being publishing */
+    newVersion: string;
+    /** Issue or pr number */
+    prOrIssue: number;
+    /** Whether it's an issue number */
+    isIssue?: boolean;
+    /** Whether the release was a prerelease */
+    isPrerelease?: boolean;
+  }) {
     // leave a comment with the new version
     const message = this.createReleasedComment(isIssue, newVersion);
     await auto.comment({ message, pr: prOrIssue, context: 'released' });
@@ -150,8 +178,16 @@ export default class ReleasedLabelPlugin implements IPlugin {
     // add a `released` label to a PR
     const labels = await auto.git!.getLabels(prOrIssue);
 
-    if (!labels.includes(this.options.label)) {
+    if (isPrerelease) {
+      if (!labels.includes(this.options.prereleaseLabel)) {
+        await auto.git!.addLabelToPr(prOrIssue, this.options.prereleaseLabel);
+      }
+    } else if (!labels.includes(this.options.label)) {
       await auto.git!.addLabelToPr(prOrIssue, this.options.label);
+
+      if (labels.includes(this.options.prereleaseLabel)) {
+        await auto.git!.removeLabel(prOrIssue, this.options.prereleaseLabel);
+      }
     }
   }
 


Test patch:
diff --git a/packages/core/src/__tests__/__snapshots__/auto.test.ts.snap b/packages/core/src/__tests__/__snapshots__/auto.test.ts.snap
index 32fb76ed..d547884b 100644
--- a/packages/core/src/__tests__/__snapshots__/auto.test.ts.snap
+++ b/packages/core/src/__tests__/__snapshots__/auto.test.ts.snap
@@ -40,13 +40,6 @@ exports[`Auto createLabels should create the labels 1`] = `
             "title": "🐛  Bug Fix",
           },
         ],
-        "prerelease": Array [
-          Object {
-            "description": "Create a pre-release version when merged",
-            "name": "prerelease",
-            "title": "🚧 Prerelease",
-          },
-        ],
         "release": Array [
           Object {
             "description": "Create a release when this pr is merged",
@@ -112,13 +105,6 @@ Object {
         "title": "🐛  Bug Fix",
       },
     ],
-    "prerelease": Array [
-      Object {
-        "description": "Create a pre-release version when merged",
-        "name": "prerelease",
-        "title": "🚧 Prerelease",
-      },
-    ],
     "release": Array [
       Object {
         "description": "Create a release when this pr is merged",
@@ -183,13 +169,6 @@ Object {
         "title": "🐛  Bug Fix",
       },
     ],
-    "prerelease": Array [
-      Object {
-        "description": "Create a pre-release version when merged",
-        "name": "prerelease",
-        "title": "🚧 Prerelease",
-      },
-    ],
     "release": Array [
       Object {
         "description": "Create a release when this pr is merged",
diff --git a/packages/core/src/__tests__/auto.test.ts b/packages/core/src/__tests__/auto.test.ts
index 3cf6ae3d..8c6d3a70 100644
--- a/packages/core/src/__tests__/auto.test.ts
+++ b/packages/core/src/__tests__/auto.test.ts
@@ -162,7 +162,6 @@ describe('Auto', () => {
       ['Version: Patch'],
       ['skip-release'],
       ['release'],
-      ['prerelease']
     ]);
   });
 
diff --git a/packages/core/src/__tests__/release.test.ts b/packages/core/src/__tests__/release.test.ts
index 6179847b..bc791167 100644
--- a/packages/core/src/__tests__/release.test.ts
+++ b/packages/core/src/__tests__/release.test.ts
@@ -119,8 +119,7 @@ describe('getVersionMap', () => {
         ['minor', ['minor']],
         ['patch', ['patch']],
         ['skip-release', ['skip-release']],
-        ['release', ['release']],
-        ['prerelease', ['prerelease']]
+        ['release', ['release']]
       ])
     );
   });
diff --git a/packages/core/src/__tests__/semver.test.ts b/packages/core/src/__tests__/semver.test.ts
index c633a550..cac9aa57 100644
--- a/packages/core/src/__tests__/semver.test.ts
+++ b/packages/core/src/__tests__/semver.test.ts
@@ -10,18 +10,6 @@ test('ranks releases right', () => {
 });
 
 describe('calculateSemVerBump', () => {
-  test('publishes pre-releases', () => {
-    expect(calculateSemVerBump([['minor', 'prerelease']], semverMap)).toBe(
-      SEMVER.preminor
-    );
-    expect(calculateSemVerBump([['patch', 'prerelease']], semverMap)).toBe(
-      SEMVER.prepatch
-    );
-    expect(calculateSemVerBump([['major', 'prerelease']], semverMap)).toBe(
-      SEMVER.premajor
-    );
-  });
-
   test('should be able to use multiple labels for skip-release', () => {
     expect(
       calculateSemVerBump([['skip-release', 'major']], semverMap, {
diff --git a/plugins/released/__tests__/released-label.test.ts b/plugins/released/__tests__/released-label.test.ts
index 06df0dd1..568c34e7 100644
--- a/plugins/released/__tests__/released-label.test.ts
+++ b/plugins/released/__tests__/released-label.test.ts
@@ -55,6 +55,10 @@ describe('release label plugin', () => {
           {
             description: 'This issue/pull request has been released.',
             name: 'released'
+          },
+          {
+            description: 'This change is available in a prerelease.',
+            name: 'prerelease'
           }
         ]
       }