Skip to content

Commit

Permalink
Merge pull request #269 from icedream/pr/job-token
Browse files Browse the repository at this point in the history
feat: Implement jobToken property to allow authentication via CI job token
  • Loading branch information
jdalrymple authored Feb 22, 2019
2 parents a52c03d + cb0826b commit 8f551f1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ const api = new Gitlab({
oauthToken: 'abcdefghij123456'
})

// You can also use a CI job token:

const api = new Gitlab({
url: 'http://example.com', // Defaults to https://gitlab.com
jobToken: process.env.CI_JOB_TOKEN
})

```

#### Specific Imports
Expand Down
4 changes: 4 additions & 0 deletions src/infrastructure/BaseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import XMLHttpRequester, { XhrStaticPromisified } from './XMLHttpRequester';
interface BaseModelOptions {
url?: string;
token?: string;
jobToken?: string;
oauthToken?: string;
useXMLHttpRequest?: boolean;
version?: string;
Expand All @@ -14,6 +15,7 @@ interface BaseModelOptions {

export type BaseModelContructorOptions =
| BaseModelOptions & Required<Pick<BaseModelOptions, 'token'>>
| BaseModelOptions & Required<Pick<BaseModelOptions, 'jobToken'>>
| BaseModelOptions & Required<Pick<BaseModelOptions, 'oauthToken'>>;
class BaseModel {
public url: string;
Expand All @@ -24,6 +26,7 @@ class BaseModel {

constructor({
token,
jobToken,
oauthToken,
sudo,
url = 'https://gitlab.com',
Expand All @@ -40,6 +43,7 @@ class BaseModel {

// Handle auth tokens
if (oauthToken) this.headers.authorization = `Bearer ${oauthToken}`;
else if (jobToken) this.headers['job-token'] = jobToken;
else if (token) this.headers['private-token'] = token;

// Set sudo
Expand Down

0 comments on commit 8f551f1

Please sign in to comment.