-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from INSO-TUWien/feature/100
Feature/100 Github Actions CI Indexer
- Loading branch information
Showing
16 changed files
with
280 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
const urlJoin = require('url-join'); | ||
const log = require('debug')('github'); | ||
const Paginator = require('../../paginator.js'); | ||
const { response } = require('express'); | ||
const { Octokit } = require('@octokit/rest'); | ||
|
||
class GitHub { | ||
constructor(options) { | ||
this.baseUrl = options.baseUrl; | ||
this.privateToken = options.privateToken; | ||
this.requestTimeout = options.requestTimeout; | ||
this.count = 0; | ||
this.stopping = false; | ||
this.github = new Octokit({ | ||
baseUrl: 'https://api.github.com', | ||
auth: this.privateToken, | ||
}); | ||
} | ||
|
||
getPipelines(projectId) { | ||
log('getPipelines(%o)', projectId); | ||
return this.paginatedRequest((page) => { | ||
return this.github.rest.actions.listWorkflowRunsForRepo({ | ||
owner: projectId.split('/')[0], | ||
repo: projectId.split('/')[1], | ||
page: page, | ||
}); | ||
}); | ||
} | ||
|
||
getPipeline(projectId, pipelineId) { | ||
log('getPipeline(%o, %o)', projectId, pipelineId); | ||
return this.github.rest.actions | ||
.getWorkflowRun({ | ||
owner: projectId.split('/')[0], | ||
repo: projectId.split('/')[1], | ||
run_id: pipelineId, | ||
}) | ||
.then((workflowRun) => { | ||
return workflowRun.data; | ||
}); | ||
} | ||
|
||
getPipelineJobs(projectId, pipelineId) { | ||
log('getPipelineJobs(%o,%o)', projectId, pipelineId); | ||
return this.github.rest.actions | ||
.listJobsForWorkflowRun({ | ||
owner: projectId.split('/')[0], | ||
repo: projectId.split('/')[1], | ||
run_id: pipelineId, | ||
}) | ||
.then((jobs) => { | ||
return jobs.data.jobs; | ||
}); | ||
} | ||
|
||
isStopping() { | ||
return this.stopping; | ||
} | ||
|
||
stop() { | ||
this.stopping = true; | ||
} | ||
|
||
paginatedRequest(rq) { | ||
return new Paginator( | ||
(page, per_page) => { | ||
if (this.stopping) { | ||
return Promise.resolve({ | ||
data: { | ||
workflow_runs: [], | ||
}, | ||
}); | ||
} | ||
return rq(page); | ||
}, | ||
(resp) => { | ||
return resp.data.workflow_runs || []; | ||
}, | ||
(resp) => { | ||
return (this.count = parseInt(resp.data.total_count, 10)); | ||
} | ||
); | ||
} | ||
} | ||
|
||
module.exports = GitHub; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
'use strict'; | ||
|
||
const Build = require('../../models/Build.js'); | ||
const FindFiles = require('file-regex'); | ||
const UrlProvider = require('../../url-providers'); | ||
const log = require('debug')('importer:github-ci-indexer'); | ||
const ConfigurationError = require('../../errors/ConfigurationError.js'); | ||
const CIIndexer = require('./CIIndexer'); | ||
const moment = require('moment'); | ||
const { Octokit } = require('@octokit/rest'); | ||
const GitHub = require('../../core/provider/github'); | ||
|
||
class GitHubCIIndexer { | ||
constructor(repository, progressReporter) { | ||
this.repo = repository; | ||
this.reporter = progressReporter; | ||
this.stopping = false; | ||
} | ||
|
||
async configure(config) { | ||
this.urlProvider = await UrlProvider.getCiUrlProvider(this.repo, this.reporter); | ||
|
||
this.github = new Octokit({ | ||
baseUrl: 'https://api.github.com', | ||
auth: config?.auth?.token, | ||
}); | ||
|
||
//prerequisites to permit the use of travis | ||
if (!this.urlProvider || !config) { | ||
throw new ConfigurationError('GitHub/Octokit cannot be configured!'); | ||
} | ||
|
||
const currentBranch = await this.repo.getCurrentBranch(); | ||
const originUrl = await this.repo.getOriginUrl(); | ||
|
||
let repoName = originUrl.substring('https://github.com'.length + 1); | ||
if (repoName.endsWith('.git')) { | ||
repoName = repoName.slice(0, -'.git'.length); | ||
} | ||
|
||
log(`fetching branch data from ${currentBranch}[${currentBranch}]`); | ||
this.controller = new GitHub({ | ||
baseUrl: 'https://api.github.com', | ||
privateToken: config?.auth?.token, | ||
requestTimeout: config.timeout, | ||
}); | ||
|
||
this.indexer = new CIIndexer(this.reporter, this.controller, repoName, async (pipeline, jobs) => { | ||
jobs = jobs || []; | ||
log( | ||
`create build ${JSON.stringify({ | ||
id: pipeline.id, | ||
number: pipeline.run_number, | ||
sha: pipeline.head_commit.sha, | ||
status: convertState(pipeline.status), | ||
updatedAt: moment(pipeline.updated_at).toISOString(), | ||
startedAt: moment(pipeline.run_started_at).toISOString(), | ||
finishedAt: moment(pipeline.updated_at).toISOString(), | ||
committedAt: moment(pipeline.head_commit.timestamp).toISOString(), | ||
})}` | ||
); | ||
const username = (pipeline.actor || {}).login; | ||
const userFullName = username !== undefined ? (await this.github.users.getByUsername({ username: username })).data.name : ''; | ||
let status = 'canceled'; | ||
let lastStartedAt = pipeline.run_started_at; | ||
let lastFinishedAt = pipeline.updated_at; | ||
if (jobs.length > 0) { | ||
status = convertState(jobs[jobs.length - 1].conclusion); | ||
lastStartedAt = jobs[jobs.length - 1].created_at; | ||
lastFinishedAt = jobs[jobs.length - 1].completed_at; | ||
} | ||
return Build.persist({ | ||
id: pipeline.id, | ||
sha: pipeline.head_sha, | ||
ref: pipeline.head_commit.id, | ||
status: status, | ||
tag: pipeline.display_title, | ||
user: username, | ||
userFullName: userFullName !== null ? userFullName : username, | ||
createdAt: moment(pipeline.created_at || (jobs.length > 0 ? jobs[0].created_at : pipeline.started_at)).toISOString(), | ||
updatedAt: moment(pipeline.updated_at).toISOString(), | ||
startedAt: moment(pipeline.run_started_at).toISOString(), | ||
finishedAt: moment(lastFinishedAt).toISOString(), | ||
committedAt: moment(pipeline.head_commit.committed_at).toISOString(), | ||
duration: moment(lastFinishedAt).unix() - moment(lastStartedAt).unix(), | ||
jobs: jobs.map((job) => ({ | ||
id: job.id, | ||
name: username, | ||
status: job.conclusion, | ||
stage: job.conclusion, | ||
createdAt: moment(job.created_at).toISOString(), | ||
finishedAt: moment(job.completed_at).toISOString(), | ||
webUrl: job.html_url, | ||
})), | ||
webUrl: pipeline.html_url, | ||
}); | ||
}); | ||
} | ||
|
||
async index() { | ||
try { | ||
return await this.indexer.index(); | ||
} catch (e) { | ||
console.log('GitHubCI Indexer Failed! - ' + e.message); | ||
} | ||
} | ||
|
||
isStopping() { | ||
return this.stopping; | ||
} | ||
|
||
stop() { | ||
if (this.indexer && !this.indexer.isStopping()) { | ||
this.indexer.stop(); | ||
} | ||
|
||
if (this.controller && !this.controller.isStopping()) { | ||
this.controller.stop(); | ||
} | ||
this.stopping = true; | ||
} | ||
} | ||
|
||
const convertState = (state) => { | ||
switch (state) { | ||
case 'failure': | ||
return 'failed'; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
module.exports = GitHubCIIndexer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.