-
Notifications
You must be signed in to change notification settings - Fork 28
/
library.js
126 lines (120 loc) · 5.47 KB
/
library.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
const {execute, getDiffInDays, writeDetectionCheck} = require('./util');
const { Octokit } = require("@octokit/rest");
/***
* Code to prevent GitHub from suspending your cronjob based triggers due to repository inactivity.
* This function uses dummy commits to keep the repository alive. If you dont want dummy commits in your git history,
* use the alternative function `APIKeepAliveWorkflow`
* @async
* @param {string} githubToken - Token of the GitHub user to create dummy commit
* @param {string} committerUsername - Username of the GitHub user to create dummy commit
* @param {string} committerEmail - Email id of the GitHub user to create dummy commit
* @param {string} commitMessage - Commit message while doing dummy commit
* @param {number} [timeElapsed=45] - Time elapsed from the last commit to trigger a new automated commit (in days). Set 0 to skip commit date checking. Default: 45
* @param {boolean} [autoPush=false] - Boolean flag to define if the library should automatically push the changes. Default: false
* @param {boolean} [autoWriteCheck=false] - Enables automatic checking of the token for branch protection rules
* @return {Promise<string, Error>} - Promise with success message or failure object
*/
const KeepAliveWorkflow = async (githubToken, committerUsername, committerEmail, commitMessage, timeElapsed = 45, autoPush = false, autoWriteCheck = false) => {
return new Promise(async (resolve, reject) => {
try {
writeDetectionCheck(autoWriteCheck, reject, resolve);
const diffInDays = timeElapsed === 0 ? 0 : await getDiffInDays();
if (diffInDays >= timeElapsed) {
// Do dummy commit if elapsed time is greater than 45 (default) days
await execute('git', [
'config',
'--global',
'user.email',
committerEmail,
]);
await execute('git', [
'remote',
'set-url',
'origin',
`https://x-access-token:${githubToken}@${process.env.GITHUB_SERVER_URL.replace(/^https?:\/\//, '')}/${process.env.GITHUB_REPOSITORY}.git`
]);
await execute('git', [
'config',
'--global',
'user.name',
committerUsername
]);
await execute('git', [
'commit',
'--allow-empty',
'-m',
`${commitMessage}`]);
if (autoPush) {
await execute('git', [
'push',
'origin',
'HEAD']);
}
resolve('Dummy commit created to keep the repository active...');
} else {
resolve(`Nothing to do... (elapsed date: ${diffInDays})`);
}
} catch (e) {
reject(e);
}
});
};
/**
* @typedef APIKeepAliveWorkflowOptions
* @property {string|null} [workflowFile=null] - Action file name to keepalive eg: `test.yaml'. If you omit this parameter,
* the script will automatically figure it out from the current run metadata.
* @property {string} [apiBaseUrl=process.env.GITHUB_API_URL] - API Base url. Change this if you are using GitHub enterprise hosted version.
* @property {number} [timeElapsed=45] - Time elapsed from the last commit to trigger a new automated commit (in days). Set 0 to skip commit date checking. Default: 45
* @property {boolean} [autoWriteCheck=false] - Enables automatic checking of the token for branch protection rules
*/
/***
* Code to prevent GitHub from suspending your cronjob based triggers due to repository inactivity
* This code uses GitHub Actions API to keep the repository alive:
* https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#enable-a-workflow
* @async
* @param {string} githubToken - Token of the GitHub user to call the API. It should have `actions:write` permission set
* @param {APIKeepAliveWorkflowOptions} [options] - Configuration options
* @return {Promise<string, Error>} - Promise with success message or failure object
*/
const APIKeepAliveWorkflow = (githubToken,
{
workflowFile = null,
apiBaseUrl= (
(!!process.env.GITHUB_API_URL) ?
process.env.GITHUB_API_URL : 'https://api.github.com'
),
timeElapsed = 45,
autoWriteCheck = false
} = {}) => {
return new Promise(async (resolve, reject) => {
try {
writeDetectionCheck(autoWriteCheck, reject, resolve);
const diffInDays = timeElapsed === 0 ? 0 : await getDiffInDays();
if (diffInDays >= timeElapsed) {
const octokit = new Octokit({
auth: githubToken,
baseUrl: apiBaseUrl
});
// GITHUB_REPOSITORY=gkr-bot/test-001
const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/');
// GITHUB_WORKFLOW_REF=gkr-bot/test-001/.github/workflows/blog-post-workflow.yml@refs/heads/main
if (!workflowFile) workflowFile = process.env.GITHUB_WORKFLOW_REF.match(/\/([^\/]+)@/)[1];
const response = await octokit.rest.actions.enableWorkflow({
owner,
repo,
workflow_id: workflowFile,
});
response.status.toString() === '204' ? resolve('Kept repo active using the GitHUb API...') :
reject(response);
} else {
resolve(`Nothing to do... (elapsed date: ${diffInDays})`);
}
} catch (e) {
reject(e);
}
});
}
module.exports = {
KeepAliveWorkflow,
APIKeepAliveWorkflow
};