-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
106 lines (87 loc) · 2.94 KB
/
script.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
// @ts-check
import { composeCreatePullRequest } from "octokit-plugin-create-pull-request";
import { getAddCacheToSetupNodeFunction } from "./utils/yaml-parser.js";
const BRANCH_NAME = "add-cache-to-node-workflows";
const PATH = ".github/workflows";
/**
* Check if a filename is a YAML file
*
* @param {string} fileName FileName to be tested
*
* @return {boolean}
*/
const isYamlFile = (fileName) => /\.ya?ml$/.test(fileName);
/**
* Add cache parameter to GitHub Actions using setup-node
*
* @param {import('@octoherd/cli').Octokit} octokit
* @param {import('@octoherd/cli').Repository} repository
* @param {object} options
* @param {string} [options.cache] Select which package manager you want to use for caching
*/
export async function script(octokit, repository, { cache = "npm" }) {
if (repository.archived) {
octokit.log.info(`${repository.html_url} is archived, ignoring.`);
return;
}
// Global variables used throughout the code
const owner = repository.owner.login;
const repo = repository.name;
const defaultBranch = repository.default_branch;
// Get all files from .github/workflows folder
let files;
try {
const { data } = await octokit.request(
"GET /repos/{owner}/{repo}/contents/{path}",
{
owner,
repo,
path: PATH,
}
);
files = data;
} catch (e) {
if (e.status === 404) {
octokit.log.warn(`"${PATH}" path not found in ${repository.full_name}`);
return;
} else {
throw e;
}
}
if (!Array.isArray(files)) {
throw new Error(`"${PATH}" is not a folder in ${repository.full_name}`);
}
const workflowFiles = files.filter((file) => isYamlFile(file.name));
const filesToEdit = {};
workflowFiles.forEach((element) => {
filesToEdit[element.path] = getAddCacheToSetupNodeFunction(cache)
});
const prCreated = await composeCreatePullRequest(octokit, {
owner,
repo,
title: "ci(workflow): add cache to workflows using actions/setup-node",
body: `## Description
Add \`cache\` to workflows using \`actions/setup-node\`
## Context
\`setup-node\` GitHub Action just released a new option to add cache to steps using it.
You can find the details here: https://github.blog/changelog/2021-07-02-github-actions-setup-node-now-supports-dependency-caching/
---
🤖 This PR has been generated automatically by [this octoherd script](https://github.com/oscard0m/octoherd-script-add-cache-to-node-github-action), feel free to run it in your GitHub user/org repositories! 💪🏾
`,
base: defaultBranch,
head: BRANCH_NAME,
createWhenEmpty: false,
changes: [
{
files: filesToEdit,
commit: `ci(workflow): add '${cache}' cache for actions/setup-node in ${PATH}`,
emptyCommit: false,
},
],
});
if (!prCreated) {
octokit.log.warn(`No Pull Request created for ${repository.full_name}`);
return;
}
octokit.log.info(`Pull Request created at ${prCreated.data.html_url}`);
}