1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | const payload_1 = require("./payload");
|
19 | function isGitHubCredential(spec) {
|
20 | return !!spec.token && !!spec.scopes;
|
21 | }
|
22 | exports.isGitHubCredential = isGitHubCredential;
|
23 | function isGitHubAppCredential(spec) {
|
24 | return !!spec.token && !!spec.permissions;
|
25 | }
|
26 | exports.isGitHubAppCredential = isGitHubAppCredential;
|
27 | const ResourceUserQuery = `query ResourceUser($id: String!) {
|
28 | ChatId(userId: $id) {
|
29 | person {
|
30 | gitHubId {
|
31 | login
|
32 | credential {
|
33 | secret
|
34 | scopes
|
35 | }
|
36 | }
|
37 | }
|
38 | }
|
39 | }
|
40 | `;
|
41 | function gitHubUserToken() {
|
42 | return async (graph, payload) => {
|
43 | var _a, _b, _c, _d, _e, _f;
|
44 | if (payload_1.isCommandIncoming(payload)) {
|
45 | const chatId = (_c = (_b = (_a = payload.source) === null || _a === void 0 ? void 0 : _a.slack) === null || _b === void 0 ? void 0 : _b.user) === null || _c === void 0 ? void 0 : _c.id;
|
46 | if (chatId) {
|
47 | const response = await graph.query(ResourceUserQuery, { id: chatId });
|
48 | const credential = (_f = (_e = (_d = response === null || response === void 0 ? void 0 : response.ChatId[0]) === null || _d === void 0 ? void 0 : _d.person) === null || _e === void 0 ? void 0 : _e.gitHubId) === null || _f === void 0 ? void 0 : _f.credential;
|
49 | if (credential) {
|
50 | return {
|
51 | scopes: credential.scopes,
|
52 | token: credential.secret,
|
53 | };
|
54 | }
|
55 | }
|
56 | }
|
57 | return undefined;
|
58 | };
|
59 | }
|
60 | exports.gitHubUserToken = gitHubUserToken;
|
61 | const ProviderQuery = `query Provider($owner: String, $repo: String, $apiUrl: String) {
|
62 | Repo(owner: $owner, name: $repo) {
|
63 | org @required {
|
64 | provider(apiUrl: $apiUrl) @required {
|
65 | id
|
66 | }
|
67 | }
|
68 | }
|
69 | }
|
70 | `;
|
71 | const ProviderByRepoIdQuery = `query ProviderByRepoId($id: ID!) {
|
72 | Repo(id: $id) {
|
73 | org @required {
|
74 | provider @required {
|
75 | id
|
76 | }
|
77 | }
|
78 | }
|
79 | }
|
80 | `;
|
81 | const GitHubAppTokenQuery = `query GitHubAppToken($id: ID!, $owner: String!) {
|
82 | GitHubAppResourceProvider(id: $id) {
|
83 | gitHubAppInstallations(owner: $owner) {
|
84 | token {
|
85 | secret
|
86 | permissions
|
87 | }
|
88 | }
|
89 | }
|
90 | }
|
91 | `;
|
92 | const ScmProviderQuery = `query ScmProvider($id: ID!) {
|
93 | SCMProvider(id: $id) {
|
94 | credential {
|
95 | secret
|
96 | scopes
|
97 | }
|
98 | }
|
99 | }`;
|
100 | function gitHubAppToken(id) {
|
101 | return async (graph) => {
|
102 | var _a, _b, _c, _d, _e, _f, _g, _h;
|
103 | let repo;
|
104 | let owner;
|
105 | let apiUrl;
|
106 | let providerId;
|
107 | if (typeof id === "string") {
|
108 | const provider = await graph.query(ProviderByRepoIdQuery, { id });
|
109 | providerId = (_c = (_b = (_a = provider === null || provider === void 0 ? void 0 : provider.Repo[0]) === null || _a === void 0 ? void 0 : _a.org) === null || _b === void 0 ? void 0 : _b.provider) === null || _c === void 0 ? void 0 : _c.id;
|
110 | }
|
111 | else {
|
112 | repo = id.repo;
|
113 | owner = id.owner;
|
114 | apiUrl = id.apiUrl;
|
115 | const provider = await graph.query(ProviderQuery, { apiUrl: apiUrl || "https://api.github.com/", owner, repo });
|
116 | providerId = (_f = (_e = (_d = provider === null || provider === void 0 ? void 0 : provider.Repo[0]) === null || _d === void 0 ? void 0 : _d.org) === null || _e === void 0 ? void 0 : _e.provider) === null || _f === void 0 ? void 0 : _f.id;
|
117 | }
|
118 | if (providerId) {
|
119 | const installations = await graph.query(GitHubAppTokenQuery, {
|
120 | id: providerId,
|
121 | owner,
|
122 | });
|
123 | const token = (_g = installations === null || installations === void 0 ? void 0 : installations.GitHubAppResourceProvider[0]) === null || _g === void 0 ? void 0 : _g.gitHubAppInstallations[0].token;
|
124 | if (token) {
|
125 | return {
|
126 | token: token.secret,
|
127 | permissions: JSON.parse(token.permissions || ""),
|
128 | };
|
129 | }
|
130 |
|
131 | const scmProvider = await graph.query(ScmProviderQuery, { id: providerId });
|
132 | const credential = (_h = scmProvider === null || scmProvider === void 0 ? void 0 : scmProvider.SCMProvider[0]) === null || _h === void 0 ? void 0 : _h.credential;
|
133 | if (credential) {
|
134 | return {
|
135 | token: credential.secret,
|
136 | scopes: credential.scopes,
|
137 | };
|
138 | }
|
139 | }
|
140 | return undefined;
|
141 | };
|
142 | }
|
143 | exports.gitHubAppToken = gitHubAppToken;
|
144 | class DefaultCredentialProvider {
|
145 | constructor(graphClient, payload) {
|
146 | this.graphClient = graphClient;
|
147 | this.payload = payload;
|
148 | }
|
149 | async resolve(spec) {
|
150 | return spec(this.graphClient, this.payload);
|
151 | }
|
152 | }
|
153 | exports.DefaultCredentialProvider = DefaultCredentialProvider;
|
154 |
|
\ | No newline at end of file |