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

compare-url orb not working reliably #83

Closed
oshimayoan opened this issue Sep 13, 2019 · 13 comments · Fixed by #84
Closed

compare-url orb not working reliably #83

oshimayoan opened this issue Sep 13, 2019 · 13 comments · Fixed by #84
Assignees
Labels

Comments

@oshimayoan
Copy link
Contributor

What happened

Currently, the orb gets the last commit hash from the previous job as the left side of the commit range. That's what makes the CI failed because the left side of the commit range is not registered either in master or current branch.

This is what we got when it happened. This is happened on reconstruct section.

fatal: Not a valid commit name a23f024ca0fe8e1c39329e2609f940087a851319
unknown return code 128 from git merge-base with base commit a23f024ca0fe8e1c39329e2609f940087a851319, from job 12
Exited with code 1

It telling us that a23f024ca0fe8e1c39329e2609f940087a851319 is not a valid commit name. Of course, it is not, because that commit is retrieved from job 12 which is a job for different PR. So it will happen when there is another PR running CI after another PR run.

Expected behaviour

The last commit hash from master should be the one being used here instead of the last commit hash from the previous job.

Commit range: 13bec7c66c05...67cecb20844b
testing project-a
yarn install v1.12.3
[1/4] Resolving packages...
[2/4] Fetching packages...
info [email protected]: The platform "linux" is incompatible with this module.
info "[email protected]" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
[4/4] Building fresh packages...
Done in 5.24s.
yarn run v1.12.3
$ jest
 PASS  ./math.test.js
  ✓ 2 + 2 = 4 (3ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.039s
Ran all test suites.
Done in 1.93s.
project-b not modified; no need to test
@oshimayoan oshimayoan self-assigned this Sep 13, 2019
@oshimayoan
Copy link
Contributor Author

The last commit hash from master should be the one being used here instead of the last commit hash from the previous job.

That approach has a weakness that the job will definitely fail if the current branch does not on the same level as the current master. Like, when there is a PR landed on the master branch. Another PR that still open won't have the same level of git history as the current master until they pull the current master.

Despite that weakness, I think it still the best approach we can use for now.

To do that, we need to research the CircleCI API docs to find whether it is possible to get the latest commit hash from the latest current master.

@oshimayoan
Copy link
Contributor Author

I think compare-url current approach is good enough for the normal workflow of GitHub. But not good enough to handle workflow with Pull Request feature.

@oshimayoan
Copy link
Contributor Author

One possible approach to get the last commit hash from the master branch is by running the same script as to get the last commit hash from the last job but with a filter to find "master" branch first before getting the commit hash.

When the branch is not master:

$ curl -u <circleci_token>: https://circleci.com/api/v1.1/project/github/oshimayoan/monorepo-example/16 | grep '"branch"'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--100 40484    0 40484    0     0  21688      0 --:--:--  0:00:01 --:--:--100 40484    0 40484    0     0  21684      0 --:--:--  0:00  "branch" : "pull/1",
:01 --:--:-- 21683
    "branch" : "pull/1",

When the branch is master:

$ curl -u <circleci_token>: https://circleci.com/api/v1.1/project/github/oshimayoan/monorepo-example/14 | grep '"branch"'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--100 15278    0 15278    0     0   9633      0 --:--:--  0:00:01 --:--:--100 40458    0 40458    0     0  25493      0 --:--:--  0:00:01 --:--:-- 25493
  "branch" : "master",
    "branch" : "master",

But for that, we need to loop that script until it finds the "master" branch.

And of course, this approach also has a weakness of a race condition when a PR been landed when the CI still looping the script. There is a chance that the last commit has that being found will not be the last commit hash from the latest master.

But is it okay? In my opinion, it's still forgivable if we can't find any other solution for this.

@oshimayoan
Copy link
Contributor Author

There is another approach by using the ENV variable to store the last commit hash from the latest master branch.

Basically, we make a filter based on the branch. So every time CircleCI running a job on the master branch, not a PR, we will get the HEAD commit hash and store it inside an ENV variable that we created it before.

That ENV variable will always change every time a job is running on the master branch.

This approach is actually better than the previous approach, in my opinion. And also this seems more reliable than the previous one.

@oshimayoan
Copy link
Contributor Author

I've made a fork of compare-url to solve this issue.
https://github.com/oshimayoan/compare-url

@oshimayoan oshimayoan pinned this issue Sep 13, 2019
@oshimayoan
Copy link
Contributor Author

OK, so I was wrong all this time. Current compare-url has been implementing the first approach that using a loop to find the ancestor commit all this time. You can take a look at https://github.com/oshimayoan/compare-url/blob/d71e8161dde30ba52a5e851625c7cd08dc920bcd/src/commands/reconstruct.yml#L129

One of the problems we often got is related to this issue iynere/compare-url#25. Regarding that issue, I think I found a fix for that, for sure.

On the source code, I print the value of COMMIT_FROM_JOB_NUM, CIRCLE_SHA1, and RETURN_CODE that being used on this line https://github.com/oshimayoan/compare-url/blob/d71e8161dde30ba52a5e851625c7cd08dc920bcd/src/commands/reconstruct.yml#L166.

So take a glance at this:

COMMIT_FROM_JOB_NUM= a525c2f94ccd761b3bc13efe0163bc6f5cc75bc6
CIRCLE_SHA1= b52f3cbc582f155c613d13cb831bec7d361a05cf
RETURN_CODE= 1
----------------------------------------------------------------------------------------------------
commit a525c2f94ccd761b3bc13efe0163bc6f5cc75bc6 from job 25 is not an ancestor of the current commit
----------------------------------------------------------------------------------------------------

On job 25, actually, it's a job that ran for master branch and the latest commit is a525c2f94ccd761b3bc13efe0163bc6f5cc75bc6. But on the current job that I showed you above, the RETURN_CODE value is 1 when the actual return value from git merge-base --is-ancestor is 0.

This happens because when git merge-base --is-ancestor returns 0, RETURN_CODE does not store it and still using the old value, which is 1. That's why the orb doesn't recognize the COMMIT_FROM_JOB_NUM is not the ancestor of the CIRCLE_SHA1.

@oshimayoan
Copy link
Contributor Author

I put the fix in this branch https://github.com/oshimayoan/compare-url/tree/fix-new-branch-fail

@oshimayoan
Copy link
Contributor Author

oshimayoan commented Sep 13, 2019

Another problem is when a PR is getting a force push from local.

Because force push behaviour is changing the commit history, this becomes a bit problematic for compare-url as it will make git diff <commit_range> fail. Of course, it will fail because git can't find a diff between two commits from a different history.

So this will happens:

Commit range: b52f3cbc582f...b41423c44770
fatal: ambiguous argument 'b52f3cbc582f...b41423c44770': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
project-a not modified; no need to test
fatal: ambiguous argument 'b52f3cbc582f...b41423c44770': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
project-b not modified; no need to test

When this happens, all tests will be skipped by the CircleCI because it doesn't find any changes from each package/project. And the job will be marked as PASSED because there is no failing test.

@oshimayoan
Copy link
Contributor Author

OK, after playing around a little more I grasp more understanding over this.

So the problem with force push is actually caused by the orb still marking the PR as an existing branch instead of marking it back as a new branch. That's why it got compared with previous commit hash from the previous git history.

Take a look at this line https://github.com/oshimayoan/compare-url/blob/d883d438cea6d74634fed86b4717a69d98144562/src/commands/reconstruct.yml#L83

To solve this problem, we have to find a way how to tell the orb to mark the branch as a new branch again when the branch got a force push.

@oshimayoan
Copy link
Contributor Author

oshimayoan commented Sep 13, 2019

And for another issue with return code 128.

fatal: Not a valid commit name a3bea042dd0e826aeca3c51fc5358ac2c7ec03a6
COMMIT_FROM_JOB_NUM= a3bea042dd0e826aeca3c51fc5358ac2c7ec03a6
CIRCLE_SHA1= 3337fc2d1878ad13368ae631ba66b301f73775eb
RETURN_CODE= 128
unknown return code 128 from git merge-base with base commit a3bea042dd0e826aeca3c51fc5358ac2c7ec03a6, from job 35
Exited with code 1

It seems this issue is not an issue from the orb itself. This could be coming from Github rate-limiting that makes this line here returns 128.

I've tried to add GITHUB_TOKEN as ENV variable but it still doesn't fix it.

Reference: https://discuss.circleci.com/t/git-command-i-returned-128/5333/13

@oshimayoan
Copy link
Contributor Author

oshimayoan commented Sep 13, 2019

Alright, I think I found another clue regarding return code 128.

It seems this issue occurs when git merge-base --is-ancestor <A> <B> running with A not registered yet. So when A coming from the job of a different branch, the current branch will not know anything about this A. I think that what makes it returning 128.

I tried it on my terminal with git know about A:

$ git merge-base --is-ancestor a3bea042dd0e826aeca3c51fc5358ac2c7ec03a6 0bfed20198aef72a8953213232512bd0ae40fa00
$ echo $?
1

Here is when I tried it with unknown A (only change the last char):

$ git merge-base --is-ancestor a3bea042dd0e826aeca3c51fc5358ac2c7ec03ax 0bfed20198aef72a8953213232512bd0ae40fa00
fatal: Not a valid object name a3bea042dd0e826aeca3c51fc5358ac2c7ec03ax
$ echo $?
128

So we have to handle that return code separately to make this issue solve.

@oshimayoan
Copy link
Contributor Author

oshimayoan commented Sep 14, 2019

OK, I have solved the issue with return code 128 and I'll put it together in #84.

Here is the workflow when the issue is fixed:

fatal: Not a valid commit name a3bea042dd0e826aeca3c51fc5358ac2c7ec03a6
COMMIT_FROM_JOB_NUM= a3bea042dd0e826aeca3c51fc5358ac2c7ec03a6
CIRCLE_SHA1= 2175d91826b1827cb93e7e268bd8afcf9b841e17
RETURN_CODE= 128
----------------------------------------------------------------------------------------------------
commit a3bea042dd0e826aeca3c51fc5358ac2c7ec03a6 from job 35 is not an ancestor of the current commit
----------------------------------------------------------------------------------------------------
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

100 40855    0 40855    0     0   590k      0 --:--:-- --:--:-- --:--:--  595k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

100 40855    0 40855    0     0   593k      0 --:--:-- --:--:-- --:--:--  595k
fatal: Not a valid commit name a3bea042dd0e826aeca3c51fc5358ac2c7ec03a6
COMMIT_FROM_JOB_NUM= a3bea042dd0e826aeca3c51fc5358ac2c7ec03a6
CIRCLE_SHA1= 2175d91826b1827cb93e7e268bd8afcf9b841e17
RETURN_CODE= 128
----------------------------------------------------------------------------------------------------
commit a3bea042dd0e826aeca3c51fc5358ac2c7ec03a6 from job 34 is not an ancestor of the current commit
----------------------------------------------------------------------------------------------------
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

100 40899    0 40899    0     0   484k      0 --:--:-- --:--:-- --:--:--  487k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

100 40899    0 40899    0     0   667k      0 --:--:-- --:--:-- --:--:--  676k
COMMIT_FROM_JOB_NUM= 62223272eda8b1438556c4237b59eb030736d132
CIRCLE_SHA1= 2175d91826b1827cb93e7e268bd8afcf9b841e17
RETURN_CODE= 1
----------------------------------------------------------------------------------------------------
commit 62223272eda8b1438556c4237b59eb030736d132 from job 33 is not an ancestor of the current commit
----------------------------------------------------------------------------------------------------
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

100 40719    0 40719    0     0   595k      0 --:--:-- --:--:-- --:--:--  602k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

100 40719    0 40719    0     0   619k      0 --:--:-- --:--:-- --:--:--  621k
COMMIT_FROM_JOB_NUM= 400ff2745045aebc3fadc8a69db3f75b754b7f8a
CIRCLE_SHA1= 2175d91826b1827cb93e7e268bd8afcf9b841e17
RETURN_CODE= 
----------------------------------------------------------------------------------------------------
commit 400ff2745045aebc3fadc8a69db3f75b754b7f8a from job 32 is an ancestor of the current commit
----------------------------------------------------------------------------------------------------
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

100 40719    0 40719    0     0   575k      0 --:--:-- --:--:-- --:--:--  576k
----------------------------------------------------------------------------------------------------
base compare commit hash is: 400ff2745045aebc3fadc8a69db3f75b754b7f8a

this job's commit hash is: 2175d91826b1827cb93e7e268bd8afcf9b841e17
----------------------------------------------------------------------------------------------------

@oshimayoan
Copy link
Contributor Author

Finally, force-pushed issue solved!

OK, so when the function check_if_branch_is_new looping every job related to the branch, I added a few lines to check whether there is an ancestor from the last commit of each job using git merge-base --is-ancestor.

If there is no ancestor found, we can assume the branch is got a force-pushed for sure. But if there is an ancestor found, we will tell the orb that the branch is not a new branch.

Here is the result:

checking if pull/1 is a new branch...
----------------------------------------------------------------------------------------------------
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

100 26535  100 26535    0     0   420k      0 --:--:-- --:--:-- --:--:--  417k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

100 34488    0 34488    0     0   569k      0 --:--:-- --:--:-- --:--:--  570k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

100 41041    0 41041    0     0   181k      0 --:--:-- --:--:-- --:--:--  182k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

100 41041    0 41041    0     0   559k      0 --:--:-- --:--:-- --:--:--  564k
fatal: Not a valid commit name f4096ef0aea14c6626fb5ac2ced5dacd5d867765
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

100 40860    0 40860    0     0   531k      0 --:--:-- --:--:-- --:--:--  539k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

100 40860    0 40860    0     0   248k      0 --:--:-- --:--:-- --:--:--  249k
fatal: Not a valid commit name 3f6f2f08c2e63a525d1c986ec636b1c71de6f0a0
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

100 41974    0 41974    0     0   221k      0 --:--:-- --:--:-- --:--:--  221k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

100 41974    0 41974    0     0   563k      0 --:--:-- --:--:-- --:--:--  569k
fatal: Not a valid commit name bdda8dca0b33f92fde558b5d8c763fc80e5c2e40
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

100 43324    0 43324    0     0   552k      0 --:--:-- --:--:-- --:--:--  556k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

100 43324    0 43324    0     0   350k      0 --:--:-- --:--:-- --:--:--  352k
fatal: Not a valid commit name a3bea042dd0e826aeca3c51fc5358ac2c7ec03a6
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

100 40855    0 40855    0     0   334k      0 --:--:-- --:--:-- --:--:--  335k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

100 40855    0 40855    0     0   613k      0 --:--:-- --:--:-- --:--:--  623k
fatal: Not a valid commit name a3bea042dd0e826aeca3c51fc5358ac2c7ec03a6
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

100 40588    0 40588    0     0   423k      0 --:--:-- --:--:-- --:--:--  426k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

100 40588    0 40588    0     0   657k      0 --:--:-- --:--:-- --:--:--  660k
fatal: Not a valid commit name ff5f4a5457d836321c79fab097788367818664d0
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

100 40484    0 40484    0     0   512k      0 --:--:-- --:--:-- --:--:--  513k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

100 40484    0 40484    0     0   603k      0 --:--:-- --:--:-- --:--:--  608k
fatal: Not a valid commit name 629453878bfc7b64b88ee0a09ef65a06aa095c74
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

100 34261    0 34261    0     0   455k      0 --:--:-- --:--:-- --:--:--  458k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

100 34261  100 34261    0     0   602k      0 --:--:-- --:--:-- --:--:--  608k
fatal: Not a valid commit name ef8e08bc3580eac59fab3a12b7d9d253ef9f98ec
----------------------------------------------------------------------------------------------------
yes, pull/1 is new and c7979d94367862d25d67d0672364f021242637c9 is its only commit
finding most recent ancestor commit from any other branch...

@oshimayoan oshimayoan unpinned this issue Sep 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant