Skip to content

fix file count github action #712

@A1L13N

Description

@A1L13N

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).);

debug: false
user-agent: actions/github-script
result-encoding: json
retries: 0
retry-exempt-status-codes: 400,401,403,404,422

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: {
accept: 'application/vnd.github.v3+json',
'user-agent': 'actions/github-script octokit-core.js/5.0.1 Node.js/20.19.4 (linux; x64)',
authorization: 'token [REDACTED]',
'content-type': 'application/json; charset=utf-8'
},
body: '{"labels":["f2"]}',
request: {
agent: [Agent],
fetch: [Function: proxyFetch],
hook: [Function: bound bound register]
}
}
}

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions