feat: create action

This commit is contained in:
Adrian Smijulj 2020-06-11 09:37:37 +02:00
parent 8aaad82c69
commit f92da1bbf8
4 changed files with 7059 additions and 35 deletions

7038
dist/main/index.js vendored

File diff suppressed because it is too large Load diff

7
index.js Normal file
View file

@ -0,0 +1,7 @@
const got = require("got");
got.get("https://api.github.com/repos/doitadrian/contreebutors-action/pulls/2/commits", {
responseType: "json",
}).then((response) => {
console.log(response.body);
});

45
main.ts
View file

@ -1,25 +1,56 @@
const { context } = require("@actions/github"); const { context } = require("@actions/github");
const core = require("@actions/core"); const core = require("@actions/core");
const get = require("lodash.get");
const got = require("got");
const isValidCommitMessage = message => message.match(/^[a-z].*:/); type Commit = {
message: string;
};
const isValidCommitMessage = (message): boolean => message.match(/^[a-z].*:/);
const extractCommits = async (): Promise<Commit[]> => {
// For "push" events, commits can be found in the "context.payload.commits".
const pushCommits = Array.isArray(get(context, "payload.commits"));
if (pushCommits) {
return context.payload.commits;
}
// For PRs, we need to get a list of commits via the GH API:
const prCommitsUrl = typeof get(context, "payload.pull_request.commits_url");
if (prCommitsUrl) {
try {
const { body } = await got.get(prCommitsUrl, {
responseType: "json",
});
if (Array.isArray(body)) {
return body.map((item) => item.commit);
}
return [];
} catch {
return [];
}
}
return [];
};
async function run() { async function run() {
core.info( core.info(
` Checking if commit messages are following the Conventional Commits specification...` ` Checking if commit messages are following the Conventional Commits specification...`
); );
console.log('context', JSON.stringify(context, null, 2)); const extractedCommits = await extractCommits();
if (extractedCommits.length === 0) {
const hasCommits = context.payload && Array.isArray(context.payload.commits);
if (!hasCommits) {
core.info(`No commits to check, skipping...`); core.info(`No commits to check, skipping...`);
return; return;
} }
let hasErrors; let hasErrors;
core.startGroup("Commit messages:"); core.startGroup("Commit messages:");
for (let i = 0; i < context.payload.commits.length; i++) { for (let i = 0; i < extractedCommits.length; i++) {
let commit = context.payload.commits[i]; let commit = extractedCommits[i];
if (isValidCommitMessage(commit.message)) { if (isValidCommitMessage(commit.message)) {
core.info(`${commit.message}`); core.info(`${commit.message}`);
} else { } else {

View file

@ -8,7 +8,9 @@
"dependencies": { "dependencies": {
"@actions/core": "^1.2.3", "@actions/core": "^1.2.3",
"@actions/exec": "^1.0.3", "@actions/exec": "^1.0.3",
"@actions/github": "^2.1.1" "@actions/github": "^2.1.1",
"got": "^11.3.0",
"lodash.get": "^4.4.2"
}, },
"devDependencies": { "devDependencies": {
"@zeit/ncc": "^0.22.0", "@zeit/ncc": "^0.22.0",