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

adds dry-run support and outputs #21

Merged
merged 1 commit into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ steps:
# (Optional) Use conventional commit messages. Default is false.
# See https://www.conventionalcommits.org.
conventional_commits: true
# (Optional) Do not make actual changes on dev.to.
dry_run: false
```

You can use [this template repository](https://github.com/sinedied/devto-github-template) as an example setup.

## Using a custom committer

You can specify who you want to appear in the commits made by this action by adding these environment variables to the action:

```yaml
env:
GIT_COMMITTER_NAME: your_name
Expand Down
9 changes: 9 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,12 @@ inputs:
description: 'Use conventional commits messages'
required: false
default: false
dry_run:
description: 'Do not make actual changes on dev.to'
required: false
default: false
outputs:
result_json:
description: 'Result as a json string'
result_summary_table_json:
description: 'Summary of actions formatted as a table'
32 changes: 29 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import core from '@actions/core';
import { table, getBorderCharacters } from 'table';
import { publishArticles } from './lib/publish.js';

async function run() {
Expand All @@ -8,6 +9,7 @@ async function run() {
const filesGlob = core.getInput('files');
const branch = core.getInput('branch');
const useConventionalCommits = core.getInput('conventional_commits');
const dryRun = core.getBooleanInput('dry_run');

core.setSecret(devtoKey);
core.setSecret(githubToken);
Expand All @@ -18,20 +20,44 @@ async function run() {
githubToken,
filesGlob,
branch,
useConventionalCommits
useConventionalCommits,
dryRun
})
);

await publishArticles({
const output = await publishArticles({
filesGlob,
devtoKey,
githubToken,
branch,
useConventionalCommits
useConventionalCommits,
dryRun
});

core.setOutput('result_json', JSON.stringify(output));
core.setOutput(
'result_summary_table_json',
JSON.stringify({
content: showResultsTable(output)
})
);
} catch (error) {
core.setFailed(error.toString());
}
}

function showResultsTable(results) {
const rows = results.map((r) => [r.status, r.publishedStatus, r.title]);
const usedWidth = 27; // Status columns + padding
const availableWidth = 80;
const maxTitleWidth = Math.max(availableWidth - usedWidth, 8);

return table(rows, {
drawHorizontalLine: () => false,
border: getBorderCharacters('void'),
columnDefault: { paddingLeft: 0, paddingRight: 1 },
columns: { 2: { truncate: maxTitleWidth, width: maxTitleWidth } }
}).slice(0, -1);
}

run();
19 changes: 17 additions & 2 deletions lib/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,24 @@ export async function publishArticles(options) {
devtoKey: options.devtoKey || process.env.DEVTO_TOKEN,
repo,
branch: options.branch,
checkImages: true
checkImages: true,
dryRun: options.dryRun
});

if (!results || results.length === 0) {
return;
return [];
}

const output = results.map((r) => ({
status: r.status,
publishedStatus: r.publishedStatus,
title: r.article.data.title,
id: r.article.data.id
}));

if (options.dryRun) {
console.log('Running in dry run mode, skipping commit step.');
return output;
}

const shouldCommit = results.some(
Expand All @@ -34,4 +47,6 @@ export async function publishArticles(options) {
} else {
console.log('No detected changes, skipping commit step.');
}

return output;
}
28 changes: 9 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"dependencies": {
"@actions/core": "^1.2.6",
"@actions/exec": "^1.0.3",
"@sinedied/devto-cli": "^1.1.0"
"@sinedied/devto-cli": "^1.1.0",
"table": "^6.8.0"
},
"devDependencies": {
"@vercel/ncc": "^0.31.1",
Expand All @@ -55,7 +56,8 @@
},
"prettier": {
"trailingComma": "none",
"bracketSpacing": true
"bracketSpacing": true,
"endOfLine": "auto"
},
"engines": {
"node": "^14.13.1 || >=16.0.0",
Expand Down