Skip to content

Commit

Permalink
Add support for github tokens for private repos. (#83)
Browse files Browse the repository at this point in the history
Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
  • Loading branch information
Marenz authored Aug 4, 2023
1 parent 294a997 commit 4170644
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ The message to emit when the check fails. All other inputs can be specified in
the message using `${}` syntax, e.g. `${file-pattern}` for the `file-pattern`
input. All values will be quoted for easy identification of any whitespace.

### `token`

A GitHub auth token to use for private repositories. Falls back to anonymous access if
not provided. Usually you want to use `${{ secrets.GITHUB_TOKEN }}` for this.

## Example usage (for requiring a news entry file)

E.g. for use with [scriv](https://scriv.readthedocs.io/):
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ inputs:
description: "String template used when the check fails"
required: false
default: "the prerequisite file pattern ${prereq-pattern} matched changed files in the pull request, but the ${file-pattern} pattern did NOT and the ${skip-label} label is not set"
token:
description: "GitHub token to use to checkout the pull request files."
default: ""
required: False
runs:
using: "node16"
main: "dist/index.js"
5 changes: 4 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11624,6 +11624,7 @@ exports.changedFiles = exports.pullRequestLabels = exports.pullRequestPayload =
const github = __importStar(__nccwpck_require__(5438));
const core_1 = __nccwpck_require__(6762);
const plugin_paginate_rest_1 = __nccwpck_require__(4193);
const core = __importStar(__nccwpck_require__(2186));
function isPullRequest(eventName, payload) {
return eventName === "pull_request";
}
Expand Down Expand Up @@ -11651,7 +11652,9 @@ exports.pullRequestLabels = pullRequestLabels;
*/
async function changedFiles(payload) {
const MyOctokit = core_1.Octokit.plugin(plugin_paginate_rest_1.paginateRest);
const octokit = new MyOctokit(); // Anonymous to avoid asking for an access token.
// Get the token from the inputs
const token = core.getInput("token");
const octokit = token ? new MyOctokit({ auth: token }) : new MyOctokit();
return await octokit.paginate("GET /repos/{owner}/{repo}/pulls/{pull_number}/files", {
owner: payload.repository.owner.login,
repo: payload.repository.name,
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion src/gh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as github from "@actions/github";
import { Octokit } from "@octokit/core";
import { paginateRest } from "@octokit/plugin-paginate-rest";
import { EventPayloads } from "@octokit/webhooks";
import * as core from "@actions/core";

function isPullRequest(
eventName: string,
Expand Down Expand Up @@ -40,7 +41,11 @@ export async function changedFiles(
payload: EventPayloads.WebhookPayloadPullRequest
): Promise<string[]> {
const MyOctokit = Octokit.plugin(paginateRest);
const octokit = new MyOctokit(); // Anonymous to avoid asking for an access token.

// Get the token from the inputs
const token = core.getInput("token");

const octokit = token ? new MyOctokit({ auth: token }) : new MyOctokit();

return await octokit.paginate(
"GET /repos/{owner}/{repo}/pulls/{pull_number}/files",
Expand Down

0 comments on commit 4170644

Please sign in to comment.