forked from projen/projen
/
projen-upgrade.ts
83 lines (71 loc) · 2.46 KB
/
projen-upgrade.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
import { GithubWorkflow } from './github-workflow';
import { NodeProject } from './node-project';
import { StartEntryCategory } from './start';
export interface ProjenUpgradeOptions {
/**
* The secret name which contains a GitHub Access Token with `repo` and
* `workflow` permissions. This token is used to submit the upgrade pull
* request, which will likely include workflow updates.
*
* @default - auto-upgrade is disabled
*/
readonly autoUpgradeSecret?: string;
/**
* Apply labels to the PR. For example, you can add the label "auto-merge",
* which, in-tandem with mergify configuraiton will automatically merge these
* PRs if their build passes.
*
* @default []
*/
readonly labels?: string[];
}
/**
* Checks for new versions of projen and creates a PR with an upgrade change.
*/
export class ProjenUpgrade {
constructor(project: NodeProject, options: ProjenUpgradeOptions = { }) {
const script = 'projen:upgrade';
project.addScript(script,
'yarn upgrade -L projen',
'CI="" yarn projen'); // if CI is defined, projen runs yarn with --frozen-lockfile
project.start?.addEntry(script, {
desc: 'upgrades projen to the latest version',
category: StartEntryCategory.MAINTAIN,
});
if (options.autoUpgradeSecret) {
const workflow = new GithubWorkflow(project, 'ProjenUpgrade');
workflow.on({
schedule: [{ cron: '0 6 * * *' }], // 6am every day
workflow_dispatch: {}, // allow manual triggering
});
const withOptions: Record<string, string> = {
'token': '${{ secrets.' + options.autoUpgradeSecret + ' }}',
'commit-message': 'chore: upgrade projen',
'branch': 'auto/projen-upgrade',
'title': 'chore: upgrade projen',
'body': 'This PR upgrades projen to the latest version',
};
if (options.labels?.length) {
withOptions.labels = options.labels.join(',');
}
workflow.addJobs({
upgrade: {
'runs-on': 'ubuntu-latest',
'steps': [
...project.workflowBootstrapSteps,
// upgrade
{ run: `yarn ${script}` },
// submit a PR
{
name: 'Create Pull Request',
uses: 'peter-evans/create-pull-request@v3',
with: withOptions,
},
],
},
});
} else {
project.addTip('Set `autoUpgradeSecret` to enable automatic projen upgrade pull requests');
}
}
}