generated from actions/javascript-action
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
181 lines (165 loc) · 4.98 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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
'use strict'
const core = require('@actions/core')
const github = require('@actions/github')
const HashIds = require('hashids/cjs')
// https://stackoverflow.com/questions/18565425/how-do-i-create-an-orphan-branch-from-the-github-api
const ORHPAN_SHA = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'
;(async function run () {
try {
if (github.context.eventName !== 'issues') {
return
}
const token = core.getInput('token')
const client = new github.GitHub(token)
await onIssue(client, github.context.repo, github.context.payload)
} catch (e) {
console.error(e)
core.setFailed(e.message)
}
})()
async function onIssue (client, repo, { action, label, issue }) {
switch (action) {
case 'opened':
// @TODO if opened by committer, just auto lable and close
break
case 'unlabeled':
// @TODO remove link on unlabled
break
case 'labeled': {
if (issue.state !== 'open' || label.name !== 'shorten') {
return
}
const { title, body, number } = issue
const { owner, repo: name } = repo
const [firstLine, secondLine, ...restLines] = body.split('\n')
// Process issue into links
let to
let from
const base = `https://${owner}.github.io`
const urlLine = (line) => new URL(`/${name}/${line || (new HashIds()).encode(number)}`, base)
try {
to = new URL(title)
from = urlLine(firstLine)
} catch (e) {
to = new URL(firstLine)
try {
from = urlLine(secondLine)
} catch (e) {
from = urlLine()
}
}
// Add to index
let ghPagesExists = false
let pagesSha = ORHPAN_SHA
try {
const resp = await client.git.getRef({
...repo,
ref: 'heads/gh-pages'
})
pagesSha = resp.data.object.sha
ghPagesExists = true
} catch (e) { }
// Get index content
let existingIndex = {}
try {
const { data: indexContent } = await client.repos.getContents({
...repo,
path: 'index.json',
ref: 'heads/gh-pages'
})
existingIndex = JSON.parse(Buffer.from(indexContent.content, indexContent.encoding).toString())
} catch (e) {
console.log(e)
// ignore, no index
}
const index = {
...existingIndex,
[from]: to
}
const { data: indexBlob } = await client.git.createBlob({
...repo,
content: Buffer.from(JSON.stringify(index)).toString('base64'),
encoding: 'base64'
})
const { data: pageBlob } = await client.git.createBlob({
...repo,
content: Buffer.from(`<!DOCTYPE html><meta http-equiv="refresh" content="0;url=${to.toString()}">`).toString('base64'),
encoding: 'base64'
})
const { data: noJekyllBlob } = await client.git.createBlob({
...repo,
content: '',
encoding: 'base64'
})
const { data: homeBlob } = await client.git.createBlob({
...repo,
content: Buffer.from(`<!DOCTYPE html><h1>Short!</h1><ul>${Object.keys(index).map((f) => {
return `<li><a href="${index[f]}">${f}</a></li>`
}).join('')}</ul>`).toString('base64'),
encoding: 'base64'
})
// Create the tree
const { data: tree } = await client.git.createTree({
...repo,
tree: [{
sha: noJekyllBlob.sha,
path: '.nojekyll',
mode: '100644',
type: 'blob'
}, {
sha: homeBlob.sha,
path: 'index.html',
mode: '100644',
type: 'blob'
}, {
sha: indexBlob.sha,
path: 'index.json',
mode: '100644',
type: 'blob'
}, {
sha: pageBlob.sha,
path: `${from.pathname.replace(`/${name}/`, '')}/index.html`,
mode: '100644',
type: 'blob'
}],
base_tree: pagesSha
})
// Create our commit
const { data: commit } = await client.git.createCommit({
...repo,
message: `${from} to ${to} (closes #${number})${restLines.length ? ` \n\n${restLines.join('\n')}` : ''}`,
tree: tree.sha,
parents: pagesSha === ORHPAN_SHA ? [] : [pagesSha]
})
// Create or update gh-pages branch if it didn't exist
if (!ghPagesExists) {
await client.git.createRef({
...repo,
ref: 'refs/heads/gh-pages',
sha: commit.sha
})
} else {
await client.git.updateRef({
...repo,
force: true,
ref: 'heads/gh-pages',
sha: commit.sha
})
}
console.log(commit.sha, commit.message)
// Comment on issue
await client.issues.createComment({
...repo,
issue_number: number,
body: `Your shortlink has been created!\n\n${from} now points to ${to}`
})
// Close issue
await client.issues.update({
...repo,
issue_number: number,
state: 'closed'
})
break
}
}
}