|
| 1 | +'use strict'; |
| 2 | +const {Octokit} = require('@octokit/rest'); |
| 3 | +const isEmpty = require('lodash.isempty'); |
| 4 | +const chalk = require('chalk'); |
| 5 | +const nodeEmoji = require('node-emoji'); |
| 6 | +const cleanDeep = require('clean-deep'); |
| 7 | + |
| 8 | +const detectEventType = (type, payload) => { |
| 9 | + let event = {}; |
| 10 | + |
| 11 | + switch (type) { |
| 12 | + case 'WatchEvent': |
| 13 | + event = { |
| 14 | + event: 'starred', |
| 15 | + postfix: '', |
| 16 | + icon: chalk.white(nodeEmoji.get('star2')) |
| 17 | + }; |
| 18 | + break; |
| 19 | + |
| 20 | + case 'ForkEvent': |
| 21 | + event = { |
| 22 | + event: 'forked', |
| 23 | + postfix: '', |
| 24 | + icon: chalk.white(nodeEmoji.get('rocket')) |
| 25 | + }; |
| 26 | + break; |
| 27 | + |
| 28 | + case 'PublicEvent': |
| 29 | + event = { |
| 30 | + event: 'made', |
| 31 | + postfix: 'public', |
| 32 | + icon: chalk.white(nodeEmoji.get('globe_with_meridians')) |
| 33 | + }; |
| 34 | + break; |
| 35 | + |
| 36 | + case 'CreateEvent': |
| 37 | + event = { |
| 38 | + event: 'created', |
| 39 | + postfix: 'a repository', |
| 40 | + icon: chalk.white(nodeEmoji.get('pencil')) |
| 41 | + }; |
| 42 | + break; |
| 43 | + |
| 44 | + case 'IssuesEvent': |
| 45 | + event = { |
| 46 | + event: 'openned', |
| 47 | + postfix: 'an issue in', |
| 48 | + issueNumber: payload.issue.number, |
| 49 | + issueTitle: payload.issue.title, |
| 50 | + icon: chalk.white(nodeEmoji.get('alarm_clock')) |
| 51 | + }; |
| 52 | + break; |
| 53 | + |
| 54 | + case 'IssueCommentEvent': |
| 55 | + event = { |
| 56 | + event: 'commented', |
| 57 | + postfix: 'in an issue', |
| 58 | + issueNumber: payload.issue.number, |
| 59 | + issueTitle: payload.issue.title, |
| 60 | + commentBody: payload.comment.body, |
| 61 | + icon: chalk.white(nodeEmoji.get('speech_balloon')) |
| 62 | + }; |
| 63 | + break; |
| 64 | + |
| 65 | + case 'PushEvent': |
| 66 | + event = { |
| 67 | + event: 'pushed', |
| 68 | + postfix: 'commits to', |
| 69 | + icon: nodeEmoji.get('sparkles') |
| 70 | + }; |
| 71 | + break; |
| 72 | + |
| 73 | + case 'PullRequestEvent': |
| 74 | + event = { |
| 75 | + event: 'opened', |
| 76 | + postfix: 'a pull request in', |
| 77 | + icon: chalk.white(nodeEmoji.get('fire')) |
| 78 | + }; |
| 79 | + break; |
| 80 | + |
| 81 | + default: |
| 82 | + event = {}; |
| 83 | + } |
| 84 | + |
| 85 | + return event; |
| 86 | +}; |
| 87 | + |
| 88 | +module.exports = async options => { |
| 89 | + /** |
| 90 | + * @Todo |
| 91 | + * Adding auth token as argument is supported but not making it available in the cli version for now. |
| 92 | + */ |
| 93 | + const octokit = new Octokit(); |
| 94 | + |
| 95 | + const feedActivites = await octokit.request('GET /users/{username}/received_events', { |
| 96 | + username: options.username, |
| 97 | + page: options.page |
| 98 | + }); |
| 99 | + |
| 100 | + const updatedEvent = feedActivites.data.map(action => { |
| 101 | + let filteredEvents = {}; |
| 102 | + |
| 103 | + if (!isEmpty(action.payload)) { |
| 104 | + filteredEvents = { |
| 105 | + actor: action.actor.display_login, |
| 106 | + repo: action.repo.name, |
| 107 | + action: detectEventType(action.type, action.payload), |
| 108 | + createdAt: action.created_at |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | + return filteredEvents; |
| 113 | + }).reverse(); |
| 114 | + |
| 115 | + return cleanDeep(updatedEvent, {emptyStrings: false}); |
| 116 | +}; |
0 commit comments