Skip to content

Commit 68894b6

Browse files
committed
update main
1 parent 4b133dc commit 68894b6

2 files changed

Lines changed: 123 additions & 48 deletions

File tree

dist/main.cjs

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15044,16 +15044,35 @@ var import_auth_app = __toESM(require_dist_node12(), 1);
1504415044

1504515045
// lib/main.js
1504615046
async function main(appId2, privateKey2, owner2, repositories2, core2, createAppAuth2, request2) {
15047-
let org = "";
15048-
if (owner2.length == 0) {
15049-
org = process.env.GITHUB_REPOSITORY_OWNER || "";
15050-
}
15051-
if (owner2.length == 0 && repositories2.length == 0) {
15052-
repositories2 = process.env.GITHUB_REPOSITORY?.split("/")[1] || "";
15053-
}
15054-
let repos = [];
15055-
if (repositories2.trim() != "") {
15056-
repos = repositories2.split(",").map((repo) => repo.trim());
15047+
let parsedOwner = "";
15048+
let parsedRepositoryNames = "";
15049+
if (!owner2 && !repositories2) {
15050+
[parsedOwner, parsedRepositoryNames] = String(
15051+
process.env.GITHUB_REPOSITORY
15052+
).split("/");
15053+
core2.info(
15054+
`owner and repositories not set, creating token for the current repository ("${parsedRepositoryNames}")`
15055+
);
15056+
}
15057+
if (owner2 && !repositories2) {
15058+
parsedOwner = owner2;
15059+
core2.info(
15060+
`repositories not set, creating token for all repositories for given owner "${owner2}"`
15061+
);
15062+
}
15063+
if (!owner2 && repositories2) {
15064+
parsedOwner = String(process.env.GITHUB_REPOSITORY_OWNER);
15065+
parsedRepositoryNames = repositories2;
15066+
core2.info(
15067+
`owner not set, creating owner for given repositories "${repositories2}" in current owner ("${parsedOwner}")`
15068+
);
15069+
}
15070+
if (owner2 && repositories2) {
15071+
parsedOwner = owner2;
15072+
parsedRepositoryNames = repositories2;
15073+
core2.info(
15074+
`owner and repositories set, creating token for repositories "${repositories2}" owned by "${owner2}"`
15075+
);
1505715076
}
1505815077
const auth = createAppAuth2({
1505915078
appId: appId2,
@@ -15063,30 +15082,43 @@ async function main(appId2, privateKey2, owner2, repositories2, core2, createApp
1506315082
const appAuthentication = await auth({
1506415083
type: "app"
1506515084
});
15066-
const { data: installation } = await request2(
15067-
"GET /orgs/{org}/installation",
15068-
{
15069-
org,
15085+
let authentication;
15086+
if (parsedRepositoryNames) {
15087+
const response = await request2("GET /repos/{owner}/{repo}/installation", {
15088+
owner: parsedOwner,
15089+
repo: parsedRepositoryNames.split(",")[0],
1507015090
headers: {
1507115091
authorization: `bearer ${appAuthentication.token}`
1507215092
}
15073-
}
15074-
);
15075-
let authentication;
15076-
if (repositories2.length == 0) {
15093+
});
1507715094
authentication = await auth({
1507815095
type: "installation",
15079-
installationId: installation.id
15096+
installationId: response.data.id,
15097+
repositoryNames: parsedRepositoryNames.split(",")
1508015098
});
1508115099
} else {
15100+
const response = await request2("GET /orgs/{org}/installation", {
15101+
org: parsedOwner,
15102+
headers: {
15103+
authorization: `bearer ${appAuthentication.token}`
15104+
}
15105+
}).catch((error) => {
15106+
if (error.status !== 404)
15107+
throw error;
15108+
return request2("GET /users/{username}/installation", {
15109+
username: parsedOwner,
15110+
headers: {
15111+
authorization: `bearer ${appAuthentication.token}`
15112+
}
15113+
});
15114+
});
1508215115
authentication = await auth({
1508315116
type: "installation",
15084-
installationId: installation.id,
15085-
repositoryNames: repos
15117+
installationId: response.data.id
1508615118
});
1508715119
}
15088-
core2.setSecret(authentication.token);
1508915120
core2.setOutput("token", authentication.token);
15121+
core2.setSecret(authentication.token);
1509015122
core2.saveState("token", authentication.token);
1509115123
}
1509215124

lib/main.js

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,47 @@ export async function main(
1818
createAppAuth,
1919
request
2020
) {
21+
let parsedOwner = "";
22+
let parsedRepositoryNames = "";
2123

22-
let org = "";
23-
if (owner.length == 0) {
24-
org = process.env.GITHUB_REPOSITORY_OWNER || "";
24+
// if neither owner nor repositories are set, default to current repository
25+
if (!owner && !repositories) {
26+
[parsedOwner, parsedRepositoryNames] = String(
27+
process.env.GITHUB_REPOSITORY
28+
).split("/");
29+
30+
core.info(
31+
`owner and repositories not set, creating token for the current repository ("${parsedRepositoryNames}")`
32+
);
33+
}
34+
35+
// if only an owner is set, default to all repositories from that owner
36+
if (owner && !repositories) {
37+
parsedOwner = owner;
38+
39+
core.info(
40+
`repositories not set, creating token for all repositories for given owner "${owner}"`
41+
);
2542
}
2643

27-
if (owner.length == 0 && repositories.length == 0) {
28-
repositories = process.env.GITHUB_REPOSITORY?.split("/")[1] || "";
44+
// if repositories are set, but no owner, default to `GITHUB_REPOSITORY_OWNER`
45+
if (!owner && repositories) {
46+
parsedOwner = String(process.env.GITHUB_REPOSITORY_OWNER);
47+
parsedRepositoryNames = repositories;
48+
49+
core.info(
50+
`owner not set, creating owner for given repositories "${repositories}" in current owner ("${parsedOwner}")`
51+
);
2952
}
3053

31-
let repos = [];
32-
if (repositories.trim() != "") {
33-
repos = repositories.split(",").map((repo) => repo.trim());
54+
// if both owner and repositories are set, use those values
55+
if (owner && repositories) {
56+
parsedOwner = owner;
57+
parsedRepositoryNames = repositories;
58+
59+
core.info(
60+
`owner and repositories set, creating token for repositories "${repositories}" owned by "${owner}"`
61+
);
3462
}
3563

3664
const auth = createAppAuth({
@@ -43,39 +71,54 @@ export async function main(
4371
type: "app",
4472
});
4573

46-
// Get the installation ID
74+
let authentication;
75+
// If at least one repository is set, get installation ID from that repository
4776
// https://docs.github.com/en/rest/apps/apps?apiVersion=2022-11-28#get-a-repository-installation-for-the-authenticated-app
48-
const { data: installation } = await request(
49-
"GET /orgs/{org}/installation",
50-
{
51-
org,
77+
if (parsedRepositoryNames) {
78+
const response = await request("GET /repos/{owner}/{repo}/installation", {
79+
owner: parsedOwner,
80+
repo: parsedRepositoryNames.split(",")[0],
5281
headers: {
5382
authorization: `bearer ${appAuthentication.token}`,
5483
},
55-
}
56-
);
57-
58-
// Create a new installation token
59-
let authentication;
84+
});
6085

61-
if (repositories.length == 0) {
86+
// get token for given repositories
6287
authentication = await auth({
6388
type: "installation",
64-
installationId: installation.id,
89+
installationId: response.data.id,
90+
repositoryNames: parsedRepositoryNames.split(","),
6591
});
6692
} else {
93+
// otherwise get the installation for the owner which can either be an organization or a user account
94+
// https://docs.github.com/en/rest/apps/apps?apiVersion=2022-11-28#get-a-repository-installation-for-the-authenticated-app
95+
const response = await request("GET /orgs/{org}/installation", {
96+
org: parsedOwner,
97+
headers: {
98+
authorization: `bearer ${appAuthentication.token}`,
99+
},
100+
}).catch((error) => {
101+
if (error.status !== 404) throw error;
102+
103+
// https://docs.github.com/en/rest/apps/apps?apiVersion=2022-11-28#get-a-user-installation-for-the-authenticated-app
104+
return request("GET /users/{username}/installation", {
105+
username: parsedOwner,
106+
headers: {
107+
authorization: `bearer ${appAuthentication.token}`,
108+
},
109+
});
110+
});
111+
112+
// get token for for all repositories of the given installation
67113
authentication = await auth({
68114
type: "installation",
69-
installationId: installation.id,
70-
repositoryNames: repos,
115+
installationId: response.data.id,
71116
});
72117
}
73-
74-
// Register the token with the runner as a secret to ensure it is masked in logs
75-
core.setSecret(authentication.token);
76118

77119
core.setOutput("token", authentication.token);
78-
120+
// Register the token with the runner as a secret to ensure it is masked in logs
121+
core.setSecret(authentication.token);
79122
// Make token accessible to post function (so we can invalidate it)
80123
core.saveState("token", authentication.token);
81124
}

0 commit comments

Comments
 (0)