|
| 1 | +//go:build !integration |
| 2 | + |
| 3 | +package cli |
| 4 | + |
| 5 | +import ( |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func TestGitHubAppClientIDCodemod(t *testing.T) { |
| 13 | + codemod := getGitHubAppClientIDCodemod() |
| 14 | + |
| 15 | + t.Run("renames app-id under github-app blocks", func(t *testing.T) { |
| 16 | + content := `--- |
| 17 | +github-app: |
| 18 | + app-id: ${{ vars.TOP_LEVEL_APP_ID }} |
| 19 | + private-key: ${{ secrets.TOP_LEVEL_APP_PRIVATE_KEY }} |
| 20 | +on: |
| 21 | + github-app: |
| 22 | + app-id: ${{ vars.ACTIVATION_APP_ID }} |
| 23 | + private-key: ${{ secrets.ACTIVATION_APP_PRIVATE_KEY }} |
| 24 | +checkout: |
| 25 | + - repository: org/repo |
| 26 | + github-app: |
| 27 | + app-id: ${{ vars.CHECKOUT_APP_ID }} |
| 28 | + private-key: ${{ secrets.CHECKOUT_APP_PRIVATE_KEY }} |
| 29 | +--- |
| 30 | +` |
| 31 | + frontmatter := map[string]any{ |
| 32 | + "github-app": map[string]any{ |
| 33 | + "app-id": "${{ vars.TOP_LEVEL_APP_ID }}", |
| 34 | + "private-key": "${{ secrets.TOP_LEVEL_APP_PRIVATE_KEY }}", |
| 35 | + }, |
| 36 | + "on": map[string]any{ |
| 37 | + "github-app": map[string]any{ |
| 38 | + "app-id": "${{ vars.ACTIVATION_APP_ID }}", |
| 39 | + "private-key": "${{ secrets.ACTIVATION_APP_PRIVATE_KEY }}", |
| 40 | + }, |
| 41 | + }, |
| 42 | + "checkout": []any{ |
| 43 | + map[string]any{ |
| 44 | + "repository": "org/repo", |
| 45 | + "github-app": map[string]any{ |
| 46 | + "app-id": "${{ vars.CHECKOUT_APP_ID }}", |
| 47 | + "private-key": "${{ secrets.CHECKOUT_APP_PRIVATE_KEY }}", |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + result, modified, err := codemod.Apply(content, frontmatter) |
| 54 | + require.NoError(t, err, "Should not error when applying codemod") |
| 55 | + assert.True(t, modified, "Should modify content") |
| 56 | + assert.NotContains(t, result, "app-id:", "Should remove deprecated app-id key from github-app blocks") |
| 57 | + assert.Contains(t, result, "client-id: ${{ vars.TOP_LEVEL_APP_ID }}", "Should migrate top-level github-app") |
| 58 | + assert.Contains(t, result, "client-id: ${{ vars.ACTIVATION_APP_ID }}", "Should migrate on.github-app") |
| 59 | + assert.Contains(t, result, "client-id: ${{ vars.CHECKOUT_APP_ID }}", "Should migrate checkout github-app") |
| 60 | + }) |
| 61 | + |
| 62 | + t.Run("does not modify content without github-app.app-id", func(t *testing.T) { |
| 63 | + content := `--- |
| 64 | +github-app: |
| 65 | + client-id: ${{ vars.APP_ID }} |
| 66 | + private-key: ${{ secrets.APP_PRIVATE_KEY }} |
| 67 | +--- |
| 68 | +` |
| 69 | + frontmatter := map[string]any{ |
| 70 | + "github-app": map[string]any{ |
| 71 | + "client-id": "${{ vars.APP_ID }}", |
| 72 | + "private-key": "${{ secrets.APP_PRIVATE_KEY }}", |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + result, modified, err := codemod.Apply(content, frontmatter) |
| 77 | + require.NoError(t, err, "Should not error") |
| 78 | + assert.False(t, modified, "Should not modify already migrated content") |
| 79 | + assert.Equal(t, content, result, "Content should remain unchanged") |
| 80 | + }) |
| 81 | + |
| 82 | + t.Run("does not rename app-id outside github-app blocks", func(t *testing.T) { |
| 83 | + content := `--- |
| 84 | +engine: |
| 85 | + provider: |
| 86 | + auth: |
| 87 | + client-id: APP_CLIENT_ID |
| 88 | +tools: |
| 89 | + custom: |
| 90 | + app-id: value |
| 91 | +--- |
| 92 | +` |
| 93 | + frontmatter := map[string]any{ |
| 94 | + "engine": map[string]any{ |
| 95 | + "provider": map[string]any{ |
| 96 | + "auth": map[string]any{ |
| 97 | + "client-id": "APP_CLIENT_ID", |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + "tools": map[string]any{ |
| 102 | + "custom": map[string]any{ |
| 103 | + "app-id": "value", |
| 104 | + }, |
| 105 | + }, |
| 106 | + } |
| 107 | + |
| 108 | + result, modified, err := codemod.Apply(content, frontmatter) |
| 109 | + require.NoError(t, err, "Should not error") |
| 110 | + assert.False(t, modified, "Should not modify non github-app app-id keys") |
| 111 | + assert.Equal(t, content, result, "Content should remain unchanged") |
| 112 | + }) |
| 113 | +} |
0 commit comments