-
Notifications
You must be signed in to change notification settings - Fork 38
/
build.js
133 lines (113 loc) · 4.34 KB
/
build.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const Parser = require('rss-parser')
const request = require('then-request');
const parser = new Parser({
headers: {
'Accept': 'application/atom+xml'
}
})
const Handlebars = require('handlebars')
const source = require('./templateREADME')
const template = Handlebars.compile(source)
const { Octokit } = require("@octokit/core")
const { createActionAuth } = require("@octokit/auth-action")
const auth = createActionAuth()
/** @type String - URL to the YouTube SAP Tech Bytes Playlist */
const URLYouTube1 = 'https://www.youtube.com/feeds/videos.xml?playlist_id=PL6RpkC85SLQC3HBShmlMaPu_nL--4f20z'
/** @type String - URL to the YouTube SAP 2 Minutes Of Playlist */
const URLYouTube2 = 'https://www.youtube.com/feeds/videos.xml?playlist_id=PL6RpkC85SLQBM78mD6AiJ1vKlSB7OWtUz'
/** @type String - URL to query SAP Community for all Blogposts with tag sap-tech-bytes */
const URLSCN = 'https://content.services.sap.com/feed?type=blogpost&tags=sap-tech-bytes'
const sort_by = (field, reverse, primer) => {
const key = primer ?
(x) => {
return primer(x[field])
} :
(x) => {
return x[field]
}
reverse = !reverse ? 1 : -1
return (a, b) => {
return a = key(a), b = key(b), reverse * ((a > b) - (b > a))
}
}
const branchProcessing = async (item, octokit) => {
let branchDetails = await octokit.request('GET /repos/{owner}/{repo}/branches/{branch}', {
owner: 'SAP-samples',
repo: 'sap-tech-bytes',
branch: item.name
})
item.url = branchDetails.data._links.html
item.authorName = branchDetails.data.commit.author.login
item.authorURL = branchDetails.data.commit.author.html_url
item.authorAvatar = branchDetails.data.commit.author.avatar_url
return Promise.resolve(item)
}
const getBranches = async octokit => {
//Read List of Branches in the SAP Tech Bytes Repo
let branches = await octokit.request('GET /repos/{owner}/{repo}/branches', {
owner: 'SAP-samples',
repo: 'sap-tech-bytes'
})
//Delete the main branch from the list
let indexOfMain = branches.data.indexOf(branches.data.find(obj => {
return obj.name === 'main'
}))
if (indexOfMain !== -1) { branches.data.splice(indexOfMain, 1) }
//Sort by branch name
branches.data.sort(sort_by('name', true, (a) => a.toUpperCase()))
//Return the most recent 6 branches and lookup the details for each branch
branchesNew = await Promise.all(branches.data.slice(0, 6).map(item => branchProcessing(item, octokit)))
return branchesNew
}
const getBlogPosts = async _ => {
//Read List of Blog Posts in the SAP Community Netwwork with tag sap-tech-bytes
const feed = await parser.parseURL(URLSCN)
//Sort by date
feed.items.sort(sort_by('pubDate', true))
//Return the most recent 6 Blog Posts
const items = feed.items.slice(0, 6).map(item => {
item.date = new Date(item.pubDate).toDateString()
return item
})
return items
}
const getYoutubeVideos = async _ => {
//Read List of Videos in the SAP Tech Bytes Playlist on YouTube
const feedNew1 = await parser.parseURL(URLYouTube1)
//Add List of Videos in the "2 Minutes of..." Playlist
const feedNew2 = await parser.parseURL(URLYouTube2)
//Concat Lists of Videos
const feedItems = [...feedNew1.items, ...feedNew2.items]
//Sort by date
feedItems.sort(sort_by('pubDate', true))
//Return the most recent 6 Videos
const itemsNew = feedItems.slice(0, 6).map(item => {
item.date = new Date(item.pubDate).toDateString()
return item
})
return itemsNew
}
const getStaticREADME = async _ => {
const fs = require('fs')
return [readme1, readme2] = await Promise.all([
fs.readFileSync('./README1.md', 'utf8'),
fs.readFileSync('./README2.md', 'utf8')
])
}
const main = async _ => {
try {
const authentication = await auth()
const octokit = new Octokit({ auth: authentication.token })
let [branches, blogPosts, videos, [readme1, readme2]] = await Promise.all([
getBranches(octokit),
getBlogPosts(),
getYoutubeVideos(),
getStaticREADME()
])
console.log(template({ readme1, readme2, blogPosts, videos, branches }))
} catch (error) {
console.log(`${error}`)
process.exit(1)
}
}
main()