diff --git a/dist/index.js b/dist/index.js index 1733482..cfbdae5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -285,5 +285,5 @@ var r;try{r=require("graceful-fs")}catch(s){r=require("fs")}function e(e,n,t){nu },{"universalify":"BgiA","graceful-fs":"X4Hm","../mkdirs":"K9sR","../path-exists":"EriC"}],"JTJ9":[function(require,module,exports) { "use strict";module.exports=Object.assign({},require("./fs"),require("./copy-sync"),require("./copy"),require("./empty"),require("./ensure"),require("./json"),require("./mkdirs"),require("./move-sync"),require("./move"),require("./output"),require("./path-exists"),require("./remove"));const e=require("fs");Object.getOwnPropertyDescriptor(e,"promises")&&Object.defineProperty(module.exports,"promises",{get:()=>e.promises}); },{"./fs":"NeZP","./copy-sync":"7ntm","./copy":"vWuT","./empty":"1Xa3","./ensure":"QssT","./json":"tcuq","./mkdirs":"K9sR","./move-sync":"z7nh","./move":"Vp9L","./output":"jQOt","./path-exists":"EriC","./remove":"0UN6"}],"7QCb":[function(require,module,exports) { -"use strict";function e(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var s=Object.getOwnPropertySymbols(e);t&&(s=s.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),n.push.apply(n,s)}return n}function t(t){for(var s=1;s{n+=e.toString()},stderr:e=>{s+=e.toString()}}};return{code:await o.exec(e,t,r),stdout:n,stderr:s}}(async()=>{let e=process.env.GITHUB_TOKEN;if(!e)return void a.setFailed("Please add the GITHUB_TOKEN to the changesets action");let n=`${i.context.repo.owner}/${i.context.repo.repo}`;const s=new i.GitHub(e);let r=s.repos.get(i.context.repo).then(e=>e.data.default_branch);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"']),await o.exec("git",["remote","add","gh-https",`https://github.com/${n}`]),console.log("setting GitHub credentials"),await c.default.writeFile(`${process.env.HOME}/.netrc`,`machine github.com\nlogin github-actions[bot]\npassword ${e}`);let u=await r;if(i.context.ref.replace("refs/heads/","")!==u)return void a.setFailed(`The changesets action should only run on ${u} but it's running on ${i.context.ref.replace("refs/heads/","")}, please change your GitHub actions config to only run the Changesets action on ${u}`);let g=c.default.readdirSync(`${process.cwd()}/.changeset`).some(e=>"config.js"!==e&&"README.md"!==e),h=a.getInput("publish");if(!g&&h){console.log("No changesets found, attempting to publish any unpublished packages to npm"),c.default.writeFileSync(`${process.env.HOME}/.npmrc`,`//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}`);let[e,...t]=h.split(/\s+/);return await o.exec(e,t),void(await o.exec("git",["push","--follow-tags","gh-https","master"]))}let{stderr:d}=await l("git",["checkout","changeset-release"]),p=!d.toString().includes("Switched to a new branch 'changeset-release'");p&&await o.exec("git",["checkout","-b","changeset-release"]);let f=p;if(!f){console.log("checking if new changesets should be added");const e=(await l("git",["merge-base","changeset-release","master"])).stdout.trim();f=(await l("git",["diff","--name-only",`${e}...master`])).stdout.trim().includes(".changeset"),console.log("checked if new changesets should be added "+f)}if(f){await o.exec("git",["reset","--hard","master"]),await o.exec("yarn",["changeset","bump"]),await o.exec("git",["add","."]),await o.exec("git",["commit","-m","Version Packages"]),await o.exec("git",["push","gh-https","changeset-release","--force"]);let e=`repo:${n}+state:open+head:changeset-release+base:master`,r=await s.search.issuesAndPullRequests({q:e});console.log(JSON.stringify(r.data,null,2)),0===r.data.items.length?(console.log("creating pull request"),await s.pulls.create(t({base:"master",head:"changeset-release",title:"Version Packages"},i.context.repo))):console.log("pull request found")}else console.log("no new changesets")})(); +"use strict";function e(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),n.push.apply(n,r)}return n}function t(t){for(var r=1;r{s+=e.toString()},stderr:e=>{a+=e.toString()}}},r)),stdout:s,stderr:a}}(async()=>{let e=process.env.GITHUB_TOKEN;if(!e)return void a.setFailed("Please add the GITHUB_TOKEN to the changesets action");let n=`${i.context.repo.owner}/${i.context.repo.repo}`;const r=new i.GitHub(e);let s=r.repos.get(i.context.repo).then(e=>e.data.default_branch);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"']),await o.exec("git",["remote","add","gh-https",`https://github.com/${n}`]),console.log("setting GitHub credentials"),await c.default.writeFile(`${process.env.HOME}/.netrc`,`machine github.com\nlogin github-actions[bot]\npassword ${e}`);let u=await s;if(i.context.ref.replace("refs/heads/","")!==u)return void a.setFailed(`The changesets action should only run on ${u} but it's running on ${i.context.ref.replace("refs/heads/","")}, please change your GitHub actions config to only run the Changesets action on ${u}`);let g=c.default.readdirSync(`${process.cwd()}/.changeset`).some(e=>"config.js"!==e&&"README.md"!==e),h=a.getInput("publish");if(!g&&h){console.log("No changesets found, attempting to publish any unpublished packages to npm"),c.default.writeFileSync(`${process.env.HOME}/.npmrc`,`//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}`);let[e,...t]=h.split(/\s+/);return await o.exec(e,t),void(await o.exec("git",["push","--follow-tags","gh-https","master"]))}let{stderr:d}=await l("git",["checkout","changeset-release"],{ignoreReturnCode:!0}),p=!d.toString().includes("Switched to a new branch 'changeset-release'");p&&await o.exec("git",["checkout","-b","changeset-release"]);let f=p;if(!f){console.log("checking if new changesets should be added");const e=(await l("git",["merge-base","changeset-release","master"])).stdout.trim();f=(await l("git",["diff","--name-only",`${e}...master`])).stdout.trim().includes(".changeset"),console.log("checked if new changesets should be added "+f)}if(f){await o.exec("git",["reset","--hard","master"]),await o.exec("yarn",["changeset","bump"]),await o.exec("git",["add","."]),await o.exec("git",["commit","-m","Version Packages"]),await o.exec("git",["push","gh-https","changeset-release","--force"]);let e=`repo:${n}+state:open+head:changeset-release+base:master`,s=await r.search.issuesAndPullRequests({q:e});console.log(JSON.stringify(s.data,null,2)),0===s.data.items.length?(console.log("creating pull request"),await r.pulls.create(t({base:"master",head:"changeset-release",title:"Version Packages"},i.context.repo))):console.log("pull request found")}else console.log("no new changesets")})(); },{"@actions/core":"4FTV","@actions/exec":"utFe","@actions/github":"8Dol","fs-extra":"JTJ9"}]},{},["7QCb"], null) \ No newline at end of file diff --git a/index.ts b/index.ts index deee4ca..acfed27 100644 --- a/index.ts +++ b/index.ts @@ -3,22 +3,27 @@ import { exec } from "@actions/exec"; import * as github from "@actions/github"; import fs from "fs-extra"; -async function execWithOutput(command: string, args?: string[]) { +async function execWithOutput( + command: string, + args?: string[], + options?: { ignoreReturnCode?: boolean } +) { let myOutput = ""; let myError = ""; - const options = { - listeners: { - stdout: (data: Buffer) => { - myOutput += data.toString(); - }, - stderr: (data: Buffer) => { - myError += data.toString(); - } - } - }; return { - code: await exec(command, args, options), + code: await exec(command, args, { + listeners: { + stdout: (data: Buffer) => { + myOutput += data.toString(); + }, + stderr: (data: Buffer) => { + myError += data.toString(); + } + }, + + ...options + }), stdout: myOutput, stderr: myError }; @@ -99,10 +104,11 @@ async function execWithOutput(command: string, args?: string[]) { return; } - let { stderr } = await execWithOutput("git", [ - "checkout", - "changeset-release" - ]); + let { stderr } = await execWithOutput( + "git", + ["checkout", "changeset-release"], + { ignoreReturnCode: true } + ); let isCreatingChangesetReleaseBranch = !stderr .toString() .includes("Switched to a new branch 'changeset-release'");