|
| 1 | + |
| 2 | +//DOM |
| 3 | +const profileWrapper = document.getElementById('profile-wrapper'); |
| 4 | +const repoWrapper = document.getElementById('repo-wrapper'); |
| 5 | + |
| 6 | +//API |
| 7 | +const USER = 'JaEngd' |
| 8 | +const PROFILE_URL = `https://api.github.com/users/${USER}` |
| 9 | +const REPO_URL = `https://api.github.com/users/${USER}/repos` |
| 10 | + |
| 11 | +const API_TOKEN = TOKEN || process.env.API_KEY; |
| 12 | + |
| 13 | +const options = { //Object |
| 14 | + method: 'GET', //POST, PATCH, DELETE |
| 15 | + headers: { |
| 16 | + Authorization: `token ${API_TOKEN}` |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +const showProfile = () => { |
| 21 | + fetch(PROFILE_URL, options) |
| 22 | + .then(res => res.json()) //Converting the response to a JSON object |
| 23 | + .then(data => { |
| 24 | + console.log(data) |
| 25 | + profileWrapper.innerHTML += ` |
| 26 | + <div id="profile"> |
| 27 | + <figure class="profile-image"> |
| 28 | + <a href="${PROFILE_URL}"> |
| 29 | + <img src="${data.avatar_url}" alt="Avatar of ${data.login}"> |
| 30 | + </a> |
| 31 | + </figure> |
| 32 | + <h3>${data.login}</h3> |
| 33 | + <p>Public repositories: ${data.public_repos}.</p> |
| 34 | + </div> |
| 35 | + ` |
| 36 | + }) |
| 37 | +} |
| 38 | +showProfile() |
| 39 | + |
| 40 | +const showRepos = () => { |
| 41 | + fetch(REPO_URL, options) |
| 42 | + .then((response) => response.json()) |
| 43 | + .then((data) => { |
| 44 | + const myRepos = data.filter((repo) => repo.name.includes("project") && repo.fork) |
| 45 | + |
| 46 | + myRepos.forEach(repo => { |
| 47 | + repoWrapper.innerHTML += ` |
| 48 | + <div class="projects-card" id="${repo.id}"> |
| 49 | + <h3><a href="${repo.html_url}"><b>${repo.name}</b></a> <strong>(${repo.default_branch})</strong></h3> |
| 50 | + <p>Most recent push: ${new Date(repo.pushed_at).toDateString()} </p> |
| 51 | + <p id="commit_${repo.name}">Number of commits:</p> |
| 52 | + <p>Main language: ${repo.language}</p> |
| 53 | + </div> |
| 54 | + ` |
| 55 | + }) |
| 56 | + showPullRequestsArray(myRepos) |
| 57 | + drawBarChart(myRepos) |
| 58 | + }) |
| 59 | +} |
| 60 | + |
| 61 | + |
| 62 | +const showPullRequestsArray = (allRepos) => { |
| 63 | + allRepos.forEach((repo) => { |
| 64 | + fetch(`https://api.github.com/repos/Technigo/${repo.name}/pulls?per_page=100`, options) |
| 65 | + .then((response) => response.json()) |
| 66 | + .then((data) => { |
| 67 | + const myPullRequest = data.find( |
| 68 | + (pull) => pull.user.login === repo.owner.login |
| 69 | + ) |
| 70 | + if (myPullRequest) { |
| 71 | + fetchCommits(myPullRequest.commits_url, repo.name) |
| 72 | + } else { |
| 73 | + document.getElementById(`commit_${repo.name}`) |
| 74 | + .innerHTML = 'Pull request unavailable, or closed.'; |
| 75 | + } |
| 76 | + }) |
| 77 | + }) |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +const fetchCommits = (myCommitsUrl, myRepoName) => { |
| 82 | + fetch(myCommitsUrl) |
| 83 | + .then((response) => response.json()) |
| 84 | + .then((data) => { |
| 85 | + document.getElementById(`commit_${myRepoName}`).innerHTML += data.length |
| 86 | + }) |
| 87 | + } |
| 88 | + |
| 89 | +showRepos() |
| 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 | + |
0 commit comments