-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
GithubDiscoveryProcessor.ts
181 lines (157 loc) · 5.41 KB
/
GithubDiscoveryProcessor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* Copyright 2021 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { LocationSpec } from '@backstage/catalog-model';
import { Config } from '@backstage/config';
import {
GithubCredentialsProvider,
ScmIntegrations,
} from '@backstage/integration';
import { graphql } from '@octokit/graphql';
import { Logger } from 'winston';
import { getOrganizationRepositories } from './github';
import * as results from './results';
import { CatalogProcessor, CatalogProcessorEmit } from './types';
/**
* Extracts repositories out of a GitHub org.
*
* The following will create locations for all projects which have a catalog-info.yaml
* on the default branch. The first is shorthand for the second.
*
* target: "https://github.com/backstage"
* or
* target: https://github.com/backstage/*\/blob/-/catalog-info.yaml
*
* You may also explicitly specify the source branch:
*
* target: https://github.com/backstage/*\/blob/main/catalog-info.yaml
**/
export class GithubDiscoveryProcessor implements CatalogProcessor {
private readonly integrations: ScmIntegrations;
private readonly logger: Logger;
static fromConfig(config: Config, options: { logger: Logger }) {
const integrations = ScmIntegrations.fromConfig(config);
return new GithubDiscoveryProcessor({
...options,
integrations,
});
}
constructor(options: { integrations: ScmIntegrations; logger: Logger }) {
this.integrations = options.integrations;
this.logger = options.logger;
}
async readLocation(
location: LocationSpec,
_optional: boolean,
emit: CatalogProcessorEmit,
): Promise<boolean> {
if (location.type !== 'github-discovery') {
return false;
}
const gitHubConfig = this.integrations.github.byUrl(
location.target,
)?.config;
if (!gitHubConfig) {
throw new Error(
`There is no GitHub integration that matches ${location.target}. Please add a configuration entry for it under integrations.github`,
);
}
const { org, repoSearchPath, catalogPath, branch, host } = parseUrl(
location.target,
);
// Building the org url here so that the github creds provider doesn't need to know
// about how to handle the wild card which is special for this processor.
const orgUrl = `https://${host}/${org}`;
const { headers } = await GithubCredentialsProvider.create(
gitHubConfig,
).getCredentials({ url: orgUrl });
const client = graphql.defaults({
baseUrl: gitHubConfig.apiBaseUrl,
headers,
});
// Read out all of the raw data
const startTimestamp = Date.now();
this.logger.info(`Reading GitHub repositories from ${location.target}`);
const { repositories } = await getOrganizationRepositories(client, org);
const matching = repositories.filter(
r => !r.isArchived && repoSearchPath.test(r.name),
);
const duration = ((Date.now() - startTimestamp) / 1000).toFixed(1);
this.logger.debug(
`Read ${repositories.length} GitHub repositories (${matching.length} matching the pattern) in ${duration} seconds`,
);
for (const repository of matching) {
const branchName =
branch === '-' ? repository.defaultBranchRef?.name : branch;
if (!branchName) {
this.logger.info(
`the repository ${repository.url} does not have a default branch, skipping`,
);
continue;
}
const path = `/blob/${branchName}${catalogPath}`;
emit(
results.location(
{
type: 'url',
target: `${repository.url}${path}`,
},
// Not all locations may actually exist, since the user defined them as a wildcard pattern.
// Thus, we emit them as optional and let the downstream processor find them while not outputting
// an error if it couldn't.
true,
),
);
}
return true;
}
}
/*
* Helpers
*/
export function parseUrl(urlString: string): {
org: string;
repoSearchPath: RegExp;
catalogPath: string;
branch: string;
host: string;
} {
const url = new URL(urlString);
const path = url.pathname.substr(1).split('/');
// /backstage/techdocs-*/blob/master/catalog-info.yaml
// can also be
// /backstage
if (path.length > 2 && path[0].length && path[1].length) {
return {
org: decodeURIComponent(path[0]),
repoSearchPath: escapeRegExp(decodeURIComponent(path[1])),
branch: decodeURIComponent(path[3]),
catalogPath: `/${decodeURIComponent(path.slice(4).join('/'))}`,
host: url.host,
};
} else if (path.length === 1 && path[0].length) {
return {
org: decodeURIComponent(path[0]),
host: url.host,
repoSearchPath: escapeRegExp('*'),
catalogPath: '/catalog-info.yaml',
branch: '-',
};
}
throw new Error(`Failed to parse ${urlString}`);
}
export function escapeRegExp(str: string): RegExp {
return new RegExp(`^${str.replace(/\*/g, '.*')}$`);
}