Skip to content

Commit

Permalink
Merge pull request c2siorg#110 from mahmoodalisha/feature/webiu-2024
Browse files Browse the repository at this point in the history
Add Contributor Search Page to Display Issues and Pull Requests
  • Loading branch information
rajutkarsh07 authored Dec 1, 2024
2 parents eecff6e + e407039 commit 4f360bc
Show file tree
Hide file tree
Showing 17 changed files with 1,262 additions and 36 deletions.
2 changes: 2 additions & 0 deletions webiu-server/__tests__/contributorController.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ describe('GET /contributors', () => {
followers: 100,
following: 50,
avatar_url: 'https://example.com/avatar1.jpg',
issues: [],
pullRequests: []
});
});

Expand Down
117 changes: 110 additions & 7 deletions webiu-server/controllers/contributorController.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,33 @@ const getAllContributors = async (req, res) => {
try {
const contributors = await fetchContributors(orgName, repo.name);
if (!contributors) return;

await Promise.all(
contributors.map(async (contributor) => {
try {
const userDetails = await fetchUserDetails(contributor.login);
if (!userDetails) return;


// Fetch issues and pull requests for each contributor
const issues = await fetchUserCreatedIssues(userDetails.login);
const pullRequests = await fetchUserCreatedPullRequests(userDetails.login);

// If user is already in the response, update their data
if (finalResponse[userDetails.login]) {
finalResponse[userDetails.login].repos.push(repo.name);
finalResponse[userDetails.login].issues = issues || [];
finalResponse[userDetails.login].pullRequests = pullRequests || [];
} else {
// If user is not yet in the response, create their entry
finalResponse[userDetails.login] = {
login: userDetails.login,
contributions: contributor.contributions,
repos: [repo.name],
followers: userDetails.followers,
following: userDetails.following,
avatar_url: userDetails.avatar_url,
issues: issues || [], // Ensure issues field is always present
pullRequests: pullRequests || [] // Ensure pullRequests field is always present
};
}
} catch (err) {
Expand All @@ -50,9 +60,9 @@ const getAllContributors = async (req, res) => {
}
})
);
let allContributors = []
for (const contributor in finalResponse){

let allContributors = [];
for (const contributor in finalResponse) {
allContributors.push(finalResponse[contributor]);
}

Expand All @@ -61,8 +71,44 @@ const getAllContributors = async (req, res) => {
console.error('Error fetching organization info:', error);
return res.status(500).json({ error: 'Failed to fetch organization info' });
}
};

// Helper functions to fetch issues and pull requests
async function fetchUserCreatedIssues(username) {
try {
const issuesResponse = await axios.get(
`${baseUrl}/search/issues?q=author:${username}+org:c2siorg+type:issue`,
{
headers: {
Authorization: `token ${accessToken}`,
},
}
);
return issuesResponse.data.items || []; // Return empty array if no issues found
} catch (error) {
console.error('Error fetching user-created issues:', error);
return []; // Return empty array in case of error
}
}

async function fetchUserCreatedPullRequests(username) {
try {
const pullRequestsResponse = await axios.get(
`${baseUrl}/search/issues?q=author:${username}+org:c2siorg+type:pr`,
{
headers: {
Authorization: `token ${accessToken}`,
},
}
);
return pullRequestsResponse.data.items || []; // Return empty array if no PRs found
} catch (error) {
console.error('Error fetching user-created pull requests:', error);
return []; // Return empty array in case of error
}
}


async function fetchRepositories(orgName) {
try {
const response = await axios.get(`${baseUrl}/orgs/${orgName}/repos`, {
Expand Down Expand Up @@ -111,6 +157,63 @@ async function fetchUserDetails(username) {
}
}

module.exports = {

const userCreatedIssues = async (req, res) => {
try {
const orgName = 'c2siorg';
const { username } = req.params;

const issuesResponse = await axios.get(
`${baseUrl}/search/issues?q=author:${username}+org:${orgName}+type:issue`,
{
headers: {
Authorization: `token ${accessToken}`,
},
}
);

const issues = issuesResponse.data.items;

if (!issues) {
return res.status(500).json({ error: 'Failed to fetch user-created issues' });
}

return res.status(200).json({ issues });
} catch (error) {
console.error('Error fetching user created issues:', error.response ? error.response.data : error.message);
return res.status(500).json({ error: 'Internal server error' });
}
};

const userCreatedPullRequests = async (req, res) => {
try {
const orgName = 'c2siorg';
const { username } = req.params;

const pullRequestsResponse = await axios.get(
`${baseUrl}/search/issues?q=author:${username}+org:${orgName}+type:pr`,
{
headers: {
Authorization: `token ${accessToken}`,
},
}
);

const pullRequests = pullRequestsResponse.data.items;

if (!pullRequests) {
return res.status(500).json({ error: 'Failed to fetch user-created pull requests' });
}

return res.status(200).json({ pullRequests });
} catch (error) {
console.error('Error fetching user created pull requests:', error.response ? error.response.data : error.message);
return res.status(500).json({ error: 'Internal server error' });
}
};

module.exports = {
getAllContributors,
};;
userCreatedIssues,
userCreatedPullRequests,
};
5 changes: 5 additions & 0 deletions webiu-server/routes/contributorRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@ const contributorController = require('../controllers/contributorController');

const router = express.Router();
router.get('/contributors', contributorController.getAllContributors);
// Route for getting user-created issues
router.get('/issues/:username', contributorController.userCreatedIssues);

// Route for getting user-created pull requests
router.get('/pull-requests/:username', contributorController.userCreatedPullRequests);

module.exports = router;
21 changes: 16 additions & 5 deletions webiu-server/server.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@

const dotenv = require('dotenv');
const path = require('path');
const express = require('express');
const cors = require('cors');
const contributorRoutes = require('./routes/contributorRoutes');

dotenv.config();

const app = express();
app.use(cors());

dotenv.config();
const app = require('./app');
app.use(express.json());


app.use('/api/contributor', contributorRoutes);

const PORT = process.env.PORT || 5000;


const server = app.listen(PORT, () => {
console.log(`Server is listening at port ${PORT}`);
});
console.log(`Server is listening at port ${PORT}`);
});
3 changes: 2 additions & 1 deletion webiu-ui/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"tsConfig": "tsconfig.app.json",
"inlineStyleLanguage": "scss",
"assets": ["src/favicon.ico", "src/assets"],
"styles": ["src/styles.scss"],
"styles": ["src/styles.scss",
"node_modules/@fortawesome/fontawesome-free/css/all.min.css"],
"scripts": []
},
"configurations": {
Expand Down
Loading

0 comments on commit 4f360bc

Please sign in to comment.