Skip to content

Commit

Permalink
octokit on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGOrtega committed Oct 16, 2020
1 parent 72106b7 commit e234780
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
19 changes: 13 additions & 6 deletions src/GithubClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,20 @@ const owner_repo = (opts) => {
return { owner, repo };
};

const octokit = (token) => {
if (!token) throw new Error('token not found');

return github.getOctokit(token);
};

class GithubClient {
constructor(opts = {}) {
const { repo = this.env_repo(), token = this.env_token() } = opts;

if (!repo) throw new Error('repo not found');
if (!token) throw new Error('token not found');

this.repo = repo.endsWith('/') ? strip_last_chars(repo, 1) : repo;
this.token = token;
this.octokit = github.getOctokit(token);
}

env_repo() {
Expand Down Expand Up @@ -67,7 +71,9 @@ class GithubClient {
async comment_create(opts = {}) {
const { report: body, commit_sha = this.env_head_sha() } = opts;

const { url: commit_url } = await this.octokit.repos.createCommitComment({
const { url: commit_url } = await octokit(
this.token
).repos.createCommitComment({
...owner_repo({ uri: this.repo }),
body,
commit_sha
Expand All @@ -88,7 +94,7 @@ class GithubClient {
status = 'completed'
} = opts;

return await this.octokit.checks.create({
return await octokit(this.token).checks.create({
...owner_repo({ uri: this.repo }),
head_sha,
started_at,
Expand All @@ -106,11 +112,12 @@ class GithubClient {

async runner_token() {
const { owner, repo } = owner_repo({ uri: this.repo });
const { actions } = octokit(this.token);

if (typeof repo !== 'undefined') {
const {
data: { token }
} = await this.octokit.actions.createRegistrationTokenForRepo({
} = await actions.createRegistrationTokenForRepo({
owner,
repo
});
Expand All @@ -120,7 +127,7 @@ class GithubClient {

const {
data: { token }
} = await this.octokit.actions.createRegistrationTokenForOrg({
} = await actions.createRegistrationTokenForOrg({
org: owner
});

Expand Down
10 changes: 6 additions & 4 deletions src/GithubClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ describe('Non Enviromental tests', () => {
expect(gh_client.token).toBe(TOKEN);

const { owner, repo } = gh_client.owner_repo();
expect(owner).toBe('DavidGOrtega');
expect(repo).toBe('3_tensorboard');
const parts = GITHUB_REPOSITORY.split('/');
expect(owner).toBe(parts[0]);
expect(repo).toBe(parts[1]);
});

test('Comment', async () => {
Expand Down Expand Up @@ -76,8 +77,9 @@ describe('Enviromental tests', () => {
expect(gh_client.token).toBe(TOKEN);

const { owner, repo } = gh_client.owner_repo();
expect(owner).toBe('DavidGOrtega');
expect(repo).toBe('3_tensorboard');
const parts = GITHUB_REPOSITORY.split('/');
expect(owner).toBe(parts[0]);
expect(repo).toBe(parts[1]);
});

test('Comment', async () => {
Expand Down
1 change: 0 additions & 1 deletion src/cml.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const get_client = (opts) => {
if (!driver) throw new Error('driver not set');

if (driver === 'github') return new GithubClient({ repo, token });

if (driver === 'gitlab') return new GitlabClient({ repo, token });

throw new Error('driver unknown!');
Expand Down

2 comments on commit e234780

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Comment Report

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Comment Report

Please sign in to comment.