11import * as smartweave from "smartweave" ;
2+ import shell from "shelljs"
3+ import { VERSION } from "../helper.js"
24
35// prettier-ignore
4- const argitRemoteURIRegex = '^dgit :\/\/([a-zA-Z0-9-_]{43})\/([A-Za-z0-9_.-]*)'
6+ const argitRemoteURIRegex = '^gitopia :\/\/([a-zA-Z0-9-_]{43})\/([A-Za-z0-9_.-]*)'
57const contractId = "UvjBvJUy8pOMR_lf85tBDaJD0jF85G5Ayj1p2h7yols" ;
68
7- const repoQuery = ( remoteURI ) => {
8- const { repoOwnerAddress, repoName } = parseArgitRemoteURI ( remoteURI ) ;
9- return {
10- op : "and" ,
11- expr1 : {
12- op : "and" ,
13- expr1 : {
14- op : "equals" ,
15- expr1 : "App-Name" ,
16- expr2 : "dgit" ,
17- } ,
18- expr2 : {
19- op : "equals" ,
20- expr1 : "from" ,
21- expr2 : repoOwnerAddress ,
22- } ,
23- } ,
24- expr2 : { op : "equals" , expr1 : "Repo" , expr2 : repoName } ,
25- } ;
26- } ;
27-
289export function parseArgitRemoteURI ( remoteURI ) {
2910 const matchGroups = remoteURI . match ( argitRemoteURIRegex ) ;
3011 const repoOwnerAddress = matchGroups [ 1 ] ;
@@ -33,57 +14,36 @@ export function parseArgitRemoteURI(remoteURI) {
3314 return { repoOwnerAddress, repoName } ;
3415}
3516
36- function addTransactionTags ( tx , repo , txType ) {
37- tx . addTag ( "Repo" , repo ) ;
38- tx . addTag ( "Type" , txType ) ;
39- tx . addTag ( "App-Name" , "dgit" ) ;
40- tx . addTag ( "version" , "0.0.1" ) ;
41- tx . addTag ( "Unix-Time" , Math . round ( new Date ( ) . getTime ( ) / 1000 ) ) ; // Add Unix timestamp
42- return tx ;
43- }
44-
4517export async function makeUpdateRefDataItem (
4618 arData ,
4719 wallet ,
4820 remoteURI ,
49- name ,
50- ref
21+ ref ,
22+ oid
5123) {
5224 const { repoName } = parseArgitRemoteURI ( remoteURI ) ;
5325 const tags = [
54- { name : "App-Name" , value : "dgit" } ,
55- { name : "version" , value : "0.0.1" } ,
5626 { name : "Repo" , value : repoName } ,
27+ { name : "Version" , value : "0.0.2" } ,
28+ { name : "Ref" , value : ref } ,
5729 { name : "Type" , value : "update-ref" } ,
30+ { name : "App-Name" , value : "gitopia" } ,
5831 {
5932 name : "Unix-Time" ,
6033 value : Math . round ( new Date ( ) . getTime ( ) / 1000 ) . toString ( ) ,
6134 } ,
62- { name : "ref" , value : name } ,
6335 { name : "Content-Type" , value : "text/plain" } ,
6436 ] ;
6537
66- const item = await arData . createData ( { data : ref , tags } , wallet ) ;
67- return await arData . sign ( item , wallet ) ;
68- }
69-
70- export async function pushGitObject ( arweave , wallet , remoteURI , oid , object ) {
71- const { repoName } = parseArgitRemoteURI ( remoteURI ) ;
72-
73- let tx = await arweave . createTransaction ( { data : object } , wallet ) ;
74- tx = addTransactionTags ( tx , repoName , "push-git-object" ) ;
75- tx . addTag ( "oid" , oid ) ;
76- tx . addTag ( "Content-Type" , "application/octet-stream" ) ;
77-
78- await arweave . transactions . sign ( tx , wallet ) ;
79- let uploader = await arweave . transactions . getUploader ( tx ) ;
80-
81- while ( ! uploader . isComplete ) {
82- await uploader . uploadChunk ( ) ;
83- // console.error(
84- // `${uploader.pctComplete}% complete, ${uploader.uploadedChunks}/${uploader.totalChunks}`
85- // );
38+ const numCommits = shell . exec ( `git rev-list --count ${ ref } ` , { silent : true } ) . stdout . trim ( ) ;
39+ const obj = {
40+ oid,
41+ numCommits
8642 }
43+ const data = JSON . stringify ( obj ) ;
44+
45+ const item = await arData . createData ( { data, tags } , wallet ) ;
46+ return await arData . sign ( item , wallet ) ;
8747}
8848
8949export const makeDataItem = async (
@@ -95,15 +55,15 @@ export const makeDataItem = async (
9555) => {
9656 const { repoName } = parseArgitRemoteURI ( remoteURI ) ;
9757 const tags = [
98- { name : "App-Name " , value : "dgit" } ,
99- { name : "version " , value : "0.0.1 " } ,
58+ { name : "Oid " , value : oid } ,
59+ { name : "Version " , value : "0.0.2 " } ,
10060 { name : "Repo" , value : repoName } ,
101- { name : "Type" , value : "push-git-object" } ,
61+ { name : "Type" , value : "git-object" } ,
62+ { name : "App-Name" , value : "gitopia" } ,
10263 {
10364 name : "Unix-Time" ,
10465 value : Math . round ( new Date ( ) . getTime ( ) / 1000 ) . toString ( ) ,
10566 } ,
106- { name : "oid" , value : oid } ,
10767 { name : "Content-Type" , value : "application/octet-stream" } ,
10868 ] ;
10969
@@ -122,12 +82,21 @@ export const postBundledTransaction = async (
12282 const bundle = await arData . bundleData ( dataItems ) ;
12383 const data = JSON . stringify ( bundle ) ;
12484 const tx = await arweave . createTransaction ( { data } , wallet ) ;
85+ tx . addTag ( "Repo" , repoName ) ;
86+ tx . addTag ( "Version" , "0.0.2" )
87+ tx . addTag ( "Type" , "git-objects-bundle" ) ;
88+ tx . addTag ( "App-Name" , "gitopia" ) ;
12589 tx . addTag ( "Bundle-Format" , "json" ) ;
12690 tx . addTag ( "Bundle-Version" , "1.0.0" ) ;
12791 tx . addTag ( "Content-Type" , "application/json" ) ;
128- tx . addTag ( "App-Name" , "dgit" ) ;
129- tx . addTag ( "Type" , "git-objects-bundle" ) ;
130- tx . addTag ( "Repo" , repoName ) ;
92+ tx . addTag ( "Helper" , VERSION ) ;
93+
94+ // Push triggered from gitopia mirror action
95+ if ( process . env . GITHUB_SHA ) {
96+ tx . addTag ( "Origin" , "gitopia-mirror-action" )
97+ } else {
98+ tx . addTag ( "Origin" , "git-remote-gitopia" )
99+ }
131100
132101 await arweave . transactions . sign ( tx , wallet ) ;
133102 const uploader = await arweave . transactions . getUploader ( tx ) ;
@@ -147,33 +116,11 @@ export const postBundledTransaction = async (
147116 { target : holder , quantity : arweave . ar . arToWinston ( "0.01" ) } ,
148117 wallet
149118 ) ;
150- pstTx . addTag ( "App-Name" , "dgit" ) ;
151- pstTx . addTag ( "version" , "0.0.1" ) ;
119+ pstTx . addTag ( "Bundle-TxID" , tx . id )
152120 pstTx . addTag ( "Repo" , repoName ) ;
121+ pstTx . addTag ( "Version" , "0.0.2" ) ;
122+ pstTx . addTag ( "App-Name" , "gitopia" ) ;
153123
154124 await arweave . transactions . sign ( pstTx , wallet ) ;
155125 await arweave . transactions . post ( pstTx ) ;
156126} ;
157-
158- export async function fetchGitObjects ( arweave , arData , remoteURI ) {
159- const query = {
160- op : "and" ,
161- expr1 : repoQuery ( remoteURI ) ,
162- expr2 : { op : "equals" , expr1 : "Type" , expr2 : "push-git-object" } ,
163- } ;
164- const txids = await arweave . arql ( query ) ;
165- const objects = await Promise . all (
166- txids . map ( async ( txid ) => {
167- const tx = await arweave . transactions . get ( txid ) ;
168- let oid = "" ;
169- tx . get ( "tags" ) . forEach ( ( tag ) => {
170- const key = tag . get ( "name" , { decode : true , string : true } ) ;
171- const value = tag . get ( "value" , { decode : true , string : true } ) ;
172- if ( key === "oid" ) oid = value ;
173- } ) ;
174- const data = await arweave . transactions . getData ( txid , { decode : true } ) ;
175- return { data, oid } ;
176- } )
177- ) ;
178- return objects ;
179- }
0 commit comments