Skip to content

Commit 1a3cbf2

Browse files
committed
feat: add useSSHSourceFormat option to configure source URL format in Wiki documentation
1 parent 46b354c commit 1a3cbf2

File tree

14 files changed

+112
-14
lines changed

14 files changed

+112
-14
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,4 @@ jobs:
5151
delete-legacy-tags: false # Note: We don't want to delete tags in this repository
5252
module-change-exclude-patterns: .gitignore,*.md,*.tftest.hcl,tests/**
5353
module-asset-exclude-patterns: .gitignore,*.md,*.tftest.hcl,tests/**
54+
use-ssh-source-format: true

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
env:
3636
GITHUB_TOKEN: ${{ secrets.GH_TOKEN_REPO_CI_TESTING }}
3737

38-
- name: SonarCloud Scan
39-
uses: sonarsource/sonarcloud-github-action@v4
38+
- name: SonarQube Scan
39+
uses: SonarSource/sonarqube-scan-action@v4
4040
env:
4141
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ configuring the following optional input parameters as needed.
182182
| `disable-branding` | Controls whether a small branding link to the action's repository is added to PR comments. Recommended to leave enabled to support OSS. | `false` |
183183
| `module-change-exclude-patterns` | A comma-separated list of file patterns to exclude from triggering version changes in Terraform modules. Patterns follow glob syntax (e.g., `.gitignore,_.md`) and are relative to each Terraform module directory. Files matching these patterns will not affect version changes. **WARNING**: Avoid excluding '`_.tf`' files, as they are essential for module detection and versioning processes. | `.gitignore, *.md, *.tftest.hcl, tests/**` |
184184
| `module-asset-exclude-patterns` | A comma-separated list of file patterns to exclude when bundling a Terraform module for tag/release. Patterns follow glob syntax (e.g., `tests/\*\*`) and are relative to each Terraform module directory. Files matching these patterns will be excluded from the bundled output. | `.gitignore, *.md, *.tftest.hcl, tests/**` |
185+
| `use-ssh-source-format` | If enabled, all links to source code in generated Wiki documentation will use SSH standard format (e.g., `git::ssh://git@github.com/owner/repo.git`) instead of HTTPS format (`git::https://github.com/owner/repo.git`) | `false` |
185186

186187
### Example Usage with Inputs
187188

@@ -217,6 +218,7 @@ jobs:
217218
wiki-sidebar-changelog-max: 10
218219
module-change-exclude-patterns: .gitignore,*.md,*.tftest.hcl,tests/**
219220
module-asset-exclude-patterns: .gitignore,*.md,*.tftest.hcl,tests/**
221+
use-ssh-source-format: false
220222
```
221223

222224
## Inspiration

__mocks__/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const defaultConfig: Config = {
2424
moduleChangeExcludePatterns: ['.gitignore', '*.md'],
2525
moduleAssetExcludePatterns: ['tests/**', 'examples/**'],
2626
githubToken: 'ghp_test_token_2c6912E7710c838347Ae178B4',
27+
useSSHSourceFormat: false,
2728
};
2829

2930
/**
@@ -42,6 +43,7 @@ const validConfigKeys = [
4243
'moduleChangeExcludePatterns',
4344
'moduleAssetExcludePatterns',
4445
'githubToken',
46+
'useSSHSourceFormat',
4547
] as const;
4648

4749
type ValidConfigKey = (typeof validConfigKeys)[number];

__tests__/config.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ describe('config', () => {
117117
expect(config.githubToken).toBe('ghp_test_token_2c6912E7710c838347Ae178B4');
118118
expect(config.moduleChangeExcludePatterns).toEqual(['.gitignore', '*.md']);
119119
expect(config.moduleAssetExcludePatterns).toEqual(['tests/**', 'examples/**']);
120+
expect(config.useSSHSourceFormat).toBe(false);
120121
expect(startGroup).toHaveBeenCalledWith('Initializing Config');
121122
expect(startGroup).toHaveBeenCalledTimes(1);
122123
expect(endGroup).toHaveBeenCalledTimes(1);

__tests__/helpers/inputs.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ export const defaultInputs = {
2121
'module-change-exclude-patterns': '.gitignore,*.md',
2222
'module-asset-exclude-patterns': 'tests/**,examples/**',
2323
github_token: 'ghp_test_token_2c6912E7710c838347Ae178B4',
24+
'use-ssh-source-format': 'false',
2425
};
2526
export const requiredInputs = Object.keys(defaultInputs);
26-
export const booleanInputs = ['delete-legacy-tags', 'disable-wiki', 'disable-branding'];
27+
export const booleanInputs = ['delete-legacy-tags', 'disable-wiki', 'disable-branding', 'use-ssh-source-format'];
2728
export const booleanConfigKeys: BooleanConfigKeys[] = ['deleteLegacyTags', 'disableWiki', 'disableBranding'];
2829

2930
/**

__tests__/terraform-docs.test.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { context } from '@/mocks/context';
77
import { ensureTerraformDocsConfigDoesNotExist, generateTerraformDocs, installTerraformDocs } from '@/terraform-docs';
88
import type { TerraformModule } from '@/types';
99
import { info } from '@actions/core';
10-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
10+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
1111
import which from 'which';
1212

1313
const execFilePromisified = promisify(execFile);
@@ -50,7 +50,8 @@ describe('terraform-docs', async () => {
5050
afterEach(() => {
5151
Object.defineProperty(process, 'platform', { value: realPlatform });
5252
Object.defineProperty(process, 'arch', { value: realArch });
53-
vi.resetAllMocks();
53+
//vi.resetAllMocks();
54+
//vi.clearAllMocks();
5455
});
5556

5657
describe('install terraform-docs (linux, darwin, freebsd)', () => {
@@ -184,6 +185,10 @@ describe('terraform-docs', async () => {
184185

185186
expect(() => installTerraformDocs(terraformDocsVersion)).toThrow('not found: invalid-non-existent-binary');
186187
});
188+
189+
afterAll(() => {
190+
mockWhichSync.mockRestore();
191+
});
187192
});
188193

189194
describe('terraform-docs version validation', () => {
@@ -219,7 +224,7 @@ describe('terraform-docs', async () => {
219224
join('C:\\Windows\\System32', 'terraform-docs.exe'),
220225
];
221226

222-
beforeEach(async () => {
227+
beforeAll(async () => {
223228
// Get real implementations
224229
const realChildProcess = (await vi.importActual('node:child_process')) as typeof import('node:child_process');
225230
const realFs = (await vi.importActual('node:fs')) as typeof import('node:fs');
@@ -232,6 +237,14 @@ describe('terraform-docs', async () => {
232237
mockWhichSync.mockImplementation(realWhich.sync);
233238
});
234239

240+
afterAll(() => {
241+
// Restore original mock implementations
242+
mockExecFileSync.mockRestore();
243+
fsExistsSyncMock.mockRestore();
244+
mockFsUnlinkSync.mockRestore();
245+
mockWhichSync.mockRestore();
246+
});
247+
235248
afterEach(() => {
236249
// Cleanup downloaded/installed files
237250
for (const file of cleanupFiles) {
@@ -243,6 +256,7 @@ describe('terraform-docs', async () => {
243256
}
244257

245258
// Restore original mock implementations (handled via global resetAllMocks())
259+
246260
});
247261

248262
it(`should install terraform-docs on the real system ${process.arch}/${process.platform}`, () => {

__tests__/wiki.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,4 +316,51 @@ describe('wiki', async () => {
316316
expect(commitCall?.[1]).toEqual(['commit', '-m', 'PR #456 - Complex PR title\n\nLine 1\nLine 2\nLine 3']);
317317
});
318318
});
319+
320+
describe('formatModuleSource()', () => {
321+
beforeEach(() => {
322+
context.set({
323+
repo: { owner: 'techpivot', repo: 'terraform-module-releaser' },
324+
repoUrl: 'https://github.com/techpivot/terraform-module-releaser',
325+
});
326+
});
327+
328+
it('should format source URL as HTTPS when useSSHSourceFormat is false', async () => {
329+
config.set({ useSSHSourceFormat: false });
330+
const files = await generateWikiFiles(terraformModules);
331+
332+
// Read each generated .md file and verify it contains HTTPS format
333+
for (const file of files) {
334+
if (file.endsWith('.md')) {
335+
const content = readFileSync(file, 'utf8');
336+
if (content.includes('source =')) {
337+
expect(content).toContain('source = "git::https://github.com/techpivot/terraform-module-releaser.git?ref=');
338+
expect(content).not.toContain(
339+
'source = "git::ssh://git@github.com/techpivot/terraform-module-releaser.git?ref=',
340+
);
341+
}
342+
}
343+
}
344+
});
345+
346+
it('should format source URL as SSH when useSSHSourceFormat is true', async () => {
347+
config.set({ useSSHSourceFormat: true });
348+
const files = await generateWikiFiles(terraformModules);
349+
350+
// Read each generated .md file and verify it contains SSH format
351+
for (const file of files) {
352+
if (file.endsWith('.md')) {
353+
const content = readFileSync(file, 'utf8');
354+
if (content.includes('source =')) {
355+
expect(content).toContain(
356+
'source = "git::ssh://git@github.com/techpivot/terraform-module-releaser.git?ref=',
357+
);
358+
expect(content).not.toContain(
359+
'source = "git::https://github.com/techpivot/terraform-module-releaser.git?ref=',
360+
);
361+
}
362+
}
363+
}
364+
});
365+
});
319366
});

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ inputs:
8383
The minimatch syntax is used for pattern matching. Files matching these patterns will be excluded from the bundled output.
8484
required: true
8585
default: ".gitignore,*.md,*.tftest.hcl,tests/**"
86+
use-ssh-source-format:
87+
description: If enabled, all links to source code in generated Wiki documentation will use SSH format instead of HTTPS format
88+
required: true
89+
default: "false"
8690
github_token:
8791
description: |
8892
Required for retrieving pull request metadata, tags, releases, updating PR comments, wiki, and creating tags/releases.

package.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,7 @@
1313
"bugs": {
1414
"url": "https://github.com/techpivot/terraform-module-releaser/issues"
1515
},
16-
"keywords": [
17-
"terraform",
18-
"module",
19-
"releaser",
20-
"github-action",
21-
"monorepo"
22-
],
16+
"keywords": ["terraform", "module", "releaser", "github-action", "monorepo"],
2317
"license": "MIT",
2418
"exports": {
2519
".": "./dist/index.js"

sonar-project.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
sonar.organization=techpivot
22
sonar.projectKey=terraform-module-releaser
33
sonar.projectName=Terraform Module Releaser
4+
sonar.projectDescription=A GitHub Action for managing Terraform modules in GitHub monorepos, automating versioning, releases, and documentation.
45

56
sonar.sourceEncoding=UTF-8
67

src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ function initializeConfig(): Config {
7171
githubToken: getInput('github_token', { required: true }),
7272
moduleChangeExcludePatterns: getArrayInput('module-change-exclude-patterns'),
7373
moduleAssetExcludePatterns: getArrayInput('module-asset-exclude-patterns'),
74+
useSSHSourceFormat: getBooleanInput('use-ssh-source-format', { required: true }),
7475
};
7576

7677
// Validate that *.tf is not in excludePatterns

src/types/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,18 @@ export interface Config {
194194
* tests and other non-functional files as needed.
195195
*/
196196
moduleAssetExcludePatterns: string[];
197+
198+
/**
199+
* If true, the wiki will use the SSH format for the source URL of the repository.
200+
* This changes the format of the source URL in the generated wiki documentation to use the SSH format.
201+
*
202+
* Example:
203+
* - SSH format: git::ssh://git@github.com/techpivot/terraform-module-releaser.git
204+
* - HTTPS format: git::https://github.com/techpivot/terraform-module-releaser.git
205+
*
206+
* When set to true, the SSH standard format (non scp variation) will be used. Otherwise, the HTTPS format will be used.
207+
*/
208+
useSSHSourceFormat: boolean;
197209
}
198210

