Skip to content

feat: add outputs for changed modules in GitHub Action #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,77 @@ jobs:
module-change-exclude-patterns: .gitignore,*.md,*.tftest.hcl,tests/**
module-asset-exclude-patterns: .gitignore,*.md,*.tftest.hcl,tests/**
use-ssh-source-format: true

- name: Test Action Outputs
id: test-outputs
run: |
echo "Testing action outputs..."

# Test if outputs are set
if [[ -n "${{ steps.test-action.outputs.changed-module-names }}" ]]; then
echo "✅ Output 'changed-modules-names': ${{ steps.test-action.outputs.changed-module-names }}"
else
echo "❌ No changed module names found"
fi

if [[ -n "${{ steps.test-action.outputs.changed-module-paths }}" ]]; then
echo "✅ Output 'changed-module-paths': ${{ steps.test-action.outputs.changed-module-paths }}"
else
echo "❌ No changed module paths found"
fi

if [[ -n "${{ steps.test-action.outputs.changed-modules-map }}" ]]; then
echo "✅ Output 'changed-modules-map':"
echo '${{ steps.test-action.outputs.changed-modules-map }}' | jq -r '
"Found \(length) changed module(s):",
(to_entries[] |
"• \(.key):",
" - Path: \(.value.path)",
" - Current Tag: \(.value.currentTag)",
" - Next Tag: \(.value.nextTag)",
" - Release Type: \(.value.releaseType)"
)
'
else
echo "❌ No changed modules map found"
fi

# Silently validate JSON structure is an object
echo '${{ steps.test-action.outputs.changed-modules-map }}' | jq -e 'type == "object"' > /dev/null || {
echo "❌ Expected object type in changed-modules-map"
exit 1
}

# Test new outputs for all modules
if [[ -n "${{ steps.test-action.outputs.all-module-names }}" ]]; then
echo "✅ Output 'all-module-names': ${{ steps.test-action.outputs.all-module-names }}"
else
echo "❌ No all module names found"
fi

if [[ -n "${{ steps.test-action.outputs.all-module-paths }}" ]]; then
echo "✅ Output 'all-module-paths': ${{ steps.test-action.outputs.all-module-paths }}"
else
echo "❌ No all module paths found"
fi

if [[ -n "${{ steps.test-action.outputs.all-modules-map }}" ]]; then
echo "✅ Output 'all-modules-map':"
echo '${{ steps.test-action.outputs.all-modules-map }}' | jq -r '
"Found \(length) total module(s):",
(to_entries[] |
"• \(.key):",
" - Path: \(.value.path)",
" - Latest Tag: \(.value.latestTag)",
" - Latest Tag Version: \(.value.latestTagVersion)"
)
'
else
echo "❌ No all modules map found"
fi

# Silently validate JSON structure is an objects
echo '${{ steps.test-action.outputs.all-modules-map }}' | jq -e 'type == "object"' > /dev/null || {
echo "❌ Expected object type in all-modules-map"
exit 1
}
3 changes: 1 addition & 2 deletions .node-version
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
22
# Used by CI only
22
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,46 @@ jobs:
use-ssh-source-format: false
```

## Outputs

The following outputs are available from this action:

| Output | Type | Description |
| ---------------------- | -------- | ---------------------------------------------------------------------------------------------------------- |
| `changed-module-names` | `string` | JSON array of module names that were changed in the current pull request |
| `changed-module-paths` | `string` | JSON array of file system paths to the modules that were changed |
| `changed-modules-map` | `string` | JSON object mapping module names to their change details including current tag, next tag, and release type |
| `all-module-names` | `string` | JSON array of all module names found in the repository |
| `all-module-paths` | `string` | JSON array of file system paths to all modules in the repository |
| `all-modules-map` | `string` | JSON object mapping all module names to their details including path, latest tag, and latest tag version |

### Example Output Structure

```json
{
"changed-modules-map": {
"aws/vpc": {
"path": "modules/aws/vpc",
"currentTag": "aws/vpc/v1.0.0",
"nextTag": "aws/vpc/v1.1.0",
"releaseType": "minor"
}
},
"all-modules-map": {
"aws/vpc": {
"path": "modules/aws/vpc",
"latestTag": "aws/vpc/v1.0.0",
"latestTagVersion": "v1.0.0"
},
"aws/s3": {
"path": "modules/aws/s3",
"latestTag": "aws/s3/v2.1.0",
"latestTagVersion": "v2.1.0"
}
}
}
```

## Inspiration

This action was inspired by the blog post
Expand Down
21 changes: 21 additions & 0 deletions __tests__/wiki.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ describe('wiki', async () => {
expect(gitCalls).toEqual([
`config --global --add safe.directory ${wikiDir}`,
`init --initial-branch=master ${wikiDir}`,
'remote',
'remote add origin https://github.com/techpivot/terraform-module-releaser.wiki',
'config --local --unset-all http.https://github.com/.extraheader',
expect.stringContaining('config --local http.https://github.com/.extraheader Authorization: Basic'),
Expand Down Expand Up @@ -153,6 +154,26 @@ describe('wiki', async () => {
checkoutWiki();
expect(existsSync(wikiDir)).toBe(true);
});

it('should update remote URL with set-url if origin already exists', () => {
const mockExecFileSync = vi.fn(
(command: string, args?: readonly string[] | undefined, options?: ExecFileSyncOptions) => {
// If the command is "git remote" return "origin" to simulate an existing remote
if (args?.length === 1 && args[0] === 'remote') {
return Buffer.from('origin');
}
return Buffer.from('');
},
);
vi.mocked(execFileSync).mockImplementation(mockExecFileSync);

checkoutWiki();

const gitCalls = mockExecFileSync.mock.calls.map((call) => call[1]?.join(' ') || '');
expect(gitCalls).toContain('remote set-url origin https://github.com/techpivot/terraform-module-releaser.wiki');

// ... Assertions for group start/end can be added if needed ...
});
});

describe('getWikiLink()', () => {
Expand Down
14 changes: 14 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@ inputs:
required: true
default: ${{ github.token }}

outputs:
changed-module-names:
description: JSON array of module names that were changed in the current pull request
changed-module-paths:
description: JSON array of file system paths to the modules that were changed
changed-modules-map:
description: JSON object mapping module names to their change details including current tag, next tag, and release type
all-module-names:
description: JSON array of all module names found in the repository
all-module-paths:
description: JSON array of file system paths to all modules in the repository
all-modules-map:
description: JSON object mapping all module names to their details including path, latest tag, and latest tag version

runs:
using: node20
main: dist/index.js
Loading