Skip to content

Commit

Permalink
Merge pull request #82 from Sanofi-IADC/add-jira-step
Browse files Browse the repository at this point in the history
add jira step & display issues in gridjs table
  • Loading branch information
PiiXiieeS authored Apr 19, 2021
2 parents 8a16ed4 + 730aaee commit eb0a38a
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/http/http.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import Config from '../config/config';
import { JiraService } from './jira.service';

@Module({
imports: [
Expand All @@ -25,7 +26,8 @@ import Config from '../config/config';
inject: [ConfigService],
}),
],
exports: [BaseHttpModule],
providers: [JiraService],
exports: [BaseHttpModule, JiraService],
})
export class HttpModule implements OnModuleInit {
constructor(private readonly httpService: HttpService) {}
Expand Down
33 changes: 33 additions & 0 deletions src/http/jira.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { HttpService, Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class JiraService {
baseUrl = 'https://iadc.atlassian.net/rest/api/2';

headers = {};

constructor(
private httpService: HttpService,
private configService: ConfigService,
) {
const credentials = `${this.configService.get(
'confluence.apiUsername',
)}:${this.configService.get('confluence.apiToken')}`;

this.headers = {
Authorization: `Basic ${credentials}`,
};
}

findTickets(jqlSearch: string, fields: string, maxResult = 100) {
const url = `${this.baseUrl}/search?jql=${jqlSearch}&fields=${fields}&maxResults=${maxResult}`;

return this.httpService
.get(url, {
headers: this.headers,
})
.toPromise()
.then((res) => res.data);
}
}
3 changes: 2 additions & 1 deletion src/proxy-page/proxy-page.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { ConfluenceModule } from '../confluence/confluence.module';
import { ContextService } from '../context/context.service';
import { ProxyPageService } from './proxy-page.service';
import { ProxyPageController } from './proxy-page.controller';
import { HttpModule } from 'src/http/http.module';

@Module({
imports: [ConfluenceModule],
imports: [ConfluenceModule, HttpModule],
providers: [ProxyPageService, ContextService],
controllers: [ProxyPageController],
exports: [],
Expand Down
5 changes: 5 additions & 0 deletions src/proxy-page/proxy-page.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import addHeaderBlog from './steps/addHeaderBlog';
import addSlides from './steps/addSlides';
import addMessageBus from './steps/addMessageBus';
import addCopyLinks from './steps/addCopyLinks';
import addJira from './steps/addJira';
import { JiraService } from 'src/http/jira.service';

@Injectable()
export class ProxyPageService {
Expand All @@ -33,6 +35,7 @@ export class ProxyPageService {
private config: ConfigService,
private confluence: ConfluenceService,
private context: ContextService,
private jiraService: JiraService,
) {}

private initContext(
Expand Down Expand Up @@ -73,6 +76,7 @@ export class ProxyPageService {
): Promise<string> {
const results = await this.confluence.getPage(spaceKey, pageId);
this.initContext(spaceKey, pageId, theme, results);
const addJiraPromise = addJira()(this.context, this.jiraService);
fixHtmlHead(this.config)(this.context);
fixContentWidth()(this.context);
fixLinks(this.config)(this.context);
Expand All @@ -98,6 +102,7 @@ export class ProxyPageService {
addTheme()(this.context);
addScrollToTop()(this.context);
addCopyLinks()(this.context);
await addJiraPromise;
this.context.Close();
return this.context.getHtmlBody();
}
Expand Down
3 changes: 2 additions & 1 deletion src/proxy-page/proxy-page.step.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { JiraService } from 'src/http/jira.service';
import { ContextService } from '../context/context.service';

export interface Step {
(context: ContextService): void | Promise<void>;
(context: ContextService, jira?: JiraService): void | Promise<void>;
}
143 changes: 143 additions & 0 deletions src/proxy-page/steps/addJira.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import { ContextService } from '../../context/context.service';
import { Step } from '../proxy-page.step';
import cheerio from 'cheerio';
import { JiraService } from 'src/http/jira.service';

export default (): Step => {
return async (
context: ContextService,
jiraService: JiraService,
): Promise<void> => {
context.setPerfMark('addJira');
const $ = context.getCheerioBody();

if (!$('.refresh-wiki') || !$('.refresh-wiki').data()) {
context.getPerfMeasure('addJira');
return;
}

const wikimarkup: string = $('.refresh-wiki').data().wikimarkup;
const xmlWikimarkup = cheerio.load(wikimarkup, { xmlMode: true });
const filter = xmlWikimarkup('ac\\:parameter[ac\\:name="jqlQuery"]').text();
const columns =
xmlWikimarkup('ac\\:parameter[ac\\:name="columns"]').text() +
',issuetype';
const maximumIssues = xmlWikimarkup(
'ac\\:parameter[ac\\:name="maximumIssues"]',
).text();
const { issues } = await jiraService.findTickets(
filter,
columns,
Number(maximumIssues),
);

const data = [];
issues.forEach((issue) => {
data.push({
key: {
name: issue.key,
link: `https://iadc.atlassian.net/browse/${issue.key}?src=confmacro`,
},
t: issue.fields.issuetype?.iconUrl,
summary: {
name: issue.fields.summary,
link: `https://iadc.atlassian.net/browse/${issue.key}?src=confmacro`,
},
updated: `${new Date(issue.fields.updated).toLocaleString('en-EN', {
year: 'numeric',
month: 'short',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
})}`,
assignee: issue.fields.assignee?.displayName,
pr: issue.fields.priority.iconUrl,
status: {
name: issue.fields.status.name,
color: issue.fields.status.statusCategory.colorName,
},
resolution: issue.fields.resolution.name,
});
});

// remove the header
$('div[id^="jira-issues-"]').remove();

// remove the 'loading...' text
$('div[id^="refresh-issues-loading-"]').remove();

// remove the actualize link
$('.refresh-issues-bottom').remove();

// add the grid using http://gridjs.io library
$('#Content').append(
'<script src="https://unpkg.com/gridjs/dist/gridjs.production.min.js"></script>',
'<link href="https://unpkg.com/gridjs/dist/theme/mermaid.min.css" rel="stylesheet" />',
'<div id="gridjs"></div>',
`<script>
document.addEventListener('DOMContentLoaded', function () {
new gridjs.Grid({
columns: [
{
name: 'Key',
width: '5%',
formatter: (cell) => gridjs.html(${'`<a href="${cell.link}" target="_blank">${cell.name}</a>`'})
},
{
name: 'Summary',
width: '30%',
formatter: (cell) => gridjs.html(${'`<a href="${cell.link}" target="_blank">${cell.name}</a>`'})
},
{
name: 'T',
width: '2%',
formatter: (cell) => gridjs.html(${'`<img src="${cell}" style="height:2.5rem"/>`'}),
},
{
name: 'Updated',
width: '7%',
sort: {
compare: (a, b) => (new Date(a) > new Date(b) ? 1 : -1),
}
},
{
name: 'Assignee',
width: '10%',
},
{
name: 'Pr',
width: '3%',
formatter: (cell) => gridjs.html(${'`<img src="${cell}" style="height:2.5rem"/>`'}),
},
{
name: 'Status',
width: '5%',
formatter: (cell) => gridjs.html(${'`<div style="color:${cell.color}">${cell.name}</div>`'})
},
{
name: 'Resolution',
width: '5%',
},
],
data: ${JSON.stringify(data)},
sort: true,
search: {
enabled: true,
selector: (cell, rowIndex, cellIndex) => cell ? cell.name || cell : ''
},
style: {
td: {
padding: '5px 5px'
},
th: {
padding: '5px 5px'
}
}
}).render(document.getElementById("gridjs"));
})
</script>`,
);

context.getPerfMeasure('addJira');
};
};

0 comments on commit eb0a38a

Please sign in to comment.