Fix file count GitHub Action permissions to allow label creation#713
Merged
Conversation
Contributor
|
Important Review skippedBot user detected. To trigger a single review, invoke the You can disable this status message by setting the Comment |
Co-authored-by: A1L13N <193832434+A1L13N@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix file count in GitHub Action
Fix file count GitHub Action permissions to allow label creation
Oct 14, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
pr-file-count-labelerGitHub Action workflow was failing with the error:This occurred when the workflow attempted to create repository labels dynamically (e.g.,
f1,f2,f3) to indicate the number of files changed in a PR.Root Cause
The workflow had
contents: readpermission, which is insufficient for creating repository labels. The GitHub API methodgithub.rest.issues.createLabel()requirescontents: writepermission to create new labels in the repository.While
issues: writepermission allows adding/removing existing labels to issues and PRs, it does not grant the ability to create new labels in the repository itself.Solution
Updated the workflow permissions from:
to:
Security Considerations
This change is safe because:
pull_request_targetwhich runs in the base repository's security contextcontents: writepermission is scoped only to this specific workflowTesting
Fixes the issue where the file count labeler workflow could not create labels for new file counts.
Original prompt
This section details on the original issue you should resolve
<issue_title>fix file count github action</issue_title>
<issue_description>Run actions/github-script@v7
with:
github-token: ***
script: const pr = context.payload.pull_request;
if (!pr) {
core.info('No pull_request in context. Skipping.');
return;
}
const owner = context.repo.owner;
const repo = context.repo.repo;
const pull_number = pr.number;
// Get all files (with pagination) and count them
const files = await github.paginate(github.rest.pulls.listFiles, {
owner,
repo,
pull_number,
per_page: 100,
});
const count = files.length;
const newLabel =
f${count};// Get current labels on the PR
const { data: current } = await github.rest.issues.listLabelsOnIssue({ owner, repo, issue_number: pull_number, per_page: 100 });
const currentNames = new Set(current.map(l => l.name));
// Remove any existing f* numeric labels (e.g., f1, f23)
const fLabelRegex = /^f\d+$/i;
for (const name of currentNames) {
if (fLabelRegex.test(name) && name !== newLabel) {
try {
await github.rest.issues.removeLabel({ owner, repo, issue_number: pull_number, name });
} catch (err) {
core.warning(
Failed to remove label ${name}: ${err.message});}
}
}
// Ensure the new label exists (create if missing)
async function ensureLabelExists(labelName) {
try {
await github.rest.issues.getLabel({ owner, repo, name: labelName });
} catch (e) {
if (e.status === 404) {
// Create with a readable teal-ish color; description explains purpose
await github.rest.issues.createLabel({
owner,
repo,
name: labelName,
color: '36b3a8',
description: 'Number of files changed in PR',
});
} else {
throw e;
}
}
}
await ensureLabelExists(newLabel);
// Add the label if it isn't already present
if (!currentNames.has(newLabel)) {
await github.rest.issues.addLabels({ owner, repo, issue_number: pull_number, labels: [newLabel] });
core.info(
Applied label ${newLabel} to PR #${pull_number});} else {
core.info(
Label ${newLabel} already present on PR #${pull_number});}
// Optional: log the count for transparency
core.info(
PR #${pull_number} has ${count} changed file(s).);RequestError [HttpError]: Resource not accessible by integration
at /home/runner/work/_actions/actions/github-script/v7/dist/index.js:9537:21
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async eval (eval at callAsyncFunction (/home/runner/work/_actions/actions/github-script/v7/dist/index.js:36187:16), :63:3)
at async main (/home/runner/work/_actions/actions/github-script/v7/dist/index.js:36285:20) {
status: 403,
response: {
url: 'https://api.github.com/repos/alphaonelabs/alphaonelabs-education-website/issues/707/labels',
status: 403,
headers: {
'access-control-allow-origin': '*',
'access-control-expose-headers': 'ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset',
'content-encoding': 'gzip',
'content-security-policy': "default-src 'none'",
'content-type': 'application/json; charset=utf-8',
date: 'Fri, 10 Oct 2025 19:25:05 GMT',
'referrer-policy': 'origin-when-cross-origin, strict-origin-when-cross-origin',
server: 'github.com',
'strict-transport-security': 'max-age=31536000; includeSubdomains; preload',
'transfer-encoding': 'chunked',
vary: 'Accept-Encoding, Accept, X-Requested-With',
'x-accepted-github-permissions': 'issues=write; pull_requests=write',
'x-content-type-options': 'nosniff',
'x-frame-options': 'deny',
'x-github-api-version-selected': '2022-11-28',
'x-github-media-type': 'github.v3; format=json',
'x-github-request-id': '0829:3B0263:3244D8:DAB5CC:68E95D91',
'x-ratelimit-limit': '5000',
'x-ratelimit-remaining': '4980',
'x-ratelimit-reset': '1760126323',
'x-ratelimit-resource': 'core',
'x-ratelimit-used': '20',
'x-xss-protection': '0'
},
Error: Unhandled error: HttpError: Resource not accessible by integration
data: {
message: 'Resource not accessible by integration',
documentation_url: 'https://docs.github.com/rest/issues/labels#add-labels-to-an-issue',
status: '403'
}
},
request: {
method: 'POST',
url: 'https://api.github.com/repos/alphaonelabs/alphaonelabs-education-website/issues/707/labels',
headers: ...
Fixes #712
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.