Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: appId argument can be set to Client ID string #606

Merged
merged 7 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ name: Release
- "*.x"
# These are recommended by the semantic-release docs: https://github.com/semantic-release/npm#npm-provenance
permissions:
contents: write # to be able to publish a GitHub release
issues: write # to be able to comment on released issues
pull-requests: write # to be able to comment on released pull requests
id-token: write # to enable use of OIDC for npm provenance
contents: write # to be able to publish a GitHub release
issues: write # to be able to comment on released issues
pull-requests: write # to be able to comment on released pull requests
id-token: write # to enable use of OIDC for npm provenance

jobs:
release:
Expand All @@ -25,7 +25,7 @@ jobs:
cache: npm
- run: npm ci
- run: npm run build
- run: npx semantic-release
- run: npx semantic-release@beta
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.OCTOKITBOT_NPM_TOKEN }}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 1 addition & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@octokit/request-error": "^6.1.1",
"@octokit/types": "^13.4.1",
"lru-cache": "^10.0.0",
"universal-github-app-jwt": "^2.0.6",
"universal-github-app-jwt": "^2.2.0",
"universal-user-agent": "^7.0.0"
},
"devDependencies": {
Expand Down Expand Up @@ -74,15 +74,6 @@
}
},
"release": {
"branches": [
"+([0-9]).x",
"main",
"next",
{
"name": "beta",
"prerelease": true
}
],
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
Expand Down
6 changes: 4 additions & 2 deletions src/get-app-authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ export async function getAppAuthentication({
appId,
privateKey,
timeDifference,
}: State & { timeDifference?: number }): Promise<AppAuthentication> {
}: State & {
timeDifference?: number;
}): Promise<AppAuthentication> {
try {
const appAuthentication = await githubAppJwt({
id: +appId,
id: appId,
privateKey,
now: timeDifference && Math.floor(Date.now() / 1000) + timeDifference,
});
Expand Down
6 changes: 1 addition & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ export function createAppAuth(options: StrategyOptions): AuthInterface {
if (!options.appId) {
throw new Error("[@octokit/auth-app] appId option is required");
}
if (!Number.isFinite(+options.appId)) {
throw new Error(
"[@octokit/auth-app] appId option must be a number or numeric string",
);
}

if (!options.privateKey) {
throw new Error("[@octokit/auth-app] privateKey option is required");
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export type UTC_TIMESTAMP = string;
export type AppAuthentication = {
type: APP_TYPE;
token: JWT;
appId: number;
appId: number | string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a later date, maybe we could use template literal strings to better define the format

Suggested change
appId: number | string;
appId: number | `Iv1.${string}`;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, this would imply a change in universal-github-app-jwt first. Do you want me to open an issue there first?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

appId: number | Iv1.${string};

I wouldn't do that, because there is a chance that future Client IDs will start with lv2.... and I also think there are old apps that still have a client ID without the lv1 prefix

expiresAt: string;
};

Expand Down
8 changes: 3 additions & 5 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2308,15 +2308,13 @@ it("throws helpful error if `appId` is not set (#184)", async () => {
}).toThrowError("[@octokit/auth-app] appId option is required");
});

it("throws helpful error if `appId` is not set to a numeric value", async () => {
it("allows passing an `appId` as a non numeric value", async () => {
expect(() => {
createAppAuth({
appId: "not-a-number",
appId: "Iv1.0123456789abcdef",
privateKey: PRIVATE_KEY,
});
}).toThrowError(
"[@octokit/auth-app] appId option must be a number or numeric string",
);
}).not.toThrow();
});

it("throws helpful error if `privateKey` is not set properly (#184)", async () => {
Expand Down
42 changes: 42 additions & 0 deletions test/typescript-validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
// THIS CODE IS NOT EXECUTED. IT IS JUST FOR TYPECHECKING
// ************************************************************

import { getAppAuthentication } from "../src/get-app-authentication.js";
import { request } from "@octokit/request";
import { createAppAuth } from "../src/index.js";
import { State, StrategyOptions } from "../src/types.js";
import { getCache } from "../src/cache.js";
import { createOAuthAppAuth } from "@octokit/auth-oauth-app";
function isString(what: string) {}

export async function readmeExample() {
Expand Down Expand Up @@ -31,3 +36,40 @@ export async function issue282() {

isString(authentication.token);
}

// https://github.com/octokit/auth-app.js/issues/603
export async function issue603() {
createAppAuth({
appId: "Iv1.0123456789abcdef",
privateKey: "",
});

const options: StrategyOptions = {
appId: "Iv1.0123456789abcdef",
privateKey: "",
};

const state: State = Object.assign(
{
request,
cache: getCache(),
},
options,
options.installationId
? { installationId: Number(options.installationId) }
: {},
{
log: {
warn: console.warn.bind(console),
},
oauthApp: createOAuthAppAuth({
clientType: "github-app",
clientId: options.clientId || "",
clientSecret: options.clientSecret || "",
request,
}),
},
);

getAppAuthentication(state);
}