From 2eb58c89dcc7f84b755c18b3cf4a6161787a3197 Mon Sep 17 00:00:00 2001 From: ocavue Date: Wed, 29 Apr 2020 10:29:15 +0800 Subject: [PATCH] Add input variables `commit` and `title` (#27) * Add input variables `commit` and `title` * Update action.yml Co-authored-by: Mitchell Hamilton --- README.md | 2 ++ action.yml | 7 +++++++ dist/index.js | 2 +- index.ts | 28 ++++++++++++++-------------- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index e7cd0d8..899543d 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,8 @@ This action for [Changesets](https://github.com/atlassian/changesets) creates a - publish - The command to use to build and publish packages - version - The command to update version, edit CHANGELOG, read and delete changesets. Default to `changeset version` if not provided +- commit - The commit message to use. Default to `Version Packages` +- title - The pull request title. Default to `Version Packages` ### Outputs diff --git a/action.yml b/action.yml index 618ace4..6120076 100644 --- a/action.yml +++ b/action.yml @@ -10,6 +10,13 @@ inputs: version: description: "The command to update version, edit CHANGELOG, read and delete changesets. Default to `changeset version` if not provided" required: false + commit: + description: | + The commit message. Default to `Version Packages` + required: false + title: + description: The pull request title. Default to `Version Packages` + required: false outputs: published: description: A boolean value to indicate whether a publishing is happened or not diff --git a/dist/index.js b/dist/index.js index 2e2b317..751e220 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1219,5 +1219,5 @@ var e=require("./conversions"),n=require("./route"),r={},o=Object.keys(e);functi },{"chalk":"ug4C"}],"PCsh":[function(require,module,exports) { "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=void 0;var e=c(require("@babel/runtime/helpers/esm/defineProperty")),r=l(require("fs-extra")),t=c(require("path")),n=c(require("@changesets/parse")),a=require("@changesets/git"),i=c(require("chalk")),o=c(require("p-filter")),s=require("@changesets/logger");function l(e){if(e&&e.__esModule)return e;var r={};if(null!=e)for(var t in e)if(Object.prototype.hasOwnProperty.call(e,t)){var n=Object.defineProperty&&Object.getOwnPropertyDescriptor?Object.getOwnPropertyDescriptor(e,t):{};n.get||n.set?Object.defineProperty(r,t,n):r[t]=e[t]}return r.default=e,r}function c(e){return e&&e.__esModule?e:{default:e}}let u=i.default.red("===============================IMPORTANT!==============================="),d=i.default.red("----------------------------------------------------------------------");async function f(e,n){const a=(await(0,o.default)(n,async n=>(await(0,r.lstat)(t.default.join(e,n))).isDirectory())).map(async n=>{const a=t.default.join(e,n,"changes.json"),[i,o]=await Promise.all([(0,r.readFile)(t.default.join(e,n,"changes.md"),"utf-8"),(0,r.readJson)(a)]);return{releases:o.releases,summary:i,id:n}});return Promise.all(a)}async function p(e,r){let t=await f(e,r);return 0===t.length?[]:((0,s.warn)(u),(0,s.warn)("There were old changesets from version 1 found"),(0,s.warn)("Theses are being applied now but the dependents graph may have changed"),(0,s.warn)("Make sure you validate all your dependencies"),(0,s.warn)("In a future major version, we will no longer apply these old changesets, and will instead throw here"),(0,s.warn)(d),t)}function h(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);r&&(n=n.filter(function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable})),t.push.apply(t,n)}return t}function w(r){for(var t=1;te.split("/")[1]);return e.filter(e=>n.includes(e))}async function g(e,a){let i,o=t.default.join(e,".changeset");try{i=await r.default.readdir(o)}catch(c){if("ENOENT"===c.code)throw new Error("There is no .changeset directory in this project");throw c}void 0!==a&&(i=await y(i,o,a));let s=p(o,i);const l=i.filter(e=>e.endsWith(".md")&&"README.md"!==e).map(async e=>{const a=await r.default.readFile(t.default.join(o,e),"utf-8");return w({},(0,n.default)(a),{id:e.replace(".md","")})});return[...await s,...await Promise.all(l)]}var O=g;exports.default=O; },{"@babel/runtime/helpers/esm/defineProperty":"9iDM","fs-extra":"rdZu","@changesets/parse":"boSt","@changesets/git":"It5m","chalk":"ug4C","p-filter":"PNks","@changesets/logger":"cocj"}],"7QCb":[function(require,module,exports) { -"use strict";function e(e,t){var a=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),a.push.apply(a,n)}return a}function t(t){for(var n=1;n{let e=process.env.GITHUB_TOKEN;if(!e)return void o.setFailed("Please add the GITHUB_TOKEN to the changesets action");let a=`${s.context.repo.owner}/${s.context.repo.repo}`,n=s.context.ref.replace("refs/heads/",""),i=await h.readPreState(process.cwd()),f=void 0!==i&&"pre"===i.mode;const b=new s.GitHub(e);console.log("setting git user"),await r.exec("git",["config","--global","user.name",'"github-actions[bot]"']),await r.exec("git",["config","--global","user.email",'"github-actions[bot]@users.noreply.github.com"']),console.log("setting GitHub credentials"),await l.default.writeFile(`${process.env.HOME}/.netrc`,`machine github.com\nlogin github-actions[bot]\npassword ${e}`);let w=await d.default(process.cwd());if(f){let e=new Set(i.changesets);w=w.filter(t=>!e.has(t.id))}let m=0!==w.length,y=o.getInput("publish"),v=o.getInput("version");if(o.setOutput("published","false"),o.setOutput("publishedPackages","[]"),m||y){if(!m&&y){console.log("No changesets found, attempting to publish any unpublished packages to npm");let e=await c.default({tools:["yarn","bolt","pnpm","root"]});if(!e)return o.setFailed("Could not find workspaces");let a=new Map(e.map(e=>[e.name,e]));l.default.writeFileSync(`${process.env.HOME}/.npmrc`,`//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}`);let[i,...p]=y.split(/\s+/),h=await g.execWithOutput(i,p);await r.exec("git",["pull","origin",n]),await r.exec("git",["push","origin",`HEAD:${n}`,"--tags"]);let d=/New tag:\s+(@[^/]+\/[^@]+|[^/]+)@([^\s]+)/,f=[];for(let t of h.stdout.split("\n")){let e=t.match(d);if(null===e)continue;let n=e[1],i=a.get(n);if(void 0===i)return o.setFailed("Workspace not found for "+n+". This is probably a bug in the action, please open an issue");f.push(i)}return await Promise.all(f.map(async e=>{try{let n=u.default.join(e.dir,"CHANGELOG.md"),i=await l.default.readFile(n,"utf8"),r=g.getChangelogEntry(i,e.config.version);if(!r)return o.setFailed(`Could not find changelog entry for ${e.name}@${e.config.version}`);await b.repos.createRelease(t({tag_name:`${e.name}@${e.config.version}`,body:r.content,prerelease:e.config.version.includes("-")},s.context.repo))}catch(a){if("ENOENT"!==a.code)throw a}})),void(f.length&&(o.setOutput("published","true"),o.setOutput("publishedPackages",JSON.stringify(f.map(e=>({name:e.name,version:e.config.version}))))))}if(m){let e=`changeset-release/${n}`,{stderr:o}=await g.execWithOutput("git",["checkout",e],{ignoreReturnCode:!0});if(!o.toString().includes(`Switched to a new branch '${e}'`)&&await r.exec("git",["checkout","-b",e]),await r.exec("git",["reset","--hard",s.context.sha]),v){let[e,...t]=v.split(/\s+/);await r.exec(e,t)}else{let e=await l.default.readJson(u.default.join("node_modules","@changesets","cli","package.json")),t=p.lt(e.version,"2.0.0")?"bump":"version";await r.exec("node",["./node_modules/@changesets/cli/bin.js",t])}let c=`repo:${a}+state:open+head:${e}+base:${n}`,h=b.search.issuesAndPullRequests({q:c}),d=await g.getChangedPackages(process.cwd()),w=(async()=>`This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and ${y?"the packages will be published to npm automatically":"publish to npm yourself or [setup this action to publish automatically](https://github.com/changesets/action#with-publishing)"}. If you're not ready to do a release yet, that's fine, whenever you add more changesets to ${n}, this PR will be updated.\n${f?`\n⚠️⚠️⚠️⚠️⚠️⚠️\n\n\`${n}\` is currently in **pre mode** so this branch has prereleases rather than normal releases. If you want to exit prereleases, run \`changeset pre exit\` on \`${n}\`.\n\n⚠️⚠️⚠️⚠️⚠️⚠️\n`:""}\n# Releases\n`+(await Promise.all(d.map(async e=>{let t=await l.default.readFile(u.default.join(e.dir,"CHANGELOG.md"),"utf8"),a=g.getChangelogEntry(t,e.config.version);return{highestLevel:a.highestLevel,private:!!e.config.private,content:`## ${e.name}@${e.config.version}\n\n`+a.content}}))).filter(e=>e).sort(g.sortTheThings).map(e=>e.content).join("\n "))();const m=`Version Packages${f?` (${i.tag})`:""}`,O=`ci(changeset): generate PR with changelog &${f?` (${i.tag})`:""} version updates`;await r.exec("git",["add","."]),await r.exec("git",["commit","-m",O]),await r.exec("git",["push","origin",e,"--force"]);let $=await h;console.log(JSON.stringify($.data,null,2)),0===$.data.items.length?(console.log("creating pull request"),await b.pulls.create(t({base:n,head:e,title:m,body:await w},s.context.repo))):(b.pulls.update(t({pull_number:$.data.items[0].number,title:m,body:await w},s.context.repo)),console.log("pull request found"))}}else console.log("No changesets found")})().catch(e=>{console.error(e),o.setFailed(e.message)}); +"use strict";function e(e,t){var i=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);t&&(a=a.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),i.push.apply(i,a)}return i}function t(t){for(var a=1;a{let e=process.env.GITHUB_TOKEN;if(!e)return void r.setFailed("Please add the GITHUB_TOKEN to the changesets action");let i=`${s.context.repo.owner}/${s.context.repo.repo}`,a=s.context.ref.replace("refs/heads/",""),n=await h.readPreState(process.cwd()),f=void 0!==n&&"pre"===n.mode;const b=new s.GitHub(e);console.log("setting git user"),await o.exec("git",["config","--global","user.name",'"github-actions[bot]"']),await o.exec("git",["config","--global","user.email",'"github-actions[bot]@users.noreply.github.com"']),console.log("setting GitHub credentials"),await l.default.writeFile(`${process.env.HOME}/.netrc`,`machine github.com\nlogin github-actions[bot]\npassword ${e}`);let m=await d.default(process.cwd());if(f){let e=new Set(n.changesets);m=m.filter(t=>!e.has(t.id))}let w=0!==m.length,y={publishScript:r.getInput("publish"),versionScript:r.getInput("version"),commit:r.getInput("commit")||"Version Packages",prTitle:r.getInput("title")||"Version Packages"};if(r.setOutput("published","false"),r.setOutput("publishedPackages","[]"),w||y.publishScript){if(!w&&y.publishScript){console.log("No changesets found, attempting to publish any unpublished packages to npm");let e=await c.default({tools:["yarn","bolt","pnpm","root"]});if(!e)return r.setFailed("Could not find workspaces");let i=new Map(e.map(e=>[e.name,e]));l.default.writeFileSync(`${process.env.HOME}/.npmrc`,`//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}`);let[n,...g]=y.publishScript.split(/\s+/),h=await p.execWithOutput(n,g);await o.exec("git",["pull","origin",a]),await o.exec("git",["push","origin",`HEAD:${a}`,"--tags"]);let d=/New tag:\s+(@[^/]+\/[^@]+|[^/]+)@([^\s]+)/,f=[];for(let t of h.stdout.split("\n")){let e=t.match(d);if(null===e)continue;let a=e[1],n=i.get(a);if(void 0===n)return r.setFailed("Workspace not found for "+a+". This is probably a bug in the action, please open an issue");f.push(n)}return await Promise.all(f.map(async e=>{try{let a=u.default.join(e.dir,"CHANGELOG.md"),n=await l.default.readFile(a,"utf8"),o=p.getChangelogEntry(n,e.config.version);if(!o)return r.setFailed(`Could not find changelog entry for ${e.name}@${e.config.version}`);await b.repos.createRelease(t({tag_name:`${e.name}@${e.config.version}`,body:o.content,prerelease:e.config.version.includes("-")},s.context.repo))}catch(i){if("ENOENT"!==i.code)throw i}})),void(f.length&&(r.setOutput("published","true"),r.setOutput("publishedPackages",JSON.stringify(f.map(e=>({name:e.name,version:e.config.version}))))))}if(w){let e=`changeset-release/${a}`,{stderr:r}=await p.execWithOutput("git",["checkout",e],{ignoreReturnCode:!0});if(!r.toString().includes(`Switched to a new branch '${e}'`)&&await o.exec("git",["checkout","-b",e]),await o.exec("git",["reset","--hard",s.context.sha]),y.versionScript){let[e,...t]=y.versionScript.split(/\s+/);await o.exec(e,t)}else{let e=await l.default.readJson(u.default.join("node_modules","@changesets","cli","package.json")),t=g.lt(e.version,"2.0.0")?"bump":"version";await o.exec("node",["./node_modules/@changesets/cli/bin.js",t])}let c=`repo:${i}+state:open+head:${e}+base:${a}`,h=b.search.issuesAndPullRequests({q:c}),d=await p.getChangedPackages(process.cwd()),m=(async()=>`This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and ${y.publishScript?"the packages will be published to npm automatically":"publish to npm yourself or [setup this action to publish automatically](https://github.com/changesets/action#with-publishing)"}. If you're not ready to do a release yet, that's fine, whenever you add more changesets to ${a}, this PR will be updated.\n${f?`\n⚠️⚠️⚠️⚠️⚠️⚠️\n\n\`${a}\` is currently in **pre mode** so this branch has prereleases rather than normal releases. If you want to exit prereleases, run \`changeset pre exit\` on \`${a}\`.\n\n⚠️⚠️⚠️⚠️⚠️⚠️\n`:""}\n# Releases\n`+(await Promise.all(d.map(async e=>{let t=await l.default.readFile(u.default.join(e.dir,"CHANGELOG.md"),"utf8"),i=p.getChangelogEntry(t,e.config.version);return{highestLevel:i.highestLevel,private:!!e.config.private,content:`## ${e.name}@${e.config.version}\n\n`+i.content}}))).filter(e=>e).sort(p.sortTheThings).map(e=>e.content).join("\n "))();const w=`${y.prTitle}${f?` (${n.tag})`:""}`,v=`${y.commit}${f?` (${n.tag})`:""}`;await o.exec("git",["add","."]),await o.exec("git",["commit","-m",v]),await o.exec("git",["push","origin",e,"--force"]);let O=await h;console.log(JSON.stringify(O.data,null,2)),0===O.data.items.length?(console.log("creating pull request"),await b.pulls.create(t({base:a,head:e,title:w,body:await m},s.context.repo))):(b.pulls.update(t({pull_number:O.data.items[0].number,title:w,body:await m},s.context.repo)),console.log("pull request found"))}}else console.log("No changesets found")})().catch(e=>{console.error(e),r.setFailed(e.message)}); },{"@actions/core":"4FTV","@actions/exec":"utFe","@actions/github":"8Dol","fs-extra":"JTJ9","get-workspaces":"kAcF","./utils":"UnXq","semver":"aric","@changesets/pre":"jEAA","@changesets/read":"PCsh"}]},{},["7QCb"], null) \ No newline at end of file diff --git a/index.ts b/index.ts index fa7ba58..0d23a34 100644 --- a/index.ts +++ b/index.ts @@ -58,16 +58,20 @@ import readChangesets from "@changesets/read"; let hasChangesets = changesets.length !== 0; - let publishScript = core.getInput("publish"); - let versionScript = core.getInput("version"); + let inputs = { + publishScript: core.getInput("publish"), + versionScript: core.getInput("version"), + commit: core.getInput("commit") || "Version Packages", + prTitle: core.getInput("title") || "Version Packages", + } core.setOutput('published', 'false'); core.setOutput('publishedPackages', '[]'); - if (!hasChangesets && !publishScript) { + if (!hasChangesets && !inputs.publishScript) { console.log("No changesets found"); return; } - if (!hasChangesets && publishScript) { + if (!hasChangesets && inputs.publishScript) { console.log( "No changesets found, attempting to publish any unpublished packages to npm" ); @@ -86,7 +90,7 @@ import readChangesets from "@changesets/read"; `//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}` ); - let [publishCommand, ...publishArgs] = publishScript.split(/\s+/); + let [publishCommand, ...publishArgs] = inputs.publishScript.split(/\s+/); let changesetPublishOutput = await execWithOutput( publishCommand, @@ -176,8 +180,8 @@ import readChangesets from "@changesets/read"; await exec("git", ["reset", "--hard", github.context.sha]); - if (versionScript) { - let [versionCommand, ...versionArgs] = versionScript.split(/\s+/); + if (inputs.versionScript) { + let [versionCommand, ...versionArgs] = inputs.versionScript.split(/\s+/); await exec(versionCommand, versionArgs); } else { let changesetsCliPkgJson = await fs.readJson( @@ -198,7 +202,7 @@ import readChangesets from "@changesets/read"; let prBodyPromise = (async () => { return ( `This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and ${ - publishScript + inputs.publishScript ? `the packages will be published to npm automatically` : `publish to npm yourself or [setup this action to publish automatically](https://github.com/changesets/action#with-publishing)` }. If you're not ready to do a release yet, that's fine, whenever you add more changesets to ${branch}, this PR will be updated. @@ -244,12 +248,8 @@ ${ ); })(); - const prTitle = `Version Packages${ - isInPreMode ? ` (${preState.tag})` : "" - }`; - const commitMsg = `ci(changeset): generate PR with changelog &${ - isInPreMode ? ` (${preState.tag})` : "" - } version updates`; + const prTitle = `${inputs.prTitle}${isInPreMode ? ` (${preState.tag})` : ""}`; + const commitMsg = `${inputs.commit}${isInPreMode ? ` (${preState.tag})` : ""}`; await exec("git", ["add", "."]); await exec("git", ["commit", "-m", commitMsg]);