Skip to content

Instantly share code, notes, and snippets.

@PedroHLC
Last active June 17, 2020 15:42
Show Gist options
  • Save PedroHLC/b983396b7f6e2b2c4300af6695b02f19 to your computer and use it in GitHub Desktop.
Save PedroHLC/b983396b7f6e2b2c4300af6695b02f19 to your computer and use it in GitHub Desktop.
Deploy to GitHub Action throught the enviroments page in your browser
// ==UserScript==
// @name Deploy Button for GitHub Actions
// @namespace https://pedrohlc.com/
// @version 0.0.2
// @downloadURL https://gist.github.com/PedroHLC/b983396b7f6e2b2c4300af6695b02f19/raw/github.deploybtn.js
// @updateURL https://gist.github.com/PedroHLC/b983396b7f6e2b2c4300af6695b02f19/raw/github.deploybtn.meta.js
// @description Add a "Deploy Master" button on GitHub enviroments page
// @author PedroHLC
// @match github.com/*/deployments
// @grant GM_xmlhttpRequest
// @run-at document-idle
// ==/UserScript==
let _parse_url = /^\/([^\/]+)\/([^\/]+)\/deployments_environment\?environment=(.+)$/
let _auth_token = localStorage.getItem('deploy_token');
function onDeployLoad(response) {
if (response.status == 201) alert ('Deploy done!');
else {
console.log(response);
alert('Deploy failed.');
}
}
var envs = document.querySelectorAll('.repository-content > .page > .Box');
envs.forEach((env) => {
if (!_auth_token || _auth_token.length === 0) {
alert('Token not set');
return;
}
var _url = _parse_url.exec(env.dataset.url);
var _repo, _env;
if (_url && _url.length === 4) {
_repo = _url[1]+'/'+_url[2];
_env = _url[3];
} else {
alert('Could not parse repo or enviroment name.');
return;
}
var deployBtn = document.createElement('button');
deployBtn.append('Deploy Master');
deployBtn.onclick = function() { GM_xmlhttpRequest({
method: 'POST',
url: 'https://api.github.com/repos/'+_repo+'/deployments',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/vnd.github.ant-man-preview+json',
'Authorization': 'token ' + _auth_token
},
data: '{"ref": "master", "required_contexts": [], "environment": "'+_env+'", "auto_merge": false}',
onload: onDeployLoad
}); };
env.append(deployBtn);
});
let setAuthForm = document.createElement("div");
let authInput = document.createElement("input");
let authButton = document.createElement("button");
authButton.append("Set deploy's token");
authButton.onclick = function () {
localStorage.setItem('deploy_token', authInput.value);
authInput.value = 'Done!';
}
setAuthForm.append(authInput);
setAuthForm.append(authButton);
document.querySelector(".repository-content > .page").prepend(setAuthForm);
// ==UserScript==
// @name Deploy Button for GitHub Actions
// @namespace https://pedrohlc.com/
// @version 0.0.2
// @description Add a "Deploy Master" button on GitHub enviroments page
// @author PedroHLC
// @match github.com/*/deployments
// @grant GM_xmlhttpRequest
// @run-at document-idle
// ==/UserScript==
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment