-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
195 lines (164 loc) · 3.94 KB
/
index.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const core = require('@actions/core');
const github = require('@actions/github');
const { bumpCdk } = require('bump-cdk');
const { Octokit } = require("@octokit/rest");
interface GithubProps {
actor: string;
token: string;
owner: string;
repo: string;
}
class Github {
constructor(props: GithubProps) {
this.actor = actor;
this.owner = owner;
this.repo = repo;
this.client = new Octokit({
auth: token
});
}
async _getTreeSha(base='master') {
try {
const { data } = await this.client.repos.listCommits({
owner: this.owner,
repo: this.repo,
sha: base,
per_page: 1
});
return data[0].commit.tree.sha
} catch (e) {
return Promise.reject(e)
}
}
async add(file='./package.json') {
try {
const content = fs.readFileSync(file);
const { data } = await this.client.git.createBlob({
owner: this.owner,
repo: this.repo,
content: content.toString()
});
const tree = [
{
path: file,
mode: '100644',
sha: data.sha
}
];
return this.client.git.createTree({
owner: this.owner,
repo: this.repo,
this.treeSha,
tree
});
} catch (e) {
console.error(e)
return Promise.reject(e)
}
}
async commit(message, tree) {
return this.client.git.createCommit({
owner: this.owner,
repo: this.repo,
message,
tree,
parents: [baseRef],
committer: {
name: this.actor
}
});
}
async createBranch(name, sha) {
try {
return this.client.git.createRef({
owner: this.owner,
repo: this.repo,
ref: `refs/heads/${name}`,
sha
});
} catch (e) {
return Promise.reject(e)
}
}
async createPull(branch, base='master', title='Initial Code') {
try {
return this.client.pulls.create({
owner: this.owner,
repo: this.repo,
base,
title,
head: branch
})
} catch (e) {
return Promise.reject(e)
}
}
}
function loadConfig() {
const workingDirectory = core.getInput('working-directory') || process.cwd();
const version = core.getInput('version') || undefined;
const debug = core.getInput('debug') || false;
const branch = core.getInput('branch-name') || `bump-cdk/${version}`;
const token = core.getInput('github-token');
const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/';
const actor = process.env.GITHUB_ACTOR;
const eventType = process.env.GITHUB_EVENT_NAME;
return {
workingDirectory,
version,
debug,
branch,
token,
owner,
repo,
actor,
eventType
}
}
async function run() {
try {
const {
workingDirectory,
version,
debug,
branch,
token,
owner,
repo,
actor,
eventType
} = loadConfig();
const github = new Github({
token,
owner,
repo,
actor
});
const { hasChanges } = await bumpCdk(workingDirectory, version, false, debug);
if (!hasChanges) {
console.log('done');
return;
}
// add changes
const { data: { sha } } = await git.add(`${workingDirectory}/package.json`);
// commit changes
const { data: { sha: commitSha } } = await git.commit("", sha);
core.setOutput('commitSha', commitSha);
switch(eventType) {
case 'schedule': {
const branchName = `bump-cdk/${version}`;
await git.createBranch(branchName, commitSha);
await git.push(commitSha, `heads/${branchName}`);
const { data: { number: pullNumber } } = await git.createPull(branchName);
core.setOutput('pullNumber', pullNumber);
break;
}
default: {
}
}
core.setOutput('version', version);
} catch (error) {
core.setFailed(error.message);
}
}
run();