1
1
import { request } from "@octokit/request" ;
2
2
3
- import { StrategyOptionsWithDefaults , AuthOptions , Permissions } from "./types" ;
3
+ import {
4
+ Authentication ,
5
+ AuthOptions ,
6
+ Permissions ,
7
+ StrategyOptionsWithDefaults ,
8
+ WithInstallationId
9
+ } from "./types" ;
4
10
import { isAppRoute } from "./is-app-route" ;
5
11
import { toTokenAuthentication } from "./to-token-authentication" ;
6
12
import { toAppAuthentication } from "./to-app-authentication" ;
7
13
import { get , set } from "./cache" ;
8
14
9
15
export async function auth (
10
16
state : StrategyOptionsWithDefaults ,
11
- options ? : AuthOptions
17
+ options : AuthOptions = { }
12
18
) {
13
- if ( options && ! options . refresh ) {
19
+ // installationId is never 0, so a simple check here is fine
20
+ const installationId = ( options . installationId ||
21
+ state . installationId ) as number ;
22
+
23
+ if ( installationId && ! options . refresh ) {
14
24
const result = get ( state . cache , options ) ;
15
25
if ( result ) {
16
26
const {
@@ -22,7 +32,7 @@ export async function auth(
22
32
} = result ;
23
33
24
34
return toTokenAuthentication (
25
- options . installationId ,
35
+ installationId ,
26
36
token ,
27
37
expiresAt ,
28
38
permissions ,
@@ -34,7 +44,7 @@ export async function auth(
34
44
35
45
const appAuthentication = toAppAuthentication ( state . id , state . privateKey ) ;
36
46
37
- if ( ! options || isAppRoute ( options . url ) ) {
47
+ if ( isAppRoute ( options ) || ! installationId ) {
38
48
return appAuthentication ;
39
49
}
40
50
@@ -43,7 +53,7 @@ export async function auth(
43
53
} = await state . request (
44
54
"POST /app/installations/:installation_id/access_tokens" ,
45
55
{
46
- installation_id : options . installationId ,
56
+ installation_id : installationId ,
47
57
repository_ids : options . repositoryIds ,
48
58
permissions : options . permissions ,
49
59
previews : [ "machine-man" ] ,
@@ -58,7 +68,7 @@ export async function auth(
58
68
const {
59
69
data : { permissions, single_file_name : singleFileName }
60
70
} = await state . request ( "GET /app/installations/:installation_id" , {
61
- installation_id : options . installationId ,
71
+ installation_id : options . installationId || state . installationId ,
62
72
previews : [ "machine-man" ] ,
63
73
headers : appAuthentication . headers
64
74
} ) ;
@@ -72,7 +82,7 @@ export async function auth(
72
82
} ) ;
73
83
74
84
return toTokenAuthentication (
75
- options . installationId ,
85
+ installationId ,
76
86
token ,
77
87
expires_at ,
78
88
permissions ,
0 commit comments