-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.ts
90 lines (78 loc) · 3.12 KB
/
main.ts
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
import * as core from '@actions/core';
import * as github from '@actions/github';
import { createReadStream, statSync } from 'fs';
import { basename, join } from 'path';
import { Glob } from 'glob';
import * as readChunk from 'read-chunk';
// FIXME Is it possible to import 'file-type'? e.g.:
// import * as fileType from 'file-type';
const fileType = require('file-type');
export async function run() {
try {
let slug = ['user','repo']
if (github.context.payload.repository && github.context.payload.repository.full_name) {
slug = github.context.payload.repository.full_name.split('/')
}
const owner = slug[0]
const repo = slug[1]
const tag_name = 'tip'
const octokit: github.GitHub = new github.GitHub(core.getInput('token', {required: true}));
core.startGroup('Update existing tip tag...')
const { data: new_tag } = await octokit.git.updateRef({ owner, repo, ref: 'tags/'+tag_name, sha: github.context.sha });
console.log(new_tag);
core.endGroup()
core.startGroup('Get existing tip release...')
const { data: tip_rel } = await octokit.repos.getReleaseByTag({ owner, repo, tag: tag_name });
console.log(tip_rel);
core.endGroup()
// TODO is it possible/better to update the release instead of removing it and creating a new one?
core.startGroup('Remove existing tip release...')
const { data: del } = await octokit.repos.deleteRelease({ owner, repo, release_id: tip_rel.id });
console.log(del);
core.endGroup()
core.startGroup('Create new tip release...')
const { data: release } = await octokit.repos.createRelease({ owner, repo, tag_name, name: 'tip', prerelease: true });
console.log(release);
core.endGroup()
const xcwd = core.getInput('cwd', {required: true});
core.getInput('files', {required: true}).split(/[\r\n]+/).forEach(async function(item){
Glob(item, { cwd: xcwd }, async function (er, files) {
if (er != null) {
core.setFailed(er.message);
throw er;
}
if (!files.length) {
console.log('WARNING! Glob pattern <' + item + '> produced an empty file list');
}
files.forEach(async function(name){
const file = join(xcwd, name);
const stats = statSync(file);
const fsize = stats.size;
let fmime = 'application/octet-stream'
if (fsize >= fileType.minimumBytes) {
// FIXME Can we use some built-in feature instead of depending on 'read-chunk'?
const buffer = readChunk.sync(file, 0, fileType.minimumBytes);
if (fileType(buffer)) {
fmime = fileType(buffer).mime
}
}
console.log('Upload ' + file + ' [size: ' + fsize + ', type:' + fmime + ']...');
await octokit.repos.uploadReleaseAsset({
file: createReadStream(file),
headers: {
'content-length': fsize,
'content-type': fmime,
},
name: basename(file),
url: release.upload_url,
});
});
})
});
}
catch (error) {
core.setFailed(error.message);
throw error;
}
}
run();