Skip to content

Commit

Permalink
Merge pull request #338 from smartlyio/VULCAN-3811/fix-find-changes-l…
Browse files Browse the repository at this point in the history
…imit-to-only-pr-changes

Fix find changes limit to only pr changes
  • Loading branch information
sjagoe authored Feb 21, 2024
2 parents 371b767 + b9ca968 commit b0a3167
Show file tree
Hide file tree
Showing 14 changed files with 153 additions and 183 deletions.
24 changes: 0 additions & 24 deletions .github/templates/jobs/build.erb

This file was deleted.

24 changes: 0 additions & 24 deletions .github/templates/jobs/test.erb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
# IMPORTANT NOTE/WARNING!
# Do not make changes to this file, your changes will be overwritten.
#
# This file is automagically generated from:
# - .github/templates/pull_request.yml.erb
# - Templates contained in the smartlyio/github-actions-templates repository
#
# This file can be updated by editing the template file, and running `devbox render workflows`

name: "Pull Request"
name: Build

on:
pull_request:
branches: [master]
workflow_call:

jobs:
build:
Expand Down Expand Up @@ -45,10 +35,10 @@ jobs:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
with:
fetch-depth: 0
- name: Set Node.js 16.x
- name: Set Node.js 20.x
uses: actions/setup-node@v4
with:
node-version: 16.x
node-version: 20.x
- name: "Build action for test"
run: |
npm install
Expand All @@ -63,20 +53,3 @@ jobs:
uses: ./.
with:
directory_containing: main.ts

release:
runs-on: ubuntu-22.04
name: "Build and release action"
needs: [build, test]
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
with:
fetch-depth: 0
persist-credentials: true
- name: Configure git
uses: smartlyio/github-actions@git-init-userinfo-v1
- name: Release flow
uses: smartlyio/github-actions@release-action-node-v1
with:
dry_run: true
token: "${{ secrets.GITHUB_TOKEN }}"
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ on:
branches: [master]

jobs:
@import ./jobs/build

@import ./jobs/test
build:
name: Build
uses: ./.github/workflows/build.yml
secrets: inherit

release:
runs-on: <%= ubuntu_version %>
runs-on: ubuntu-22.04
name: "Build and release action"
needs: [build, test]
needs: [build]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
with:
fetch-depth: 0
persist-credentials: true
Expand All @@ -24,4 +25,4 @@ jobs:
uses: smartlyio/github-actions@release-action-node-v1
with:
dry_run: true
token: "${{ secrets.GITHUB_TOKEN }}"
token: "${{ secrets.GITHUB_TOKEN }}"
56 changes: 0 additions & 56 deletions .github/workflows/release.generated.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ on:
branches: [master]

jobs:
@import ./jobs/build
build:
name: Build
uses: ./.github/workflows/build.yml
secrets: inherit

