Skip to content

Commit

Permalink
feat: composite actions to work with github PRs
Browse files Browse the repository at this point in the history
  • Loading branch information
lotyp committed Sep 5, 2024
1 parent a6bf8fb commit d769688
Show file tree
Hide file tree
Showing 10 changed files with 293 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
runs-on: ${{ inputs.os }}
steps:
- name: 📦 Check out the codebase
uses: actions/checkout@v4
uses: actions/checkout@v4.1.7

- name: 🛠️ Install goss and dgoss
uses: e1himself/goss-installation-action@v1.2.1
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/create-changesets-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:

steps:
- name: 📦 Check out the codebase
uses: actions/checkout@v4
uses: actions/checkout@v4.1.7
with:
fetch-depth: 0

Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/integrate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ jobs:
pull-requests: read
steps:
- name: 📦 Check out the codebase
uses: actions/checkout@v4.1.5
uses: actions/checkout@v4.1.7

- name: 🧐 Lint commits using "commitlint"
uses: wagoid/commitlint-github-action@v6.0.1
uses: wagoid/commitlint-github-action@v6.1.1
with:
configFile: ${{ github.workspace }}/.github/.commitlint.config.mjs
failOnWarnings: false
Expand All @@ -37,7 +37,7 @@ jobs:
group: coding-standards-${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
steps:
- name: 📦 Check out the codebase
uses: actions/checkout@v4.1.5
uses: actions/checkout@v4.1.7

- name: 🧐 Lint YAML files
uses: ibiqlik/action-yamllint@v3.1.1
Expand All @@ -54,7 +54,7 @@ jobs:
group: markdown-linting-${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
steps:
- name: 📦 Check out the codebase
uses: actions/checkout@v4.1.5
uses: actions/checkout@v4.1.7

- name: 🧐 Lint Markdown files
uses: DavidAnson/markdownlint-cli2-action@v16.0.0
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/shellcheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
runs-on: ${{ inputs.os }}
steps:
- name: 📦 Check out the codebase
uses: actions/checkout@v4
uses: actions/checkout@v4.1.7
with:
fetch-depth: 0

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/triage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ name: 🏷️ Add labels

jobs:
label:
uses: wayofdev/gh-actions/.github/workflows/apply-labels.yml@v3.1.0
uses: wayofdev/gh-actions/.github/workflows/apply-labels.yml@v3.1.1
with:
os: ubuntu-latest
secrets:
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repos:
- id: fix-encoding-pragma

- repo: https://github.com/commitizen-tools/commitizen
rev: v3.24.0
rev: v3.29.0
hooks:
- id: commitizen
stages:
Expand Down
74 changes: 74 additions & 0 deletions actions/github/pull-request/add-assignee/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---

# https://docs.github.com/en/actions/creating-actions/creating-a-composite-action
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#inputs
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runs-for-composite-run-steps-actions
# https://docs.github.com/en/rest/issues/assignees#add-assignees-to-an-issue
# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request
# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#workflow_run

name: 🙋‍♂️ Add assignee to pull request

description: Adds an assignee to a pull request

inputs:
assignee:
description: Username of user to add as an assignee to a pull request
required: true
github-token:
description: GitHub token of a user with permission to add assignees to a pull request
required: true

runs:
using: 'composite'

steps:
- name: 🤔 Determine pull request number
uses: actions/github-script@v7.0.1
with:
github-token: "${{ inputs.github-token }}"
script: |
if (
context.eventName == 'pull_request' ||
context.eventName == 'pull_request_target'
) {
core.exportVariable("PULL_REQUEST_NUMBER", context.payload.pull_request.number);
return;
}
if (context.eventName == 'workflow_run') {
core.exportVariable("PULL_REQUEST_NUMBER", context.payload.workflow_run.pull_requests[0].number);
return;
}
core.setFailed(`Unable to determine the pull request number for event "${context.eventName}"`);
- name: 🙋‍♂️ Add assignee to pull request
uses: actions/github-script@v7.0.1
env:
ASSIGNEE: "${{ inputs.assignee }}"
with:
github-token: "${{ inputs.github-token }}"
script: |
if (!process.env.PULL_REQUEST_NUMBER) {
core.setFailed("The environment variable PULL_REQUEST_NUMBER is not defined.")
return;
}
const assignees = [
process.env.ASSIGNEE,
];
try {
await github.rest.issues.addAssignees({
assignees: assignees,
issue_number: process.env.PULL_REQUEST_NUMBER,
owner: context.repo.owner,
repo: context.repo.repo,
});
} catch (error) {
core.setFailed(error.message);
}
65 changes: 65 additions & 0 deletions actions/github/pull-request/approve/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---

# https://docs.github.com/en/actions/creating-actions/creating-a-composite-action
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#inputs
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runs-for-composite-run-steps-actions
# https://docs.github.com/en/rest/pulls/reviews#create-a-review-for-a-pull-request
# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request
# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#workflow_run

name: ✅ Approve pull request

description: Approves a pull request

inputs:
github-token:
description: GitHub token of a user with permission to approve a pull request
required: true

runs:
using: 'composite'

steps:
- name: 🤔 Determine pull request number
uses: actions/github-script@v7.0.1
with:
github-token: "${{ inputs.github-token }}"
script: |
if (
context.eventName == 'pull_request' ||
context.eventName == 'pull_request_target'
) {
core.exportVariable("PULL_REQUEST_NUMBER", context.payload.pull_request.number);
return;
}
if (context.eventName == 'workflow_run') {
core.exportVariable("PULL_REQUEST_NUMBER", context.payload.workflow_run.pull_requests[0].number);
return;
}
core.setFailed(`Unable to determine the pull request number for event "${context.eventName}"`);
- name: ✅ Approve pull request
uses: actions/github-script@v7.0.1
with:
github-token: "${{ inputs.github-token }}"
script: |
if (!process.env.PULL_REQUEST_NUMBER) {
core.setFailed("The environment variable PULL_REQUEST_NUMBER is not defined.");
return;
}
try {
await github.rest.pulls.createReview({
event: "APPROVE",
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: process.env.PULL_REQUEST_NUMBER,
});
} catch (error) {
core.setFailed(error.message);
}
71 changes: 71 additions & 0 deletions actions/github/pull-request/merge/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
---

# https://docs.github.com/en/actions/creating-actions/creating-a-composite-action
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#inputs
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runs-for-composite-run-steps-actions
# https://docs.github.com/en/rest/pulls/pulls#merge-a-pull-request
# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request
# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#workflow_run

name: 📥 Merge pull request

description: Merges a pull request

inputs:
github-token:
description: GitHub token of a user with permission to merge a pull request
required: true
merge-method:
default: merge
description: 'Which merge method to use, one "merge", "rebase", "squash"'
required: true

runs:
using: 'composite'

steps:
- name: 🤔 Determine pull request number
uses: actions/github-script@v7.0.1
with:
github-token: "${{ inputs.github-token }}"
script: |
if (
context.eventName == 'pull_request' ||
context.eventName == 'pull_request_target'
) {
core.exportVariable("PULL_REQUEST_NUMBER", context.payload.pull_request.number);
return;
}
if (context.eventName == 'workflow_run') {
core.exportVariable("PULL_REQUEST_NUMBER", context.payload.workflow_run.pull_requests[0].number);
return;
}
core.setFailed(`Unable to determine the pull request number for event "${context.eventName}"`);
- name: 📥 Merge pull request
uses: actions/github-script@v7.0.1
env:
MERGE_METHOD: "${{ inputs.merge-method }}"
with:
github-token: "${{ inputs.github-token }}"
script: |
if (!process.env.PULL_REQUEST_NUMBER) {
core.setFailed("The environment variable PULL_REQUEST_NUMBER is not defined.");
return;
}
try {
await github.rest.pulls.merge({
merge_method: process.env.MERGE_METHOD,
owner: context.repo.owner,
pull_number: process.env.PULL_REQUEST_NUMBER,
repo: context.repo.repo,
});
} catch (error) {
core.setFailed(error.message);
}
74 changes: 74 additions & 0 deletions actions/github/pull-request/request-review/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---

# https://docs.github.com/en/actions/creating-actions/creating-a-composite-action
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#inputs
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runs-for-composite-run-steps-actions
# https://docs.github.com/en/rest/pulls/review-requests#request-reviewers-for-a-pull-request
# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request
# https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#workflow_run

name: Request review from reviewer for pull request

description: Requests a review from a reviewer for a pull request

inputs:
github-token:
description: GitHub token of a user with permission to request reviewers for a pull request
required: true
reviewer:
description: Username of user to request review from for a pull request
required: true

runs:
using: 'composite'

steps:
- name: 🤔 Determine pull request number
uses: actions/github-script@v7.0.1
with:
github-token: "${{ inputs.github-token }}"
script: |
if (
context.eventName == 'pull_request' ||
context.eventName == 'pull_request_target'
) {
core.exportVariable("PULL_REQUEST_NUMBER", context.payload.pull_request.number);
return;
}
if (context.eventName == 'workflow_run') {
core.exportVariable("PULL_REQUEST_NUMBER", context.payload.workflow_run.pull_requests[0].number);
return;
}
core.setFailed(`Unable to determine the pull request number for event "${context.eventName}"`);
- name: "Request reviewer"
uses: "actions/github-script@v7.0.1"
env:
REVIEWER: "${{ inputs.reviewer }}"
with:
github-token: "${{ inputs.github-token }}"
script: |
if (!process.env.PULL_REQUEST_NUMBER) {
core.setFailed("The environment variable PULL_REQUEST_NUMBER is not defined.");
return;
}
const reviewers = [
process.env.REVIEWER,
];
try {
await github.rest.pulls.requestReviewers({
owner: context.repo.owner,
pull_number: process.env.PULL_REQUEST_NUMBER,
repo: context.repo.repo,
reviewers: reviewers,
});
} catch (error) {
core.setFailed(error.message);
}

0 comments on commit d769688

Please sign in to comment.