-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
90 lines (73 loc) · 2.68 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
const core = require('@actions/core')
const { Octokit } = require('@octokit/core');
const { languagesSupported, base64_encode } = require('./common/utils');
const fetchTopLanguages = require('./top-languages-fetcher')
const fs = require('fs');
const octokit = new Octokit({ auth: core.getInput('gh_token') });
(async () => {
try {
const username = process.env.GITHUB_REPOSITORY.split("/")[0]
const repo = process.env.GITHUB_REPOSITORY.split("/")[1]
console.log("Hello", username, ", the workflow is being deployed in the", repo, "repo")
const getLanguage = await fetchTopLanguages();
console.log("Your top language is", Object.keys(getLanguage)[0])
const lang = Object.keys(getLanguage)[0].toLowerCase()
if(Object.keys(languagesSupported).includes(lang)){
console.log(lang, "is a supported language ✅")
let octoLang = await octokit.request('GET /repos/{owner}/{repo}/contents/{path}', {
owner: 'arndom',
repo: 'octo-my-readme-workflow',
path: `lib/octo-lang/${lang}.png`,
}).then((res)=>{
return res.data.content
}).catch(e => {
console.error("Failed: ", e)
core.setFailed("Failed: ", e.message)
})
let base64str = octoLang;
const getOctoLang = await octokit.request('GET /repos/{owner}/{repo}/contents/{path}', {
owner: username,
repo: repo,
path: 'my-octo-lang.png',
}).then( res => {
return res.data
}
).catch(e => {
console.log("ocoto-lang not in repo 🔔")
})
const sha = getOctoLang !== undefined ? getOctoLang.sha : ''
if(sha === ''){
await octokit.request('PUT /repos/{owner}/{repo}/contents/{path}',{
owner: username,
repo: repo,
path: "my-octo-lang.png",
message: "created/updated octo-lang",
content: base64str,
}).then(()=>{
console.log("octo-lang generated ✅")
}).catch((e) => {
console.error("Failed: ", e)
core.setFailed("Failed: ", e.message)
})
}else{
await octokit.request('PUT /repos/{owner}/{repo}/contents/{path}',{
owner: username,
repo: repo,
path: "my-octo-lang.png",
message: "created/updated octo-lang",
content: base64str,
sha: sha,
}).then(()=>{
console.log("octo-lang generated ✅")
}).catch((e) => {
console.error("Failed: ", e)
core.setFailed("Failed: ", e.message)
})
}
}else{
console.error(lang, "is currently an unsupported language ❌")
}
} catch (error) {
core.setFailed(error.message);
}
})()