199211
/**

src/wiki.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,23 @@ export function getWikiLink(moduleName: string, relative = true): string {
190190
return `${baseUrl}/wiki/${getWikiSlug(moduleName)}`;
191191
}
192192

193+
/**
194+
* Formats the module source URL based on configuration settings.
195+
*
196+
* @param repoUrl - The repository URL
197+
* @param useSSH - Whether to use SSH format
198+
* @returns The formatted source URL for the module
199+
*/
200+
function formatModuleSource(repoUrl: string, useSSH: boolean): string {
201+
if (useSSH) {
202+
// Convert HTTPS URL to SSH format
203+
// From: https://github.com/owner/repo
204+
// To: ssh://git@github.com/owner/repo
205+
return `ssh://${repoUrl.replace(/^https:\/\/github\.com/, 'git@github.com')}.git`;
206+
}
207+
return `${repoUrl}.git`;
208+
}
209+
193210
/**
194211
* Generates the wiki file associated with the specified Terraform module.
195212
* Ensures that the directory structure is created if it doesn't exist and handles overwriting
@@ -209,12 +226,13 @@ async function generateWikiModule(terraformModule: TerraformModule): Promise<str
209226
// Generate a module changelog
210227
const changelog = getModuleReleaseChangelog(terraformModule);
211228
const tfDocs = await generateTerraformDocs(terraformModule);
229+
const moduleSource = formatModuleSource(context.repoUrl, config.useSSHSourceFormat);
212230
const wikiContent = [
213231
'# Usage\n',
214232
'To use this module in your Terraform, refer to the below module example:\n',
215233
'```hcl',
216234
`module "${moduleName.replace(/[^a-zA-Z0-9]/g, '_').toLowerCase()}" {`,
217-
` source = "git::${context.repoUrl}.git?ref=${latestTag}"`,
235+
` source = "git::${moduleSource}?ref=${latestTag}"`,
218236
'\n # See inputs below for additional required parameters',
219237
'}',
220238
'```',

0 commit comments

Comments
 (0)