Skip to content

Commit

Permalink
[server] Improve logging / error messages of GitLab app and context p…
Browse files Browse the repository at this point in the history
…arser
  • Loading branch information
corneliusludmann committed Jul 13, 2021
1 parent a805386 commit 343b3c1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
23 changes: 13 additions & 10 deletions components/server/ee/src/prebuilds/gitlab-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { StartPrebuildResult } from './github-app';
import { TokenService } from '../../../src/user/token-service';
import { HostContextProvider } from '../../../src/auth/host-context-provider';
import { GitlabService } from './gitlab-service';
import { log } from '@gitpod/gitpod-protocol/lib/util/logging';

@injectable()
export class GitLabApp {
Expand All @@ -29,19 +30,21 @@ export class GitLabApp {
@postConstruct()
protected init() {
this._router.post('/', async (req, res) => {
console.log(req.header('X-Gitlab-Event'));
if (req.header('X-Gitlab-Event') === 'Push Hook') {
const event = req.header('X-Gitlab-Event');
if (event === 'Push Hook') {
const context = req.body as GitLabPushHook;
const span = TraceContext.startSpan("GitLapApp.handleEvent", {});
span.setTag("request", context);
console.log(JSON.stringify(context, undefined, ' '));
log.debug("GitLab push hook received", { event, context });
const user = await this.findUser({span},context, req);
if (!user) {
res.statusCode = 503;
res.send();
return;
}
this.handlePushHook({span},context, user);
} else {
log.debug("Unknown GitLab event received", { event });
}
res.send('OK');
});
Expand All @@ -53,7 +56,7 @@ export class GitLabApp {
const secretToken = req.header('X-Gitlab-Token');
span.setTag('secret-token', secretToken);
if (!secretToken) {
throw new Error('No sceretToken provided.');
throw new Error('No secretToken provided.');
}
const [userid, tokenValue] = secretToken.split('|');
const user = await this.userDB.findUserById(userid);
Expand Down Expand Up @@ -85,14 +88,15 @@ export class GitLabApp {
const span = TraceContext.startSpan("GitLapApp.handlePushHook", ctx);
try {
const contextURL = this.createContextUrl(body);
span.setTag('contextURl', contextURL);
log.debug({ userId: user.id }, "GitLab push hook: Context URL", { context: body, contextURL });
span.setTag('contextURL', contextURL);
const config = await this.prebuildManager.fetchConfig({ span }, user, contextURL);
if (!this.prebuildManager.shouldPrebuild(config)) {
console.log('No config. No prebuild.');
log.debug({ userId: user.id }, "GitLab push hook: There is no prebuild config.", { context: body, contextURL });
return undefined;
}

console.log('Starting prebuild.', { contextURL, commit: body.after, gitUrl: body.repository.git_http_url })
log.debug({ userId: user.id }, "GitLab push hook: Starting prebuild", { context: body, contextURL });
const ws = await this.prebuildManager.startPrebuild({ span }, user, contextURL, body.repository.git_http_url, body.after);
return ws;
} finally {
Expand All @@ -102,9 +106,8 @@ export class GitLabApp {

protected createContextUrl(body: GitLabPushHook) {
const repoUrl = body.repository.git_http_url;
const contextUrl = `${repoUrl.substr(0, repoUrl.length - 4)}/tree${body.ref.substr('refs/head/'.length)}`;
console.log(contextUrl);
return contextUrl;
const contextURL = `${repoUrl.substr(0, repoUrl.length - 4)}/-/tree${body.ref.substr('refs/head/'.length)}`;
return contextURL;
}

get router(): express.Router {
Expand Down
34 changes: 18 additions & 16 deletions components/server/src/gitlab/gitlab-context-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,29 +214,31 @@ export class GitlabContextParser extends AbstractContextParser implements IConte
const possibleBranches = await this.gitlabApi.run<GitLab.Branch[]>(user, async g => {
return g.Branches.all(`${owner}/${repoName}`, { search });
});
if (!GitLab.ApiError.is(possibleBranches)) {
for (let candidate of possibleBranches) {
const name = candidate.name;
const nameSegements = candidate.name.split('/');
if (segments.length >= nameSegements.length && segments.slice(0, nameSegements.length).join('/') === name) {
branchOrTagObject = { type: 'branch', name, revision: candidate.commit.id };
break;
}
if (GitLab.ApiError.is(possibleBranches)) {
throw new Error(`GitLab ApiError on searching for possible branches for ${owner}/${repoName}/tree/${segments.join('/')}: ${possibleBranches}`);
}
for (let candidate of possibleBranches) {
const name = candidate.name;
const nameSegements = candidate.name.split('/');
if (segments.length >= nameSegements.length && segments.slice(0, nameSegements.length).join('/') === name) {
branchOrTagObject = { type: 'branch', name, revision: candidate.commit.id };
break;
}
}

if (branchOrTagObject === undefined) {
const possibleTags = await this.gitlabApi.run<GitLab.Tag[]>(user, async g => {
return g.Tags.all(`${owner}/${repoName}`, { search });
});
if (!GitLab.ApiError.is(possibleTags)) {
for (let candidate of possibleTags) {
const name = candidate.name;
const nameSegements = candidate.name.split('/');
if (segments.length >= nameSegements.length && segments.slice(0, nameSegements.length).join('/') === name) {
branchOrTagObject = { type: 'tag', name, revision: candidate.commit.id };
break;
}
if (GitLab.ApiError.is(possibleTags)) {
throw new Error(`GitLab ApiError on searching for possible tags for ${owner}/${repoName}/tree/${segments.join('/')}: ${possibleTags}`);
}
for (let candidate of possibleTags) {
const name = candidate.name;
const nameSegements = candidate.name.split('/');
if (segments.length >= nameSegements.length && segments.slice(0, nameSegements.length).join('/') === name) {
branchOrTagObject = { type: 'tag', name, revision: candidate.commit.id };
break;
}
}
}
Expand Down

0 comments on commit 343b3c1

Please sign in to comment.