Skip to content

Commit

Permalink
Fix issue #28
Browse files Browse the repository at this point in the history
  - Retry over HTTP in case HTTPS request fails
  - Thanks @lelvisl and @bennypowers
  • Loading branch information
Xavier Parizet committed Apr 10, 2018
1 parent d06ca26 commit 5d4461e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 15 deletions.
19 changes: 15 additions & 4 deletions lib/gitlab.coffee
Expand Up @@ -8,10 +8,11 @@ class GitlabStatus
@unsecureSsl = atom.config.get('gitlab-integration.unsecureSsl')
@updating = {}
@watchTimeout = null
@protocol = 'https'

fetch: (host, q, paging=false) ->
log " -> fetch '#{q}' from '#{host}'"
@get("https://#{host}/api/v4/#{q}").then((res) =>
@get("#{@protocol}://#{host}/api/v4/#{q}").then((res) =>
log " <- ", res
if res.headers['x-next-page']
if paging
Expand All @@ -24,7 +25,7 @@ class GitlabStatus
(dum, i) =>
log " -> page #{i + 2}"
@get(
"https://#{host}/api/v4/#{q}" +
"#{@protocol}://#{host}/api/v4/#{q}" +
(if q.includes('?') then '&' else '?') +
"per_page=" + res.headers['x-per-page'] +
"&page=#{i+2}"
Expand Down Expand Up @@ -63,7 +64,16 @@ class GitlabStatus
agentOptions: {
rejectUnauthorized: @unsecureSsl is false,
}
})
}).catch((error) =>
if url.startsWith('https')
console.error "cannot perform request #{url}, retrying using http", error
@get(url.replace('https', 'http')).then((result) =>
@protocol = 'http'
Promise.resolve(result)
).catch((error) => Promise.reject(error))
else
Promise.reject(error)
)

watch: (host, projectPath, repos) ->
projectPath = projectPath.toLowerCase()
Expand Down Expand Up @@ -97,7 +107,8 @@ class GitlabStatus
)

schedule: ->
@timeout = setTimeout @update.bind(@), @period
if @period?
@timeout = setTimeout @update.bind(@), @period

update: ->
@pending = Object.keys(@projects).slice()
Expand Down
51 changes: 40 additions & 11 deletions spec/gitlab-spec.coffee
Expand Up @@ -4,6 +4,8 @@ nock = require 'nock'

describe "GitLab API", ->
gitlab =
scopeHttp =
scopeHttps =
project =
id: 666
path_with_namespace: 'dummy/project'
Expand Down Expand Up @@ -45,14 +47,15 @@ describe "GitLab API", ->
{ name: 'stage-5', status: 'pending', jobs: jobs.slice(9, 13).reverse() },
]
beforeEach ->
log.debug = true
scopeHttp = nock('http://gitlab-api').get('*').replyWithError('HTTP not working')
scopeHttps = nock('https://gitlab-api')
view = jasmine.createSpyObj 'view', [
'loading', 'unknown', 'onStagesUpdate'
]
gitlab = new GitLab view

it "correctly watches a project", ->
scope = nock('https://gitlab-api')
scope = scopeHttps
.get('/api/v4/projects?membership=yes')
.reply(200, [ project ])

Expand All @@ -74,7 +77,7 @@ describe "GitLab API", ->
)

it "ignores case when looking for a project", ->
scope = nock('https://gitlab-api')
scope = scopeHttps
.get('/api/v4/projects?membership=yes')
.reply(200, [ sensitiveProject ])
scopePipelines = nock('https://gitlab-api')
Expand Down Expand Up @@ -105,7 +108,7 @@ describe "GitLab API", ->
)

it "processes only the last pipeline", ->
scope = nock('https://gitlab-api')
scope = scopeHttps
.get('/api/v4/projects/666/pipelines')
.reply(200, pipelines)

Expand Down Expand Up @@ -134,7 +137,7 @@ describe "GitLab API", ->
)

it "handles jobs with stages", ->
scope = nock('https://gitlab-api')
scope = scopeHttps
.get('/api/v4/projects/666/pipelines/2/jobs')
.reply(200, jobs)

Expand All @@ -155,7 +158,7 @@ describe "GitLab API", ->
)

it "correctly handles external jobs", ->
scope = nock('https://gitlab-api')
scope = scopeHttps
.get('/api/v4/projects/666/pipelines/3/jobs')
.reply(200, [])

Expand All @@ -176,7 +179,7 @@ describe "GitLab API", ->
)

it "correctly handles project with no pipelines", ->
scope = nock('https://gitlab-api')
scope = scopeHttps
.get('/api/v4/projects/666/pipelines')
.reply(200, [])

Expand Down Expand Up @@ -204,15 +207,15 @@ describe "GitLab API", ->
)

it "correctly handles paging", ->
scope = nock('https://gitlab-api')
scope = scopeHttps
.get('/api/v4/projects?membership=yes')
.reply(200, [ project ], {
'X-Total-Pages': 2,
'X-Next-Page': 2,
'X-Page': 1,
'X-Per-Page': 1,
})
scopePage2 = nock('https://gitlab-api')
scopePage2 = scopeHttps
.get('/api/v4/projects?membership=yes&per_page=1&page=2')
.reply(200, [ anotherProject ], {
'X-Page': 2,
Expand All @@ -239,10 +242,10 @@ describe "GitLab API", ->
)

it "correctly requests pipelines for current branch", ->
projectScope = nock('https://gitlab-api')
projectScope = scopeHttps
.get('/api/v4/projects?membership=yes')
.reply(200, [ project ])
scope = nock('https://gitlab-api')
scope = scopeHttps
.get('/api/v4/projects/666/pipelines?ref=abranch')
.reply(200, pipelines)

Expand Down Expand Up @@ -296,3 +299,29 @@ describe "GitLab API", ->
expect(gitlab.updateJobs).not.toHaveBeenCalled()
expect(gitlab.view.loading).not.toHaveBeenCalled()
expect(gitlab.view.onStagesUpdate).toHaveBeenCalledWith({})

it "fallbacks to HTTP in case HTTPS request does not work", ->
scopeHttps = scopeHttps
.get('/api/v4/projects?membership=yes')
.replyWithError('a HTTPS timeout was simulated')
scopeHttp = nock('http://gitlab-api')
.get('/api/v4/projects?membership=yes')
.reply(200, [ project ])

promise = gitlab.watch('gitlab-api', 'dummy/project')
expect(gitlab.view.loading).toHaveBeenCalledWith(
project.path_with_namespace,
'loading project...',
)

waitsForPromise ->
promise

runs ->
expect(scopeHttps.isDone()).toBe(true)
expect(scopeHttp.isDone()).toBe(true)
expect(gitlab.projects['dummy/project']).toEqual(
host: 'gitlab-api'
project: project
repos: undefined
)

0 comments on commit 5d4461e

Please sign in to comment.