Skip to content

Commit

Permalink
Workflow to Automatically Title Case PRs (#468)
Browse files Browse the repository at this point in the history
# Description

Hooray, more JavaScript!
I tested this on a personal repo with a new PAT, but it *should* work
here first try.

---------

Signed-off-by: DEATHB4DEFEAT <[email protected]>
  • Loading branch information
DEATHB4DEFEAT authored Jun 17, 2024
1 parent 2bf744d commit 5a379f2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/prtitlecase.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: PR Title Case
on:
pull_request_target:
types: [opened, edited, synchronize]

env:
GITHUB_TOKEN: ${{ secrets.BOT_TOKEN }}
PR_NUMBER: ${{ github.event.pull_request.number }}

jobs:
prtitlecase:
runs-on: ubuntu-latest
steps:
- name: Checkout Master
uses: actions/checkout@v3
with:
token: ${{ secrets.BOT_TOKEN }}

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 18.x

- name: Install Dependencies
run: |
cd "Tools/prtitlecase"
npm install
shell: bash

- name: Change Title
run: |
cd "Tools/prtitlecase"
node src/index.js
shell: bash
2 changes: 2 additions & 0 deletions Tools/changelogs/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"name": "changelogs",
"author": "DEATHB4DEFEAT",
"license": "MIT",
"dependencies": {
"axios": "^1.3.4",
"js-yaml": "^4.1.0"
Expand Down
19 changes: 19 additions & 0 deletions Tools/prtitlecase/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import axios from 'axios';
import { titleCase } from 'title-case';

if (process.env.GITHUB_TOKEN) axios.defaults.headers.common['Authorization'] = `token ${process.env.GITHUB_TOKEN}`;
else throw new Error('BOT_TOKEN was not provided in repository secrets or GITHUB_TOKEN was not set correctly.');


// Get PR title
let prTitle = await axios.get(`https://api.github.com/repos/${process.env.GITHUB_REPOSITORY}/pulls/${process.env.PR_NUMBER}`)
.then(res => res.data.title);

// Title case PR title
console.log(`Old PR title: ${prTitle}`);
prTitle = titleCase(prTitle);
console.log(`New PR title: ${prTitle}`);

// Update PR title
await axios.patch(`https://api.github.com/repos/${process.env.GITHUB_REPOSITORY}/pulls/${process.env.PR_NUMBER}`,
{ title: prTitle });
12 changes: 12 additions & 0 deletions Tools/prtitlecase/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "prtitlecase",
"description": "Converts PR titles to Title Case using title-case package",
"type": "module",
"exports": "./index.js",
"author": "DEATHB4DEFEAT",
"license": "MIT",
"dependencies": {
"axios": "^1.7.2",
"title-case": "^4.3.1"
}
}

0 comments on commit 5a379f2

Please sign in to comment.