This repository has been archived by the owner on May 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
164 lines (138 loc) · 3.64 KB
/
utils.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
const github = require("@actions/github");
const getUtilities = (repoName, token) => {
const octokit = github.getOctokit(token);
const repo =
repoName && repoName.includes("/")
? { owner: repoName.split("/")[0], repo: repoName.split("/")[1] }
: github.context.repo;
console.log(`Init Repo ${repo.owner}/${repo.repo}`);
const createTag = async (objectSha, sprint, releaseNotes) => {
const tag = await octokit.git.createTag({
owner: repo.owner,
repo: repo.repo,
object: objectSha,
message: releaseNotes,
tag: `${sprint}`,
type: "commit",
});
await octokit.git.createRef({
owner: repo.owner,
repo: repo.repo,
sha: tag.data.object.sha,
ref: `refs/tags/${sprint}`,
});
await octokit.repos.createRelease({
owner: repo.owner,
repo: repo.repo,
tag_name: sprint,
name: `Release ${sprint}`,
body: releaseNotes,
});
};
const getDefaultBranch = async () => {
const repository = await octokit.repos.get({
owner: repo.owner,
repo: repo.repo,
});
return repository.data.default_branch;
};
const createNewBranch = async (baseBranch, newBranchName) => {
const newRef = `refs/heads/${newBranchName}`;
const masterBranch = await octokit.git.getRef({
owner: repo.owner,
repo: repo.repo,
ref: `heads/${baseBranch}`,
});
await octokit.git.createRef({
owner: repo.owner,
repo: repo.repo,
ref: newRef,
sha: masterBranch.data.object.sha,
});
};
const mergeBranches = async (baseBranch, headBranch) => {
const merge = await octokit.repos.merge({
owner: repo.owner,
repo: repo.repo,
base: baseBranch,
head: headBranch,
commit_message: `Merging ${headBranch}`,
});
return merge.data;
};
const getFilesThatChanged = async (baseBranc, headBranch) => {
const result = await octokit.repos.compareCommits({
owner: repo.owner,
repo: repo.repo,
base: baseBranc,
head: headBranch,
});
return result.data.files;
};
const createPR = async (baseBranch, headBranch) => {
const pr = await octokit.pulls.create({
owner: repo.owner,
repo: repo.repo,
head: headBranch,
base: baseBranch,
title: headBranch,
});
return pr.data;
};
const mergePR = async (prNumber) => {
const merge = await octokit.pulls.merge({
owner: repo.owner,
repo: repo.repo,
pull_number: prNumber,
});
return merge.data;
};
const closePR = (prNumber) =>
octokit.pulls.update({
owner: repo.owner,
repo: repo.repo,
pull_number: prNumber,
state: "closed",
});
const deleteBranch = (branchName) =>
octokit.git.deleteRef({
owner: repo.owner,
repo: repo.repo,
ref: `heads/${branchName}`,
});
const getContent = async (branch, path) => {
console.log(`Fetching ${path}`);
const ref = `refs/heads/${branch}`;
const packageJson = await octokit.repos.getContent({
owner: repo.owner,
repo: repo.repo,
path: path,
ref: ref,
});
return packageJson.data;
};
const commitContent = (path, message, content, sha, branch) =>
octokit.repos.createOrUpdateFileContents({
owner: repo.owner,
repo: repo.repo,
path: path,
message: message,
content: content,
sha: sha,
branch: branch,
});
return {
createTag,
getDefaultBranch,
createNewBranch,
mergeBranches,
createPR,
deleteBranch,
getContent,
commitContent,
mergePR,
closePR,
getFilesThatChanged,
};
};
exports.getUtilities = getUtilities;