-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub-utils.js
119 lines (108 loc) · 3.52 KB
/
github-utils.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
const { readFileSync, readdirSync } = require("fs");
const {basename} = require("path");
const { App } = require("@octokit/app");
const {Octokit} = require("@octokit/rest");
const APP_ID = process.env.APP_ID;
const PRIVATE_KEY = readFileSync(process.env.PRIVATE_KEY_FILE, "utf8");
const app = new App({ id: APP_ID, privateKey: PRIVATE_KEY });
const octokit = new Octokit({
async auth() {
const installationAccessToken = await app.getInstallationAccessToken({
installationId: process.env.INSTALLATION_ID
}).catch((err)=>console.log(err));
// console.log(`token ${installationAccessToken}`);
return `token ${installationAccessToken}`;
}
});
// Commit constants
const REPO = 'codelabs';
const OWNER = 'MOOC-tutorials';
const BASE_PATH = 'codelabs/';
const BASE = 'master';
const BASE_REF = 'heads/master';
const commitFile = async (path, fileContent, baseDir) => {
console.log(path, OWNER, REPO, baseDir);
const base_ref = await octokit.git.getRef({
owner: OWNER,
repo: REPO,
ref: BASE_REF
})
const base_sha = base_ref.data.object.sha;
await octokit.git.createRef({
owner: OWNER,
repo: REPO,
ref: 'refs/heads/' + baseDir,
sha: base_sha
}).catch((err) => {
console.log(err.errors);
//Already a branch exists
});
const existingFile = await octokit.repos.getContents({
owner: OWNER,
repo: REPO,
path,
ref: baseDir
}).catch(async (err) => {
//console.log(err);
const newFile = await octokit.repos.createOrUpdateFile(
{owner: OWNER,
repo: REPO,
branch: baseDir,
path,
content: fileContent,
message: 'Update ' + path,
}).catch((err) =>{
console.log("Fail creation of file");
console.log(err);
});
//return newFile;
});
if(existingFile){
console.log(existingFile.data);
return await octokit.repos.createOrUpdateFile(
{owner: OWNER,
repo: REPO,
branch: baseDir,
path,
content: fileContent,
message: 'Update ' + path,
sha: existingFile.data.sha});
}
}
exports.commitBuild = async (baseDir) => {
baseDir = baseDir.trim();
console.log(baseDir);
// index
const indexPath = BASE_PATH + baseDir + '/index.html';
const indexFile = new Buffer(readFileSync(__dirname+ '/' + indexPath)).toString('base64');
const indexCommit = await commitFile(indexPath, indexFile, baseDir);
console.log(indexPath);
// codelabs.json
const codelabsJsonPath = BASE_PATH + baseDir + '/codelab.json';
const codelabsJsonFile = new Buffer(readFileSync(__dirname+ '/' + codelabsJsonPath)).toString('base64');;
const codelabsJsonCommit = await commitFile(codelabsJsonPath, codelabsJsonFile, baseDir);
console.log(codelabsJsonPath);
// img
const baseImgPath = __dirname + '/' + BASE_PATH + baseDir + '/img/';
const images = readdirSync(baseImgPath);
for (let i = 0; i < images.length; i++) {
const image = images[i];
const imgPath = BASE_PATH + baseDir + '/img/' + image;
const imgFile = new Buffer(readFileSync(baseImgPath + image)).toString('base64');
const imgCommit = await commitFile(imgPath, imgFile, baseDir);
console.log(imgPath);
}
// Open pull request
const pullRequest = await octokit.pulls.create({
owner: OWNER,
repo: REPO,
title: 'Update codelab: ' + baseDir ,
head: baseDir,
base: BASE
}).catch((err) => {
console.log(err.errors);
//Already a PR existis or no changes are detected between the branches (master vs staging)
return {data: {}};
});
console.log(pullRequest.data.url);
};