Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get the PR info by ID or associated commit #12

Merged
merged 9 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
4 changes: 2 additions & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
dist/
lib/
dist/
node_modules/
jest.config.js
coverage/
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
* text=auto
* text=auto eol=lf

dist/** -diff linguist-generated=true
2 changes: 1 addition & 1 deletion .github/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ _extends: .github

repository:
name: get-pr
description: Get the PR info associated with the commit
description: Get the PR info by ID or associated commit
homepage: https://cloudposse.com/accelerate
topics: "github-action"
10 changes: 5 additions & 5 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
node-version-file: package.json
cache: yarn
- name: install dependencies
run: yarn install --frozen-lockfile --prefer-offline
run: yarn add ci
- uses: cloudposse/github-action-setup-atmos@v2
with:
install-wrapper: false
Expand All @@ -43,10 +43,10 @@ jobs:
with:
node-version: '20'
cache: 'npm'
- run: npm ci
- run: yarn add ci
- name: Build Action
run: |
npm run build
yarn build
- name: Run Action
uses: ./
id: pr
Expand All @@ -66,10 +66,10 @@ jobs:
with:
node-version: '20'
cache: 'npm'
- run: npm ci
- run: yarn add ci
- name: Build Action
run: |
npm run build
yarn build
- name: Run Action
uses: ./
id: pr
Expand Down
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ Thumbs.db

# Ignore built ts files
__tests__/runner/*
lib/**/*

#Idea
.idea/

# IDE files
.idea
.vscode
*.code-workspace

.build-harness
build-harness
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20.6.0
4 changes: 2 additions & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
dist/
lib/
node_modules/
node_modules/
coverage/
10 changes: 8 additions & 2 deletions .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
"useTabs": false,
"semi": false,
"singleQuote": true,
"quoteProps": "as-needed",
"jsxSingleQuote": false,
"trailingComma": "none",
"bracketSpacing": false,
"arrowParens": "avoid"
"bracketSpacing": true,
"bracketSameLine": true,
"arrowParens": "avoid",
"proseWrap": "always",
"htmlWhitespaceSensitivity": "css",
"endOfLine": "lf"
}
39 changes: 39 additions & 0 deletions README.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,44 @@ references: []

# How to use this project
usage: |-
The action allows getting PR info by `id` or `sha.`
`id` has precedence over `sha`. It will seek for PR by `sha` only if `id` input is empty.
goruha marked this conversation as resolved.
Show resolved Hide resolved

### By ID

```yaml
on:
pull_request:
branches:
- main
types: [opened, synchronize, reopened]

jobs:
pr:
name: PR Info
runs-on: ubuntu-latest
steps:
- uses: cloudposse-github-actions/get-pr@main
id: pr
with:
id: ${{ github.event.number }}
outputs:
base: ${{ fromJSON(steps.pr.outputs.pr).base.sha }}
head: ${{ fromJSON(steps.pr.outputs.pr).head.sha }}
found: ${{ steps.pr.outputs.found }}
json: ${{ steps.pr.outputs.json }}
number: ${{ steps.pr.outputs.number }}
title: ${{ steps.pr.outputs.title }}
body: ${{ steps.pr.outputs.body }}
url: ${{ steps.pr.outputs.url }}
created_at: ${{ steps.pr.outputs.created_at }}
merged_at: ${{ steps.pr.outputs.merged_at }}
closed_at: ${{ steps.pr.outputs.closed_at }}
labels: ${{ steps.pr.outputs.labels }}
```

### By SHA

```yaml
on:
push:
Expand Down Expand Up @@ -62,6 +100,7 @@ usage: |-
closed_at: ${{ steps.pr.outputs.closed_at }}
labels: ${{ steps.pr.outputs.labels }}
```

include:
- "docs/github-action.md"

Expand Down
9 changes: 0 additions & 9 deletions __tests__/.eslintrc.json

This file was deleted.

6 changes: 3 additions & 3 deletions __tests__/create-dummy-pr.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {PR} from '../src/types/pull-request'
import {PRSimple} from '../src/types/pull-request'

