-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.ts
222 lines (181 loc) · 7.05 KB
/
main.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright 2020-2021 Cristian Greco
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as core from '@actions/core';
import * as git from '../git/git-cmds';
import * as gitAuth from '../git/git-auth';
import * as store from '../store';
import {commit} from '../git/git-commit';
import {createWrapperInfo} from '../wrapperInfo';
import {createWrapperUpdater} from '../wrapperUpdater';
import {findWrapperPropertiesFiles} from '../wrapper/find';
import {GitHubOps} from '../github/gh-ops';
import {IGitHubApi} from '../github/gh-api';
import {Inputs} from '../inputs';
import {Releases} from '../releases';
export class MainAction {
private inputs: Inputs;
private githubApi: IGitHubApi;
private githubOps: GitHubOps;
private releases: Releases;
constructor(
inputs: Inputs,
githubApi: IGitHubApi,
githubOps: GitHubOps,
releases: Releases
) {
this.inputs = inputs;
this.githubApi = githubApi;
this.githubOps = githubOps;
this.releases = releases;
}
async run() {
try {
store.setMainActionExecuted();
core.debug(JSON.stringify(process.env, null, 2));
await gitAuth.setup(this.inputs);
const releaseChannel = this.inputs.releaseChannel;
const targetRelease = await this.releases.fetchReleaseInformation(
releaseChannel
);
core.info(
`Latest release: ${targetRelease.version} (channel ${releaseChannel})`
);
const ref = await this.githubOps.findMatchingRef(targetRelease.version);
if (ref) {
core.info('Found an existing ref, stopping here.');
core.debug(`Ref url: ${ref.url}`);
core.debug(`Ref sha: ${ref.object.sha}`);
core.warning(
`A pull request already exists that updates Gradle Wrapper to ${targetRelease.version}.`
);
return;
}
const wrappers = await findWrapperPropertiesFiles(
this.inputs.paths,
this.inputs.pathsIgnore
);
core.debug(`Wrappers: ${JSON.stringify(wrappers, null, 2)}`);
if (!wrappers.length) {
core.warning('Unable to find Gradle Wrapper files in this project.');
return;
}
core.debug(`Wrappers count: ${wrappers.length}`);
const wrapperInfos = wrappers.map(path => createWrapperInfo(path));
const commitDataList: {
files: string[];
targetVersion: string;
sourceVersion: string;
}[] = [];
await git.config('user.name', 'gradle-update-robot');
await git.config('user.email', 'gradle-update-robot@regolo.cc');
const baseBranch =
this.inputs.baseBranch !== ''
? this.inputs.baseBranch
: await this.githubApi.repoDefaultBranch();
core.debug(`Base branch: ${baseBranch}`);
await this.switchBranch(baseBranch);
const currentCommitSha = await git.parseHead();
core.debug(`Head for branch ${baseBranch} is at ${currentCommitSha}`);
core.startGroup('Creating branch');
const branchName = `gradlew-update-${targetRelease.version}`;
await git.checkoutCreateBranch(branchName, currentCommitSha);
core.endGroup();
const distTypes = new Set<string>();
for (const wrapper of wrapperInfos) {
core.startGroup(`Working with Wrapper at: ${wrapper.path}`);
// read current version before updating the wrapper
core.debug(`Current Wrapper version: ${wrapper.version}`);
if (wrapper.version === targetRelease.version) {
core.info(`Wrapper is already up-to-date`);
continue;
}
distTypes.add(wrapper.distType);
const updater = createWrapperUpdater(
wrapper,
targetRelease,
this.inputs.setDistributionChecksum
);
core.startGroup('Updating Wrapper');
await updater.update();
core.endGroup();
core.startGroup('Checking whether any file has been updated');
let modifiedFiles = await git.gitDiffNameOnly();
core.debug(`Modified files count: ${modifiedFiles.length}`);
core.debug(`Modified files list: ${modifiedFiles}`);
core.endGroup();
if (modifiedFiles.length) {
// Running the `wrapper` task a second time ensures that the wrapper jar itself
// and the wrapper scripts get updated if a new version is available (happens infrequently).
// https://docs.gradle.org/current/userguide/gradle_wrapper.html#sec:upgrading_wrapper
core.startGroup('Updating Wrapper (2nd update)');
await updater.update();
modifiedFiles = await git.gitDiffNameOnly();
core.debug(`Modified files count: ${modifiedFiles.length}`);
core.debug(`Modified files list: ${modifiedFiles}`);
core.endGroup();
core.startGroup('Verifying Wrapper');
await updater.verify();
core.endGroup();
core.startGroup('Committing');
await commit(modifiedFiles, targetRelease.version, wrapper.version);
core.endGroup();
commitDataList.push({
files: modifiedFiles,
targetVersion: targetRelease.version,
sourceVersion: wrapper.version
});
} else {
core.info(`Nothing to update for Wrapper at ${wrapper.path}`);
}
core.endGroup();
}
if (!commitDataList.length) {
core.warning(
`✅ Gradle Wrapper is already up-to-date (version ${targetRelease.version})! 👍`
);
return;
}
const changedFilesCount = commitDataList
.map(cd => cd.files.length)
.reduce((acc, item) => acc + item);
core.debug(
`Have added ${commitDataList.length} commits for a total of ${changedFilesCount} files`
);
core.info('Pushing branch');
await git.push(branchName);
core.info('Creating Pull Request');
const pullRequestData = await this.githubOps.createPullRequest(
branchName,
distTypes,
targetRelease,
commitDataList.length === 1
? commitDataList[0].sourceVersion
: undefined
);
core.info(`✅ Created a Pull Request at ${pullRequestData.url} ✨`);
store.setPullRequestData(pullRequestData);
} catch (error) {
// setFailed is fatal (terminates action), core.error
// creates a failure annotation instead
core.setFailed(`❌ ${error instanceof Error && error.message}`);
}
}
private async switchBranch(branchName: string) {
await git.fetch();
const exitCode = await git.checkout(branchName);
if (exitCode !== 0) {
throw new Error(`Invalid base branch ${branchName}`);
}
}
}