From 6bd878604761c793ea2307fc9e2e51fa0aa9186c Mon Sep 17 00:00:00 2001 From: "R.Andres Castaneda" Date: Tue, 26 Apr 2022 13:05:09 -0400 Subject: [PATCH 1/2] Fix reading tags when repository has been transferred to another user --- githubquery.ado | 167 ++++++++++++++++++++---------------------------- 1 file changed, 71 insertions(+), 96 deletions(-) diff --git a/githubquery.ado b/githubquery.ado index 61f0caf..499e24b 100644 --- a/githubquery.ado +++ b/githubquery.ado @@ -1,120 +1,95 @@ *cap prog drop githubquery program githubquery, rclass +version 16 + +syntax anything + +qui { - syntax anything - - tempfile api tmp - tempname hitch knot + preserve + drop _all - capture qui copy "https://api.github.com/repos/`anything'/releases" `api', replace + cap scalar page = fileread("https://api.github.com/repos/`anything'/releases") if _rc { // Identify the errors with useful information if _rc == 679 display as err "GitHub API rate limit exceeded... try again after some time" - error _rc } - file open `hitch' using "`api'", read - qui file open `knot' using "`tmp'", write replace - file read `hitch' line + // load data and transform to dataframe + mata { + lines = st_strscalar("page") + lines = ustrsplit(lines, ",")' + lines = strtrim(lines) + lines = stritrim(lines) + + lines = subinstr(lines, `"":""', "->") + lines = subinstr(lines, `"""', "") + } + getmata lines, replace + + split lines, parse ("->") + rename lines? (code url) + + // get date + tempname ghpb + frame copy `c(frame)' `ghpb' + + frame `ghpb' { + keep if regexm(code, "published_at") + gen n = _n + rename url date + } + + // merge date and tags frames + keep if regexm(url, "releases/tag") + gen n = _n + gen tag = regexs(2) if regexm(url, "(releases/tag/)(.*)") + + frlink 1:1 n, frame(`ghpb') + frget date, from(`ghpb') + - //remove the beginning brackets - local line : subinstr local line "[{" "" - local line : subinstr local line "`" "", all + // display - di in text _n " {hline 40}" _n /// - " {bf:Version}" _col(16) "{bf:Release Date}" _col(34) "{bf:Install} " _n /// + noi di in text _n " {hline 40}" _n /// + " {bf:Version}" _col(16) "{bf:Release Date}" _col(34) "{bf:Install} " _n /// " {hline 40}" - while r(eof) == 0 { - tokenize `"`macval(line)'"' , parse(",") + local N = _N + local bskip = 8 // basic skip + local vlnth = 5 // version length + + forvalues i = 1/`N' { + local link = url[`i'] + local version = tag[`i'] + local date = date[`i'] + if regexm("`date'", "([0-9]+\-[0-9]+\-[0-9]+)(.*)") { + local date = regexs(1) + } - while !missing(`"`macval(1)'"') { - - if `"`macval(1)'"' == "," { - macro shift - } - else if `"`macval(1)'"' == "html_url" { - if strlen(`"`macval(2)'"') < strlen(`"https://github.com/`anything'/"') { - macro shift - macro shift - } - else { - file write `knot' `"`macval(1)'"' _n - macro shift - local 1 : di substr(`"`macval(1)'"', 2,.) - file write `knot' `"`macval(1)'"' _n - macro shift - } - } - else if `"`macval(1)'"' == "tag_name" { - file write `knot' `"`macval(1)'"' _n - macro shift - local a strlen(`"`macval(1)'"') - - local 1 : di substr(`"`macval(1)'"', 3,`a'-3) - file write `knot' `"`macval(1)'"' _n - macro shift - } - - else if `"`macval(1)'"' == "published_at" { - file write `knot' `"`macval(1)'"' _n - macro shift - local 1 : di substr(`"`macval(1)'"', 3,10) - file write `knot' `"`macval(1)'"' _n - macro shift - } - else { - macro shift - } + if (length("`version'") <= `vlnth') { + local skip = 8 } - file read `hitch' line - } - - file close `hitch' - file close `knot' - - file open `hitch' using "`tmp'", read - file read `hitch' line - local latestversion - - while r(eof) == 0 { - if `"`macval(line)'"' == "html_url" { - file read `hitch' line - local link `"`macval(line)'"' - file read `hitch' line - - if `"`macval(line)'"' == "tag_name" { - file read `hitch' line - local version `"`macval(line)'"' - file read `hitch' line - } - if `"`macval(line)'"' == "published_at" { - file read `hitch' line - local date `"`macval(line)'"' - } - - // get the latest version - // ----------------------------------------------------------------------- - if missing("`latestversion'") local latestversion "`version'" - - if !missing(`"`link'"') & !missing("`version'") & !missing("`date'") { - - di `" {browse `link':`version'}"' _skip(8) "`date'" _skip(7) /// - "{stata github install `anything', version(`version') : Install}" - - local link - local version - local date - } + else { + local skip = `vlnth'+`bskip' - length("`version'") } - file read `hitch' line + noi di `" {browse "`link'":`version'}"' _skip(`skip') "`date'" _skip(7) /// + "{stata github install `anything', version(`version') : Install}" } - di in text " {hline 40}" _n - return local latestversion `latestversion' + noi di in text " {hline 40}" _n + + local latestversion = tag[1] + +} // end of qui + +return local latestversion `latestversion' + end +exit + From 2168967689ff76d2c7e09299a749b61b402d32fc Mon Sep 17 00:00:00 2001 From: "R.Andres Castaneda" Date: Tue, 16 Aug 2022 16:16:52 -0400 Subject: [PATCH 2/2] make it work with Stata 14 --- githubquery.ado | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/githubquery.ado b/githubquery.ado index 499e24b..39d6179 100644 --- a/githubquery.ado +++ b/githubquery.ado @@ -1,6 +1,6 @@ *cap prog drop githubquery program githubquery, rclass -version 16 +version 14 syntax anything