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

Added support for labels and existing PR check (v2 from master) #61

Merged
merged 6 commits into from Jul 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
81 changes: 81 additions & 0 deletions .github/workflows/master-CI.yml
Expand Up @@ -113,3 +113,84 @@ jobs:
https://api.github.com/repos/${{ github.repository }}/pulls/${{ steps.create-pr-with-reviewers.outputs.number }} \
-d '{"state":"closed"}'
if: always()

create-pr-with-labels:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: |
git config user.email noreply@github.com
git config user.name "GitHub actions"
git checkout -b feature/test/${{ github.sha }}-labels
echo date > current-date.txt
git add current-date.txt
git commit --message "Some changes"
git push origin feature/test/${{ github.sha }}-labels
- uses: ./
id: create-pr-with-labels
with:
head: feature/test/${{ github.sha }}-labels
title: Testing commit ${{ github.sha }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
labels: label1,label2
- name: Validate that PR exists
run: curl --fail https://api.github.com/repos/${{ github.repository }}/pulls/${{ steps.create-pr-with-labels.outputs.number }}
- name: Close PR
run: |
curl \
-X PATCH \
-H 'Accept: application/vnd.github.v3+json' \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Content-Type: application/json" \
https://api.github.com/repos/${{ github.repository }}/pulls/${{ steps.create-pr-with-labels.outputs.number }} \
-d '{"state":"closed"}'
if: always()

do-not-fail-on-existing_pr:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: |
git config user.email noreply@github.com
git config user.name "GitHub actions"
git checkout -b feature/test/${{ github.sha }}-idempotent-case
echo date > current-date.txt
git add current-date.txt
git commit --message "Some changes"
git push origin feature/test/${{ github.sha }}-idempotent-case
- uses: ./
id: create-pr
with:
head: feature/test/${{ github.sha }}-idempotent-case
title: Testing commit ${{ github.sha }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Validate that PR exists
run: curl --fail https://api.github.com/repos/${{ github.repository }}/pulls/${{ steps.create-pr.outputs.number }}
- uses: ./
id: second-create-pr
with:
head: feature/test/${{ github.sha }}-idempotent-case
title: Testing commit ${{ github.sha }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Validate that only first PR is created
run: |
if [ "${{ steps.create-pr.outputs.created }}" != "true" ]
then
echo "First PR is not created: ${{ steps.create-pr.outputs.created }}"
exit 1
fi
if [ "${{ steps.second-create-pr.outputs.created }}" != "false" ]
then
echo "Second PR is created: ${{ steps.second-create-pr.outputs.created }}"
exit 1
fi
- name: Close PR
run: |
curl \
-X PATCH \
-H 'Accept: application/vnd.github.v3+json' \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-H "Content-Type: application/json" \
https://api.github.com/repos/${{ github.repository }}/pulls/${{ steps.create-pr.outputs.number }} \
-d '{"state":"closed"}'
if: always()
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Support for adding labels by setting `labels`
- Check for existing pull request and `created` action output

## [1.1.0] - 2021-06-16

### Added
Expand Down
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -40,3 +40,11 @@ For self-hosted runners behind a corporate proxy, set the https_proxy environmen
env:
https_proxy: http://<proxy_address>:<port>
```

### Action outputs

The following outputs can be set by action

- `number` - Number of the created pull request.
- `html_url` - URL of the created pull request.
- `created` - 'true' if pull request was successfully created, 'false' if pull request existed already.
26 changes: 26 additions & 0 deletions __tests__/getInputs.test.ts
Expand Up @@ -76,3 +76,29 @@ it('should trim reviewer names', function() {

expect(inputs).toHaveProperty('reviewers', ['d4nte', 'bonomat', 'luckysori']);
});

it('should default to empty list of labels', function() {
const inputs = morph(getInputs, {
...MANDATORY_INPUTS,
});

expect(inputs).toHaveProperty('labels', []);
});

it('should split labels by comma', function() {
const inputs = morph(getInputs, {
...MANDATORY_INPUTS,
INPUT_LABELS: 'label1,label2',
});

expect(inputs).toHaveProperty('labels', ['label1', 'label2']);
});

it('should trim labels', function() {
const inputs = morph(getInputs, {
...MANDATORY_INPUTS,
INPUT_LABELS: 'label1, label2, label3',
});

expect(inputs).toHaveProperty('labels', ['label1', 'label2', 'label3']);
});
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/getInputs.ts
@@ -1,5 +1,6 @@
import { getInput } from '@actions/core/lib/core';
import {
IssuesAddLabelsParams,
PullsCreateParams,
PullsCreateReviewRequestParams,
} from '@octokit/plugin-rest-endpoint-methods/dist-types/generated/rest-endpoint-methods-types';
Expand All @@ -8,6 +9,9 @@ type Inputs =
& PullsCreateParams
& Required<
Omit<PullsCreateReviewRequestParams, 'pull_number' | 'team_reviewers'>
>
& Required<
Omit<IssuesAddLabelsParams, 'issue_number'>
>;

export function getInputs(): Inputs {
Expand All @@ -17,6 +21,7 @@ export function getInputs(): Inputs {
const draft = getInput('draft') ? JSON.parse(getInput('draft')) : undefined;
const body = getInput('body') || undefined;
const reviewers = getInput('reviewers');
const labels = getInput('labels');

const githubRepository = process.env.GITHUB_REPOSITORY;

Expand All @@ -37,5 +42,8 @@ export function getInputs(): Inputs {
reviewers: reviewers
? reviewers.split(',').map(reviewer => reviewer.trim())
: [],
labels: labels
? labels.split(',').map(label => label.trim())
: [],
};
}
18 changes: 16 additions & 2 deletions src/index.ts
Expand Up @@ -6,7 +6,7 @@ import { getInputs } from './getInputs';

async function run(): Promise<void> {
try {
const { reviewers, ...pullParams } = getInputs();
const { reviewers, labels, ...pullParams } = getInputs();

const options: OctokitOptions = {};
options.baseUrl = process.env.GITHUB_API_URL;
Expand All @@ -32,10 +32,24 @@ async function run(): Promise<void> {
});
}

if (labels.length > 0) {
await octokit.issues.addLabels({
owner: pullParams.owner,
repo: pullParams.repo,
issue_number: pullNumber,
labels: labels,
});
}

setOutput('number', pullNumber.toString());
setOutput('html_url', htmlUrl);
setOutput('created', 'true');
} catch (error) {
setFailed(error.message);
if (error.message && error.message.includes('A pull request already exists')) {
setOutput('created', 'false');
} else {
setFailed(error.message);
}
}
}

Expand Down