This repository has been archived by the owner on Sep 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
codecov.coffee
152 lines (137 loc) · 3.97 KB
/
codecov.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
class window.Codecov
slug: null # :owner/:repo
ref: null # sha of head
base: '' # sha of base (compare|pull only)
file: '' # specific file name viewing (blob only)
page: null # type of github page: blob|compare|pull
found: no # was coverage found
urlid: 0 # which url to use when searching reports
cache: [null, null]
settings:
urls: []
first_view: 'im'
debug: no
callback: null
debug_url: null
log: (title, data) -> console.log(@, title, data) if @settings.debug
constructor: (prefs, cb) ->
###
Called once at start of extension
###
# establish settings
@settings = $.extend(@settings, prefs)
@settings.urls = [] unless @settings.urls
@settings.urls.unshift 'https://codecov.io'
# callback to allow custom events for each browser
cb? @
# Go
@_start()
get_ref: -> # find and return the page ref
prepare: -> # first prepare the page for coverage overlay
overlay: -> # method to overlay coverage results
_start: ->
###
CALLED: when dom changes and page first loads
GOAL: is to collect page variables, insert dom elements, bind callbacks
###
@log('::start')
href = (@settings.debug_url or document.URL).split('/')
@slug = "#{href[3]}/#{href[4]}"
@page = href[5]
@ref = @get_ref href
@log('::ref', @ref)
if @ref
@prepare()
@_run()
_run: ->
###
CALLED: when coverage should be retrieved.
GOAL: get coverage from cache -> storage -> URL
###
return if @_processing
@log('::run')
self = @
slugref = "#{self.slug}/#{self.ref}"
# get fron storage
# ----------------
@_processing = yes
if @cache[0] == slugref
self.log('process(cache)')
@_process @cache[1]
else
storage_get slugref, (res) ->
if res?[self.ref]
self.log('process(storage)', res[self.ref])
self._process res[self.ref]
else
# run first url
self._get self.settings.urls[self.urlid]
_get: (endpoint) ->
###
CALLED: to get the coverage report from Codecov (or Enterprise urls)
GOAL: http fetch coverage
###
@log('::get', endpoint)
self = @
# get coverage
# ============
$.ajax
url: "#{endpoint}/#{@service}/#{@slug}?ref=#{@ref}#{@base}"
type: 'get'
dataType: 'json'
headers: Accept: 'application/json'
success: (res) ->
self.url = endpoint # keep the url that worked
self.found = yes
self._process res, yes
# for testing purposes
complete: ->
self.log('::ajax.complete', arguments)
self.settings?.callback?()
# try to get coverage data from enterprise urls if any
error: (xhr, type, reason) ->
self.log(arguments)
self._get(self.settings.urls[self.urlid+=1]) if self.settings.urls.length > self.urlid+1
self.error(xhr.status, reason)
_process: (res, store=no) ->
###
CALLED: to process report data
GOAL: to update the dom with coverage
###
@_processing = no
@log('::process')
slugref = "#{@slug}/#{@ref}"
# cache in extension
@cache = [slugref, res]
# cache in storage
if store and @cacheable
storage_set {slugref: res}, -> null
try
@overlay(res)
catch error
@log error
@error 500, error
find_best_fit_path: (fp, files) ->
matches = [path for path of files when path[fp.length*-1..] is fp or fp[path.length*-1..] is path]
files[Math.max(matches)] if matches.length > 0
color: (ln) ->
if ln is 0
"missed"
else if not ln
null
else if ln is true
"partial"
else if ln instanceof Array
h = $.grep(ln, (p) -> p[2]>0).length > 0
m = $.grep(ln, (p) -> p[2]==0).length > 0
if h and m then "partial" else if h then "hit" else "missed"
else if '/' in ln.toString()
v = ln.split('/')
if v[0] is '0'
"missed"
else if v[0] == v[1]
"hit"
else
"partial"
else
"hit"