Skip to content

Commit

Permalink
fix: resolve implementation for multiple contributors (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
tenshiAMD authored Oct 11, 2022
1 parent fb2d7c7 commit eb44cea
Show file tree
Hide file tree
Showing 11 changed files with 936 additions and 994 deletions.
101 changes: 83 additions & 18 deletions lib/parse-comment.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module.exports = parseComment;

const nlp = require("compromise");

// Types that are valid (multi words must all be lower case)
Expand Down Expand Up @@ -133,12 +131,11 @@ const plugin = {

nlp.plugin(plugin);

function getMatcher(message, action) {
const whoMatched = nlp(message)
.match(`${action} [.]`)
.normalize({
// We cannot use whitespace: true, because that gets rid of trailing hyphens.
whitespace: false, // remove hyphens, newlines, and force one space between words
function findWho(message, action) {
function findWhoSafe(match) {
message = message.replace("-", "#/#"); // workaround (https://github.com/spencermountain/compromise/issues/726)
const whoNormalizeSettings = {
whitespace: true, // remove hyphens, newlines, and force one space between words
case: false, // keep only first-word, and 'entity' titlecasing
numbers: false, // turn 'seven' to '7'
punctuation: true, // remove commas, semicolons - but keep sentence-ending punctuation
Expand All @@ -150,31 +147,79 @@ function getMatcher(message, action) {
plurals: false, // turn "batmobiles" into "batmobile"
verbs: false, // turn all verbs into Infinitive form - "I walked" → "I walk"
honorifics: false, //turn 'Vice Admiral John Smith' to 'John Smith'
})
.data()[0]
.text.replace(/\s/g, "");
};
const matchedSet = nlp(message)
.match(match)
.normalize(whoNormalizeSettings)
.data();

if (matchedSet.length > 0) {
matchedText = matchedSet[0].text;
matchedText = matchedText.replace("#/#", "-");

return matchedText;
}
}

const whoMatchedMention = findWhoSafe(`@[.]`);
// TODO: Unused
// if (whoMatchedMention) {
// return whoMatchedMention;
// }

const whoMatchedByAction = findWhoSafe(`${action} [.]`);
if (whoMatchedByAction) {
return whoMatchedByAction;
}

return whoMatched.startsWith("@") ? whoMatched.substr(1) : whoMatched;
const whoMatchedByFor = findWhoSafe(`[.] for`);
if (whoMatchedByFor) {
return whoMatchedByFor;
}
}

function parseAddComment(message, action) {
const who = getMatcher(message, action);
function parseAddSentence(message, action) {
message = message
.split(",")
.map((e) => e.trim())
.join(", ");

const whoMatched = findWho(message, action);
if (!whoMatched) {
return {
who: undefined,
};
}

const who = whoMatched.startsWith("@") ? whoMatched.substr(1) : whoMatched;

// Contributions
const doc = nlp(message).toLowerCase();
// This is to support multi word 'matches' (although the compromise docs say it supports this *confused*)
// This is to support multi word 'matches' (altho the compromise docs say it supports this *confused*)
Object.entries(contributionTypeMultiWordMapping).forEach(
([multiWordType, singleWordType]) => {
doc.replace(multiWordType, singleWordType);
}
);

const rawContributions = doc
.match("#Contribution")
.data()
.map((data) => {
// This removes whitespace, commas etc
return data.normal;
});

const contributions = doc
.match("#Contribution")
.data()
.map((data) => {
// This removes whitespace, commas etc
return data.normal;
})
.filter((element, index) => {
return rawContributions.indexOf(element) === index;
})
.map((type) => {
if (contributionTypeMappings[type]) return contributionTypeMappings[type];
return type;
Expand All @@ -187,22 +232,42 @@ function parseAddComment(message, action) {
});

return {
action: "add",
who,
contributions,
};
}

function parseAddComment(message, action) {
const contributors = {};

const sentences = nlp(message).sentences();
sentences.forEach(function (sentence) {
const sentenceRaw = sentence.data()[0].text;
const { who, contributions } = parseAddSentence(sentenceRaw, action);

if (who) {
contributors[who] = contributions;
}
});

return {
action: "add",
contributors,
};
}

function parseComment(message) {
const doc = nlp(message);

const action = doc.toLowerCase().match("#Action").normalize().out("string");

if (action === "add") {
if (action.match("add")) {
return parseAddComment(message, action);
}

return {
action: false,
};
}

module.exports = parseComment;
19 changes: 14 additions & 5 deletions lib/process-issue-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ async function processIssueComment({ context, commentReply }) {
const commentBody = context.payload.comment.body;
const repo = context.payload.repository;
const createdBy = context.payload.comment.user;
const { who, action, contributions } = parseComment(commentBody);

const { action, contributors } = parseComment(commentBody);

const log = context.log.child({
who,
action,
contributions,
contributors,
account: repo.owner.id,
accountType: repo.owner.type.toLowerCase(),
accountLogin: repo.owner.login,
Expand All @@ -39,6 +39,16 @@ async function processIssueComment({ context, commentReply }) {
return;
}

for (var contributor in contributors) {
const who = contributor
const contributions = contributors[who]
const branchName = `all-contributors/add-${toSafeGitReferenceName(who)}`;

await triggerActionAdd(context, commentReply, log, who, contributions, branchName)
}
}

async function triggerActionAdd(context, commentReply, log, who, contributions, branchName) {
if (contributions.length === 0) {
log.info("No contributions");
commentReply.reply(
Expand All @@ -48,8 +58,6 @@ async function processIssueComment({ context, commentReply }) {
return;
}

const branchName = `all-contributors/add-${toSafeGitReferenceName(who)}`;

// set up repository instance. Uses branch if it exists, falls back to repository's default branch
const repository = await setupRepository({ context, branchName });

Expand All @@ -58,6 +66,7 @@ async function processIssueComment({ context, commentReply }) {

repository.setSkipCi(config.options.skipCi);

// TODO: Add implement to support single PR
const { pullCreated, user } = await addContributor({
context,
commentReply,
Expand Down
Loading

1 comment on commit eb44cea

@vercel
Copy link

@vercel vercel bot commented on eb44cea Oct 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.