Skip to content

Commit d870d4f

Browse files
authored
Guess updateinformation for GitHub Actions (#13)
Guess updateinformation for GitHub Actions
1 parent 3876206 commit d870d4f

File tree

1 file changed

+62
-6
lines changed

1 file changed

+62
-6
lines changed

src/appimagetool.c

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ static GOptionEntry entries[] =
508508
{
509509
{ "list", 'l', 0, G_OPTION_ARG_NONE, &list, "List files in SOURCE AppImage", NULL },
510510
{ "updateinformation", 'u', 0, G_OPTION_ARG_STRING, &updateinformation, "Embed update information STRING; if zsyncmake is installed, generate zsync file", NULL },
511-
{ "guess", 'g', 0, G_OPTION_ARG_NONE, &guess_update_information, "Guess update information based on Travis CI or GitLab environment variables", NULL },
511+
{ "guess", 'g', 0, G_OPTION_ARG_NONE, &guess_update_information, "Guess update information based on GitHub or GitLab environment variables", NULL },
512512
{ "version", 0, 0, G_OPTION_ARG_NONE, &showVersionOnly, "Show version number", NULL },
513513
{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Produce verbose output", NULL },
514514
{ "sign", 's', 0, G_OPTION_ARG_NONE, &sign, "Sign with gpg[2]", NULL },
@@ -540,10 +540,34 @@ main (int argc, char *argv[])
540540
travis_tag = getenv("TRAVIS_TAG");
541541
char* travis_pull_request;
542542
travis_pull_request = getenv("TRAVIS_PULL_REQUEST");
543+
544+
/* Parse GitHub Actions environment variables.
545+
* https://docs.github.com/en/actions/learn-github-actions/variables
546+
* GITHUB_REPOSITORY: The owner and repository name. For example, octocat/Hello-World.
547+
* GITHUB_REPOSITORY_OWNER: The repository owner's name. For example, octocat.
548+
*/
549+
543550
/* https://github.com/probonopd/uploadtool */
544551
char* github_token;
545552
github_token = getenv("GITHUB_TOKEN");
546553

554+
// Construct the repository name from github_repository and github_repository_owner
555+
// by removing the github_repository_owner from the beginning of github_repository
556+
// and the slash that follows it
557+
char* github_repository = getenv("GITHUB_REPOSITORY");
558+
char* github_repository_owner = getenv("GITHUB_REPOSITORY_OWNER");
559+
char* github_repository_name = NULL;
560+
if (github_repository_owner != NULL && github_repository != NULL) {
561+
char* owner_start = strstr(github_repository, github_repository_owner);
562+
if (owner_start != NULL) {
563+
owner_start += strlen(github_repository_owner);
564+
// Skip the '/'
565+
if (*owner_start == '/')
566+
owner_start++;
567+
github_repository_name = owner_start;
568+
}
569+
}
570+
547571
/* Parse GitLab CI environment variables.
548572
* https://docs.gitlab.com/ee/ci/variables/#predefined-variables-environment-variables
549573
* echo "${CI_PROJECT_URL}/-/jobs/artifacts/${CI_COMMIT_REF_NAME}/raw/QtQuickApp-x86_64.AppImage?job=${CI_JOB_NAME}"
@@ -913,10 +937,32 @@ main (int argc, char *argv[])
913937
exit(1);
914938
}
915939

916-
/* If the user has not provided update information but we know this is a Travis CI build,
917-
* then fill in update information based on TRAVIS_REPO_SLUG */
940+
/* If the user has not provided update information but we know this is a CI build,
941+
* then fill in update information based on well-known CI environment variables */
918942
if(guess_update_information){
919-
if(travis_repo_slug){
943+
944+
if(github_repository_name){
945+
if(!github_token) {
946+
printf("Will not guess update information since $GITHUB_TOKEN is missing\n");
947+
} else {
948+
gchar *zsyncmake_path = g_find_program_in_path ("zsyncmake");
949+
if(zsyncmake_path){
950+
char buf[1024];
951+
// gh-releases-zsync|probono|AppImages|latest|Subsurface-*x86_64.AppImage.zsync
952+
int ret = snprintf(buf, "gh-releases-zsync|%s|%s|latest|%s*-%s.AppImage.zsync", github_repository_owner, github_repository_name, app_name_for_filename, arch);
953+
if (ret < 0) {
954+
die("snprintf error");
955+
} else if (ret >= sizeof(buf)) {
956+
die("snprintf buffer overflow");
957+
}
958+
updateinformation = buf;
959+
printf("Guessing update information based on $GITHUB_REPOSITORY=%s\n", github_repository);
960+
printf("%s\n", updateinformation);
961+
} else {
962+
printf("Will not guess update information since zsyncmake is missing\n");
963+
}
964+
}
965+
} else if(travis_repo_slug){
920966
if(!github_token) {
921967
printf("Will not guess update information since $GITHUB_TOKEN is missing,\n");
922968
if(0 != strcmp(travis_pull_request, "false")){
@@ -938,7 +984,12 @@ main (int argc, char *argv[])
938984
channel = "latest";
939985
}
940986
}
941-
sprintf(buf, "gh-releases-zsync|%s|%s|%s|%s*-%s.AppImage.zsync", parts[0], parts[1], channel, app_name_for_filename, arch);
987+
int ret = snprintf(buf, sizeof(buf), "gh-releases-zsync|%s|%s|%s|%s*-%s.AppImage.zsync", parts[0], parts[1], channel, app_name_for_filename, arch);
988+
if (ret < 0) {
989+
die("snprintf error");
990+
} else if (ret >= sizeof(buf)) {
991+
die("snprintf buffer overflow");
992+
}
942993
updateinformation = buf;
943994
printf("Guessing update information based on $TRAVIS_TAG=%s and $TRAVIS_REPO_SLUG=%s\n", travis_tag, travis_repo_slug);
944995
printf("%s\n", updateinformation);
@@ -951,7 +1002,12 @@ main (int argc, char *argv[])
9511002
gchar *zsyncmake_path = g_find_program_in_path ("zsyncmake");
9521003
if(zsyncmake_path){
9531004
char buf[1024];
954-
sprintf(buf, "zsync|%s/-/jobs/artifacts/%s/raw/%s-%s.AppImage.zsync?job=%s", CI_PROJECT_URL, CI_COMMIT_REF_NAME, app_name_for_filename, arch, CI_JOB_NAME);
1005+
int ret = snprintf(buf, sizeof(buf), "zsync|%s/-/jobs/artifacts/%s/raw/%s-%s.AppImage.zsync?job=%s", CI_PROJECT_URL, CI_COMMIT_REF_NAME, app_name_for_filename, arch, CI_JOB_NAME);
1006+
if (ret < 0) {
1007+
die("snprintf error");
1008+
} else if (ret >= sizeof(buf)) {
1009+
die("snprintf buffer overflow");
1010+
}
9551011
updateinformation = buf;
9561012
printf("Guessing update information based on $CI_COMMIT_REF_NAME=%s and $CI_JOB_NAME=%s\n", CI_COMMIT_REF_NAME, CI_JOB_NAME);
9571013
printf("%s\n", updateinformation);

0 commit comments

Comments
 (0)