Skip to content

Commit f3c8a3b

Browse files
authored
Add GitHub token support for better PR number detection (#3)
1 parent 5d5f265 commit f3c8a3b

7 files changed

Lines changed: 55 additions & 24 deletions

File tree

README.md

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ Now your PRs get friendly names automatically. Your teammates will thank you (pr
4141
4242
### Inputs
4343
44-
| Input | Description | Required | Default |
45-
| ---------- | ------------------------------------------------------------- | -------- | ------------- |
46-
| `number` | Number to convert (auto-detects PR number) | No | Auto-detected |
47-
| `theme` | Word theme to use (see available themes below) | No | `cities-20` |
48-
| `template` | Output template with `{codename}` and `{number}` placeholders | No | - |
44+
| Input | Description | Required | Default |
45+
| ---------- | ------------------------------------------------------------- | -------- | ---------------------- |
46+
| `number` | Number to convert (auto-detects PR number) | No | Auto-detected |
47+
| `theme` | Word theme to use (see available themes below) | No | `cities-20` |
48+
| `template` | Output template with `{codename}` and `{number}` placeholders | No | - |
49+
| `token` | GitHub token for API access (for push events) | No | `GITHUB_TOKEN` env var |
4950

5051
### Outputs
5152

@@ -188,10 +189,37 @@ The magic happens in the [codenames library](https://github.com/kriasoft/codenam
188189
189190
No randomness, no database, no external calls. Just pure, predictable naming.
190191
192+
## GitHub Token Setup
193+
194+
For push events and other non-PR contexts, the action needs GitHub API access to find the associated PR. Add these permissions to your workflow:
195+
196+
```yaml
197+
jobs:
198+
codename:
199+
runs-on: ubuntu-latest
200+
permissions:
201+
contents: read
202+
pull-requests: read # Required for API fallback
203+
steps:
204+
- uses: kriasoft/pr-codename@v1
205+
with:
206+
token: ${{ secrets.GITHUB_TOKEN }} # Explicit token
207+
```
208+
209+
Or use the environment variable approach:
210+
211+
```yaml
212+
- uses: kriasoft/pr-codename@v1
213+
env:
214+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Environment variable
215+
```
216+
217+
The action will gracefully handle missing tokens by falling back to manual number input.
218+
191219
## Common Issues
192220
193221
**Q: Why am I getting "Could not determine number"?**
194-
A: The action tries to auto-detect PR numbers, but if you're not in a PR context, pass the `number` input manually.
222+
A: The action tries to auto-detect PR numbers, but if you're not in a PR context, pass the `number` input manually or ensure proper GitHub token permissions.
195223

196224
**Q: Can I use custom word lists?**
197225
A: Not directly in this action, but you can fork the [codenames library](https://github.com/kriasoft/codenames) and add your own themes.

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ inputs:
1313
template:
1414
description: "Output template with placeholders. Supports: {codename}, {number}"
1515
required: false
16+
token:
17+
description: "GitHub token for API access (defaults to GITHUB_TOKEN environment variable)"
18+
required: false
1619

1720
outputs:
1821
codename:

dist/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29899,7 +29899,7 @@ async function getNumber() {
2989929899
return _actions_github__WEBPACK_IMPORTED_MODULE_1__.context.issue.number;
2990029900
}
2990129901
// Method 4: API fallback using commit SHA
29902-
const token = process.env.GITHUB_TOKEN;
29902+
const token = (0,_actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput)("token") || process.env.GITHUB_TOKEN;
2990329903
if (!token) {
2990429904
console.log("No GITHUB_TOKEN provided for API fallback");
2990529905
return null;

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ async function getNumber(): Promise<number | null> {
7373
}
7474

7575
// Method 4: API fallback using commit SHA
76-
const token = process.env.GITHUB_TOKEN;
76+
const token = getInput("token") || process.env.GITHUB_TOKEN;
7777
if (!token) {
7878
console.log("No GITHUB_TOKEN provided for API fallback");
7979
return null;

package-lock.json

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pr-codename",
3-
"version": "0.0.0",
3+
"version": "1.0.0",
44
"private": true,
55
"type": "module",
66
"dependencies": {
@@ -9,10 +9,10 @@
99
"codenames": "^1.1.0"
1010
},
1111
"devDependencies": {
12-
"@eslint/js": "^9.31.0",
12+
"@eslint/js": "^9.32.0",
1313
"@types/node": "^24.1.0",
1414
"@vercel/ncc": "^0.38.3",
15-
"eslint": "^9.31.0",
15+
"eslint": "^9.32.0",
1616
"jiti": "^2.5.1",
1717
"prettier": "^3.6.2",
1818
"typescript": "^5.8.3",

0 commit comments

Comments
 (0)