Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Gardening] Refactor parse xml #212

Merged
merged 4 commits into from
Sep 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions __tests__/parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
import path from 'path'
import {Annotation} from '../src/annotation'
import {parseXml} from '../src/parser'
import {parseXmls, parseXml} from '../src/parser'

test('test parse', () => {
test('test parseXmls', () => {
const file = path.join(__dirname, 'resource', 'sample-lint-results.xml')

const annotation1 = new Annotation(
'Warning',
'Useless parent layout: This `RelativeLayout` layout or its `FrameLayout` parent is useless; transfer the `background` attribute to the other view',
'layout.xml',
11,
22
)
const annotation2 = new Annotation(
'Error',
'Ignoring results: The result of `subscribe` is not used',
'Foo.kt',
33,
44
)

expect(parseXmls([file])).resolves.toEqual([annotation1, annotation2])
})

test('test parseXml', () => {
const xml = `<?xml version="1.0" encoding="UTF-8"?>
<issues format="5" by="lint 3.6.1">
<issue
Expand Down
29 changes: 17 additions & 12 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

12 changes: 3 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import fs from 'fs'
import * as core from '@actions/core'
import * as glob from '@actions/glob'
import {echoMessages} from './command'
import {parseXml} from './parser'
import {parseXmls} from './parser'

async function run(): Promise<void> {
try {
Expand All @@ -14,14 +13,9 @@ async function run(): Promise<void> {
const globber = await glob.create(xmlPath, globOptions)
const files = await globber.glob()

const annotationsList = await Promise.all(
files.map(async file => {
const xml = fs.readFileSync(file, 'utf-8')
return await parseXml(xml)
})
)
const annotations = await parseXmls(files)

echoMessages(annotationsList.flat())
echoMessages(annotations)
} catch (error) {
core.setFailed(error.message)
}
Expand Down
15 changes: 13 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import fs from 'fs'
import * as core from '@actions/core'
import * as xml2js from 'xml2js'
import {Annotation} from './annotation'

export const parseXml = async (reportXml: string): Promise<Annotation[]> => {
export const parseXmls = async (files: string[]): Promise<Annotation[]> => {
const list = await Promise.all(
files.map(async file => {
const xml = fs.readFileSync(file, 'utf-8')
return await parseXml(xml)
})
)
return list.flat()
}

export const parseXml = async (text: string): Promise<Annotation[]> => {
const parser = new xml2js.Parser()
const xml = await parser.parseStringPromise(reportXml)
const xml = await parser.parseStringPromise(text)
return new Promise(resolve => {
try {
const annotations: Annotation[] = []
Expand Down