Skip to content

Instantly share code, notes, and snippets.

@basictomonokai
Created October 2, 2018 04:40
Show Gist options
  • Save basictomonokai/c2facf803e774a694b323a4e43016724 to your computer and use it in GitHub Desktop.
Save basictomonokai/c2facf803e774a694b323a4e43016724 to your computer and use it in GitHub Desktop.
bitbucketからソースプログラムを取得
<html>
<head>
<style>
#user,#pass,#source {
margin: 5px;
width: 30%;
}
#repo {
margin: 5px;
width: 70%;
}
#prog {
margin: 5px;
width: 80%;
}
</style>
</head>
<body>
<button id="getsource">ソース取得</button>
<div>
<input type="text" id="user" placeholder="ユーザー名">
<input type="text" id="pass" placeholder="パスワード">
<input type="text" id="source" placeholder="ソース名(aaa/bbb)">
</div>
<input type="text" id="repo" placeholder="リポジトリ名(xxx/yyy)">
<div>
</div>
<textarea id="prog" rows="20" placeholder="取得したソース"></textarea>
<div>
</div>
<script>
let username;
let password;
let sourcename;
let reponame;
// bitbucketソース取得関数
function bitbucketGet(){
let url = 'https://api.bitbucket.org/2.0/repositories/'+ reponame + '/src/master/' + sourcename;
// ユーザー、パスワードをbase64化する
var blob = new Blob([username + ":" + password]),
r = new FileReader();
r.onload = function() {
console.info(r.result.substr(r.result.indexOf(',')+1));
// ヘッダー生成
let headers = new Headers();
headers.append('Authorization', 'Basic ' + r.result.substr(r.result.indexOf(',')+1));
console.log(headers);
// bitbucket apiを実行
fetch(url, {method:'GET',
headers: headers,
//credentials: 'user:passwd'
})
.then(response => response.text())
.then(function(text) {
console.log(text);
document.getElementById('prog').value = text;
});
};
r.readAsDataURL(blob);
};
// ソース取得ボタンの処理
var btn = document.getElementById('getsource');
btn.addEventListener('click', function() {
username = document.getElementById('user').value;
password = document.getElementById('pass').value;
sourcename = document.getElementById('source').value;
reponame = document.getElementById('repo').value;
bitbucketGet();
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment