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 ( ) ;
0 commit comments