Skip to content

Commit

Permalink
Merge pull request #4 from tony84727/pushing_action
Browse files Browse the repository at this point in the history
Use last commit as the base commit to compare when triggered by push events
  • Loading branch information
tony84727 authored May 15, 2020
2 parents 2e5308e + 72f57f7 commit bab5dab
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 16 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/push-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# test that run on each push to test the action in push workflow
name: 'push-test'
on: ['push']

jobs:
push-test-1:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
# add a fake commit
- name: Add Commit
run: |
mkdir testzone && \
touch testzone/touched.txt && \
git config user.name 'Github Action' && \
git config user.email '[email protected]' && \
git add testzone/touched.txt && \
git commit -m "add testzone/touched.txt"
- uses: ./
id: filter
with:
head-ref: HEAD
filters: |
test:
- 'testzone/*.txt'
should-not-changed:
- 'not-exsits'
- name: Check Output
run: |
[[ "${{ steps.filter.outputs.test}}" = "true" ]] && \
[[ "${{ steps.filter.outputs.should-not-changed }}" = "false" ]]
8 changes: 0 additions & 8 deletions =0.2.1

This file was deleted.

8 changes: 8 additions & 0 deletions __tests__/glob.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ describe('globber returned by newGlobber', () => {
['src/BUILD', false],
['BUILD.bazel', true]
]
},
{
globRules: ['src/**/*'],
expectedMatches: [
['file', false],
['src/file', true],
['src/nested/file', true]
]
}
]
it('matches paths correclty', () => {
Expand Down
7 changes: 6 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ inputs:
filters:
description: 'Specify filter rules'
required: true
event:
description: 'Github event name. (github.event_name)'
default: ${{ github.event_name }}
base:
description: 'SHA of base commit to compare'
default: ${{ github.event.pull_request.base.sha }}
head:
description: 'SHA of head commit to compare'
default: ${{ github.event.pull_request.head.sha }}
default: ${{ github.event.pull_request.head.sha || github.sha }}
head-ref:
description: 'ref of the head commit to compare (mainly for testing the action)'
runs:
using: 'node12'
main: 'dist/index.js'
24 changes: 21 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1569,14 +1569,32 @@ function evaluateRule(rule, changedFiles) {
const globber = glob_1.newGlobber(rule.match);
return changedFiles.find(globber) !== undefined;
}
function getBaseSha(event) {
return __awaiter(this, void 0, void 0, function* () {
if (event === 'push') {
return git_1.revParse('HEAD^');
}
return core.getInput('base');
});
}
function getHeadSha() {
return __awaiter(this, void 0, void 0, function* () {
const headRef = core.getInput('head-ref');
if (headRef.length > 0) {
return git_1.revParse(headRef);
}
return core.getInput('head');
});
}
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
const baseSha = core.getInput('base');
const headSha = core.getInput('head');
const event = core.getInput('event');
yield git_1.unshallow();
const baseSha = yield getBaseSha(event);
const headSha = yield getHeadSha();
core.debug(`baseSha: ${baseSha}`);
core.debug(`headSha: ${headSha}`);
yield git_1.unshallow();
const rules = rule_1.parseRules(core.getInput('filters'));
const changedFiles = yield git_1.getChangedFiles(baseSha, headSha);
core.debug(`changedFiles: ${changedFiles}`);
Expand Down
25 changes: 21 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
import * as core from '@actions/core'
import {getChangedFiles, unshallow} from './git'
import {getChangedFiles, unshallow, revParse} from './git'
import {Rule, parseRules} from './rule'
import {newGlobber} from './glob'
function evaluateRule(rule: Rule, changedFiles: string[]): boolean {
const globber = newGlobber(rule.match)
return changedFiles.find(globber) !== undefined
}

async function getBaseSha(event: string): Promise<string> {
if (event === 'push') {
return revParse('HEAD^')
}
return core.getInput('base')
}

async function getHeadSha(): Promise<string> {
const headRef = core.getInput('head-ref')
if (headRef.length > 0) {
return revParse(headRef)
}
return core.getInput('head')
}

async function run(): Promise<void> {
try {
const baseSha = core.getInput('base')
const headSha = core.getInput('head')
const event = core.getInput('event')
await unshallow()
const baseSha = await getBaseSha(event)
const headSha = await getHeadSha()
core.debug(`baseSha: ${baseSha}`)
core.debug(`headSha: ${headSha}`)
await unshallow()

const rules = parseRules(core.getInput('filters'))
const changedFiles = await getChangedFiles(baseSha, headSha)
Expand Down

0 comments on commit bab5dab

Please sign in to comment.