-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
166 lines (135 loc) · 4.62 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
const util = require("util");
const core = require("@actions/core");
const yaml = require("js-yaml");
const promiseRetry = require("promise-retry");
const _get = require("lodash.get");
const _set = require("lodash.set");
async function readPath(repo, tree, path) {
const parts = path.split("/");
let cur = tree;
for (const p of parts.slice(0, parts.length - 1)) {
cur = await util.promisify(repo.loadAs)("tree", cur[p].hash);
}
return {
mode: cur[parts[parts.length - 1]].mode,
contents: await util.promisify(repo.loadAs)(
"blob",
cur[parts[parts.length - 1]].hash
),
};
}
async function makeRepo(repoString, token) {
const repo = {};
require("js-github/mixins/github-db")(repo, repoString, token);
require("js-git/mixins/create-tree")(repo);
//require('js-git/mixins/formats')(repo);
return repo;
}
async function getContents(repo, repoPath, ref) {
var commitHash = await util.promisify(repo.readRef)(ref);
var commit = await util.promisify(repo.loadAs)("commit", commitHash);
var tree = await util.promisify(repo.loadAs)("tree", commit.tree);
const { contents, mode } = await readPath(repo, tree, repoPath);
const d = yaml.safeLoadAll(contents);
return { contents: d, mode, commit, commitHash };
}
async function run(callback) {
try {
const retryCount = parseInt(core.getInput("retryCount") ?? 2);
await promiseRetry(
async function (retry) {
const repo = await makeRepo(
core.getInput("repository"),
core.getInput("token")
);
const versionToSet = core.getInput("new-version");
const repoPath = core.getInput("path");
const ref = core.getInput("ref");
if (!versionToSet) {
throw new Error(
"new-version is not set to anything, cannot update git"
);
}
console.log(`Updating ${repoPath} to version ${versionToSet}`);
let contents, mode, commit, commitHash;
try {
({ contents, mode, commit, commitHash } = await getContents(
repo,
repoPath,
ref
));
} catch (err) {
console.log(util.inspect(err));
throw new Error(
`could not read contents of configured gitops file, please make sure ${repoPath} exists!`
);
}
// update the contents with the new data
let newData;
const existingData = _get(contents, core.getInput("field"));
if (existingData.indexOf(":") !== -1) {
newData =
existingData.substr(0, existingData.indexOf(":")) +
":" +
versionToSet;
} else {
newData = versionToSet;
}
if (newData == existingData) {
console.log(
"nothing to commit, deploy is already set on gitops repo."
);
return core.setOutput("commit", commitHash);
}
const newContents = _set(contents, core.getInput("field"), newData);
console.log(newContents);
const newFile = newContents
.map((c) => yaml.safeDump(c, { noArrayIndent: true }))
.join("\n---\n");
const newTree = [
{
path: repoPath,
mode: mode,
content: Buffer.from(newFile),
},
];
newTree.base = commit.tree;
const newTreeHash = await util.promisify(repo.createTree)(newTree);
console.log("created tree with hash:", newTreeHash);
var newCommitHash = await util.promisify(repo.saveAs)("commit", {
tree: newTreeHash,
author: {
name: "GitOps CI",
email: "ci@example.com",
date: { seconds: Math.floor(Date.now() / 1000), offset: 0 },
},
committer: {
name: "GitOps CI",
email: "ci@example.com",
date: { seconds: Math.floor(Date.now() / 1000), offset: 0 },
},
parents: [commitHash],
message: `${
process.env["GITHUB_REPOSITORY"].split("/")[1]
}: new deploy (${versionToSet})`,
});
console.log("created commit with hash:", newCommitHash);
try {
await util.promisify(repo.updateRef)(ref, newCommitHash);
console.log("updated ref");
core.setOutput("commit", newCommitHash);
} catch (error) {
console.log("commit failed, retrying:", error);
return retry();
}
},
{ minTimeout: 1000, retries: retryCount }
);
} catch (error) {
core.setFailed(util.inspect(error));
}
}
if (module === require.main) {
run();
}
module.exports = { readPath, makeRepo, getContents };