Skip to content

Commit

Permalink
Support pull_request_target workflow event (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
namoscato authored Mar 20, 2021
1 parent fb7ec54 commit cb48fc0
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 106 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
14.15.0
12
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"cSpell.words": [
"octokit",
"repos",
"tinify"
]
}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ jobs:
github_token: ${{ secrets.GITHUB_TOKEN }}
```
The following [workflow events](https://docs.github.com/en/actions/reference/events-that-trigger-workflows) are supported:
* `pull_request`
* `pull_request_target`
* `push`

## Inputs

| input | description |
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@types/jest": "^26.0.20",
"@types/mime": "^2.0.3",
"@types/nock": "^11.1.0",
"@types/node": "^14.14.31",
"@types/node": "^12.20.6",
"@typescript-eslint/parser": "^3.10.1",
"eslint": "^7.21.0",
"eslint-plugin-github": "^4.1.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
getResizeOptions,
isResizable,
ResizeMethod
} from '../src/image-utils'
} from '../image-utils'

test('getCompressionSummary', () => {
expect(getCompressionSummary([20000, 9876])).toEqual('-9.89KB (-50%)')
Expand Down
2 changes: 1 addition & 1 deletion __tests__/image.test.ts → src/__tests__/image.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Image from '../src/image'
import Image from '../image'

let image: Image

Expand Down
17 changes: 0 additions & 17 deletions src/git-utils.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {getCommitMessage} from '../src/git-utils'
import {Commit} from '../src/git'
import Image from '../src/image'
import Image from '../../image'
import {getCommitMessage} from '../functions'
import {Commit} from '../types'

describe('getCommitMessage', () => {
test('defined message', () => {
Expand Down
88 changes: 45 additions & 43 deletions __tests__/git.test.ts → src/git/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Git, {ContextEventName} from '../src/git'
import nock, {Scope} from 'nock'
import {Context} from '@actions/github/lib/context'
import nock, {Scope} from 'nock'
import Git from '..'

describe('Git', () => {
let target: Git
Expand Down Expand Up @@ -40,7 +40,7 @@ describe('Git', () => {

test('should fetch files', async () => {
const files = await target.getFiles(({
eventName: ContextEventName.Push,
eventName: 'push',
payload: {
commits: [{id: 'C1'}, {id: 'C2'}]
},
Expand All @@ -65,49 +65,51 @@ 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'
}
])
})
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'
},
{
id: 2,
status: 'removed'
},
{
id: 3,
status: 'modified'
}
])
})

test('should fetch files', async () => {
const files = await target.getFiles(({
eventName: ContextEventName.PullRequest,
payload: {
number: 1
},
repo: {
owner: 'OWNER',
repo: 'REPO'
}
} as unknown) as Context)
test('should fetch files', async () => {
const files = await target.getFiles(({
eventName,
payload: {
number: 1
},
repo: {
owner: 'OWNER',
repo: 'REPO'
}
} as unknown) as Context)

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

expect(scope.isDone()).toBe(true)
expect(scope.isDone()).toBe(true)
})
})
})
}
})
})
26 changes: 26 additions & 0 deletions src/git/functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {Context} from '@actions/github/lib/context'
import {Commit, supportedEvents} from './types'

export function getCommitMessage(commit: Commit): string {
let message = commit.message

if (message) {
return message
}

message = 'Compress image'

if (commit.files.length > 1) {
message += 's'
}

return message
}

export function assertUnsupportedEvent(context: Context): never {
throw new Error(
`Unsupported event ${
context.eventName
} (currently supported events include ${supportedEvents.join(', ')})`
)
}
47 changes: 11 additions & 36 deletions src/git.ts → src/git/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
import {info} from '@actions/core'
import {exec} from '@actions/exec'
import * as github from '@actions/github'
import {Context} from '@actions/github/lib/context'
import {GitHub} from '@actions/github/lib/utils'
import Image from './image'
import {getCommitMessage} from './git-utils'
import {Endpoints} from '@octokit/types'
import {Context} from '@actions/github/lib/context'

export enum ContextEventName {
Push = 'push',
PullRequest = 'pull_request'
}

export interface File {
filename: string
status: string
}

export interface Commit {
files: Image[]
userName: string
userEmail: string
message: string
}
import {assertUnsupportedEvent, getCommitMessage} from './functions'
import {Commit, File, SupportedEvent} from './types'

export default class Git {
private octokit: InstanceType<typeof GitHub>
Expand All @@ -33,13 +16,14 @@ export default class Git {

async getFiles(context: Context): Promise<File[]> {
const filesPromises: Promise<File[]>[] = []
const eventName = context.eventName as SupportedEvent

switch (context.eventName) {
case ContextEventName.Push:
switch (eventName) {
case 'push':
for (const commit of context.payload.commits) {
const ref = commit.id

info(`[${context.eventName}] Fetching files for commit ${ref}`)
info(`[${eventName}] Fetching files for commit ${ref}`)

filesPromises.push(
this.getCommitFiles({
Expand All @@ -49,9 +33,10 @@ export default class Git {
)
}
break
case ContextEventName.PullRequest:
case 'pull_request':
case 'pull_request_target':
info(
`[${context.eventName}] Fetching files for pull request ${context.payload.number}`
`[${eventName}] Fetching files for pull request ${context.payload.number}`
)

filesPromises.push(
Expand All @@ -65,7 +50,7 @@ export default class Git {
)
break
default:
return assertUnsupportedEvent(context)
assertUnsupportedEvent(eventName)
}

const files = await Promise.all(filesPromises)
Expand Down Expand Up @@ -115,13 +100,3 @@ export default class Git {
return response.data.files
}
}

function assertUnsupportedEvent(context: Context): never {
throw new Error(
`Unsupported event ${
context.eventName
} (currently supported events include ${Object.values(
ContextEventName
).join(', ')})`
)
}
21 changes: 21 additions & 0 deletions src/git/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Image from '../image'

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

export type SupportedEvent = typeof supportedEvents[number]

export interface File {
filename: string
status: string
}

export interface Commit {
files: Image[]
userName: string
userEmail: string
message: string
}

0 comments on commit cb48fc0

Please sign in to comment.