feat: allow repositories input to be comma or newline-separated (#169)
Resolves https://github.com/actions/create-github-app-token/issues/106 - Fixes the parsing to cope with whitespace in the input string. - Allows the input to be comma or newline-separated. (I've done this for all array-type inputs in my own actions, but I'm happy to remove this if you only want to support comma-separated.) - Added tests for parsing comma and newline-separated inputs.
This commit is contained in:
32
lib/main.js
32
lib/main.js
@@ -5,7 +5,7 @@ import pRetry from "p-retry";
|
||||
* @param {string} appId
|
||||
* @param {string} privateKey
|
||||
* @param {string} owner
|
||||
* @param {string} repositories
|
||||
* @param {string[]} repositories
|
||||
* @param {import("@actions/core")} core
|
||||
* @param {import("@octokit/auth-app").createAppAuth} createAppAuth
|
||||
* @param {import("@octokit/request").request} request
|
||||
@@ -22,21 +22,23 @@ export async function main(
|
||||
skipTokenRevoke
|
||||
) {
|
||||
let parsedOwner = "";
|
||||
let parsedRepositoryNames = "";
|
||||
let parsedRepositoryNames = [];
|
||||
|
||||
// If neither owner nor repositories are set, default to current repository
|
||||
if (!owner && !repositories) {
|
||||
[parsedOwner, parsedRepositoryNames] = String(
|
||||
if (!owner && repositories.length === 0) {
|
||||
const [owner, repo] = String(
|
||||
process.env.GITHUB_REPOSITORY
|
||||
).split("/");
|
||||
parsedOwner = owner;
|
||||
parsedRepositoryNames = [repo];
|
||||
|
||||
core.info(
|
||||
`owner and repositories not set, creating token for the current repository ("${parsedRepositoryNames}")`
|
||||
`owner and repositories not set, creating token for the current repository ("${repo}")`
|
||||
);
|
||||
}
|
||||
|
||||
// If only an owner is set, default to all repositories from that owner
|
||||
if (owner && !repositories) {
|
||||
if (owner && repositories.length === 0) {
|
||||
parsedOwner = owner;
|
||||
|
||||
core.info(
|
||||
@@ -45,22 +47,22 @@ export async function main(
|
||||
}
|
||||
|
||||
// If repositories are set, but no owner, default to `GITHUB_REPOSITORY_OWNER`
|
||||
if (!owner && repositories) {
|
||||
if (!owner && repositories.length > 0) {
|
||||
parsedOwner = String(process.env.GITHUB_REPOSITORY_OWNER);
|
||||
parsedRepositoryNames = repositories;
|
||||
|
||||
core.info(
|
||||
`owner not set, creating owner for given repositories "${repositories}" in current owner ("${parsedOwner}")`
|
||||
`owner not set, creating owner for given repositories "${repositories.join(',')}" in current owner ("${parsedOwner}")`
|
||||
);
|
||||
}
|
||||
|
||||
// If both owner and repositories are set, use those values
|
||||
if (owner && repositories) {
|
||||
if (owner && repositories.length > 0) {
|
||||
parsedOwner = owner;
|
||||
parsedRepositoryNames = repositories;
|
||||
|
||||
core.info(
|
||||
`owner and repositories set, creating token for repositories "${repositories}" owned by "${owner}"`
|
||||
`owner and repositories set, creating token for repositories "${repositories.join(',')}" owned by "${owner}"`
|
||||
);
|
||||
}
|
||||
|
||||
@@ -73,11 +75,11 @@ export async function main(
|
||||
let authentication, installationId, appSlug;
|
||||
// If at least one repository is set, get installation ID from that repository
|
||||
|
||||
if (parsedRepositoryNames) {
|
||||
if (parsedRepositoryNames.length > 0) {
|
||||
({ authentication, installationId, appSlug } = await pRetry(() => getTokenFromRepository(request, auth, parsedOwner, parsedRepositoryNames), {
|
||||
onFailedAttempt: (error) => {
|
||||
core.info(
|
||||
`Failed to create token for "${parsedRepositoryNames}" (attempt ${error.attemptNumber}): ${error.message}`
|
||||
`Failed to create token for "${parsedRepositoryNames.join(',')}" (attempt ${error.attemptNumber}): ${error.message}`
|
||||
);
|
||||
},
|
||||
retries: 3,
|
||||
@@ -144,7 +146,7 @@ async function getTokenFromRepository(request, auth, parsedOwner, parsedReposito
|
||||
// https://docs.github.com/rest/apps/apps?apiVersion=2022-11-28#get-a-repository-installation-for-the-authenticated-app
|
||||
const response = await request("GET /repos/{owner}/{repo}/installation", {
|
||||
owner: parsedOwner,
|
||||
repo: parsedRepositoryNames.split(",")[0],
|
||||
repo: parsedRepositoryNames[0],
|
||||
request: {
|
||||
hook: auth.hook,
|
||||
},
|
||||
@@ -154,11 +156,11 @@ async function getTokenFromRepository(request, auth, parsedOwner, parsedReposito
|
||||
const authentication = await auth({
|
||||
type: "installation",
|
||||
installationId: response.data.id,
|
||||
repositoryNames: parsedRepositoryNames.split(","),
|
||||
repositoryNames: parsedRepositoryNames,
|
||||
});
|
||||
|
||||
const installationId = response.data.id;
|
||||
const appSlug = response.data['app_slug'];
|
||||
|
||||
return { authentication, installationId, appSlug };
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user