Created
February 19, 2020 16:00
-
-
Save Xotabu4/a163978e50294059bcc968acd7472c36 to your computer and use it in GitHub Desktop.
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
// name: Trigger tests for run | |
// description: Triggers automated tests for a test run | |
// author: Oleksandr Khotemskyi <xotabu4@github.io> | |
// version: 1.0 | |
// includes: ^runs/view | |
// excludes: | |
// js: | |
$(document).ready( function () { | |
// TestRail project id -> gitlab pipeline | |
document.TEST_RAIL_PROJECT_MAPPING = { | |
12: { | |
hookURL: 'https://gitlab.com/api/v4/projects/1/trigger/pipeline', | |
// Access token for triggering this project | |
token: "123", | |
// branch to use | |
ref: "master", | |
}, | |
9: { | |
hookURL: 'https://gitlab.com/api/v4/projects/2/trigger/pipeline', | |
token: '123', | |
ref: "master", | |
}, | |
5: { | |
hookURL: 'https://gitlab.com/api/v4/projects/3/trigger/pipeline', | |
token: '123', | |
ref: "master", | |
}, | |
13: { | |
hookURL: 'https://gitlab.com/api/v4/projects/4/trigger/pipeline', | |
// Access token for triggering this project | |
token: "123", | |
// branch to use | |
ref: "master", | |
}, | |
} | |
// Showing button only if available mapping exist | |
if (document.TEST_RAIL_PROJECT_MAPPING[uiscripts.context.project.id]) { | |
/* Create the button */ | |
var button = $('<div class="toolbar content-header-toolbar"><a class="toolbar-button toolbar-button-last toolbar-button-first content-header-button button-start" href="javascript:void(0)">Launch Autotests</a></div>'); | |
/* Add it to the toolbar */ | |
$("#content-header .content-header-inner").prepend(button); | |
/* Bind the click event to trigger the automated tests */ | |
$("a", button).click(async function () { | |
App.Dialogs.message( | |
`Starting automated tests. This might take up to 1 min ...`, | |
); | |
const projectProps = document.TEST_RAIL_PROJECT_MAPPING[uiscripts.context.project.id] | |
const resp = await fetch( | |
// Hardcoded url to own small proxy, since we need to bypass CORS: | |
// https://github.com/Xotabu4/xotabu4-cors-forwarder | |
// Feel free to redeploy to own server if needed. | |
'https://xotabu4-cors-forwarder.herokuapp.com/', | |
{ | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
body: JSON.stringify({ | |
method: 'POST', | |
url: projectProps.hookURL, | |
formData: { | |
token: projectProps.token, | |
ref: projectProps.ref, | |
// Will be saved to env variable in gitlab runner as string | |
"variables[TEST_RAIL_RUN_PARAMS]": JSON.stringify(uiscripts.context) | |
} | |
}), | |
} | |
).catch(fetchFail => { | |
App.Dialogs.error('An error occurred while trying to trigger the automated tests. Watch browser console for details'); | |
console.error('=== Make sure that you allowed unsafe scripts, and probably adblock and ghostery should be disabled for this domain') | |
console.error(fetchFail) | |
throw fetchFail | |
}) | |
if (!resp.ok) { | |
App.Dialogs.error('An error occurred while trying to trigger the automated tests. Watch browser console for details'); | |
console.error(resp) | |
throw resp | |
} | |
let ciResp = await resp.json() | |
let ciRespBody = JSON.parse(ciResp.body) | |
App.Dialogs.message( | |
`Frontend e2e tests started. Test cases statuses will be updated with automation results. | |
Launch URL: ${ciRespBody.web_url} | |
`, | |
); | |
console.log('CI response:', ciResp) | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment