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

Adding pagination #26

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ outputs:
jira-keys:
description: 'Jira keys that were found in input string(commit-message) in commad delimited format'
runs:
using: 'node12'
using: 'node16'
main: 'index.js'
197 changes: 114 additions & 83 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,96 +1,127 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core = require('@actions/core');
const github = require('@actions/github');
const core = require("@actions/core");
const github = require("@actions/github");
const matchAll = require("match-all");
const Octokit = require("@octokit/rest");

const os = require("os");
const fs = require("fs");

function setOutput(key, value) {
// Temporary hack until core actions library catches up with github new recommendations
const output = process.env["GITHUB_OUTPUT"];
fs.appendFileSync(output, `${key}=${value}${os.EOL}`);
}

async function extractJiraKeysFromCommit() {
try {
const regex = /((([A-Z]+)|([0-9]+))+-\d+)/g;
const isPullRequest = core.getInput('is-pull-request') == 'true';
// console.log("isPullRequest: " + isPullRequest);
const commitMessage = core.getInput('commit-message');
// console.log("commitMessage: " + commitMessage);
// console.log("core.getInput('parse-all-commits'): " + core.getInput('parse-all-commits'));
const parseAllCommits = core.getInput('parse-all-commits') == 'true';
// console.log("parseAllCommits: " + parseAllCommits);
const payload = github.context.payload;
const token = process.env['GITHUB_TOKEN'];
const octokit = new Octokit({
auth: token,
try {
const regex = /((([A-Z]+)|([0-9]+))+-\d+)/g;
const isPullRequest = core.getInput("is-pull-request") == "true";
// console.log("isPullRequest: " + isPullRequest);
const commitMessage = core.getInput("commit-message");
// console.log("commitMessage: " + commitMessage);
// console.log("core.getInput('parse-all-commits'): " + core.getInput('parse-all-commits'));
const parseAllCommits = core.getInput("parse-all-commits") == "true";
// console.log("parseAllCommits: " + parseAllCommits);
const payload = github.context.payload;
const token = process.env["GITHUB_TOKEN"];
const octokit = new Octokit({
auth: token,
});
if (isPullRequest) {
let resultArr = [];
// console.log("is pull request...");
const owner = payload.repository.owner.login;
const repo = payload.repository.name;
const prNum = payload.number;
let all_data = [];
const page_size = 100;
const max_pages_to_fetch = 10;
const { data } = await octokit.pulls.listCommits({
owner: owner,
repo: repo,
pull_number: prNum,
per_page: page_size,
});

all_data = all_data.concat(data);

if (data.length === page_size) {
for (let i = 2; i < max_pages_to_fetch; i++) {
const result = await octokit.pulls.listCommits({
owner: owner,
repo: repo,
pull_number: prNum,
per_page: page_size,
page: i,
});

all_data = all_data.concat(result.data);
if (result.data.length < page_size) {
break;
}
}
}

all_data.forEach((item) => {
const commit = item.commit;
const matches = matchAll(commit.message, regex).toArray();
matches.forEach((match) => {
if (resultArr.find((element) => element == match)) {
// console.log(match + " is already included in result array");
} else {
// console.log(" adding " + match + " to result array");
resultArr.push(match);
}
});
if (isPullRequest) {
let resultArr = [];
// console.log("is pull request...");
const owner = payload.repository.owner.login;
const repo = payload.repository.name;
const prNum = payload.number;
const { data } = await octokit.pulls.listCommits({
owner: owner,
repo: repo,
pull_number: prNum
});
data.forEach((item) => {
const commit = item.commit;
const matches = matchAll(commit.message, regex).toArray();
matches.forEach((match) => {
if (resultArr.find((element) => element == match)) {
// console.log(match + " is already included in result array");
}
else {
// console.log(" adding " + match + " to result array");
resultArr.push(match);
}
});
});
const result = resultArr.join(",");
setOutput("jira-keys", result);
} else {
// console.log("not a pull request");
if (commitMessage) {
// console.log("commit-message input val provided...");
const matches = matchAll(commitMessage, regex).toArray();
const result = matches.join(",");
setOutput("jira-keys", result);
} else {
// console.log("no commit-message input val provided...");
const payload = github.context.payload;
if (parseAllCommits) {
// console.log("parse-all-commits input val is true");
let resultArr = [];
payload.commits.forEach((commit) => {
const matches = matchAll(commit.message, regex).toArray();
matches.forEach((match) => {
if (resultArr.find((element) => element == match)) {
// console.log(match + " is already included in result array");
} else {
// console.log(" adding " + match + " to result array");
resultArr.push(match);
}
});
const result = resultArr.join(',');
core.setOutput("jira-keys", result);
}
else {
// console.log("not a pull request");
if (commitMessage) {
// console.log("commit-message input val provided...");
const matches = matchAll(commitMessage, regex).toArray();
const result = matches.join(',');
core.setOutput("jira-keys", result);
}
else {
// console.log("no commit-message input val provided...");
const payload = github.context.payload;
if (parseAllCommits) {
// console.log("parse-all-commits input val is true");
let resultArr = [];
payload.commits.forEach((commit) => {
const matches = matchAll(commit.message, regex).toArray();
matches.forEach((match) => {
if (resultArr.find((element) => element == match)) {
// console.log(match + " is already included in result array");
}
else {
// console.log(" adding " + match + " to result array");
resultArr.push(match);
}
});
});
const result = resultArr.join(',');
core.setOutput("jira-keys", result);
}
else {
// console.log("parse-all-commits input val is false");
// console.log("head_commit: ", payload.head_commit);
const matches = matchAll(payload.head_commit.message, regex).toArray();
const result = matches.join(',');
core.setOutput("jira-keys", result);
}
}
});
const result = resultArr.join(",");
setOutput("jira-keys", result);
} else {
// console.log("parse-all-commits input val is false");
// console.log("head_commit: ", payload.head_commit);
const matches = matchAll(
payload.head_commit.message,
regex
).toArray();
const result = matches.join(",");
setOutput("jira-keys", result);
}
}
}
catch (error) {
core.setFailed(error.message);
}
} catch (error) {
core.setFailed(error.message);
}
}
(async function () {
await extractJiraKeysFromCommit();
// console.log("finished extracting jira keys from commit message");
await extractJiraKeysFromCommit();
// console.log("finished extracting jira keys from commit message");
})();
exports.default = extractJiraKeysFromCommit;