Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guess updateinformation for GitHub Actions #13

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
56 changes: 51 additions & 5 deletions src/appimagetool.c
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ static GOptionEntry entries[] =
{
{ "list", 'l', 0, G_OPTION_ARG_NONE, &list, "List files in SOURCE AppImage", NULL },
{ "updateinformation", 'u', 0, G_OPTION_ARG_STRING, &updateinformation, "Embed update information STRING; if zsyncmake is installed, generate zsync file", NULL },
{ "guess", 'g', 0, G_OPTION_ARG_NONE, &guess_update_information, "Guess update information based on Travis CI or GitLab environment variables", NULL },
{ "guess", 'g', 0, G_OPTION_ARG_NONE, &guess_update_information, "Guess update information based on GitHub or GitLab environment variables", NULL },
{ "version", 0, 0, G_OPTION_ARG_NONE, &showVersionOnly, "Show version number", NULL },
{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Produce verbose output", NULL },
{ "sign", 's', 0, G_OPTION_ARG_NONE, &sign, "Sign with gpg[2]", NULL },
Expand Down Expand Up @@ -522,10 +522,34 @@ main (int argc, char *argv[])
travis_tag = getenv("TRAVIS_TAG");
char* travis_pull_request;
travis_pull_request = getenv("TRAVIS_PULL_REQUEST");

/* Parse GitHub Actions environment variables.
* https://docs.github.com/en/actions/learn-github-actions/variables
* GITHUB_REPOSITORY: The owner and repository name. For example, octocat/Hello-World.
* GITHUB_REPOSITORY_OWNER: The repository owner's name. For example, octocat.
*/

/* https://github.com/probonopd/uploadtool */
char* github_token;
github_token = getenv("GITHUB_TOKEN");

// Construct the repository name from github_repository and github_repository_owner
// by removing the github_repository_owner from the beginning of github_repository
// and the slash that follows it
char* github_repository = getenv("GITHUB_REPOSITORY");
TheAssassin marked this conversation as resolved.
Show resolved Hide resolved
char* github_repository_owner = getenv("GITHUB_REPOSITORY_OWNER");
char* github_repository_name = NULL;
if (github_repository_owner != NULL && github_repository != NULL) {
char* owner_start = strstr(github_repository, github_repository_owner);
if (owner_start != NULL) {
owner_start += strlen(github_repository_owner);
// Skip the '/'
probonopd marked this conversation as resolved.
Show resolved Hide resolved
if (*owner_start == '/')
owner_start++;
github_repository_name = owner_start;
}
}

/* Parse GitLab CI environment variables.
* https://docs.gitlab.com/ee/ci/variables/#predefined-variables-environment-variables
* echo "${CI_PROJECT_URL}/-/jobs/artifacts/${CI_COMMIT_REF_NAME}/raw/QtQuickApp-x86_64.AppImage?job=${CI_JOB_NAME}"
Expand Down Expand Up @@ -890,10 +914,27 @@ main (int argc, char *argv[])
exit(1);
}

/* If the user has not provided update information but we know this is a Travis CI build,
* then fill in update information based on TRAVIS_REPO_SLUG */
/* If the user has not provided update information but we know this is a CI build,
* then fill in update information based on well-known CI environment variables */
if(guess_update_information){
if(travis_repo_slug){

if(github_repository_name){
if(!github_token) {
printf("Will not guess update information since $GITHUB_TOKEN is missing\n");
} else {
gchar *zsyncmake_path = g_find_program_in_path ("zsyncmake");
if(zsyncmake_path){
char buf[1024];
probonopd marked this conversation as resolved.
Show resolved Hide resolved
// gh-releases-zsync|probono|AppImages|latest|Subsurface-*x86_64.AppImage.zsync
sprintf(buf, "gh-releases-zsync|%s|%s|latest|%s*-%s.AppImage.zsync", github_repository_owner, github_repository_name, app_name_for_filename, arch);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another sprintf.

updateinformation = buf;
printf("Guessing update information based on $GITHUB_REPOSITORY=%s\n", github_repository);
printf("%s\n", updateinformation);
} else {
printf("Will not guess update information since zsyncmake is missing\n");
}
}
} else if(travis_repo_slug){
TheAssassin marked this conversation as resolved.
Show resolved Hide resolved
if(!github_token) {
printf("Will not guess update information since $GITHUB_TOKEN is missing,\n");
if(0 != strcmp(travis_pull_request, "false")){
Expand All @@ -915,7 +956,12 @@ main (int argc, char *argv[])
channel = "latest";
}
}
sprintf(buf, "gh-releases-zsync|%s|%s|%s|%s*-%s.AppImage.zsync", parts[0], parts[1], channel, app_name_for_filename, arch);
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);
if (ret < 0) {
die("snprintf error");
} else if (ret >= sizeof(buf)) {
die("snprintf buffer overflow");
}
updateinformation = buf;
printf("Guessing update information based on $TRAVIS_TAG=%s and $TRAVIS_REPO_SLUG=%s\n", travis_tag, travis_repo_slug);
printf("%s\n", updateinformation);
Expand Down