release:
runs-on: <%= ubuntu_version %>
runs-on: ubuntu-22.04
name: "Build and release action"
needs: [build]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
with:
fetch-depth: 0
persist-credentials: true
Expand Down
62 changes: 57 additions & 5 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,75 @@ afterEach(() => {
describe('get branch point', () => {
test('missing GITHUB_EVENT_NAME', async () => {
delete process.env['GITHUB_EVENT_NAME']
await expect(getBranchPoint()).rejects.toThrow(/only works on pull_request/)
const context: Context = {
fromOriginalBranchPoint: false,
directoryContaining: null, // Not used by getChangedDirectories
directoryLevels: null,
exclude: /^\.github\/.*/ // Not used by getChangedDirectories
}
await expect(getBranchPoint(context)).rejects.toThrow(
/only works on pull_request/
)
})

test('wrong GITHUB_EVENT_NAME', async () => {
process.env['GITHUB_EVENT_NAME'] = 'push'
await expect(getBranchPoint()).rejects.toThrow(/only works on pull_request/)
const context: Context = {
fromOriginalBranchPoint: false,
directoryContaining: null, // Not used by getChangedDirectories
directoryLevels: null,
exclude: /^\.github\/.*/ // Not used by getChangedDirectories
}
await expect(getBranchPoint(context)).rejects.toThrow(
/only works on pull_request/
)
})

test('missing GITHUB_EVENT_PATH', async () => {
process.env['GITHUB_EVENT_NAME'] = 'pull_request'
delete process.env['GITHUB_EVENT_PATH']
await expect(getBranchPoint()).rejects.toThrow(
const context: Context = {
fromOriginalBranchPoint: false,
directoryContaining: null, // Not used by getChangedDirectories
directoryLevels: null,
exclude: /^\.github\/.*/ // Not used by getChangedDirectories
}
await expect(getBranchPoint(context)).rejects.toThrow(
/Could not find event payload/
)
})

test('read event file', async () => {
test('get sha of main merged to PR', async () => {
const eventPath = path.join(__dirname, 'pr_event.json')
const eventData = await fsReal.readFile(eventPath)
const event = JSON.parse(eventData.toString())
process.env['GITHUB_EVENT_NAME'] = 'pull_request'
process.env['GITHUB_EVENT_PATH'] = eventPath

const branchPoint = await getBranchPoint()
const context: Context = {
fromOriginalBranchPoint: false,
directoryContaining: null, // Not used by getChangedDirectories
directoryLevels: null,
exclude: /^\.github\/.*/ // Not used by getChangedDirectories
}
const branchPoint = await getBranchPoint(context)
expect(branchPoint).toEqual('origin/master')
})

test('from original branch point', async () => {
const eventPath = path.join(__dirname, 'pr_event.json')
const eventData = await fsReal.readFile(eventPath)
const event = JSON.parse(eventData.toString())
process.env['GITHUB_EVENT_NAME'] = 'pull_request'
process.env['GITHUB_EVENT_PATH'] = eventPath

const context: Context = {
fromOriginalBranchPoint: true,
directoryContaining: null, // Not used by getChangedDirectories
directoryLevels: null,
exclude: /^\.github\/.*/ // Not used by getChangedDirectories
}
const branchPoint = await getBranchPoint(context)
expect(branchPoint).toEqual(event.pull_request.base.sha)
})
})
Expand All @@ -84,6 +129,7 @@ deeply/nested/package/README
top-level-file
`
const context: Context = {
fromOriginalBranchPoint: false,
directoryContaining: null, // Not used by getChangedDirectories
directoryLevels: null,
exclude: /^\.github\/.*/ // Not used by getChangedDirectories
Expand Down Expand Up @@ -115,6 +161,7 @@ deeply/nested/package/README
top-level-file
`
const context: Context = {
fromOriginalBranchPoint: false,
directoryContaining: null,
directoryLevels: 1,
exclude: /^\.github\/.*/ // Not used by getChangedDirectories
Expand Down Expand Up @@ -146,6 +193,7 @@ deeply/nested/package/README
top-level-file
`
const context: Context = {
fromOriginalBranchPoint: false,
directoryContaining: null,
directoryLevels: 2,
exclude: /^\.github\/.*/ // Not used by getChangedDirectories
Expand Down Expand Up @@ -180,6 +228,7 @@ deeply/nested/package/README
top-level-file
`
const context: Context = {
fromOriginalBranchPoint: false,
directoryContaining: null, // Not used by getChangedDirectories
directoryLevels: null,
exclude: /^\.github\/.*/ // Not used by getChangedDirectories
Expand Down Expand Up @@ -240,6 +289,7 @@ describe('filterGitOutputByFile', () => {
]
const expected = ['package1', 'package2', 'nested', 'deeply']
const context: Context = {
fromOriginalBranchPoint: false,
directoryContaining: null,
directoryLevels: null,
exclude: /^\.github($|\/.*)/
Expand All @@ -257,6 +307,7 @@ describe('filterGitOutputByFile', () => {
]
const expected = ['nested/package', 'deeply/nested']
const context: Context = {
fromOriginalBranchPoint: false,
directoryContaining: null,
directoryLevels: null,
exclude: /^\.github($|\/.*)/
Expand All @@ -270,6 +321,7 @@ describe('filterGitOutputByFile', () => {
const changedDirectories = ['__tests__', 'deeply/nested', '.github/actions']
const expected = ['__tests__']
const context: Context = {
fromOriginalBranchPoint: false,
directoryContaining: 'pr_event.json',
directoryLevels: null,
exclude: /^\.github($|\/.*)/
Expand Down
3 changes: 3 additions & 0 deletions __tests__/pr_event.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"repository": {
"default_branch": "master"
},
"pull_request": {
"base": {
"sha": "pull_request_sha"
Expand Down
10 changes: 9 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ name: 'Find changed directories'
description: 'Create matrix expression to build only changed packages'
author: 'Smartly.io'
inputs:
from-original-branch-point:
description: >-
Find the changes from the original branch point.
The default is to find changes relative to the *current* main
branch, after merging the main branch into this PR.
required: false
default: false
directory_containing:
description: >-
Find changes to directories containing a file with this name.
Expand Down Expand Up @@ -39,5 +47,5 @@ outputs:
diff_base:
description: "Git commit used as the base for finding pull request changes"
runs:
using: 'node16'
using: 'node20'
main: 'dist/index.js'
Loading

0 comments on commit b0a3167

Please sign in to comment.