Skip to content

Commit

Permalink
Use logging provided by @actions/core (#289)
Browse files Browse the repository at this point in the history
* chore: migrate to logging from @actions/core

* Update src/index.ts

* Update .changeset/blue-hornets-marry.md

---------

Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
  • Loading branch information
varl and Andarist authored May 8, 2023
1 parent ab485bf commit 8b28186
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/blue-hornets-marry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@changesets/action": patch
---

Use logging provided by `@actions/core`
22 changes: 11 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined;

const inputCwd = core.getInput("cwd");
if (inputCwd) {
console.log("changing directory to the one given as the input");
core.info("changing directory to the one given as the input");
process.chdir(inputCwd);
}

let setupGitUser = core.getBooleanInput("setupGitUser");

if (setupGitUser) {
console.log("setting git user");
core.info("setting git user");
await gitUtils.setupUser();
}

console.log("setting GitHub credentials");
core.info("setting GitHub credentials");
await fs.writeFile(
`${process.env.HOME}/.netrc`,
`machine github.com\nlogin github-actions[bot]\npassword ${githubToken}`
Expand All @@ -48,27 +48,27 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined;

switch (true) {
case !hasChangesets && !hasPublishScript:
console.log("No changesets found");
core.info("No changesets found");
return;
case !hasChangesets && hasPublishScript: {
console.log(
core.info(
"No changesets found, attempting to publish any unpublished packages to npm"
);

let userNpmrcPath = `${process.env.HOME}/.npmrc`;
if (fs.existsSync(userNpmrcPath)) {
console.log("Found existing user .npmrc file");
core.info("Found existing user .npmrc file");
const userNpmrcContent = await fs.readFile(userNpmrcPath, "utf8");
const authLine = userNpmrcContent.split("\n").find((line) => {
// check based on https://github.com/npm/cli/blob/8f8f71e4dd5ee66b3b17888faad5a7bf6c657eed/test/lib/adduser.js#L103-L105
return /^\s*\/\/registry\.npmjs\.org\/:[_-]authToken=/i.test(line);
});
if (authLine) {
console.log(
core.info(
"Found existing auth token for the npm registry in the user .npmrc file"
);
} else {
console.log(
core.info(
"Didn't find existing auth token for the npm registry in the user .npmrc file, creating one"
);
fs.appendFileSync(
Expand All @@ -77,7 +77,7 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined;
);
}
} else {
console.log("No user .npmrc file found, creating one");
core.info("No user .npmrc file found, creating one");
fs.writeFileSync(
userNpmrcPath,
`//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}\n`
Expand All @@ -100,7 +100,7 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined;
return;
}
case hasChangesets && !hasNonEmptyChangesets:
console.log("All changesets are empty; not creating PR");
core.info("All changesets are empty; not creating PR");
return;
case hasChangesets:
const { pullRequestNumber } = await runVersion({
Expand All @@ -116,6 +116,6 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined;
return;
}
})().catch((err) => {
console.error(err);
core.error(err);
core.setFailed(err.message);
});
15 changes: 8 additions & 7 deletions src/run.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { exec, getExecOutput } from "@actions/exec";
import { GitHub, getOctokitOptions } from "@actions/github/lib/utils";
import * as github from "@actions/github";
import * as core from "@actions/core";
import fs from "fs-extra";
import { getPackages, Package } from "@manypkg/get-packages";
import path from "path";
Expand Down Expand Up @@ -86,12 +87,12 @@ export async function runPublish({
getOctokitOptions(githubToken, {
throttle: {
onRateLimit: (retryAfter, options: any, octokit, retryCount) => {
console.log(
core.warning(
`Request quota exhausted for request ${options.method} ${options.url}`
);

if (retryCount <= 2) {
console.log(`Retrying after ${retryAfter} seconds!`);
core.info(`Retrying after ${retryAfter} seconds!`);
return true;
}
},
Expand All @@ -101,12 +102,12 @@ export async function runPublish({
octokit,
retryCount
) => {
console.log(
core.warning(
`SecondaryRateLimit detected for request ${options.method} ${options.url}`
);

if (retryCount <= 2) {
console.log(`Retrying after ${retryAfter} seconds!`);
core.info(`Retrying after ${retryAfter} seconds!`);
return true;
}
},
Expand Down Expand Up @@ -360,7 +361,7 @@ export async function runVersion({
await gitUtils.push(versionBranch, { force: true });

let searchResult = await searchResultPromise;
console.log(JSON.stringify(searchResult.data, null, 2));
core.info(JSON.stringify(searchResult.data, null, 2));

const changedPackagesInfo = (await changedPackagesInfoPromises)
.filter((x) => x)
Expand All @@ -375,7 +376,7 @@ export async function runVersion({
});

if (searchResult.data.items.length === 0) {
console.log("creating pull request");
core.info("creating pull request");
const { data: newPullRequest } = await octokit.rest.pulls.create({
base: branch,
head: versionBranch,
Expand All @@ -390,7 +391,7 @@ export async function runVersion({
} else {
const [pullRequest] = searchResult.data.items;

console.log(`updating found pull request #${pullRequest.number}`);
core.info(`updating found pull request #${pullRequest.number}`);
await octokit.rest.pulls.update({
pull_number: pullRequest.number,
title: finalPrTitle,
Expand Down

0 comments on commit 8b28186

Please sign in to comment.