-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
64 lines (50 loc) · 2.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
'use strict';
const core = require('@actions/core'),
github = require('@actions/github'),
twitter = require('twitter-text');
if(!github.context.payload.issue && !/\/(?:issue|pull-request)s\/\d+$/.test(github.context.payload.project_card?.content_url)) {
core.info('Not running on an event with an associated issue.');
return;
}
const MAX_WEIGHTED_LENGTH = 280;
function isValidTweetUrl(url) {
return /^https?:\/\/(?:www\.)?twitter.com\/[^/]+\/status\/([0-9]+)\/?$/.test(url);
}
const inputs = {
cardContent: JSON.parse(core.getInput('cardContent')),
failOnValidationError: core.getBooleanInput('failOnValidationError'),
};
async function validate() {
const content = inputs.cardContent;
const errors = [];
if (!content.description) {
errors.push(`No content description provided. Please add a content description section to your issue.`);
}
if (content.hasOwnProperty('replyTo') && (!content.replyTo || !isValidTweetUrl(content.replyTo))) {
errors.push(`The reply section needs to have content and only link a Tweet URL to reply to.`);
}
if (content.hasOwnProperty('repost') && (!content.repost || !isValidTweetUrl(content.repost))) {
errors.push(`Retweets need to have a Tweet URL to retweet in the retweet section.`);
}
if (!content.hasOwnProperty('repost') && !content.content) {
errors.push(`Tweets need to have a content section.`);
}
if (content.hasOwnProperty('content')) {
const pureTweet = content.content.replace(/!\[[^\]]*\]\(([^)]+)\)/g, '');
const parsedTweet = twitter.parseTweet(pureTweet);
if(parsedTweet.weightedLength > MAX_WEIGHTED_LENGTH) {
errors.push(`Tweet content too long by ${parsedTweet.weightedLength - MAX_WEIGHTED_LENGTH} weighted characters.`);
}
}
core.setOutput('validationErrors', JSON.stringify(errors));
if (inputs.failOnValidationError && errors.length > 0) {
const errorMessage = 'Content validations failed';
console.error(errorMessage, errors);
core.setFailed(errorMessage);
return;
}
}
validate().catch((error) => {
console.error(error);
core.setFailed(error.message);
});