Skip to content
Draft
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
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ inputs:
required: false
runs:
using: 'docker'
image: 'docker://namoscato/github-action-tinify:v1'
image: 'docker://namoscato/github-action-tinify:pr-235'
branding:
icon: 'image'
color: 'green'
86 changes: 44 additions & 42 deletions src/git/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,52 +65,54 @@ describe('Git', () => {
})
})

describe('pull_request', () => {
beforeEach(() => {
scope.get('/repos/OWNER/REPO/pulls/1/files').reply(200, [
{
id: 1,
status: 'added'
},
{
id: 2,
status: 'removed'
},
{
id: 3,
status: 'modified'
}
])
})

test('should fetch files', async () => {
const files = await new Git({
token: 'TOKEN',
context: ({
eventName: 'pull_request',
payload: {
number: 1
for (const eventName of ['pull_request', 'pull_request_target']) {
describe(eventName, () => {
beforeEach(() => {
scope.get('/repos/OWNER/REPO/pulls/1/files').reply(200, [
{
id: 1,
status: 'added'
},
repo: {
owner: 'OWNER',
repo: 'REPO'
{
id: 2,
status: 'removed'
},
{
id: 3,
status: 'modified'
}
} as unknown) as Context
}).getFiles()
])
})

expect(files).toEqual([
{
id: 1,
status: 'added'
},
{
id: 3,
status: 'modified'
}
])
test('should fetch files', async () => {
const files = await new Git({
token: 'TOKEN',
context: ({
eventName,
payload: {
number: 1
},
repo: {
owner: 'OWNER',
repo: 'REPO'
}
} as unknown) as Context
}).getFiles()

expect(scope.isDone()).toBe(true)
expect(files).toEqual([
{
id: 1,
status: 'added'
},
{
id: 3,
status: 'modified'
}
])

expect(scope.isDone()).toBe(true)
})
})
})
}
})
})
23 changes: 15 additions & 8 deletions src/git/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export default class Git {
}
break
case 'pull_request':
case 'pull_request_target':
info(
`[${this.context.eventName}] Fetching files for pull request ${this.context.payload.number}`
)
Expand Down Expand Up @@ -71,14 +72,20 @@ export default class Git {
}

async commit(commit: Commit): Promise<void> {
let remote = 'origin'

info('Detecting detached state')
if (isPullRequestContext(this.context) && (await this.isDetached())) {
info('Checking out branch from detached state')
await exec('git', [
'checkout',
'-b',
this.context.payload.pull_request.head.ref
])
if (isPullRequestContext(this.context)) {
remote = this.context.payload.pull_request.head.repo.clone_url

if (await this.isDetached()) {
info('Checking out branch from detached state')
await exec('git', [
'checkout',
'-b',
this.context.payload.pull_request.head.ref
])
}
}

info('Adding modified images')
Expand All @@ -103,7 +110,7 @@ export default class Git {
])

info('Push commit')
await exec('git', ['push', 'origin'])
await exec('git', ['push', remote])
}

private async getCommitFiles(
Expand Down
13 changes: 10 additions & 3 deletions src/git/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import {Context} from '@actions/github/lib/context'
import {PullRequestEvent, PushEvent} from '@octokit/webhooks-definitions/schema'
import Image from '../image'

export const supportedEvents = ['push', 'pull_request'] as const
export const supportedEvents = [
'push',
'pull_request',
'pull_request_target'
] as const

type SupportedEvent = typeof supportedEvents[number]

Expand All @@ -16,7 +20,7 @@ interface ContextPush extends ContextBase<PushEvent> {
}

interface ContextPullRequest extends ContextBase<PullRequestEvent> {
eventName: 'pull_request'
eventName: 'pull_request' | 'pull_request_target'
}

export type SupportedContext = ContextPush | ContextPullRequest
Expand All @@ -30,7 +34,10 @@ export function isPushContext(
export function isPullRequestContext(
context: SupportedContext
): context is ContextPullRequest {
return 'pull_request' === context.eventName
return (
'pull_request' === context.eventName ||
'pull_request_target' === context.eventName
)
}

export interface File {
Expand Down