forked from badges/shields
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e01272d
commit 4175b51
Showing
7 changed files
with
189 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { BaseJsonService } from '../index.js' | ||
|
||
export default class YoutrackBase extends BaseJsonService { | ||
static auth = { | ||
passKey: 'youtrack_token', | ||
serviceKey: 'youtrack', | ||
} | ||
|
||
async fetch({ url, options, schema, httpErrors }) { | ||
console.log(url) | ||
console.log(options) | ||
|
||
return this._requestJson( | ||
this.authHelper.withBearerAuthHeader({ | ||
schema, | ||
url, | ||
options, | ||
httpErrors: { 500: 'invalid query' }, | ||
}), | ||
) | ||
} | ||
} |
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,48 @@ | ||
import Joi from 'joi' | ||
import { expect } from 'chai' | ||
import nock from 'nock' | ||
import { cleanUpNockAfterEach, defaultContext } from '../test-helpers.js' | ||
import YoutrackBase from './youtrack-base.js' | ||
|
||
class DummyYoutrackService extends YoutrackBase { | ||
static route = { base: 'fake-base' } | ||
|
||
async handle() { | ||
const data = await this.fetch({ | ||
schema: Joi.any(), | ||
url: 'https://youtrack.jetbrains.com//api/issuesGetter/count?fields=count', | ||
}) | ||
return { message: data.message } | ||
} | ||
} | ||
|
||
describe('YoutrackBase', function () { | ||
describe('auth', function () { | ||
cleanUpNockAfterEach() | ||
|
||
const config = { | ||
public: { | ||
services: { | ||
youtrack: { | ||
authorizedOrigins: ['https://youtrack.jetbrains.com'], | ||
}, | ||
}, | ||
}, | ||
private: { | ||
youtrack_token: 'fake-key', | ||
}, | ||
} | ||
|
||
it('sends the auth information as configured', async function () { | ||
const scope = nock('https://youtrack.jetbrains.com') | ||
.get('/api/issuesGetter/count?fields=count') | ||
.matchHeader('Authorization', 'Bearer fake-key') | ||
.reply(200, { message: 'fake message' }) | ||
expect( | ||
await DummyYoutrackService.invoke(defaultContext, config, {}), | ||
).to.not.have.property('isError') | ||
|
||
scope.done() | ||
}) | ||
}) | ||
}) |
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,6 @@ | ||
const description = ` | ||
By default this badge looks for projects on [youtrack.jetbrains.com](https://youtrack.jetbrains.com). | ||
To specify a self-hosted instance, use the \`youtrack_url\` query param. | ||
` | ||
|
||
export { description } |
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,93 @@ | ||
import Joi from 'joi' | ||
import { pathParam, queryParam } from '../index.js' | ||
import { optionalUrl } from '../validators.js' | ||
import { metric } from '../text-formatters.js' | ||
import { description } from './youtrack-helper.js' | ||
import YoutrackBase from './youtrack-base.js' | ||
|
||
const schema = Joi.array().items( | ||
Joi.object({ | ||
count: Joi.number().required(), | ||
type: Joi.string().required(), | ||
}), | ||
) | ||
|
||
const queryParamSchema = Joi.object({ | ||
query: Joi.string(), | ||
youtrack_url: optionalUrl, | ||
}).required() | ||
|
||
export default class YoutrackIssues extends YoutrackBase { | ||
static category = 'issue-tracking' | ||
|
||
static route = { | ||
base: 'youtrack/issues', | ||
pattern: ':project+', | ||
queryParamSchema, | ||
} | ||
|
||
static openApi = { | ||
'/youtrack/issues/{project}': { | ||
get: { | ||
summary: 'Youtrack Issues', | ||
description, | ||
parameters: [ | ||
pathParam({ | ||
name: 'project', | ||
example: 'RIDER', | ||
}), | ||
queryParam({ | ||
name: 'youtrack_url', | ||
example: 'https://youtrack.jetbrains.com', | ||
}), | ||
queryParam({ | ||
name: 'query', | ||
example: 'bug #Unresolved', | ||
description: 'A valid YouTrack search query.', | ||
}), | ||
], | ||
}, | ||
}, | ||
} | ||
|
||
static defaultBadgeData = { label: 'issues', color: 'informational' } | ||
|
||
static render({ count }) { | ||
return { | ||
label: 'issues', | ||
message: metric(count), | ||
color: count > 0 ? 'yellow' : 'brightgreen', | ||
} | ||
} | ||
|
||
async fetch({ baseUrl, query }) { | ||
// https://www.jetbrains.com.cn/en-us/help/youtrack/devportal/resource-api-issuesGetter-count.html | ||
return super.fetch({ | ||
schema, | ||
options: { | ||
method: 'POST', | ||
searchParams: { | ||
query, | ||
}, | ||
}, | ||
url: `${baseUrl}/api/issuesGetter/count?fields=count`, | ||
}) | ||
} | ||
|
||
async handle( | ||
{ project }, | ||
{ youtrack_url: baseUrl = 'https://youtrack.jetbrains.com', query }, | ||
) { | ||
const { res } = await this.fetch({ | ||
baseUrl, | ||
query: `project: ${project} ${query}`, | ||
}) | ||
|
||
console.log(res) | ||
|
||
const data = this.constructor._validate(res.headers, schema) | ||
|
||
const count = data.count() | ||
return this.constructor.render({ count }) | ||
} | ||
} |
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,9 @@ | ||
import { createServiceTester } from '../tester.js' | ||
import { isMetric } from '../test-validators.js' | ||
|
||
export const t = await createServiceTester() | ||
|
||
t.create('Issues (RIDER)').get('/RIDER?query=#Unresolved').expectBadge({ | ||
label: 'issues', | ||
message: isMetric, | ||
}) |