Skip to content

Commit

Permalink
Merge pull request #21 from nearform/feature/limiting-commit-message-…
Browse files Browse the repository at this point in the history
…body

Limiting commit messages in issue description
  • Loading branch information
gilach authored May 7, 2021
2 parents 9e3dc79 + 2e52076 commit 269e82a
Show file tree
Hide file tree
Showing 6 changed files with 342 additions and 6 deletions.
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ inputs:
description: 'Number of days of inactivity before a release becomes stale'
required: false
default: 7
commit-messages-lines:
description: 'Limit the number of first x lines from commit messages that will be added in the issue description. No truncation when set to 0.'
required: false
default: 1
runs:
using: 'node12'
main: 'dist/index.js'
17 changes: 15 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6009,10 +6009,22 @@ async function updateLastOpenPendingIssue(token, issueTitle, issueBody, issueNo)
return updatedIssue.data.length ? updatedIssue.data[0] : null;
}

function formatCommitMessage(fullCommitMessage, numberOfLines) {
if (!numberOfLines || numberOfLines < 0) {
return fullCommitMessage;
}
return fullCommitMessage
.split('\n')
.slice(0, numberOfLines)
.join('\n')
.trim();
}

module.exports = {
createIssue,
getLastOpenPendingIssue,
updateLastOpenPendingIssue,
formatCommitMessage,
};


Expand Down Expand Up @@ -6174,12 +6186,13 @@ var __webpack_exports__ = {};
const core = __nccwpck_require__(186);
const { logInfo } = __nccwpck_require__(653);
const { getLatestRelease, getUnreleasedCommits } = __nccwpck_require__(26);
const { createIssue, getLastOpenPendingIssue, updateLastOpenPendingIssue } = __nccwpck_require__(608);
const { createIssue, getLastOpenPendingIssue, updateLastOpenPendingIssue, formatCommitMessage } = __nccwpck_require__(608);

async function run() {
try {
const token = core.getInput('github-token', { required: true });
const staleDays = Number(core.getInput('stale-days'));
const commitMessageLines = Number(core.getInput('commit-messages-lines'));
const latestRelease = await getLatestRelease(token);
const label = 'notify-release';

Expand All @@ -6197,7 +6210,7 @@ Tag:${latestRelease.tag_name}, author:${latestRelease.author.login}`);
);

if (unreleasedCommits.length) {
const commitStr = unreleasedCommits.map((commit) => `Commit: ${commit.commit.message}
const commitStr = unreleasedCommits.map((commit) => `Commit: ${formatCommitMessage(commit.commit.message, commitMessageLines)}
Author: ${commit.commit.author.name}
`).join('');
Expand Down
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
const core = require('@actions/core');
const { logInfo } = require('./log');
const { getLatestRelease, getUnreleasedCommits } = require('./release');
const { createIssue, getLastOpenPendingIssue, updateLastOpenPendingIssue } = require('./utils');
const { createIssue, getLastOpenPendingIssue, updateLastOpenPendingIssue, formatCommitMessage } = require('./utils');

async function run() {
try {
const token = core.getInput('github-token', { required: true });
const staleDays = Number(core.getInput('stale-days'));
const commitMessageLines = Number(core.getInput('commit-messages-lines'));
const latestRelease = await getLatestRelease(token);
const label = 'notify-release';

Expand All @@ -25,7 +26,7 @@ Tag:${latestRelease.tag_name}, author:${latestRelease.author.login}`);
);

if (unreleasedCommits.length) {
const commitStr = unreleasedCommits.map((commit) => `Commit: ${commit.commit.message}
const commitStr = unreleasedCommits.map((commit) => `Commit: ${formatCommitMessage(commit.commit.message, commitMessageLines)}
Author: ${commit.commit.author.name}
`).join('');
Expand Down
12 changes: 12 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,20 @@ async function updateLastOpenPendingIssue(token, issueTitle, issueBody, issueNo)
return updatedIssue.data.length ? updatedIssue.data[0] : null;
}

function formatCommitMessage(fullCommitMessage, numberOfLines) {
if (!numberOfLines || numberOfLines < 0) {
return fullCommitMessage;
}
return fullCommitMessage
.split('\n')
.slice(0, numberOfLines)
.join('\n')
.trim();
}

module.exports = {
createIssue,
getLastOpenPendingIssue,
updateLastOpenPendingIssue,
formatCommitMessage,
};
280 changes: 278 additions & 2 deletions test/testData.js

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions test/util.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const utils = require('../src/utils');

const {
unreleasedCommitsData1,
} = require('./testData');

test('Should limit commit message to only first line', async () => {
const formattedCommitMessage = utils.formatCommitMessage(unreleasedCommitsData1[0].commit.message, 1);
expect(formattedCommitMessage).toStrictEqual('variable changed');
});

test('Should return full commit message', async () => {
const formattedCommitMessage = utils.formatCommitMessage(unreleasedCommitsData1[0].commit.message, 0);
expect(formattedCommitMessage).toStrictEqual(`variable changed
this message has multiple lines
`);
});

test('Should trim start and end of commit message while limiting the lines', async () => {
const formattedCommitMessage = utils.formatCommitMessage(unreleasedCommitsData1[0].commit.message, 5);
expect(formattedCommitMessage).toStrictEqual(`variable changed
this message has multiple lines`);
});

0 comments on commit 269e82a

Please sign in to comment.