Skip to content

Commit

Permalink
Merge pull request #1 from internetofwater/dev
Browse files Browse the repository at this point in the history
Initial Form
  • Loading branch information
webb-ben authored Jun 25, 2024
2 parents dfeef2b + e17a955 commit 84a4330
Show file tree
Hide file tree
Showing 6 changed files with 301 additions and 1 deletion.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# https://register.geoconnex.us

The Geoconnex Registration Service makes it easy to contribute PIDs to geoconnex.us without a GitHub Account.

67 changes: 67 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Upload CSV to Geoconnex</title>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<link rel="stylesheet" href="static/css/default.css">
</head>
<body>
<div class="crumbs">
<div class="container">
<div class="row">
<div class="col-sm-12">
<a href="https://geoconnex.us">
<img src="static/img/logo.png"
title="register.geoconnex.us" />
</a>
</div>
</div>
</div>
</div>

<h1>Upload CSV to Geoconnex</h1>
<form id="uploadForm">
<label for="namespace">Namespace:</label>
<input type="text" id="namespace" name="namespace" required>

<label for="accesstoken">GitHub Access Token:</label>
<input type="password" id="accesstoken" name="accesstoken" required>

<label for="file">Choose CSV file:</label>
<input type="file" id="file" name="file" accept=".csv" required>

<button type="submit">Upload and Create Pull Request</button>
</form>

<div id="result"></div>

<!-- Contribution Documentation -->
<div id="markdownContent" class="container-narrow"></div>

<script>
// Fetch and render Markdown content from GitHub
const url = 'https://api.github.com/repos/internetofwater/geoconnex.us/contents/CONTRIBUTING.md';

fetch(url)
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then(data => {
// Decode content from Base64
const markdown = atob(data.content);
const html = marked.parse(markdown);
document.getElementById('markdownContent').innerHTML = html;
})
.catch(error => {
console.error('Error fetching Markdown:', error);
document.getElementById('markdownContent').innerHTML = '<p>Error loading Markdown content.</p>';
});
</script>
<script src="script.js"></script>
</body>
</html>
134 changes: 134 additions & 0 deletions script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
document.getElementById('uploadForm').addEventListener('submit', async function(event) {
event.preventDefault();

const namespace = document.getElementById('namespace').value;
const fileInput = document.getElementById('file');
const file = fileInput.files[0];
const token = document.getElementById('accesstoken').value;

if (!file) {
alert('Please select a file.');
return;
}

const repo = 'internetofwater/geoconnex.us';
const baseBranch = 'master';
const newBranch = `upload-${namespace}-${Date.now()}`;
const filePath = `namespaces/${namespace}/${file.name}`;

// Read the file and encode it in Base64
const reader = new FileReader();
reader.onload = async function(e) {
const content = e.target.result;
const base64Content = btoa(content);

try {
// Step 1: Create a new branch
const baseBranchUrl = `https://api.github.com/repos/${repo}/git/refs/heads/${baseBranch}`;
const baseBranchResponse = await fetch(baseBranchUrl, {
method: 'GET',
headers: {
'Authorization': `token ${token}`,
'Content-Type': 'application/json'
}
});

if (!baseBranchResponse.ok) {
throw new Error(`Error fetching base branch: ${baseBranchResponse.status} - ${baseBranchResponse.statusText}`);
}

const baseBranchData = await baseBranchResponse.json();
const newBranchUrl = `https://api.github.com/repos/${repo}/git/refs`;
const newBranchData = {
ref: `refs/heads/${newBranch}`,
sha: baseBranchData.object.sha
};

const newBranchResponse = await fetch(newBranchUrl, {
method: 'POST',
headers: {
'Authorization': `token ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(newBranchData)
});

if (!newBranchResponse.ok) {
throw new Error(`Error creating new branch: ${newBranchResponse.status} - ${newBranchResponse.statusText}`);
}

// Step 2: Check if the file exists and get its SHA and content
const fileUrl = `https://api.github.com/repos/${repo}/contents/${filePath}?ref=${newBranch}`;
const fileResponse = await fetch(fileUrl, {
method: 'GET',
headers: {
'Authorization': `token ${token}`,
'Content-Type': 'application/json'
}
});

let fileSha = null;
if (fileResponse.ok) {
const fileData = await fileResponse.json();
fileSha = fileData.sha;

const existingFileContent = atob(fileData.content.replace(/\n/g, ''));
if (existingFileContent === content) {
document.getElementById('result').innerHTML = `<p>No changes detected, pull request not created.</p>`;
return;
}
}

// Step 3: Upload the file to the new branch
const uploadUrl = `https://api.github.com/repos/${repo}/contents/${filePath}`;
const uploadData = {
message: `Add CSV file to ${namespace}`,
content: base64Content,
branch: newBranch,
sha: fileSha
};

const uploadResponse = await fetch(uploadUrl, {
method: 'PUT',
headers: {
'Authorization': `token ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(uploadData)
});

if (!uploadResponse.ok) {
throw new Error(`Error uploading file: ${uploadResponse.status} - ${uploadResponse.statusText}`);
}

// Step 4: Create a pull request
const prUrl = `https://api.github.com/repos/${repo}/pulls`;
const prData = {
title: `Add CSV file to ${namespace}`,
head: newBranch,
base: baseBranch,
body: `This PR adds a CSV file to the ${namespace} namespace.`
};

const prResponse = await fetch(prUrl, {
method: 'POST',
headers: {
'Authorization': `token ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(prData)
});

if (!prResponse.ok) {
throw new Error(`Error creating pull request: ${prResponse.status} - ${prResponse.statusText}`);
}

const prResult = await prResponse.json();
document.getElementById('result').innerHTML = `<p>Pull Request created: <a href="${prResult.html_url}" target="_blank">${prResult.html_url}</a></p>`;
} catch (error) {
document.getElementById('result').innerHTML = `<p>${error.message}</p>`;
}
};

reader.readAsBinaryString(file);
});
100 changes: 100 additions & 0 deletions static/css/default.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
color: #333;
margin: 0;
display: flex;
flex-direction: column;
align-items: center;
}

.crumbs {
width: 100%;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
margin-bottom: 20px;
}

.container {
max-width: 1200px;
margin: 0 auto;
padding: 10px 20px;
}

.container-narrow {
max-width: 800px;
margin: 0 auto;
padding: 10px 20px;
}

.crumbs img {
height: 60px;
vertical-align: middle;
}

h1 {
color: #1B335F;
}

form {
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
max-width: 400px;
width: 100%;
}

label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}

input[type="text"],
input[type="file"],
input[type="password"],
button {
width: 100%;
padding: 10px;
margin-bottom: 20px;
border: 1px solid #ddd;
border-radius: 5px;
box-sizing: border-box;
}

input[type="text"],
input[type="file"],
input[type="password"] {
border: 1px solid #1B335F;
}

button {
background-color: #1B335F;
color: white;
border: none;
cursor: pointer;
}

button:hover {
background-color: #007A68;
}

#result {
margin-top: 20px;
}

#result p {
background: #e7f7e7;
padding: 10px;
border-left: 5px solid #00A087;
}

#result a {
color: #E6000B;
text-decoration: none;
}

#result a:hover {
text-decoration: underline;
}

Binary file added static/img/favicon.ico
Binary file not shown.
Binary file added static/img/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 84a4330

Please sign in to comment.