Skip to content

Commit 5e68751

Browse files
committed
misc
1 parent 42f1087 commit 5e68751

File tree

19 files changed

+1234
-994
lines changed

19 files changed

+1234
-994
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ on:
1111
- main
1212

1313
permissions:
14-
contents: write
15-
pull-requests: write
14+
contents: read
15+
pull-requests: none
16+
#pull-requests: write
1617

1718
jobs:
1819
test-action:

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ requests and creating releases.
124124
for `pull-requests` must be set to `"write"`.
125125
- Additionally, the workflow permission for `contents` must also be set to `"write"` to allow the action to create tags
126126
and releases.
127-
- For security considerations and best practices when using the `github_token` or PATs, please refer to the
127+
- For security considerations and best practices when using the `github_token`, please refer to the
128128
[Security Documentation](./security.md).
129129
- Ensure the **Restrict editing to users in teams with push access only** setting is enabled for public repositories, as
130130
the GitHub Actions Bot can write to the wiki by default.
@@ -190,7 +190,7 @@ by Piotr Krukowski.
190190
## Notes
191191

192192
- This action uses [Conventional Commits](https://www.conventionalcommits.org/) to automatically determine the release
193-
type _(major, minor, or patch)_ based on commit messages.
193+
type _(major, minor, or patch)_ based on commit messages. This behavior is configurable via [inputs](#optional-inputs).
194194
- Versioning is done using [Semantic Versioning (SemVer)](https://semver.org/), which provides a clear and consistent
195195
way to manage module versions.
196196
- Commit messages are linked to the respective Terraform directories _(handling PRs that may have separate modules and

action.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ inputs:
2424
description: Specifies the default tag version. (Should be in format v#.#.#)
2525
required: true
2626
default: v1.0.0
27-
github_token:
28-
description: Use this if you wish to use a different GitHub token than the one provided by the workflow.
29-
required: true
30-
default: ${{ github.token }}default
3127
terraform-docs-version:
3228
description: >
3329
Specifies the terraform-docs version used to generate documentation for the wiki.
@@ -55,7 +51,12 @@ inputs:
5551
Adjust this value to control the visibility of changelog entries in the module sidebar.
5652
required: true
5753
default: "5"
58-
54+
github_token:
55+
description: |
56+
Required for retrieving pull request metadata, tags, releases, updating PR comments, wiki, and creating tags/releases.
57+
Automatically injected for convenience; no need to provide a custom token unless you have specific requirements.
58+
required: true
59+
default: ${{ github.token }}
5960
runs:
6061
using: node20
6162
main: dist/index.js

dist/index.js

Lines changed: 426 additions & 343 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/sourcemap-register.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"@actions/core": "^1.10.1",
5252
"@actions/github": "6.0.0",
5353
"@octokit/request-error": "^6.1.5",
54+
"@octokit/request-error": "^6.1.5",
5455
"p-limit": "^6.1.0"
5556
},
5657
"devDependencies": {

src/changelog.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { config } from './config';
1+
import { context } from './context';
22
import type { TerraformChangedModule, TerraformModule } from './terraform-module';
33

44
/**
@@ -29,7 +29,7 @@ const createModuleChangelogEntry = (heading: string, commits: string[]): string
2929
*/
3030
export const getPullRequestChangelog = (terraformChangedModules: TerraformChangedModule[]): string => {
3131
const pullRequestChangelog: string[] = [];
32-
const { prNumber, prTitle } = config;
32+
const { prNumber, prTitle } = context;
3333

3434
for (const { nextTag, commitMessages } of terraformChangedModules) {
3535
const cleanedCommitMessages = commitMessages.map((commitMessage) => {
@@ -56,7 +56,7 @@ export const getPullRequestChangelog = (terraformChangedModules: TerraformChange
5656
* @returns {string} The content of the module's changelog.
5757
*/
5858
export const getModuleChangelog = (terraformChangedModule: TerraformChangedModule): string => {
59-
const { prNumber, prTitle, repoUrl } = config;
59+
const { prNumber, prTitle, repoUrl } = context;
6060
const { nextTagVersion, commitMessages } = terraformChangedModule;
6161

6262
const cleanedCommitMessages = commitMessages.map((commitMessage) => {

src/config.ts

Lines changed: 53 additions & 193 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import { getInput } from '@actions/core';
2-
import { context } from '@actions/github';
1+
import { endGroup, getInput, info, startGroup } from '@actions/core';
32

43
/**
5-
* Configuration interface used for defining key properties related to a GitHub Action that processes Terraform modules.
6-
* This interface captures settings such as keywords for semantic versioning, GitHub-specific tokens and events,
7-
* and directory paths. It also provides helper fields for working with pull request (PR) data.
4+
* Configuration interface used for defining key GitHub Action input configuration.
85
*/
9-
interface ConfigInterface {
6+
interface Config {
107
/**
118
* List of keywords to identify major changes (e.g., breaking changes).
129
* These keywords are used to trigger a major version bump in semantic versioning.
@@ -37,46 +34,10 @@ interface ConfigInterface {
3734
terraformDocsVersion: string;
3835

3936
/**
40-
* GitHub token used to authenticate API requests. Can either be the default GitHub Actions token
41-
* or a personal access token (PAT) with the appropriate scopes.
42-
*/
43-
githubToken: string;
44-
45-
/**
46-
* Flag to indicate whether the default GitHub Actions token is being used. This token has limited permissions,
47-
* and some actions (e.g., PR reads and writes) may require additional permissions.
48-
*/
49-
isDefaultGithubActionsToken: boolean;
50-
51-
/**
52-
* The pull request (PR) number associated with the current GitHub Action. Used to fetch and interact with PR data.
53-
*/
54-
prNumber: number;
55-
56-
/**
57-
* The title of the pull request. This field is extracted for convenience since it is commonly referenced.
58-
*/
59-
prTitle: string;
60-
61-
/**
62-
* The message/description of the pull request. Similar to `prTitle`, this field is used in multiple locations.
63-
*/
64-
prBody: string;
65-
66-
/**
67-
* Flag to indicate if the current event is a pull request merge event.
68-
*/
69-
isPrMergeEvent: boolean;
70-
71-
/**
72-
* The root directory of the GitHub Action's workspace. This directory contains the repository files being processed.
73-
*/
74-
workspaceDir: string;
75-
76-
/**
77-
* The GitHub server URL associated with this repository
37+
* Whether to delete legacy tags (tags that do not follow the semantic versioning format or from
38+
* modules that have been since removed) from the repository.
7839
*/
79-
repoUrl: string;
40+
deleteLegacyTags: boolean;
8041

8142
/**
8243
* Whether to disable wiki generation for Terraform modules.
@@ -90,156 +51,55 @@ interface ConfigInterface {
9051
wikiSidebarChangelogMax: number;
9152

9253
/**
93-
* Whether to delete legacy tags (tags that do not follow the semantic versioning format or from
94-
* modules that have been since removed) from the repository.
54+
* The GitHub token (`GITHUB_TOKEN`) used for API authentication.
55+
* This token is required to make secure API requests to GitHub during the action.
9556
*/
96-
deleteLegacyTags: boolean;
57+
githubToken: string;
9758
}
9859

99-
class Config {
100-
private _majorKeywords!: string[];
101-
private _minorKeywords!: string[];
102-
private _patchKeywords!: string[];
103-
private _defaultFirstTag!: string;
104-
private _terraformDocsVersion!: string;
105-
private _githubToken!: string;
106-
private _isDefaultGithubActionsToken!: boolean;
107-
private _prNumber!: number;
108-
private _prTitle!: string;
109-
private _prBody!: string;
110-
private _isPrMergeEvent!: boolean;
111-
private _workspaceDir!: string;
112-
private _repoUrl!: string;
113-
private _disableWiki!: boolean;
114-
private _wikiSidebarChangelogMax!: number;
115-
private _deleteLegacyTags!: boolean;
116-
117-
constructor() {
118-
this.init();
119-
}
120-
121-
private init() {
122-
// Function to split keywords
123-
const getKeywords = (inputName: string): string[] => {
124-
return getInput(inputName, { required: true }).split(',');
125-
};
126-
127-
this._majorKeywords = getKeywords('major-keywords');
128-
this._minorKeywords = getKeywords('minor-keywords');
129-
this._patchKeywords = getKeywords('patch-keywords');
130-
131-
let githubToken = getInput('github_token', { required: true });
132-
133-
// Determine if it's the default GitHub Actions token and remove "default" suffix
134-
this._isDefaultGithubActionsToken = githubToken.endsWith('default');
135-
if (this._isDefaultGithubActionsToken) {
136-
githubToken = githubToken.slice(0, -7); // Remove the "default" suffix
137-
}
138-
139-
this._githubToken = githubToken;
140-
this._defaultFirstTag = getInput('default-first-tag', { required: true });
141-
this._terraformDocsVersion = getInput('terraform-docs-version', { required: true });
142-
143-
const workspaceDir = process.env.GITHUB_WORKSPACE;
144-
if (!workspaceDir) {
145-
throw new Error('GITHUB_WORKSPACE is not defined');
146-
}
147-
this._workspaceDir = workspaceDir;
148-
149-
const prNumber = context.payload.pull_request?.number;
150-
if (prNumber === undefined) {
151-
throw new Error(
152-
'Pull Request Number is not defined. Ensure this workflow is being run in the context of a pull request',
153-
);
154-
}
155-
this._prNumber = prNumber;
156-
157-
this._prTitle = context.payload.pull_request?.title.trim() || '';
158-
this._prBody = context.payload.pull_request?.body || '';
159-
160-
this._isPrMergeEvent =
161-
(context.eventName === 'pull_request' &&
162-
context.payload.action === 'closed' &&
163-
context.payload.pull_request?.merged) ||
164-
false;
165-
166-
this._repoUrl = this.getGithubRepoUrl();
167-
this._disableWiki = getInput('disable-wiki', { required: true }).toLowerCase() === 'true';
168-
this._wikiSidebarChangelogMax = Number.parseInt(getInput('wiki-sidebar-changelog-max', { required: true }), 10);
169-
this._deleteLegacyTags = getInput('delete-legacy-tags', { required: true }).toLowerCase() === 'true';
170-
}
171-
172-
private getGithubRepoUrl() {
173-
const { owner, repo } = context.repo; // Get the repository owner and name
174-
const serverUrl = context.serverUrl; // Get the server URL
175-
return `${serverUrl}/${owner}/${repo}`; // Construct the full repo URL
176-
}
60+
// The config object will be initialized lazily
61+
let configInstance: Config | null = null;
17762

178-
get majorKeywords(): string[] {
179-
return this._majorKeywords;
180-
}
63+
// Function to split keywords
64+
const getKeywords = (inputName: string): string[] => {
65+
return getInput(inputName, { required: true }).split(',');
66+
};
18167

182-
get minorKeywords(): string[] {
183-
return this._minorKeywords;
184-
}
185-
186-
get patchKeywords(): string[] {
187-
return this._patchKeywords;
188-
}
189-
190-
get defaultFirstTag(): string {
191-
return this._defaultFirstTag;
192-
}
193-
194-
get terraformDocsVersion(): string {
195-
return this._terraformDocsVersion;
196-
}
197-
198-
get githubToken(): string {
199-
return this._githubToken;
200-
}
201-
202-
get isDefaultGithubActionsToken(): boolean {
203-
return this._isDefaultGithubActionsToken;
204-
}
205-
206-
get prNumber(): number {
207-
return this._prNumber;
208-
}
209-
210-
get prTitle(): string {
211-
return this._prTitle;
212-
}
213-
214-
get prBody(): string {
215-
return this._prBody;
216-
}
217-
218-
get isPrMergeEvent(): boolean {
219-
return this._isPrMergeEvent;
220-
}
221-
222-
get workspaceDir(): string {
223-
return this._workspaceDir;
224-
}
225-
226-
get repoUrl(): string {
227-
return this._repoUrl;
228-
}
229-
230-
get disableWiki(): boolean {
231-
return this._disableWiki;
232-
}
233-
234-
get wikiSidebarChangelogMax(): number {
235-
return this._wikiSidebarChangelogMax;
236-
}
237-
238-
get deleteLegacyTags(): boolean {
239-
return this._deleteLegacyTags;
240-
}
241-
}
242-
243-
const config = new Config();
244-
245-
export { config };
68+
/**
69+
* Lazy-initialized configuration object.
70+
*/
71+
const initializeConfig = (): Config => {
72+
if (configInstance) {
73+
return configInstance;
74+
}
75+
76+
startGroup('Initializing Config');
77+
78+
// Initialize the config instance
79+
configInstance = {
80+
majorKeywords: getKeywords('major-keywords'),
81+
minorKeywords: getKeywords('minor-keywords'),
82+
patchKeywords: getKeywords('patch-keywords'),
83+
defaultFirstTag: getInput('default-first-tag', { required: true }),
84+
terraformDocsVersion: getInput('terraform-docs-version', { required: true }),
85+
deleteLegacyTags: getInput('delete-legacy-tags', { required: true }).toLowerCase() === 'true',
86+
disableWiki: getInput('disable-wiki', { required: true }).toLowerCase() === 'true',
87+
wikiSidebarChangelogMax: Number.parseInt(getInput('wiki-sidebar-changelog-max', { required: true }), 10),
88+
githubToken: getInput('GITHUB_TOKEN', { required: true }),
89+
};
90+
91+
info(`Major Keywords: ${configInstance.majorKeywords.join(', ')}`);
92+
info(`Minor Keywords: ${configInstance.minorKeywords.join(', ')}`);
93+
info(`Patch Keywords: ${configInstance.patchKeywords.join(', ')}`);
94+
info(`Default First Tag: ${configInstance.defaultFirstTag}`);
95+
info(`Terraform Docs Version: ${configInstance.terraformDocsVersion}`);
96+
info(`Delete Legacy Tags: ${configInstance.deleteLegacyTags}`);
97+
info(`Disable Wiki: ${configInstance.disableWiki}`);
98+
info(`Wiki Sidebar Changelog Max: ${configInstance.wikiSidebarChangelogMax}`);
99+
100+
endGroup();
101+
102+
return configInstance;
103+
};
104+
105+
export const config: Config = initializeConfig();

src/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const GITHUB_ACTIONS_BOT_NAME = 'GitHub Actions';
2+
export const GITHUB_ACTIONS_BOT_EMAIL = '41898282+github-actions[bot]@users.noreply.github.com';

0 commit comments

Comments
 (0)