interface Options {
sha?: string
draft?: boolean
}

export default function createDummyPR(id: number, options: Options): PR {
return Object.assign({} as PR, {
export default function createDummyPR(id: number, options: Options): PRSimple {
return Object.assign({} as PRSimple, {
id,
draft: options.draft || false,
head: {
Expand Down
94 changes: 69 additions & 25 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,74 @@
import {expect, test} from '@jest/globals'
import * as octokit from "@octokit/core";
import * as github from "@actions/github";

// @ts-ignore
import createDummyPR from './create-dummy-pr'
import getLastPullRequest from '../src/get-last-pr'
import getPullRequestByID from "../src/adapter/get-pr-by-id";
import os from "os";
import {GitHub} from "@actions/github/lib/utils";

import {Api} from '@octokit/plugin-rest-endpoint-methods/dist-types/types'
import {PaginateInterface} from "@octokit/plugin-paginate-rest"
import {Constructor} from "@octokit/core/dist-types/types";

let getOctokitMock: jest.SpiedFunction<typeof github.getOctokit>
describe('action', () => {
beforeEach(() => {
jest.clearAllMocks()

getOctokitMock = jest.spyOn(github, 'getOctokit').mockImplementation()

process.env['RUNNER_TOOL_CACHE'] = os.tmpdir()
process.env['GITHUB_REPOSITORY'] = 'acme/repo'
})

it('prefers PR with commit as head SHA', async () => {
const testPRs = [
createDummyPR(1, {sha: '09e30775c'}),
createDummyPR(2, {sha: '90775cae3'})
]
const options = {
preferWithHeadSha: testPRs[1].head.sha
}
const foundPR = getLastPullRequest(testPRs, options) || {id: null}
expect(foundPR.id).toBe(testPRs[1].id)
})

it('filter out draft PRs', async () => {
const testPRs = [createDummyPR(1, {draft: true})]

const foundPR = getLastPullRequest(testPRs, {draft: false})
expect(foundPR).toBeNull()
})

it('find a draft PRs', async () => {
const testPRs = [createDummyPR(11, {draft: true})]

const foundPR = getLastPullRequest(testPRs, {draft: true}) || {id: null}
expect(foundPR.id).toBe(testPRs[0].id)
})

it('find PR by id', async () => {
getOctokitMock.mockImplementation((token: string, options?, ...additionalPlugins) => {
return {
rest: {
pulls: {
get: jest.fn().mockImplementation((params) => {
if (params.owner == 'acme' && params.repo == 'repo') {
return {data: {id: params.pull_number}}
}
return {data: null}
})
}
},
} as unknown as InstanceType<typeof GitHub>
})

const test = github.getOctokit('token')

test('prefers PR with commit as head SHA', () => {
const testPRs = [
createDummyPR(1, {sha: '09e30775c'}),
createDummyPR(2, {sha: '90775cae3'})
]
const options = {
preferWithHeadSha: testPRs[1].head.sha
}
const foundPR = getLastPullRequest(testPRs, options) || {id: null}
expect(foundPR.id).toBe(testPRs[1].id)
})

test('filter out draft PRs', () => {
const testPRs = [createDummyPR(1, {draft: true})]

const foundPR = getLastPullRequest(testPRs, {draft: false})
expect(foundPR).toBeNull()
})

test('find a draft PRs', () => {
const testPRs = [createDummyPR(11, {draft: true})]

const foundPR = getLastPullRequest(testPRs, {draft: true}) || {id: null}
expect(foundPR.id).toBe(testPRs[0].id)
})
const foundPR = await getPullRequestByID(test, 12)
expect(foundPR.id).toBe(12)
})
})
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ inputs:
description: The GitHub token used to create an authenticated client.
required: false
default: ${{ github.token }}
id:
description: PR ID to get info for.
required: false
sha:
description: Sha to get PR for. Defaults to current sha.
required: false
Expand Down
Loading
Loading