This commit is contained in:
Aarni Koskela
2022-02-11 16:54:44 +02:00
parent 5c237084f5
commit a4a04f1dac
14 changed files with 172 additions and 224 deletions

View File

@@ -0,0 +1,47 @@
import { GithubApi } from "parse-github-event/lib/types";
import { KKEvent } from "../../data/events";
import githubEvent from "parse-github-event";
import lodashTemplate from "lodash/template";
import defaultTemplateSettings from "lodash/templateSettings";
const isVisibleGithubEvent = ({ type }: GithubApi.GithubEvent) =>
type !== "PushEvent" && type !== "DeleteEvent";
const templateSettings = {
...defaultTemplateSettings,
interpolate: /{{([\s\S]+?)}}/g,
};
function convertGithubEvent(item: GithubApi.GithubEvent): KKEvent {
const parsedEvent = githubEvent.parse(item);
const template = lodashTemplate(parsedEvent?.text, templateSettings, false);
const repository = `https://github.com/${item.repo.name}`;
let branch;
if (item.payload.ref) {
branch = item.payload.ref.replace("refs/heads/", "");
}
const message = template({
repository: `<a target="_blank" href="${repository}">${item.repo.name}</a>`,
branch: branch,
number: item.payload.number,
ref_type: item.payload.ref_type,
ref: item.payload.ref,
});
const url = `https://github.com/${item.actor.login}`;
return {
user: item.actor.login,
userLink: url,
image: item.actor.avatar_url,
imageLink: url,
body: message,
timestamp: new Date(item.created_at),
type: "github",
};
}
export function convertGithubItems(items: readonly GithubApi.GithubEvent[]) {
return items.filter(isVisibleGithubEvent).map(convertGithubEvent);
}

View File

@@ -0,0 +1,35 @@
import { KKEvent } from "../../data/events";
import twitterText from "twitter-text";
export interface TwitterItem {
retweeted?: boolean;
retweeted_status?: TwitterItem;
user: {
screen_name: string;
profile_image_url_https: string;
};
text: string;
created_at: number | string; // TODO: type?
}
function convertTwitterItem(item: TwitterItem): KKEvent {
if (item.retweeted && item.retweeted_status) {
item = item.retweeted_status;
}
const url = `https://twitter.com/${item.user.screen_name}`;
return {
user: `@${item.user.screen_name}`,
userLink: url,
image: item.user.profile_image_url_https,
imageLink: url,
body: twitterText.autoLink(item.text),
timestamp: new Date(item.created_at),
type: "twitter",
};
}
export function convertTwitterItems(items: readonly TwitterItem[]) {
return items.map(convertTwitterItem);
}