-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (74 loc) · 2.41 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
const core = require('@actions/core');
const github = require('@actions/github');
const token = core.getInput("gh-token", { required: true });
const errorMessage = core.getInput("error-message", { required: false });
const client = github.getOctokit(token);
// https://api.rubyonrails.org/v5.2.1/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-rename_table_indexes
const alterTableMethods = [
'add_column',
'add_foreign_key',
'add_index',
'add_reference',
'add_timestamps',
'change_column',
'change_column_default',
'change_column_null',
'change_table',
'rename_column',
'rename_index',
'rename_table',
'remove_column',
'remove_columns',
'rename_column_indexes',
'remove_foreign_key',
'remove_index',
'remove_index',
'remove_reference',
'remove_timestamps',
'rename_table_indexes'
]
function getPrNumber() {
const pullRequest = github.context.payload.pull_request;
if (!pullRequest) {
return undefined;
}
return pullRequest.number;
}
async function getChangedFiles(client, prNumber) {
const listFilesOptions = client.rest.pulls.listFiles.endpoint.merge({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
pull_number: prNumber,
});
const listFilesResponse = await client.paginate(listFilesOptions);
let changedFiles = listFilesResponse.map((f) => f.filename);
// "db/migration" or "db/other_db_migration"
changedFiles = changedFiles.filter(file => file.match(/db\/.*migrate\//))
core.notice(`Migration files:\n` + changedFiles.join('\n'));
return changedFiles;
}
async function fetchContent(
client,
repoPath
) {
const response = await client.rest.repos.getContent({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
path: repoPath,
ref: github.context.sha,
});
return Buffer.from(response.data.content, response.data.encoding).toString();
}
async function detectAlterTable() {
const filesPath = await getChangedFiles(client, getPrNumber())
filesPath.find(file => {
fetchContent(client, file).then(content => {
alterTableMethods.forEach(method => {
if(content.includes(method)) {
core.setFailed(`${errorMessage}: '${method}' in '${file}'`);
}
})
})
})
}
detectAlterTable();