Skip to content
This repository has been archived by the owner on Feb 5, 2021. It is now read-only.

Commit

Permalink
Merge pull request #15 from etcdigital/v2
Browse files Browse the repository at this point in the history
V2
  • Loading branch information
etc-tiago authored Jul 10, 2020
2 parents 0d2ebb5 + d5ce47f commit f229a8c
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 108 deletions.
25 changes: 25 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "daily"
assignees:
- "etc-tiago"
reviewers:
- "etc-tiago"
labels:
- "dependencies"

- package-ecosystem: "npm"
package_manager: "javascript"
directory: "/"
open-pull-requests-limit: 1
schedule:
interval: "daily"
assignees:
- "etc-tiago"
reviewers:
- "etc-tiago"
labels:
- "dependencies"
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pull-request-changelog",
"version": "1.2.1",
"version": "2.0.0",
"description": "Github action that automatically generates changelog when PR is opened/updated",
"main": "dist/index.js",
"scripts": {
Expand Down
8 changes: 8 additions & 0 deletions src/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
gitPrume:
"git fetch --no-tags --prune origin +refs/pull/*/head:refs/remotes/origin/pr/*",
gitNoTag: "git fetch --no-tags origin +refs/heads/*:refs/remotes/origin/*",
getCommits: (pullRequestId) =>
`git log --no-merges origin/pr/${pullRequestId} ^origin/master --pretty=oneline --no-abbrev-commit`,
changeFiles: (sha) => `git diff-tree --no-commit-id --name-only -r ${sha}`,
};
49 changes: 38 additions & 11 deletions src/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const headers = {
"feat:": "feat",
"fix:": "fix",
"docs:": "docs",
"ci:": "ci",
"test:": "test",
"refactor:": "refactor",
};

const prepareCommit = (str) => {
Expand All @@ -18,7 +21,7 @@ const prepareCommit = (str) => {
return { prefix: "", message: str };
}
const { prefix, scope } = getScope(str.substr(0, dotsIndex + 1));
const message = str.substr(dotsIndex + 2);
const message = str.substr(dotsIndex + 1).trim();

return { prefix, message, scope };
};
Expand Down Expand Up @@ -53,21 +56,18 @@ const getHeader = (prefix) => {
return changesHeader;
};

const commitUrl = (hash) => `${PR_URL}/commits/${hash}`;
const prepareOutput = (sha, contentLine) => {
const messageLine = contentLine.message;
const filesLine = contentLine.files;

const prepareOutput = (line) => {
// Get Hash, prefix and message
const hash = line.substr(0, 40);
const { prefix, scope, message } = prepareCommit(line.substr(41));
const { prefix, scope, message } = prepareCommit(messageLine);

// Check if commit has a valid message
if (!prefix && !message) {
return;
}

// Create a hash link
const hashLink = `([${hash.substr(0, 7)}](${commitUrl(hash)}))`;

// Prepare
const h = getHeader(prefix);
if (!changes[h]) {
Expand All @@ -76,10 +76,16 @@ const prepareOutput = (line) => {

const prefixBold = prefix ? `**${prefix}** ` : "";

const changedFiles = filesLine.map((file) => `- ${file}`);

const showPrefix = h === changesHeader ? prefixBold : "";
changes[h].push({
scope: scope || "no-scope",
message: `- ${showPrefix}${message} ${hashLink}`,
message: `<details>
<summary>${sha.substr(0, 7)} - ${showPrefix}${message}</summary>
${breakline}#### Changed files${breakline}
${changedFiles.join("\n")}
</details>`,
});
};

Expand All @@ -105,7 +111,7 @@ const showList = (topic) => {

module.exports = function MakeTemplate(commits, pullRequestUrl = "") {
PR_URL = pullRequestUrl;
commits.split("\n").forEach(prepareOutput);
Object.keys(commits).forEach((sha) => prepareOutput(sha, commits[sha]));

let changesTemplate = "";

Expand All @@ -118,7 +124,7 @@ module.exports = function MakeTemplate(commits, pullRequestUrl = "") {

const separator = () => {
if (changesTemplate) {
changesTemplate += `${breakline}---${breakline}`;
changesTemplate += `${breakline}${breakline}`;
}
};

Expand All @@ -136,6 +142,27 @@ module.exports = function MakeTemplate(commits, pullRequestUrl = "") {
changesTemplate += showList("fix");
}

if (changes["ci"]) {
separator();
doubleBreakline();
changesTemplate += `## 🏗 CI${breakline}`;
changesTemplate += showList("fix");
}

if (changes["test"]) {
separator();
doubleBreakline();
changesTemplate += `## 🧪 Tests${breakline}`;
changesTemplate += showList("fix");
}

if (changes["refactor"]) {
separator();
doubleBreakline();
changesTemplate += `## ♻️ Refactors${breakline}`;
changesTemplate += showList("fix");
}

if (changes[changesHeader]) {
separator();
doubleBreakline();
Expand Down
67 changes: 49 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const exec = require("@actions/exec");
const github = require("@actions/github");
const core = require("@actions/core");
const makeTemplate = require("./converter");
const { gitNoTag, changeFiles, getCommits, gitPrume } = require("./commands");

const pull_request = github.context.payload.pull_request;
const PR_ID = pull_request.number;
Expand All @@ -26,12 +27,6 @@ const postToGit = async (url, key, body) => {
return content;
};

const gitPrume =
"git fetch --no-tags --prune origin +refs/pull/*/head:refs/remotes/origin/pr/*";
const gitNoTag =
"git fetch --no-tags origin +refs/heads/*:refs/remotes/origin/*";
const getCommits = `git log --no-merges origin/pr/${PR_ID} ^origin/master --pretty=oneline --no-abbrev-commit`;

/**
* Action core
*/
Expand All @@ -46,28 +41,64 @@ const getCommits = `git log --no-merges origin/pr/${PR_ID} ^origin/master --pret
await exec.exec(gitNoTag);

// then we fetch the diff and grab the output
let commits = "";
let commits = {};
let commitsStr = "";
let myError = "";
const options = {};
options.listeners = {
stdout: (data) => {
commits = `${commits}${data.toString()}`;
},
stderr: (data) => {
myError = `${myError}${data.toString()}`;
},
};

// get diff between master and current branch
await exec.exec(getCommits, [], options);
await exec.exec(getCommits(PR_ID), [], {
listeners: {
stdout: (data) => {
const splitted = data.toString().split("\n");
splitted.forEach((item) => {
if (item === "") {
return;
}
const sha = item.substr(0, 40);
if (sha === "") {
return;
}
const message = item.substr(41);
commits[sha] = { message };
});

// remove
commitsStr = `${commitsStr}${data.toString()}`;
},
stderr: (data) => {
myError = `${myError}${data.toString()}`;
},
},
});

// If there were errors, we throw it
if (myError !== "") {
throw new Error(myError);
}

const shaKeys = Object.keys(commits).map(
(sha) =>
new Promise((resolve, reject) => {
exec.exec(changeFiles(sha), [], {
listeners: {
stdout: (data) => {
commits[sha].files = data
.toString()
.split("\n")
.filter((i) => i);
resolve();
},
stderr: (data) => {
myError = `${myError}${data.toString()}`;
},
},
});
})
);

await Promise.all(shaKeys);

await postToGit(URL, GITHUB_TOKEN, makeTemplate(commits, PR_URL));
console.log("Changelog successfully posted");
} catch (e) {
console.log(e);
process.exit(1);
Expand Down
Loading

0 comments on commit f229a8c

Please sign in to comment.