Skip to content

Commit

Permalink
API friendly runners (#657)
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGOrtega authored Jul 13, 2021
1 parent 64066b1 commit b5c1afb
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 16 deletions.
8 changes: 6 additions & 2 deletions bin/cml-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,9 @@ const run = async (opts) => {
// if (name !== NAME) {
await cml.repoTokenCheck();

if (await cml.runnerByName({ name })) {
const runners = await cml.runners();
const runner = await cml.runnerByName({ name, runners });
if (runner) {
if (!reuse)
throw new Error(
`Runner name ${name} is already in use. Please change the name or terminate the other runner.`
Expand All @@ -334,7 +336,9 @@ const run = async (opts) => {

if (
reuse &&
(await cml.runnersByLabels({ labels })).find((runner) => runner.online)
(await cml.runnersByLabels({ labels, runners })).find(
(runner) => runner.online
)
) {
console.log(`Reusing existing online runners with the ${labels} labels...`);
process.exit(0);
Expand Down
142 changes: 142 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
"dependencies": {
"@actions/core": "^1.2.5",
"@actions/github": "^4.0.0",
"@octokit/plugin-throttling": "^3.5.1",
"@octokit/rest": "^18.6.7",
"form-data": "^3.0.0",
"fs-extra": "^9.0.1",
"git-url-parse": "^11.4.0",
Expand Down
16 changes: 10 additions & 6 deletions src/cml.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,19 +217,23 @@ class CML {
return await getDriver(this).unregisterRunner({ runnerId, ...opts });
}

async getRunners(opts = {}) {
return await getDriver(this).getRunners(opts);
async runners(opts = {}) {
return await getDriver(this).runners(opts);
}

async runnerByName(opts = {}) {
const { name } = opts;
const runners = await this.getRunners(opts);
let { name, runners } = opts;

if (!runners) runners = await this.runners(opts);

return runners.find((runner) => runner.name === name);
}

async runnersByLabels(opts = {}) {
const { labels } = opts;
const runners = await this.getRunners(opts);
let { labels, runners } = opts;

if (!runners) runners = await this.runners(opts);

return runners.filter((runner) =>
labels.split(',').every((label) => runner.labels.includes(label))
);
Expand Down
4 changes: 2 additions & 2 deletions src/drivers/bitbucket_cloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ class BitbucketCloud {
throw new Error('Bitbucket Cloud does not support unregisterRunner!');
}

async getRunners(opts = {}) {
throw new Error('Bitbucket Cloud does not support getRunners!');
async runners(opts = {}) {
throw new Error('Bitbucket Cloud does not support runners!');
}

async prCreate(opts = {}) {
Expand Down
27 changes: 22 additions & 5 deletions src/drivers/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const { resolve } = require('path');
const fs = require('fs').promises;

const github = require('@actions/github');
const { Octokit } = require('@octokit/rest');
const { throttling } = require('@octokit/plugin-throttling');
const tar = require('tar');

const { download, exec } = require('../utils');
Expand Down Expand Up @@ -42,7 +44,19 @@ const ownerRepo = (opts) => {
const octokit = (token, repo) => {
if (!token) throw new Error('token not found');

const octokitOptions = {};
const throttleHandler = (retryAfter, options) => {
if (options.request.retryCount <= 5) {
console.log(`Retrying after ${retryAfter} seconds!`);
return true;
}
};
const octokitOptions = {
auth: token,
throttle: {
onRateLimit: throttleHandler,
onAbuseLimit: throttleHandler
}
};

if (!repo.includes('github.com')) {
// GitHub Enterprise, use the: repo URL host + '/api/v3' - as baseURL
Expand All @@ -51,7 +65,8 @@ const octokit = (token, repo) => {
octokitOptions.baseUrl = `https://${host}/api/v3`;
}

return github.getOctokit(token, octokitOptions);
const MyOctokit = Octokit.plugin(throttling);
return new MyOctokit(octokitOptions);
};

class Github {
Expand Down Expand Up @@ -220,19 +235,21 @@ class Github {
}
}

async getRunners(opts = {}) {
async runners(opts = {}) {
const { owner, repo } = ownerRepo({ uri: this.repo });
const { paginate, actions } = octokit(this.token, this.repo);

let runners;
if (typeof repo === 'undefined') {
runners = await paginate(actions.listSelfHostedRunnersForOrg, {
org: owner
org: owner,
per_page: 100
});
} else {
runners = await paginate(actions.listSelfHostedRunnersForRepo, {
owner,
repo
repo,
per_page: 100
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/drivers/gitlab.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class Gitlab {
}
}

async getRunners(opts = {}) {
async runners(opts = {}) {
const endpoint = `/runners?per_page=100`;
const runners = await this.request({ endpoint, method: 'GET' });
return await Promise.all(
Expand Down

0 comments on commit b5c1afb

Please sign in to comment.